home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / Python 1.1 / Lib / os.py < prev    next >
Encoding:
Text File  |  1994-10-05  |  2.7 KB  |  104 lines  |  [TEXT/R*ch]

  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':   (':', '::', ':', ' ', ':'),
  24. }
  25.  
  26. # For freeze.py script:
  27. if 0:
  28.     import posix
  29.     import posixpath
  30.  
  31. import sys
  32. for name in _osindex.keys():
  33.     if name in sys.builtin_module_names:
  34.         curdir, pardir, sep, pathsep, defpath = _osindex[name]
  35.         exec 'from %s import *' % name
  36.         exec 'import %spath' % name
  37.         exec 'path = %spath' % name
  38.         exec 'del %spath' % name
  39.         try:
  40.             exec 'from %s import _exit' % name
  41.         except ImportError:
  42.             pass
  43.         break
  44. else:
  45.     del name
  46.     raise ImportError, 'no os specific module found'
  47.  
  48. def execl(file, *args):
  49.     execv(file, args)
  50.  
  51. def execle(file, *args):
  52.     env = args[-1]
  53.     execve(file, args[:-1], env)
  54.  
  55. def execlp(file, *args):
  56.     execvp(file, args)
  57.  
  58. _notfound = None
  59. def execvp(file, args):
  60.     global _notfound
  61.     head, tail = path.split(file)
  62.     if head:
  63.         execv(file, args)
  64.         return
  65.     ENOENT = 2
  66.     if environ.has_key('PATH'):
  67.         envpath = environ['PATH']
  68.     else:
  69.         envpath = defpath
  70.     import string
  71.     PATH = string.splitfields(envpath, pathsep)
  72.     if not _notfound:
  73.         import tempfile
  74.         # Exec a file that is guaranteed not to exist
  75.         try: execv(tempfile.mktemp(), ())
  76.         except error, _notfound: pass
  77.     exc, arg = error, _notfound
  78.     for dir in PATH:
  79.         fullname = path.join(dir, file)
  80.         try:
  81.             execv(fullname, args)
  82.         except error, (errno, msg):
  83.             if errno != arg[0]:
  84.                 exc, arg = error, (errno, msg)
  85.     raise exc, arg
  86.  
  87. # Provide listdir for Windows NT that doesn't have it built in
  88. if name == 'nt':
  89.     try:
  90.         _tmp = listdir
  91.         del _tmp
  92.     except NameError:
  93.         def listdir(name):
  94.             if path.ismount(name):
  95.                 list = ['.']
  96.             else:
  97.                 list = ['.', '..']
  98.             f = popen('dir/l/b ' + name, 'r')
  99.             line = f.readline()
  100.             while line:
  101.                 list.append(line[:-1])
  102.                 line = f.readline()
  103.             return list
  104.