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