home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 5 / DATAFILE_PDCD5.iso / utilities / p / python / !Python / Lib / Lib1 / py / os < prev    next >
Encoding:
Text File  |  1996-11-06  |  3.5 KB  |  145 lines

  1. # os.py -- either mac, dos or posix depending on what system we're on.
  2.  
  3. # This exports:
  4. # - all functions from either posix or mac, e.g., os.unlink, os.stat, etc.
  5. # - os.path is either module posixpath or macpath
  6. # - os.name is either 'posix' or 'mac'
  7. # - os.curdir is a string representing the current directory ('.' or ':')
  8. # - os.pardir is a string representing the parent directory ('..' or '::')
  9. # - os.sep is the (or a most common) pathname separator ('/' or ':')
  10. # - os.pathsep is the component separator used in $PATH etc
  11. # - os.defpath is the default search path for executables
  12.  
  13. # Programs that import and use 'os' stand a better chance of being
  14. # portable between different platforms.  Of course, they must then
  15. # only use functions that are defined by all platforms (e.g., unlink
  16. # and opendir), and leave all pathname manipulation to os.path
  17. # (e.g., split and join).
  18.  
  19. _osindex = {
  20.       'posix': ('.', '..', '/', ':', ':/bin:/usr/bin'),
  21.       'dos':   ('.', '..', '\\', ';', '.;C:\\bin'),
  22.       'nt':    ('.', '..', '\\', ';', '.;C:\\bin'),
  23.       'mac':   (':', '::', ':', '\n', ':'),
  24.       'riscos': ('@','^','.',',','<Run$Dir>')
  25.  
  26. }
  27.  
  28. # For freeze.py script:
  29. if 0:
  30.     import posix
  31.     import posixpath
  32.  
  33. import sys
  34. for name in _osindex.keys():
  35.     if name in sys.builtin_module_names:
  36.         curdir, pardir, sep, pathsep, defpath = _osindex[name]
  37.         exec 'from %s import *' % name
  38.         exec 'import %spath' % name
  39.         exec 'path = %spath' % name
  40.         exec 'del %spath' % name
  41.         try:
  42.             exec 'from %s import _exit' % name
  43.         except ImportError:
  44.             pass
  45.         try:
  46.             environ
  47.         except:
  48.             environ = {} # Make sure os.environ exists, at least
  49.         break
  50. else:
  51.     del name
  52.     raise ImportError, 'no os specific module found'
  53.  
  54. def execl(file, *args):
  55.     execv(file, args)
  56.  
  57. def execle(file, *args):
  58.     env = args[-1]
  59.     execve(file, args[:-1], env)
  60.  
  61. def execlp(file, *args):
  62.     execvp(file, args)
  63.  
  64. def execlpe(file, *args):
  65.     env = args[-1]
  66.     execvpe(file, args[:-1], env)
  67.  
  68. def execvp(file, args):
  69.     _execvpe(file, args)
  70.  
  71. def execvpe(file, args, env):
  72.     _execvpe(file, args, env)
  73.  
  74. _notfound = None
  75. def _execvpe(file, args, env = None):
  76.     if env:
  77.         func = execve
  78.         argrest = (args, env)
  79.     else:
  80.         func = execv
  81.         argrest = (args,)
  82.         env = environ
  83.     global _notfound
  84.     head, tail = path.split(file)
  85.     if head:
  86.         apply(func, (file,) + argrest)
  87.         return
  88.     if env.has_key('PATH'):
  89.         envpath = env['PATH']
  90.     else:
  91.         envpath = defpath
  92.     import string
  93.     PATH = string.splitfields(envpath, pathsep)
  94.     if not _notfound:
  95.         import tempfile
  96.         # Exec a file that is guaranteed not to exist
  97.         try: execv(tempfile.mktemp(), ())
  98.         except error, _notfound: pass
  99.     exc, arg = error, _notfound
  100.     for dir in PATH:
  101.         fullname = path.join(dir, file)
  102.         try:
  103.             apply(func, (fullname,) + argrest)
  104.         except error, (errno, msg):
  105.             if errno != arg[0]:
  106.                 exc, arg = error, (errno, msg)
  107.     raise exc, arg
  108.  
  109. # Provide listdir for Windows NT that doesn't have it built in
  110. if name == 'nt':
  111.     try:
  112.         _tmp = listdir
  113.         del _tmp
  114.     except NameError:
  115.         def listdir(name):
  116.             if path.ismount(name):
  117.                 list = ['.']
  118.             else:
  119.                 list = ['.', '..']
  120.             f = popen('dir/l/b ' + name, 'r')
  121.             line = f.readline()
  122.             while line:
  123.                 list.append(line[:-1])
  124.                 line = f.readline()
  125.             return list
  126.  
  127.  
  128. # Change environ to automatically call putenv() if it exists
  129. try:
  130.     _putenv = putenv
  131. except NameError:
  132.     _putenv = None
  133. if _putenv:
  134.     import UserDict
  135.  
  136.     class _Environ(UserDict.UserDict):
  137.         def __init__(self, environ):
  138.             UserDict.UserDict.__init__(self)
  139.             self.data = environ
  140.         def __setitem__(self, key, item):
  141.             putenv(key, item)
  142.             self.data[key] = item
  143.  
  144.     environ = _Environ(environ)
  145.