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