Guido van Rossum | dd8cb44 | 1993-12-29 15:33:08 +0000 | [diff] [blame] | 1 | # os.py -- either mac, dos or posix depending on what system we're on. |
Guido van Rossum | 31104f4 | 1992-01-14 18:28:36 +0000 | [diff] [blame] | 2 | |
| 3 | # This exports: |
| 4 | # - all functions from either posix or mac, e.g., os.unlink, os.stat, etc. |
Guido van Rossum | 1a76ef2 | 1992-03-31 18:57:28 +0000 | [diff] [blame] | 5 | # - os.path is either module posixpath or macpath |
Guido van Rossum | 31104f4 | 1992-01-14 18:28:36 +0000 | [diff] [blame] | 6 | # - os.name is either 'posix' or 'mac' |
| 7 | # - os.curdir is a string representing the current directory ('.' or ':') |
Guido van Rossum | 1a76ef2 | 1992-03-31 18:57:28 +0000 | [diff] [blame] | 8 | # - os.pardir is a string representing the parent directory ('..' or '::') |
Guido van Rossum | a28dab5 | 1997-08-29 22:36:47 +0000 | [diff] [blame] | 9 | # - os.sep is the (or a most common) pathname separator ('/' or ':' or '\\') |
| 10 | # - os.altsep is the alternatte pathname separator (None or '/') |
Guido van Rossum | 2979b01 | 1994-08-01 11:18:30 +0000 | [diff] [blame] | 11 | # - os.pathsep is the component separator used in $PATH etc |
| 12 | # - os.defpath is the default search path for executables |
Guido van Rossum | 31104f4 | 1992-01-14 18:28:36 +0000 | [diff] [blame] | 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 | |
Guido van Rossum | 2979b01 | 1994-08-01 11:18:30 +0000 | [diff] [blame] | 20 | import sys |
Guido van Rossum | a28dab5 | 1997-08-29 22:36:47 +0000 | [diff] [blame] | 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 'mac' in _names: |
| 63 | name = 'mac' |
| 64 | curdir = ':'; pardir = '::'; sep = ':'; pathsep = '\n' |
| 65 | defpath = ':' |
| 66 | from mac import * |
| 67 | try: |
| 68 | from mac import _exit |
| 69 | except ImportError: |
| 70 | pass |
| 71 | import macpath |
| 72 | path = macpath |
| 73 | del macpath |
Guido van Rossum | 2979b01 | 1994-08-01 11:18:30 +0000 | [diff] [blame] | 74 | else: |
Guido van Rossum | 2979b01 | 1994-08-01 11:18:30 +0000 | [diff] [blame] | 75 | raise ImportError, 'no os specific module found' |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 76 | |
Guido van Rossum | a28dab5 | 1997-08-29 22:36:47 +0000 | [diff] [blame] | 77 | del _names |
| 78 | |
| 79 | # Make sure os.environ exists, at least |
| 80 | try: |
| 81 | environ |
| 82 | except NameError: |
| 83 | environ = {} |
| 84 | |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 85 | def execl(file, *args): |
| 86 | execv(file, args) |
| 87 | |
| 88 | def execle(file, *args): |
| 89 | env = args[-1] |
| 90 | execve(file, args[:-1], env) |
| 91 | |
| 92 | def execlp(file, *args): |
| 93 | execvp(file, args) |
| 94 | |
Guido van Rossum | 030afb1 | 1995-03-14 17:27:18 +0000 | [diff] [blame] | 95 | def execlpe(file, *args): |
| 96 | env = args[-1] |
| 97 | execvpe(file, args[:-1], env) |
| 98 | |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 99 | def execvp(file, args): |
Guido van Rossum | 030afb1 | 1995-03-14 17:27:18 +0000 | [diff] [blame] | 100 | _execvpe(file, args) |
| 101 | |
| 102 | def execvpe(file, args, env): |
| 103 | _execvpe(file, args, env) |
| 104 | |
| 105 | _notfound = None |
| 106 | def _execvpe(file, args, env = None): |
| 107 | if env: |
| 108 | func = execve |
| 109 | argrest = (args, env) |
| 110 | else: |
| 111 | func = execv |
| 112 | argrest = (args,) |
| 113 | env = environ |
Guido van Rossum | 2979b01 | 1994-08-01 11:18:30 +0000 | [diff] [blame] | 114 | global _notfound |
| 115 | head, tail = path.split(file) |
| 116 | if head: |
Guido van Rossum | 030afb1 | 1995-03-14 17:27:18 +0000 | [diff] [blame] | 117 | apply(func, (file,) + argrest) |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 118 | return |
Guido van Rossum | 030afb1 | 1995-03-14 17:27:18 +0000 | [diff] [blame] | 119 | if env.has_key('PATH'): |
| 120 | envpath = env['PATH'] |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 121 | else: |
Guido van Rossum | 2979b01 | 1994-08-01 11:18:30 +0000 | [diff] [blame] | 122 | envpath = defpath |
| 123 | import string |
| 124 | PATH = string.splitfields(envpath, pathsep) |
| 125 | if not _notfound: |
| 126 | import tempfile |
| 127 | # Exec a file that is guaranteed not to exist |
| 128 | try: execv(tempfile.mktemp(), ()) |
| 129 | except error, _notfound: pass |
| 130 | exc, arg = error, _notfound |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 131 | for dir in PATH: |
| 132 | fullname = path.join(dir, file) |
| 133 | try: |
Guido van Rossum | 030afb1 | 1995-03-14 17:27:18 +0000 | [diff] [blame] | 134 | apply(func, (fullname,) + argrest) |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 135 | except error, (errno, msg): |
Guido van Rossum | 2979b01 | 1994-08-01 11:18:30 +0000 | [diff] [blame] | 136 | if errno != arg[0]: |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 137 | exc, arg = error, (errno, msg) |
| 138 | raise exc, arg |
Guido van Rossum | 2979b01 | 1994-08-01 11:18:30 +0000 | [diff] [blame] | 139 | |
Guido van Rossum | 3b8e20d | 1996-07-24 00:55:17 +0000 | [diff] [blame] | 140 | # Change environ to automatically call putenv() if it exists |
| 141 | try: |
Guido van Rossum | a28dab5 | 1997-08-29 22:36:47 +0000 | [diff] [blame] | 142 | # This will fail if there's no putenv |
| 143 | putenv |
Guido van Rossum | 3b8e20d | 1996-07-24 00:55:17 +0000 | [diff] [blame] | 144 | except NameError: |
Guido van Rossum | a28dab5 | 1997-08-29 22:36:47 +0000 | [diff] [blame] | 145 | pass |
| 146 | else: |
Guido van Rossum | 3b8e20d | 1996-07-24 00:55:17 +0000 | [diff] [blame] | 147 | import UserDict |
| 148 | |
| 149 | class _Environ(UserDict.UserDict): |
| 150 | def __init__(self, environ): |
| 151 | UserDict.UserDict.__init__(self) |
| 152 | self.data = environ |
| 153 | def __setitem__(self, key, item): |
| 154 | putenv(key, item) |
| 155 | self.data[key] = item |
| 156 | |
| 157 | environ = _Environ(environ) |