home *** CD-ROM | disk | FTP | other *** search
/ One Click 11 / OneClick11.iso / Bancos de Dados / Conversao / Mysql2Excel / Setup.exe / Mysql2Excel.exe / tempfile.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2003-06-23  |  7.5 KB  |  235 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.2)
  3.  
  4. '''Temporary files and filenames.'''
  5. import os
  6. __all__ = [
  7.     'mktemp',
  8.     'TemporaryFile',
  9.     'tempdir',
  10.     'gettempprefix']
  11. tempdir = None
  12. template = None
  13.  
  14. def gettempdir():
  15.     '''Function to calculate the directory to use.'''
  16.     if tempdir is not None:
  17.         return tempdir
  18.     
  19.     _tempdir_lock.acquire()
  20.     
  21.     try:
  22.         return _gettempdir_inner()
  23.     finally:
  24.         _tempdir_lock.release()
  25.  
  26.  
  27.  
  28. def _gettempdir_inner():
  29.     '''Function to calculate the directory to use.'''
  30.     global tempdir, tempdir
  31.     if tempdir is not None:
  32.         return tempdir
  33.     
  34.     
  35.     try:
  36.         pwd = os.getcwd()
  37.     except (AttributeError, os.error):
  38.         pwd = os.curdir
  39.  
  40.     attempdirs = [
  41.         '/tmp',
  42.         '/var/tmp',
  43.         '/usr/tmp',
  44.         pwd]
  45.     if os.name == 'nt':
  46.         attempdirs.insert(0, 'C:\\TEMP')
  47.         attempdirs.insert(0, '\\TEMP')
  48.     elif os.name == 'mac':
  49.         import macfs
  50.         import MACFS
  51.         
  52.         try:
  53.             (refnum, dirid) = macfs.FindFolder(MACFS.kOnSystemDisk, MACFS.kTemporaryFolderType, 1)
  54.             dirname = macfs.FSSpec((refnum, dirid, '')).as_pathname()
  55.             attempdirs.insert(0, dirname)
  56.         except macfs.error:
  57.             pass
  58.  
  59.     elif os.name == 'riscos':
  60.         scrapdir = os.getenv('Wimp$ScrapDir')
  61.         if scrapdir:
  62.             attempdirs.insert(0, scrapdir)
  63.         
  64.     
  65.     for envname in ('TMPDIR', 'TEMP', 'TMP'):
  66.         if os.environ.has_key(envname):
  67.             attempdirs.insert(0, os.environ[envname])
  68.         
  69.     
  70.     testfile = gettempprefix() + 'test'
  71.     for dir in attempdirs:
  72.         
  73.         try:
  74.             filename = os.path.join(dir, testfile)
  75.             if os.name == 'posix':
  76.                 
  77.                 try:
  78.                     fd = os.open(filename, os.O_RDWR | os.O_CREAT | os.O_EXCL, 448)
  79.                 except OSError:
  80.                     pass
  81.  
  82.                 fp = os.fdopen(fd, 'w')
  83.                 fp.write('blat')
  84.                 fp.close()
  85.                 os.unlink(filename)
  86.                 del fp
  87.                 del fd
  88.                 tempdir = dir
  89.                 break
  90.             else:
  91.                 fp = open(filename, 'w')
  92.                 fp.write('blat')
  93.                 fp.close()
  94.                 os.unlink(filename)
  95.                 tempdir = dir
  96.         except IOError:
  97.             pass
  98.  
  99.     
  100.     if tempdir is None:
  101.         msg = "Can't find a usable temporary directory amongst " + `attempdirs`
  102.         raise IOError, msg
  103.     
  104.     return tempdir
  105.  
  106. if os.name == 'posix':
  107.     template = None
  108. elif os.name == 'nt':
  109.     template = '~' + `os.getpid()` + '-'
  110. elif os.name in ('mac', 'riscos'):
  111.     template = 'Python-Tmp-'
  112. else:
  113.     template = 'tmp'
  114.  
  115. def gettempprefix():
  116.     """Function to calculate a prefix of the filename to use.
  117.  
  118.     This incorporates the current process id on systems that support such a
  119.     notion, so that concurrent processes don't generate the same prefix.
  120.     """
  121.     if template is None:
  122.         return '@' + `os.getpid()` + '.'
  123.     else:
  124.         return template
  125.  
  126.  
  127. def mktemp(suffix = ''):
  128.     '''User-callable function to return a unique temporary file name.'''
  129.     dir = gettempdir()
  130.     pre = gettempprefix()
  131.     while 1:
  132.         i = _counter.get_next()
  133.         file = os.path.join(dir, pre + str(i) + suffix)
  134.         if not os.path.exists(file):
  135.             return file
  136.         
  137.  
  138.  
  139. class TemporaryFileWrapper:
  140.     '''Temporary file wrapper
  141.  
  142.     This class provides a wrapper around files opened for temporary use.
  143.     In particular, it seeks to automatically remove the file when it is
  144.     no longer needed.
  145.     '''
  146.     unlink = os.unlink
  147.     
  148.     def __init__(self, file, path):
  149.         self.file = file
  150.         self.path = path
  151.         self.close_called = 0
  152.  
  153.     
  154.     def close(self):
  155.         if not (self.close_called):
  156.             self.close_called = 1
  157.             self.file.close()
  158.             self.unlink(self.path)
  159.         
  160.  
  161.     
  162.     def __del__(self):
  163.         self.close()
  164.  
  165.     
  166.     def __getattr__(self, name):
  167.         file = self.__dict__['file']
  168.         a = getattr(file, name)
  169.         if type(a) != type(0):
  170.             setattr(self, name, a)
  171.         
  172.         return a
  173.  
  174.  
  175.  
  176. def TemporaryFile(mode = 'w+b', bufsize = -1, suffix = ''):
  177.     '''Create and return a temporary file (opened read-write by default).'''
  178.     name = mktemp(suffix)
  179.     if os.name == 'posix':
  180.         fd = os.open(name, os.O_RDWR | os.O_CREAT | os.O_EXCL, 448)
  181.         
  182.         try:
  183.             os.unlink(name)
  184.             return os.fdopen(fd, mode, bufsize)
  185.         except:
  186.             os.close(fd)
  187.             raise 
  188.  
  189.     else:
  190.         file = open(name, mode, bufsize)
  191.         return TemporaryFileWrapper(file, name)
  192.  
  193.  
  194. class _ThreadSafeCounter:
  195.     
  196.     def __init__(self, mutex, initialvalue = 0):
  197.         self.mutex = mutex
  198.         self.i = initialvalue
  199.  
  200.     
  201.     def get_next(self):
  202.         self.mutex.acquire()
  203.         result = self.i
  204.         
  205.         try:
  206.             newi = result + 1
  207.         except OverflowError:
  208.             newi = long(result) + 1
  209.  
  210.         self.i = newi
  211.         self.mutex.release()
  212.         return result
  213.  
  214.  
  215.  
  216. try:
  217.     import thread
  218. except ImportError:
  219.     
  220.     class _DummyMutex:
  221.         
  222.         def acquire(self):
  223.             pass
  224.  
  225.         release = acquire
  226.  
  227.     _counter = _ThreadSafeCounter(_DummyMutex())
  228.     _tempdir_lock = _DummyMutex()
  229.     del _DummyMutex
  230.  
  231. _counter = _ThreadSafeCounter(thread.allocate_lock())
  232. _tempdir_lock = thread.allocate_lock()
  233. del thread
  234. del _ThreadSafeCounter
  235.