home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pyos2bin.zip / Lib / os.py < prev    next >
Text File  |  1997-12-05  |  5KB  |  189 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 ':' or '\\')
  10. # - os.altsep is the alternatte pathname separator (None or '/')
  11. # - os.pathsep is the component separator used in $PATH etc
  12. # - os.defpath is the default search path for executables
  13.  
  14. # Programs that import and use 'os' stand a better chance of being
  15. # portable between different platforms.  Of course, they must then
  16. # only use functions that are defined by all platforms (e.g., unlink
  17. # and opendir), and leave all pathname manipulation to os.path
  18. # (e.g., split and join).
  19.  
  20. import sys
  21.  
  22. _names = sys.builtin_module_names
  23.  
  24. altsep = None
  25.  
  26. if 'posix' in _names:
  27.     name = 'posix'
  28.     curdir = '.'; pardir = '..'; sep = '/'; pathsep = ':'
  29.     defpath = ':/bin:/usr/bin'
  30.     from posix import *
  31.     try:
  32.         from posix import _exit
  33.     except ImportError:
  34.         pass
  35.     import posixpath
  36.     path = posixpath
  37.     del posixpath
  38. elif 'nt' in _names:
  39.     name = 'nt'
  40.     curdir = '.'; pardir = '..'; sep = '\\'; pathsep = ';'
  41.     defpath = '.;C:\\bin'
  42.     from nt import *
  43.     try:
  44.         from nt import _exit
  45.     except ImportError:
  46.         pass
  47.     import ntpath
  48.     path = ntpath
  49.     del ntpath
  50. elif 'dos' in _names:
  51.     name = 'dos'
  52.     curdir = '.'; pardir = '..'; sep = '\\'; pathsep = ';'
  53.     defpath = '.;C:\\bin'
  54.     from dos import *
  55.     try:
  56.         from dos import _exit
  57.     except ImportError:
  58.         pass
  59.     import dospath
  60.     path = dospath
  61.     del dospath
  62. elif 'os2' in _names:
  63.     name = 'os2'
  64.     curdir = '.'; pardir = '..'; sep = '\\'; pathsep = ';'
  65.     defpath = '.;C:\\bin'
  66.     from os2 import *
  67.     try:
  68.         from os2 import _exit
  69.     except ImportError:
  70.         pass
  71.     import ntpath
  72.     path = ntpath
  73.     del ntpath
  74. elif 'mac' in _names:
  75.     name = 'mac'
  76.     curdir = ':'; pardir = '::'; sep = ':'; pathsep = '\n'
  77.     defpath = ':'
  78.     from mac import *
  79.     try:
  80.         from mac import _exit
  81.     except ImportError:
  82.         pass
  83.     import macpath
  84.     path = macpath
  85.     del macpath
  86. else:
  87.     raise ImportError, 'no os specific module found'
  88.  
  89. del _names
  90.  
  91. # Make sure os.environ exists, at least
  92. try:
  93.     environ
  94. except NameError:
  95.     environ = {}
  96.  
  97. def execl(file, *args):
  98.     execv(file, args)
  99.  
  100. def execle(file, *args):
  101.     env = args[-1]
  102.     execve(file, args[:-1], env)
  103.  
  104. def execlp(file, *args):
  105.     execvp(file, args)
  106.  
  107. def execlpe(file, *args):
  108.     env = args[-1]
  109.     execvpe(file, args[:-1], env)
  110.  
  111. def execvp(file, args):
  112.     _execvpe(file, args)
  113.  
  114. def execvpe(file, args, env):
  115.     _execvpe(file, args, env)
  116.  
  117. _notfound = None
  118. def _execvpe(file, args, env = None):
  119.     if env:
  120.         func = execve
  121.         argrest = (args, env)
  122.     else:
  123.         func = execv
  124.         argrest = (args,)
  125.         env = environ
  126.     global _notfound
  127.     head, tail = path.split(file)
  128.     if head:
  129.         apply(func, (file,) + argrest)
  130.         return
  131.     if env.has_key('PATH'):
  132.         envpath = env['PATH']
  133.     else:
  134.         envpath = defpath
  135.     import string
  136.     PATH = string.splitfields(envpath, pathsep)
  137.     if not _notfound:
  138.         import tempfile
  139.         # Exec a file that is guaranteed not to exist
  140.         try: execv(tempfile.mktemp(), ())
  141.         except error, _notfound: pass
  142.     exc, arg = error, _notfound
  143.     for dir in PATH:
  144.         fullname = path.join(dir, file)
  145.         try:
  146.             apply(func, (fullname,) + argrest)
  147.         except error, (errno, msg):
  148.             if errno != arg[0]:
  149.                 exc, arg = error, (errno, msg)
  150.     raise exc, arg
  151.  
  152. # Change environ to automatically call putenv() if it exists
  153. try:
  154.     # This will fail if there's no putenv
  155.     putenv
  156. except NameError:
  157.     pass
  158. else:
  159.     import UserDict
  160.  
  161.     if name in ('os2', ):  # Where Env Var Names Must Be UPPERCASE
  162.         import string
  163.         class _Environ(UserDict.UserDict):
  164.             def __init__(self, environ):
  165.                 UserDict.UserDict.__init__(self)
  166.                 self.data = environ
  167.             def __setitem__(self, key, item):
  168.                 key = string.upper(key)
  169.                 putenv(key, item)
  170.                 self.data[key] = item
  171.             def __getitem__(self, key):
  172.                 return self.data[string.upper(key)]
  173.  
  174.     else:  # Where Env Var Names Can Be Mixed Case
  175.         class _Environ(UserDict.UserDict):
  176.             def __init__(self, environ):
  177.                 UserDict.UserDict.__init__(self)
  178.                 self.data = environ
  179.             def __getinitargs__(self):
  180.                 import copy
  181.                 return (copy.copy(self.data),)
  182.             def __setitem__(self, key, item):
  183.                 putenv(key, item)
  184.                 self.data[key] = item
  185.             def __copy__(self):
  186.                 return _Environ(self.data.copy())
  187.  
  188.     environ = _Environ(environ)
  189.