home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / Other Langs / python / python folder / Lib / os.py < prev    next >
Encoding:
Text File  |  1994-01-05  |  2.0 KB  |  81 lines  |  [TEXT/????]

  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.  
  11. # Programs that import and use 'os' stand a better chance of being
  12. # portable between different platforms.  Of course, they must then
  13. # only use functions that are defined by all platforms (e.g., unlink
  14. # and opendir), and leave all pathname manipulation to os.path
  15. # (e.g., split and join).
  16.  
  17. # XXX This is incorrect if the import *path fails...
  18.  
  19. try:
  20.     from posix import *
  21.     try:
  22.         from posix import _exit
  23.     except ImportError:
  24.         pass
  25.     name = 'posix'
  26.     curdir = '.'
  27.     pardir = '..'
  28.     sep = '/'
  29.     import posixpath
  30.     path = posixpath
  31.     del posixpath
  32. except ImportError:
  33.     try:
  34.         from mac import *
  35.         name = 'mac'
  36.         curdir = ':'
  37.         pardir = '::'
  38.         sep = ':'
  39.         import macpath
  40.         path = macpath
  41.         del macpath
  42.     except ImportError:
  43.         from dos import *
  44.         name = 'dos'
  45.         curdir = '.'        # XXX doesn't always work
  46.         pardir = '..'        # XXX doesn't always work
  47.         sep = '/'        # XXX or '\\' ???
  48.         import dospath
  49.         path = dospath
  50.         del dospath
  51.  
  52. def execl(file, *args):
  53.     execv(file, args)
  54.  
  55. def execle(file, *args):
  56.     env = args[-1]
  57.     execve(file, args[:-1], env)
  58.  
  59. def execlp(file, *args):
  60.     execvp(file, args)
  61.  
  62. def execvp(file, args):
  63.     if '/' in file:
  64.         execv(file, args)
  65.         return
  66.     ENOENT = 2
  67.     if environ.has_key('PATH'):
  68.         import string
  69.         PATH = string.splitfields(environ['PATH'], ':')
  70.     else:
  71.         PATH = ['', '/bin', '/usr/bin']
  72.     exc, arg = (ENOENT, 'No such file or directory')
  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 != ENOENT:
  79.                 exc, arg = error, (errno, msg)
  80.     raise exc, arg
  81.