home *** CD-ROM | disk | FTP | other *** search
/ Netrunner 2004 October / NETRUNNER0410.ISO / regular / iria107a.lzh / script / tempfile.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2000-11-17  |  5.2 KB  |  178 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.0)
  3.  
  4. '''Temporary files and filenames.'''
  5. import os
  6. tempdir = None
  7. template = None
  8.  
  9. def gettempdir():
  10.     '''Function to calculate the directory to use.'''
  11.     global tempdir, tempdir
  12.     if tempdir is not None:
  13.         return tempdir
  14.     
  15.     
  16.     try:
  17.         pwd = os.getcwd()
  18.     except (AttributeError, os.error):
  19.         pwd = os.curdir
  20.  
  21.     attempdirs = [
  22.         '/var/tmp',
  23.         '/usr/tmp',
  24.         '/tmp',
  25.         pwd]
  26.     if os.name == 'nt':
  27.         attempdirs.insert(0, 'C:\\TEMP')
  28.         attempdirs.insert(0, '\\TEMP')
  29.     elif os.name == 'mac':
  30.         import macfs
  31.         import MACFS
  32.         
  33.         try:
  34.             (refnum, dirid) = macfs.FindFolder(MACFS.kOnSystemDisk, MACFS.kTemporaryFolderType, 1)
  35.             dirname = macfs.FSSpec((refnum, dirid, '')).as_pathname()
  36.             attempdirs.insert(0, dirname)
  37.         except macfs.error:
  38.             pass
  39.  
  40.     
  41.     for envname in ('TMPDIR', 'TEMP', 'TMP'):
  42.         pass
  43.     
  44.     testfile = gettempprefix() + 'test'
  45.     for dir in attempdirs:
  46.         
  47.         try:
  48.             filename = os.path.join(dir, testfile)
  49.             if os.name == 'posix':
  50.                 
  51.                 try:
  52.                     fd = os.open(filename, os.O_RDWR | os.O_CREAT | os.O_EXCL, 448)
  53.                 except OSError:
  54.                     0
  55.                     0
  56.                     attempdirs
  57.                 except:
  58.                     None if os.environ.has_key(envname) else ('TMPDIR', 'TEMP', 'TMP')
  59.  
  60.                 fp = os.fdopen(fd, 'w')
  61.                 fp.write('blat')
  62.                 fp.close()
  63.                 os.unlink(filename)
  64.                 del fp
  65.                 del fd
  66.                 tempdir = dir
  67.                 break
  68.             else:
  69.                 fp = open(filename, 'w')
  70.                 fp.write('blat')
  71.                 fp.close()
  72.                 os.unlink(filename)
  73.                 tempdir = dir
  74.         except IOError:
  75.             0
  76.             0
  77.             attempdirs
  78.         except:
  79.             None if os.environ.has_key(envname) else ('TMPDIR', 'TEMP', 'TMP')
  80.  
  81.     
  82.     if tempdir is None:
  83.         msg = "Can't find a usable temporary directory amongst " + `attempdirs`
  84.         raise IOError, msg
  85.     
  86.     return tempdir
  87.  
  88. _pid = None
  89.  
  90. def gettempprefix():
  91.     '''Function to calculate a prefix of the filename to use.'''
  92.     global template, _pid, template, template, template, template
  93.     if os.name == 'posix' and _pid and _pid != os.getpid():
  94.         template = None
  95.     
  96.     if template is None:
  97.         if os.name == 'posix':
  98.             _pid = os.getpid()
  99.             template = '@' + `_pid` + '.'
  100.         elif os.name == 'nt':
  101.             template = '~' + `os.getpid()` + '-'
  102.         elif os.name == 'mac':
  103.             template = 'Python-Tmp-'
  104.         else:
  105.             template = 'tmp'
  106.     
  107.     return template
  108.  
  109. counter = 0
  110.  
  111. def mktemp(suffix = ''):
  112.     '''User-callable function to return a unique temporary file name.'''
  113.     global counter
  114.     dir = gettempdir()
  115.     pre = gettempprefix()
  116.     while 1:
  117.         counter = counter + 1
  118.         file = os.path.join(dir, pre + `counter` + suffix)
  119.         if not os.path.exists(file):
  120.             return file
  121.         
  122.  
  123.  
  124. class TemporaryFileWrapper:
  125.     '''Temporary file wrapper
  126.  
  127.     This class provides a wrapper around files opened for temporary use.
  128.     In particular, it seeks to automatically remove the file when it is
  129.     no longer needed.
  130.     '''
  131.     
  132.     def __init__(self, file, path):
  133.         self.file = file
  134.         self.path = path
  135.  
  136.     
  137.     def close(self):
  138.         self.file.close()
  139.         os.unlink(self.path)
  140.  
  141.     
  142.     def __del__(self):
  143.         
  144.         try:
  145.             self.close()
  146.         except:
  147.             pass
  148.  
  149.  
  150.     
  151.     def __getattr__(self, name):
  152.         file = self.__dict__['file']
  153.         a = getattr(file, name)
  154.         if type(a) != type(0):
  155.             setattr(self, name, a)
  156.         
  157.         return a
  158.  
  159.  
  160.  
  161. def TemporaryFile(mode = 'w+b', bufsize = -1, suffix = ''):
  162.     '''Create and return a temporary file (opened read-write by default).'''
  163.     name = mktemp(suffix)
  164.     if os.name == 'posix':
  165.         fd = os.open(name, os.O_RDWR | os.O_CREAT | os.O_EXCL, 448)
  166.         
  167.         try:
  168.             os.unlink(name)
  169.             return os.fdopen(fd, mode, bufsize)
  170.         except:
  171.             os.close(fd)
  172.             raise 
  173.  
  174.     else:
  175.         file = open(name, mode, bufsize)
  176.         return TemporaryFileWrapper(file, name)
  177.  
  178.