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: |
Guido van Rossum | 61de0ac | 1997-12-05 21:24:30 +0000 | [diff] [blame] | 27 | name = 'posix' |
Guido van Rossum | e9387ea | 1998-05-22 15:26:04 +0000 | [diff] [blame] | 28 | linesep = '\n' |
Guido van Rossum | 61de0ac | 1997-12-05 21:24:30 +0000 | [diff] [blame] | 29 | curdir = '.'; pardir = '..'; sep = '/'; pathsep = ':' |
| 30 | defpath = ':/bin:/usr/bin' |
| 31 | from posix import * |
| 32 | try: |
| 33 | from posix import _exit |
| 34 | except ImportError: |
| 35 | pass |
| 36 | import posixpath |
| 37 | path = posixpath |
| 38 | del posixpath |
Guido van Rossum | a28dab5 | 1997-08-29 22:36:47 +0000 | [diff] [blame] | 39 | elif 'nt' in _names: |
Guido van Rossum | 61de0ac | 1997-12-05 21:24:30 +0000 | [diff] [blame] | 40 | name = 'nt' |
Guido van Rossum | e9387ea | 1998-05-22 15:26:04 +0000 | [diff] [blame] | 41 | linesep = '\r\n' |
Guido van Rossum | 61de0ac | 1997-12-05 21:24:30 +0000 | [diff] [blame] | 42 | curdir = '.'; pardir = '..'; sep = '\\'; pathsep = ';' |
| 43 | defpath = '.;C:\\bin' |
| 44 | from nt import * |
| 45 | try: |
| 46 | from nt import _exit |
| 47 | except ImportError: |
| 48 | pass |
| 49 | import ntpath |
| 50 | path = ntpath |
| 51 | del ntpath |
Guido van Rossum | a28dab5 | 1997-08-29 22:36:47 +0000 | [diff] [blame] | 52 | elif 'dos' in _names: |
Guido van Rossum | 61de0ac | 1997-12-05 21:24:30 +0000 | [diff] [blame] | 53 | name = 'dos' |
Guido van Rossum | e9387ea | 1998-05-22 15:26:04 +0000 | [diff] [blame] | 54 | linesep = '\r\n' |
Guido van Rossum | 61de0ac | 1997-12-05 21:24:30 +0000 | [diff] [blame] | 55 | curdir = '.'; pardir = '..'; sep = '\\'; pathsep = ';' |
| 56 | defpath = '.;C:\\bin' |
| 57 | from dos import * |
| 58 | try: |
| 59 | from dos import _exit |
| 60 | except ImportError: |
| 61 | pass |
| 62 | import dospath |
| 63 | path = dospath |
| 64 | del dospath |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 65 | elif 'os2' in _names: |
Guido van Rossum | 61de0ac | 1997-12-05 21:24:30 +0000 | [diff] [blame] | 66 | name = 'os2' |
Guido van Rossum | e9387ea | 1998-05-22 15:26:04 +0000 | [diff] [blame] | 67 | linesep = '\r\n' |
Guido van Rossum | 61de0ac | 1997-12-05 21:24:30 +0000 | [diff] [blame] | 68 | curdir = '.'; pardir = '..'; sep = '\\'; pathsep = ';' |
| 69 | defpath = '.;C:\\bin' |
| 70 | from os2 import * |
| 71 | try: |
| 72 | from os2 import _exit |
| 73 | except ImportError: |
| 74 | pass |
| 75 | import ntpath |
| 76 | path = ntpath |
| 77 | del ntpath |
Guido van Rossum | a28dab5 | 1997-08-29 22:36:47 +0000 | [diff] [blame] | 78 | elif 'mac' in _names: |
Guido van Rossum | 61de0ac | 1997-12-05 21:24:30 +0000 | [diff] [blame] | 79 | name = 'mac' |
Guido van Rossum | e9387ea | 1998-05-22 15:26:04 +0000 | [diff] [blame] | 80 | linesep = '\r' |
Guido van Rossum | 61de0ac | 1997-12-05 21:24:30 +0000 | [diff] [blame] | 81 | curdir = ':'; pardir = '::'; sep = ':'; pathsep = '\n' |
| 82 | defpath = ':' |
| 83 | from mac import * |
| 84 | try: |
| 85 | from mac import _exit |
| 86 | except ImportError: |
| 87 | pass |
| 88 | import macpath |
| 89 | path = macpath |
| 90 | del macpath |
Guido van Rossum | 2979b01 | 1994-08-01 11:18:30 +0000 | [diff] [blame] | 91 | else: |
Guido van Rossum | 61de0ac | 1997-12-05 21:24:30 +0000 | [diff] [blame] | 92 | raise ImportError, 'no os specific module found' |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 93 | |
Guido van Rossum | a28dab5 | 1997-08-29 22:36:47 +0000 | [diff] [blame] | 94 | del _names |
| 95 | |
Guido van Rossum | 4def7de | 1998-07-24 20:48:03 +0000 | [diff] [blame] | 96 | # Super directory utilities. |
| 97 | # (Inspired by Eric Raymond; the doc strings are mostly his) |
| 98 | |
| 99 | def makedirs(name, mode=0777): |
| 100 | """makedirs(path [, mode=0777]) -> None |
| 101 | |
| 102 | Super-mkdir; create a leaf directory and all intermediate ones. |
| 103 | Works like mkdir, except that any intermediate path segment (not |
| 104 | just the rightmost) will be created if it does not exist. This is |
| 105 | recursive. |
| 106 | |
| 107 | """ |
| 108 | head, tail = path.split(name) |
| 109 | if head and tail and not path.exists(head): |
| 110 | makedirs(head, mode) |
| 111 | mkdir(name, mode) |
| 112 | |
| 113 | def removedirs(name): |
| 114 | """removedirs(path) -> None |
| 115 | |
| 116 | Super-rmdir; remove a leaf directory and empty all intermediate |
| 117 | ones. Works like rmdir except that, if the leaf directory is |
| 118 | successfully removed, directories corresponding to rightmost path |
| 119 | segments will be pruned way until either the whole path is |
| 120 | consumed or an error occurs. Errors during this latter phase are |
| 121 | ignored -- they generally mean that a directory was not empty. |
| 122 | |
| 123 | """ |
| 124 | rmdir(name) |
| 125 | head, tail = path.split(name) |
| 126 | while head and tail: |
| 127 | try: |
| 128 | rmdir(head) |
| 129 | except error: |
| 130 | break |
| 131 | head, tail = path.split(head) |
| 132 | |
| 133 | def renames(old, new): |
| 134 | """renames(old, new) -> None |
| 135 | |
| 136 | Super-rename; create directories as necessary and delete any left |
| 137 | empty. Works like rename, except creation of any intermediate |
| 138 | directories needed to make the new pathname good is attempted |
| 139 | first. After the rename, directories corresponding to rightmost |
| 140 | path segments of the old name will be pruned way until either the |
| 141 | whole path is consumed or a nonempty directory is found. |
| 142 | |
| 143 | Note: this function can fail with the new directory structure made |
| 144 | if you lack permissions needed to unlink the leaf directory or |
| 145 | file. |
| 146 | |
| 147 | """ |
| 148 | head, tail = path.split(new) |
| 149 | if head and tail and not path.exists(head): |
| 150 | makedirs(head) |
| 151 | rename(old, new) |
| 152 | head, tail = path.split(old) |
| 153 | if head and tail: |
| 154 | try: |
| 155 | removedirs(head) |
| 156 | except error: |
| 157 | pass |
| 158 | |
Guido van Rossum | a28dab5 | 1997-08-29 22:36:47 +0000 | [diff] [blame] | 159 | # Make sure os.environ exists, at least |
| 160 | try: |
Guido van Rossum | 61de0ac | 1997-12-05 21:24:30 +0000 | [diff] [blame] | 161 | environ |
Guido van Rossum | a28dab5 | 1997-08-29 22:36:47 +0000 | [diff] [blame] | 162 | except NameError: |
Guido van Rossum | 61de0ac | 1997-12-05 21:24:30 +0000 | [diff] [blame] | 163 | environ = {} |
Guido van Rossum | a28dab5 | 1997-08-29 22:36:47 +0000 | [diff] [blame] | 164 | |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 165 | def execl(file, *args): |
Guido van Rossum | 61de0ac | 1997-12-05 21:24:30 +0000 | [diff] [blame] | 166 | execv(file, args) |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 167 | |
| 168 | def execle(file, *args): |
Guido van Rossum | 61de0ac | 1997-12-05 21:24:30 +0000 | [diff] [blame] | 169 | env = args[-1] |
| 170 | execve(file, args[:-1], env) |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 171 | |
| 172 | def execlp(file, *args): |
Guido van Rossum | 61de0ac | 1997-12-05 21:24:30 +0000 | [diff] [blame] | 173 | execvp(file, args) |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 174 | |
Guido van Rossum | 030afb1 | 1995-03-14 17:27:18 +0000 | [diff] [blame] | 175 | def execlpe(file, *args): |
Guido van Rossum | 61de0ac | 1997-12-05 21:24:30 +0000 | [diff] [blame] | 176 | env = args[-1] |
| 177 | execvpe(file, args[:-1], env) |
Guido van Rossum | 030afb1 | 1995-03-14 17:27:18 +0000 | [diff] [blame] | 178 | |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 179 | def execvp(file, args): |
Guido van Rossum | 61de0ac | 1997-12-05 21:24:30 +0000 | [diff] [blame] | 180 | _execvpe(file, args) |
Guido van Rossum | 030afb1 | 1995-03-14 17:27:18 +0000 | [diff] [blame] | 181 | |
| 182 | def execvpe(file, args, env): |
Guido van Rossum | 61de0ac | 1997-12-05 21:24:30 +0000 | [diff] [blame] | 183 | _execvpe(file, args, env) |
Guido van Rossum | 030afb1 | 1995-03-14 17:27:18 +0000 | [diff] [blame] | 184 | |
| 185 | _notfound = None |
| 186 | def _execvpe(file, args, env = None): |
Guido van Rossum | 61de0ac | 1997-12-05 21:24:30 +0000 | [diff] [blame] | 187 | if env: |
| 188 | func = execve |
| 189 | argrest = (args, env) |
| 190 | else: |
| 191 | func = execv |
| 192 | argrest = (args,) |
| 193 | env = environ |
| 194 | global _notfound |
| 195 | head, tail = path.split(file) |
| 196 | if head: |
| 197 | apply(func, (file,) + argrest) |
| 198 | return |
| 199 | if env.has_key('PATH'): |
| 200 | envpath = env['PATH'] |
| 201 | else: |
| 202 | envpath = defpath |
| 203 | import string |
| 204 | PATH = string.splitfields(envpath, pathsep) |
| 205 | if not _notfound: |
| 206 | import tempfile |
| 207 | # Exec a file that is guaranteed not to exist |
| 208 | try: execv(tempfile.mktemp(), ()) |
| 209 | except error, _notfound: pass |
| 210 | exc, arg = error, _notfound |
| 211 | for dir in PATH: |
| 212 | fullname = path.join(dir, file) |
| 213 | try: |
| 214 | apply(func, (fullname,) + argrest) |
| 215 | except error, (errno, msg): |
| 216 | if errno != arg[0]: |
| 217 | exc, arg = error, (errno, msg) |
| 218 | raise exc, arg |
Guido van Rossum | 2979b01 | 1994-08-01 11:18:30 +0000 | [diff] [blame] | 219 | |
Guido van Rossum | 3b8e20d | 1996-07-24 00:55:17 +0000 | [diff] [blame] | 220 | # Change environ to automatically call putenv() if it exists |
| 221 | try: |
Guido van Rossum | 61de0ac | 1997-12-05 21:24:30 +0000 | [diff] [blame] | 222 | # This will fail if there's no putenv |
| 223 | putenv |
Guido van Rossum | 3b8e20d | 1996-07-24 00:55:17 +0000 | [diff] [blame] | 224 | except NameError: |
Guido van Rossum | 61de0ac | 1997-12-05 21:24:30 +0000 | [diff] [blame] | 225 | pass |
Guido van Rossum | a28dab5 | 1997-08-29 22:36:47 +0000 | [diff] [blame] | 226 | else: |
Guido van Rossum | 61de0ac | 1997-12-05 21:24:30 +0000 | [diff] [blame] | 227 | import UserDict |
Guido van Rossum | 3b8e20d | 1996-07-24 00:55:17 +0000 | [diff] [blame] | 228 | |
Guido van Rossum | da4d6da | 1998-08-04 16:01:23 +0000 | [diff] [blame] | 229 | if name in ('os2', 'nt', 'dos'): # Where Env Var Names Must Be UPPERCASE |
| 230 | # But we store them as upper case |
Guido van Rossum | 61de0ac | 1997-12-05 21:24:30 +0000 | [diff] [blame] | 231 | import string |
| 232 | class _Environ(UserDict.UserDict): |
| 233 | def __init__(self, environ): |
| 234 | UserDict.UserDict.__init__(self) |
Guido van Rossum | da4d6da | 1998-08-04 16:01:23 +0000 | [diff] [blame] | 235 | data = self.data |
| 236 | upper = string.upper |
| 237 | for k, v in environ.items(): |
| 238 | data[upper(k)] = v |
Guido van Rossum | 61de0ac | 1997-12-05 21:24:30 +0000 | [diff] [blame] | 239 | def __setitem__(self, key, item): |
Guido van Rossum | 61de0ac | 1997-12-05 21:24:30 +0000 | [diff] [blame] | 240 | putenv(key, item) |
Guido van Rossum | da4d6da | 1998-08-04 16:01:23 +0000 | [diff] [blame] | 241 | key = string.upper(key) |
Guido van Rossum | 61de0ac | 1997-12-05 21:24:30 +0000 | [diff] [blame] | 242 | self.data[key] = item |
| 243 | def __getitem__(self, key): |
| 244 | return self.data[string.upper(key)] |
Guido van Rossum | 3b8e20d | 1996-07-24 00:55:17 +0000 | [diff] [blame] | 245 | |
Guido van Rossum | 61de0ac | 1997-12-05 21:24:30 +0000 | [diff] [blame] | 246 | else: # Where Env Var Names Can Be Mixed Case |
| 247 | class _Environ(UserDict.UserDict): |
| 248 | def __init__(self, environ): |
| 249 | UserDict.UserDict.__init__(self) |
| 250 | self.data = environ |
Guido van Rossum | 61de0ac | 1997-12-05 21:24:30 +0000 | [diff] [blame] | 251 | def __setitem__(self, key, item): |
| 252 | putenv(key, item) |
| 253 | self.data[key] = item |
Guido van Rossum | 61de0ac | 1997-12-05 21:24:30 +0000 | [diff] [blame] | 254 | |
| 255 | environ = _Environ(environ) |