Guido van Rossum | 4b8c6ea | 2000-02-04 15:39:30 +0000 | [diff] [blame] | 1 | """OS routines for Mac, DOS, NT, or Posix depending on what system we're on. |
Guido van Rossum | 31104f4 | 1992-01-14 18:28:36 +0000 | [diff] [blame] | 2 | |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 3 | This exports: |
Guido van Rossum | 4b8c6ea | 2000-02-04 15:39:30 +0000 | [diff] [blame] | 4 | - all functions from posix, nt, dos, os2, mac, or ce, e.g. unlink, stat, etc. |
| 5 | - os.path is one of the modules posixpath, ntpath, macpath, or dospath |
| 6 | - os.name is 'posix', 'nt', 'dos', 'os2', 'mac', or 'ce' |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 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 '\\') |
Guido van Rossum | 4b8c6ea | 2000-02-04 15:39:30 +0000 | [diff] [blame] | 10 | - os.altsep is the alternate pathname separator (None or '/') |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 11 | - os.pathsep is the component separator used in $PATH etc |
Guido van Rossum | 4b8c6ea | 2000-02-04 15:39:30 +0000 | [diff] [blame] | 12 | - os.linesep is the line separator in text files ('\r' or '\n' or '\r\n') |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 13 | - os.defpath is the default search path for executables |
Guido van Rossum | 31104f4 | 1992-01-14 18:28:36 +0000 | [diff] [blame] | 14 | |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 15 | Programs that import and use 'os' stand a better chance of being |
| 16 | portable between different platforms. Of course, they must then |
| 17 | only use functions that are defined by all platforms (e.g., unlink |
| 18 | and opendir), and leave all pathname manipulation to os.path |
| 19 | (e.g., split and join). |
| 20 | """ |
Guido van Rossum | 31104f4 | 1992-01-14 18:28:36 +0000 | [diff] [blame] | 21 | |
Skip Montanaro | 269b83b | 2001-02-06 01:07:02 +0000 | [diff] [blame^] | 22 | #' |
| 23 | |
Guido van Rossum | 2979b01 | 1994-08-01 11:18:30 +0000 | [diff] [blame] | 24 | import sys |
Guido van Rossum | a28dab5 | 1997-08-29 22:36:47 +0000 | [diff] [blame] | 25 | |
| 26 | _names = sys.builtin_module_names |
| 27 | |
| 28 | altsep = None |
| 29 | |
Skip Montanaro | 269b83b | 2001-02-06 01:07:02 +0000 | [diff] [blame^] | 30 | __all__ = [] |
| 31 | |
| 32 | def _get_exports_list(module): |
| 33 | try: |
| 34 | return list(module.__all__) |
| 35 | except AttributeError: |
| 36 | return [n for n in dir(module) if n[0] != '_'] |
| 37 | |
Guido van Rossum | a28dab5 | 1997-08-29 22:36:47 +0000 | [diff] [blame] | 38 | if 'posix' in _names: |
Guido van Rossum | 61de0ac | 1997-12-05 21:24:30 +0000 | [diff] [blame] | 39 | name = 'posix' |
Guido van Rossum | e9387ea | 1998-05-22 15:26:04 +0000 | [diff] [blame] | 40 | linesep = '\n' |
Guido van Rossum | 61de0ac | 1997-12-05 21:24:30 +0000 | [diff] [blame] | 41 | curdir = '.'; pardir = '..'; sep = '/'; pathsep = ':' |
| 42 | defpath = ':/bin:/usr/bin' |
| 43 | from posix import * |
| 44 | try: |
| 45 | from posix import _exit |
| 46 | except ImportError: |
| 47 | pass |
| 48 | import posixpath |
| 49 | path = posixpath |
| 50 | del posixpath |
Skip Montanaro | 269b83b | 2001-02-06 01:07:02 +0000 | [diff] [blame^] | 51 | |
| 52 | import posix |
| 53 | __all__.extend(_get_exports_list(posix)) |
| 54 | del posix |
| 55 | |
Guido van Rossum | a28dab5 | 1997-08-29 22:36:47 +0000 | [diff] [blame] | 56 | elif 'nt' in _names: |
Guido van Rossum | 61de0ac | 1997-12-05 21:24:30 +0000 | [diff] [blame] | 57 | name = 'nt' |
Guido van Rossum | e9387ea | 1998-05-22 15:26:04 +0000 | [diff] [blame] | 58 | linesep = '\r\n' |
Guido van Rossum | 61de0ac | 1997-12-05 21:24:30 +0000 | [diff] [blame] | 59 | curdir = '.'; pardir = '..'; sep = '\\'; pathsep = ';' |
| 60 | defpath = '.;C:\\bin' |
| 61 | from nt import * |
Guido van Rossum | fb801e7 | 1999-02-22 15:40:34 +0000 | [diff] [blame] | 62 | for i in ['_exit']: |
Guido van Rossum | 67c65b2 | 1999-02-01 23:52:29 +0000 | [diff] [blame] | 63 | try: |
| 64 | exec "from nt import " + i |
| 65 | except ImportError: |
| 66 | pass |
Guido van Rossum | 61de0ac | 1997-12-05 21:24:30 +0000 | [diff] [blame] | 67 | import ntpath |
| 68 | path = ntpath |
| 69 | del ntpath |
Skip Montanaro | 269b83b | 2001-02-06 01:07:02 +0000 | [diff] [blame^] | 70 | |
| 71 | import nt |
| 72 | __all__.extend(_get_exports_list(nt)) |
| 73 | del nt |
| 74 | |
Guido van Rossum | a28dab5 | 1997-08-29 22:36:47 +0000 | [diff] [blame] | 75 | elif 'dos' in _names: |
Guido van Rossum | 61de0ac | 1997-12-05 21:24:30 +0000 | [diff] [blame] | 76 | name = 'dos' |
Guido van Rossum | e9387ea | 1998-05-22 15:26:04 +0000 | [diff] [blame] | 77 | linesep = '\r\n' |
Guido van Rossum | 61de0ac | 1997-12-05 21:24:30 +0000 | [diff] [blame] | 78 | curdir = '.'; pardir = '..'; sep = '\\'; pathsep = ';' |
| 79 | defpath = '.;C:\\bin' |
| 80 | from dos import * |
| 81 | try: |
| 82 | from dos import _exit |
| 83 | except ImportError: |
| 84 | pass |
| 85 | import dospath |
| 86 | path = dospath |
| 87 | del dospath |
Skip Montanaro | 269b83b | 2001-02-06 01:07:02 +0000 | [diff] [blame^] | 88 | |
| 89 | import dos |
| 90 | __all__.extend(_get_exports_list(dos)) |
| 91 | del dos |
| 92 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 93 | elif 'os2' in _names: |
Guido van Rossum | 61de0ac | 1997-12-05 21:24:30 +0000 | [diff] [blame] | 94 | name = 'os2' |
Guido van Rossum | e9387ea | 1998-05-22 15:26:04 +0000 | [diff] [blame] | 95 | linesep = '\r\n' |
Guido van Rossum | 61de0ac | 1997-12-05 21:24:30 +0000 | [diff] [blame] | 96 | curdir = '.'; pardir = '..'; sep = '\\'; pathsep = ';' |
| 97 | defpath = '.;C:\\bin' |
| 98 | from os2 import * |
| 99 | try: |
| 100 | from os2 import _exit |
| 101 | except ImportError: |
| 102 | pass |
| 103 | import ntpath |
| 104 | path = ntpath |
| 105 | del ntpath |
Skip Montanaro | 269b83b | 2001-02-06 01:07:02 +0000 | [diff] [blame^] | 106 | |
| 107 | import os2 |
| 108 | __all__.extend(_get_exports_list(os2)) |
| 109 | del os2 |
| 110 | |
Guido van Rossum | a28dab5 | 1997-08-29 22:36:47 +0000 | [diff] [blame] | 111 | elif 'mac' in _names: |
Guido van Rossum | 61de0ac | 1997-12-05 21:24:30 +0000 | [diff] [blame] | 112 | name = 'mac' |
Guido van Rossum | e9387ea | 1998-05-22 15:26:04 +0000 | [diff] [blame] | 113 | linesep = '\r' |
Guido van Rossum | 61de0ac | 1997-12-05 21:24:30 +0000 | [diff] [blame] | 114 | curdir = ':'; pardir = '::'; sep = ':'; pathsep = '\n' |
| 115 | defpath = ':' |
| 116 | from mac import * |
| 117 | try: |
| 118 | from mac import _exit |
| 119 | except ImportError: |
| 120 | pass |
| 121 | import macpath |
| 122 | path = macpath |
| 123 | del macpath |
Skip Montanaro | 269b83b | 2001-02-06 01:07:02 +0000 | [diff] [blame^] | 124 | |
| 125 | import mac |
| 126 | __all__.extend(_get_exports_list(mac)) |
| 127 | del mac |
| 128 | |
Guido van Rossum | 18df5d4 | 1999-06-11 01:37:27 +0000 | [diff] [blame] | 129 | elif 'ce' in _names: |
| 130 | name = 'ce' |
| 131 | linesep = '\r\n' |
| 132 | curdir = '.'; pardir = '..'; sep = '\\'; pathsep = ';' |
| 133 | defpath = '\\Windows' |
| 134 | from ce import * |
| 135 | for i in ['_exit']: |
| 136 | try: |
| 137 | exec "from ce import " + i |
| 138 | except ImportError: |
| 139 | pass |
| 140 | # We can use the standard Windows path. |
| 141 | import ntpath |
| 142 | path = ntpath |
| 143 | del ntpath |
Skip Montanaro | 269b83b | 2001-02-06 01:07:02 +0000 | [diff] [blame^] | 144 | |
| 145 | import ce |
| 146 | __all__.extend(_get_exports_list(ce)) |
| 147 | del ce |
| 148 | |
Guido van Rossum | 2979b01 | 1994-08-01 11:18:30 +0000 | [diff] [blame] | 149 | else: |
Guido van Rossum | 61de0ac | 1997-12-05 21:24:30 +0000 | [diff] [blame] | 150 | raise ImportError, 'no os specific module found' |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 151 | |
Skip Montanaro | 269b83b | 2001-02-06 01:07:02 +0000 | [diff] [blame^] | 152 | __all__.append("path") |
| 153 | |
Guido van Rossum | a28dab5 | 1997-08-29 22:36:47 +0000 | [diff] [blame] | 154 | del _names |
| 155 | |
Fred Drake | 0237909 | 1999-01-19 16:05:13 +0000 | [diff] [blame] | 156 | sys.modules['os.path'] = path |
| 157 | |
Skip Montanaro | 269b83b | 2001-02-06 01:07:02 +0000 | [diff] [blame^] | 158 | #' |
| 159 | |
Guido van Rossum | 4def7de | 1998-07-24 20:48:03 +0000 | [diff] [blame] | 160 | # Super directory utilities. |
| 161 | # (Inspired by Eric Raymond; the doc strings are mostly his) |
| 162 | |
| 163 | def makedirs(name, mode=0777): |
| 164 | """makedirs(path [, mode=0777]) -> None |
| 165 | |
| 166 | Super-mkdir; create a leaf directory and all intermediate ones. |
| 167 | Works like mkdir, except that any intermediate path segment (not |
| 168 | just the rightmost) will be created if it does not exist. This is |
| 169 | recursive. |
| 170 | |
| 171 | """ |
| 172 | head, tail = path.split(name) |
Fred Drake | 9f2550f | 2000-07-25 15:16:40 +0000 | [diff] [blame] | 173 | if not tail: |
| 174 | head, tail = path.split(head) |
Guido van Rossum | 4def7de | 1998-07-24 20:48:03 +0000 | [diff] [blame] | 175 | if head and tail and not path.exists(head): |
| 176 | makedirs(head, mode) |
| 177 | mkdir(name, mode) |
| 178 | |
| 179 | def removedirs(name): |
| 180 | """removedirs(path) -> None |
| 181 | |
| 182 | Super-rmdir; remove a leaf directory and empty all intermediate |
| 183 | ones. Works like rmdir except that, if the leaf directory is |
| 184 | successfully removed, directories corresponding to rightmost path |
| 185 | segments will be pruned way until either the whole path is |
| 186 | consumed or an error occurs. Errors during this latter phase are |
| 187 | ignored -- they generally mean that a directory was not empty. |
| 188 | |
| 189 | """ |
| 190 | rmdir(name) |
| 191 | head, tail = path.split(name) |
Fred Drake | 9f2550f | 2000-07-25 15:16:40 +0000 | [diff] [blame] | 192 | if not tail: |
| 193 | head, tail = path.split(head) |
Guido van Rossum | 4def7de | 1998-07-24 20:48:03 +0000 | [diff] [blame] | 194 | while head and tail: |
| 195 | try: |
| 196 | rmdir(head) |
| 197 | except error: |
| 198 | break |
| 199 | head, tail = path.split(head) |
| 200 | |
| 201 | def renames(old, new): |
| 202 | """renames(old, new) -> None |
| 203 | |
| 204 | Super-rename; create directories as necessary and delete any left |
| 205 | empty. Works like rename, except creation of any intermediate |
| 206 | directories needed to make the new pathname good is attempted |
| 207 | first. After the rename, directories corresponding to rightmost |
| 208 | path segments of the old name will be pruned way until either the |
| 209 | whole path is consumed or a nonempty directory is found. |
| 210 | |
| 211 | Note: this function can fail with the new directory structure made |
| 212 | if you lack permissions needed to unlink the leaf directory or |
| 213 | file. |
| 214 | |
| 215 | """ |
| 216 | head, tail = path.split(new) |
| 217 | if head and tail and not path.exists(head): |
| 218 | makedirs(head) |
| 219 | rename(old, new) |
| 220 | head, tail = path.split(old) |
| 221 | if head and tail: |
| 222 | try: |
| 223 | removedirs(head) |
| 224 | except error: |
| 225 | pass |
| 226 | |
Skip Montanaro | 269b83b | 2001-02-06 01:07:02 +0000 | [diff] [blame^] | 227 | __all__.extend(["makedirs", "removedirs", "renames"]) |
| 228 | |
Guido van Rossum | a28dab5 | 1997-08-29 22:36:47 +0000 | [diff] [blame] | 229 | # Make sure os.environ exists, at least |
| 230 | try: |
Guido van Rossum | 61de0ac | 1997-12-05 21:24:30 +0000 | [diff] [blame] | 231 | environ |
Guido van Rossum | a28dab5 | 1997-08-29 22:36:47 +0000 | [diff] [blame] | 232 | except NameError: |
Guido van Rossum | 61de0ac | 1997-12-05 21:24:30 +0000 | [diff] [blame] | 233 | environ = {} |
Guido van Rossum | a28dab5 | 1997-08-29 22:36:47 +0000 | [diff] [blame] | 234 | |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 235 | def execl(file, *args): |
Guido van Rossum | 7da3cc5 | 2000-04-25 10:53:22 +0000 | [diff] [blame] | 236 | """execl(file, *args) |
| 237 | |
| 238 | Execute the executable file with argument list args, replacing the |
| 239 | current process. """ |
Guido van Rossum | 61de0ac | 1997-12-05 21:24:30 +0000 | [diff] [blame] | 240 | execv(file, args) |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 241 | |
| 242 | def execle(file, *args): |
Guido van Rossum | 7da3cc5 | 2000-04-25 10:53:22 +0000 | [diff] [blame] | 243 | """execle(file, *args, env) |
| 244 | |
| 245 | Execute the executable file with argument list args and |
| 246 | environment env, replacing the current process. """ |
Guido van Rossum | 61de0ac | 1997-12-05 21:24:30 +0000 | [diff] [blame] | 247 | env = args[-1] |
| 248 | execve(file, args[:-1], env) |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 249 | |
| 250 | def execlp(file, *args): |
Guido van Rossum | 7da3cc5 | 2000-04-25 10:53:22 +0000 | [diff] [blame] | 251 | """execlp(file, *args) |
| 252 | |
| 253 | Execute the executable file (which is searched for along $PATH) |
| 254 | with argument list args, replacing the current process. """ |
Guido van Rossum | 61de0ac | 1997-12-05 21:24:30 +0000 | [diff] [blame] | 255 | execvp(file, args) |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 256 | |
Guido van Rossum | 030afb1 | 1995-03-14 17:27:18 +0000 | [diff] [blame] | 257 | def execlpe(file, *args): |
Guido van Rossum | 7da3cc5 | 2000-04-25 10:53:22 +0000 | [diff] [blame] | 258 | """execlpe(file, *args, env) |
| 259 | |
| 260 | Execute the executable file (which is searched for along $PATH) |
| 261 | with argument list args and environment env, replacing the current |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 262 | process. """ |
Guido van Rossum | 61de0ac | 1997-12-05 21:24:30 +0000 | [diff] [blame] | 263 | env = args[-1] |
| 264 | execvpe(file, args[:-1], env) |
Guido van Rossum | 030afb1 | 1995-03-14 17:27:18 +0000 | [diff] [blame] | 265 | |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 266 | def execvp(file, args): |
Guido van Rossum | 7da3cc5 | 2000-04-25 10:53:22 +0000 | [diff] [blame] | 267 | """execp(file, args) |
| 268 | |
| 269 | Execute the executable file (which is searched for along $PATH) |
| 270 | with argument list args, replacing the current process. |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 271 | args may be a list or tuple of strings. """ |
Guido van Rossum | 61de0ac | 1997-12-05 21:24:30 +0000 | [diff] [blame] | 272 | _execvpe(file, args) |
Guido van Rossum | 030afb1 | 1995-03-14 17:27:18 +0000 | [diff] [blame] | 273 | |
| 274 | def execvpe(file, args, env): |
Guido van Rossum | 7da3cc5 | 2000-04-25 10:53:22 +0000 | [diff] [blame] | 275 | """execv(file, args, env) |
| 276 | |
| 277 | Execute the executable file (which is searched for along $PATH) |
| 278 | with argument list args and environment env , replacing the |
| 279 | current process. |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 280 | args may be a list or tuple of strings. """ |
Guido van Rossum | 61de0ac | 1997-12-05 21:24:30 +0000 | [diff] [blame] | 281 | _execvpe(file, args, env) |
Guido van Rossum | 030afb1 | 1995-03-14 17:27:18 +0000 | [diff] [blame] | 282 | |
Skip Montanaro | 269b83b | 2001-02-06 01:07:02 +0000 | [diff] [blame^] | 283 | __all__.extend(["execl","execle","execlp","execlpe","execvp","execvpe"]) |
| 284 | |
Guido van Rossum | 030afb1 | 1995-03-14 17:27:18 +0000 | [diff] [blame] | 285 | _notfound = None |
Guido van Rossum | 5a2ca93 | 1999-11-02 13:27:32 +0000 | [diff] [blame] | 286 | def _execvpe(file, args, env=None): |
| 287 | if env is not None: |
Guido van Rossum | 61de0ac | 1997-12-05 21:24:30 +0000 | [diff] [blame] | 288 | func = execve |
| 289 | argrest = (args, env) |
| 290 | else: |
| 291 | func = execv |
| 292 | argrest = (args,) |
| 293 | env = environ |
| 294 | global _notfound |
| 295 | head, tail = path.split(file) |
| 296 | if head: |
| 297 | apply(func, (file,) + argrest) |
| 298 | return |
| 299 | if env.has_key('PATH'): |
| 300 | envpath = env['PATH'] |
| 301 | else: |
| 302 | envpath = defpath |
Guido van Rossum | 965fdae | 2000-04-04 19:50:04 +0000 | [diff] [blame] | 303 | PATH = envpath.split(pathsep) |
Guido van Rossum | 61de0ac | 1997-12-05 21:24:30 +0000 | [diff] [blame] | 304 | if not _notfound: |
| 305 | import tempfile |
| 306 | # Exec a file that is guaranteed not to exist |
Guido van Rossum | 868b50a | 2000-04-26 20:32:08 +0000 | [diff] [blame] | 307 | try: execv(tempfile.mktemp(), ('blah',)) |
Guido van Rossum | 61de0ac | 1997-12-05 21:24:30 +0000 | [diff] [blame] | 308 | except error, _notfound: pass |
| 309 | exc, arg = error, _notfound |
| 310 | for dir in PATH: |
| 311 | fullname = path.join(dir, file) |
| 312 | try: |
| 313 | apply(func, (fullname,) + argrest) |
| 314 | except error, (errno, msg): |
| 315 | if errno != arg[0]: |
| 316 | exc, arg = error, (errno, msg) |
| 317 | raise exc, arg |
Guido van Rossum | 2979b01 | 1994-08-01 11:18:30 +0000 | [diff] [blame] | 318 | |
Guido van Rossum | 3b8e20d | 1996-07-24 00:55:17 +0000 | [diff] [blame] | 319 | # Change environ to automatically call putenv() if it exists |
| 320 | try: |
Guido van Rossum | 61de0ac | 1997-12-05 21:24:30 +0000 | [diff] [blame] | 321 | # This will fail if there's no putenv |
| 322 | putenv |
Guido van Rossum | 3b8e20d | 1996-07-24 00:55:17 +0000 | [diff] [blame] | 323 | except NameError: |
Guido van Rossum | 61de0ac | 1997-12-05 21:24:30 +0000 | [diff] [blame] | 324 | pass |
Guido van Rossum | a28dab5 | 1997-08-29 22:36:47 +0000 | [diff] [blame] | 325 | else: |
Guido van Rossum | 61de0ac | 1997-12-05 21:24:30 +0000 | [diff] [blame] | 326 | import UserDict |
Guido van Rossum | 3b8e20d | 1996-07-24 00:55:17 +0000 | [diff] [blame] | 327 | |
Guido van Rossum | da4d6da | 1998-08-04 16:01:23 +0000 | [diff] [blame] | 328 | if name in ('os2', 'nt', 'dos'): # Where Env Var Names Must Be UPPERCASE |
| 329 | # But we store them as upper case |
Guido van Rossum | 61de0ac | 1997-12-05 21:24:30 +0000 | [diff] [blame] | 330 | class _Environ(UserDict.UserDict): |
| 331 | def __init__(self, environ): |
| 332 | UserDict.UserDict.__init__(self) |
Guido van Rossum | da4d6da | 1998-08-04 16:01:23 +0000 | [diff] [blame] | 333 | data = self.data |
Guido van Rossum | da4d6da | 1998-08-04 16:01:23 +0000 | [diff] [blame] | 334 | for k, v in environ.items(): |
Guido van Rossum | 965fdae | 2000-04-04 19:50:04 +0000 | [diff] [blame] | 335 | data[k.upper()] = v |
Guido van Rossum | 61de0ac | 1997-12-05 21:24:30 +0000 | [diff] [blame] | 336 | def __setitem__(self, key, item): |
Guido van Rossum | 61de0ac | 1997-12-05 21:24:30 +0000 | [diff] [blame] | 337 | putenv(key, item) |
Guido van Rossum | 965fdae | 2000-04-04 19:50:04 +0000 | [diff] [blame] | 338 | self.data[key.upper()] = item |
Guido van Rossum | 61de0ac | 1997-12-05 21:24:30 +0000 | [diff] [blame] | 339 | def __getitem__(self, key): |
Guido van Rossum | 965fdae | 2000-04-04 19:50:04 +0000 | [diff] [blame] | 340 | return self.data[key.upper()] |
| 341 | def __delitem__(self, key): |
| 342 | del self.data[key.upper()] |
Guido van Rossum | b46413f | 1999-05-03 15:23:24 +0000 | [diff] [blame] | 343 | def has_key(self, key): |
Guido van Rossum | 965fdae | 2000-04-04 19:50:04 +0000 | [diff] [blame] | 344 | return self.data.has_key(key.upper()) |
| 345 | def get(self, key, failobj=None): |
| 346 | return self.data.get(key.upper(), failobj) |
| 347 | def update(self, dict): |
| 348 | for k, v in dict.items(): |
| 349 | self[k] = v |
Guido van Rossum | 3b8e20d | 1996-07-24 00:55:17 +0000 | [diff] [blame] | 350 | |
Guido van Rossum | 61de0ac | 1997-12-05 21:24:30 +0000 | [diff] [blame] | 351 | else: # Where Env Var Names Can Be Mixed Case |
| 352 | class _Environ(UserDict.UserDict): |
| 353 | def __init__(self, environ): |
| 354 | UserDict.UserDict.__init__(self) |
| 355 | self.data = environ |
Guido van Rossum | 61de0ac | 1997-12-05 21:24:30 +0000 | [diff] [blame] | 356 | def __setitem__(self, key, item): |
| 357 | putenv(key, item) |
| 358 | self.data[key] = item |
Guido van Rossum | 965fdae | 2000-04-04 19:50:04 +0000 | [diff] [blame] | 359 | def update(self, dict): |
| 360 | for k, v in dict.items(): |
| 361 | self[k] = v |
Guido van Rossum | 61de0ac | 1997-12-05 21:24:30 +0000 | [diff] [blame] | 362 | |
| 363 | environ = _Environ(environ) |
Guido van Rossum | 5a2ca93 | 1999-11-02 13:27:32 +0000 | [diff] [blame] | 364 | |
| 365 | def getenv(key, default=None): |
| 366 | """Get an environment variable, return None if it doesn't exist. |
| 367 | |
Fred Drake | 20af317 | 2000-09-28 19:10:56 +0000 | [diff] [blame] | 368 | The optional second argument can specify an alternate default.""" |
Guido van Rossum | 5a2ca93 | 1999-11-02 13:27:32 +0000 | [diff] [blame] | 369 | return environ.get(key, default) |
Skip Montanaro | 269b83b | 2001-02-06 01:07:02 +0000 | [diff] [blame^] | 370 | __all__.append("getenv") |
Guido van Rossum | 5a2ca93 | 1999-11-02 13:27:32 +0000 | [diff] [blame] | 371 | |
| 372 | def _exists(name): |
| 373 | try: |
| 374 | eval(name) |
| 375 | return 1 |
| 376 | except NameError: |
| 377 | return 0 |
| 378 | |
| 379 | # Supply spawn*() (probably only for Unix) |
| 380 | if _exists("fork") and not _exists("spawnv") and _exists("execv"): |
| 381 | |
| 382 | P_WAIT = 0 |
| 383 | P_NOWAIT = P_NOWAITO = 1 |
| 384 | |
| 385 | # XXX Should we support P_DETACH? I suppose it could fork()**2 |
| 386 | # and close the std I/O streams. Also, P_OVERLAY is the same |
| 387 | # as execv*()? |
| 388 | |
| 389 | def _spawnvef(mode, file, args, env, func): |
| 390 | # Internal helper; func is the exec*() function to use |
| 391 | pid = fork() |
| 392 | if not pid: |
| 393 | # Child |
| 394 | try: |
| 395 | if env is None: |
| 396 | func(file, args) |
| 397 | else: |
| 398 | func(file, args, env) |
| 399 | except: |
| 400 | _exit(127) |
| 401 | else: |
| 402 | # Parent |
| 403 | if mode == P_NOWAIT: |
| 404 | return pid # Caller is responsible for waiting! |
| 405 | while 1: |
| 406 | wpid, sts = waitpid(pid, 0) |
| 407 | if WIFSTOPPED(sts): |
| 408 | continue |
| 409 | elif WIFSIGNALED(sts): |
| 410 | return -WTERMSIG(sts) |
| 411 | elif WIFEXITED(sts): |
| 412 | return WEXITSTATUS(sts) |
| 413 | else: |
| 414 | raise error, "Not stopped, signaled or exited???" |
| 415 | |
| 416 | def spawnv(mode, file, args): |
Guido van Rossum | e0cd291 | 2000-04-21 18:35:36 +0000 | [diff] [blame] | 417 | """spawnv(mode, file, args) -> integer |
| 418 | |
| 419 | Execute file with arguments from args in a subprocess. |
| 420 | If mode == P_NOWAIT return the pid of the process. |
| 421 | If mode == P_WAIT return the process's exit code if it exits normally; |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 422 | otherwise return -SIG, where SIG is the signal that killed it. """ |
Guido van Rossum | 5a2ca93 | 1999-11-02 13:27:32 +0000 | [diff] [blame] | 423 | return _spawnvef(mode, file, args, None, execv) |
| 424 | |
| 425 | def spawnve(mode, file, args, env): |
Guido van Rossum | e0cd291 | 2000-04-21 18:35:36 +0000 | [diff] [blame] | 426 | """spawnve(mode, file, args, env) -> integer |
| 427 | |
| 428 | Execute file with arguments from args in a subprocess with the |
| 429 | specified environment. |
| 430 | If mode == P_NOWAIT return the pid of the process. |
| 431 | If mode == P_WAIT return the process's exit code if it exits normally; |
| 432 | otherwise return -SIG, where SIG is the signal that killed it. """ |
Guido van Rossum | 5a2ca93 | 1999-11-02 13:27:32 +0000 | [diff] [blame] | 433 | return _spawnvef(mode, file, args, env, execve) |
| 434 | |
Guido van Rossum | dd7cbbf | 1999-11-02 20:44:07 +0000 | [diff] [blame] | 435 | # Note: spawnvp[e] is't currently supported on Windows |
| 436 | |
| 437 | def spawnvp(mode, file, args): |
Guido van Rossum | e0cd291 | 2000-04-21 18:35:36 +0000 | [diff] [blame] | 438 | """spawnvp(mode, file, args) -> integer |
| 439 | |
| 440 | Execute file (which is looked for along $PATH) with arguments from |
| 441 | args in a subprocess. |
| 442 | If mode == P_NOWAIT return the pid of the process. |
| 443 | If mode == P_WAIT return the process's exit code if it exits normally; |
| 444 | otherwise return -SIG, where SIG is the signal that killed it. """ |
Guido van Rossum | dd7cbbf | 1999-11-02 20:44:07 +0000 | [diff] [blame] | 445 | return _spawnvef(mode, file, args, None, execvp) |
| 446 | |
| 447 | def spawnvpe(mode, file, args, env): |
Guido van Rossum | e0cd291 | 2000-04-21 18:35:36 +0000 | [diff] [blame] | 448 | """spawnvpe(mode, file, args, env) -> integer |
| 449 | |
| 450 | Execute file (which is looked for along $PATH) with arguments from |
| 451 | args in a subprocess with the supplied environment. |
| 452 | If mode == P_NOWAIT return the pid of the process. |
| 453 | If mode == P_WAIT return the process's exit code if it exits normally; |
| 454 | otherwise return -SIG, where SIG is the signal that killed it. """ |
Guido van Rossum | dd7cbbf | 1999-11-02 20:44:07 +0000 | [diff] [blame] | 455 | return _spawnvef(mode, file, args, env, execvpe) |
| 456 | |
| 457 | if _exists("spawnv"): |
| 458 | # These aren't supplied by the basic Windows code |
| 459 | # but can be easily implemented in Python |
Guido van Rossum | 5a2ca93 | 1999-11-02 13:27:32 +0000 | [diff] [blame] | 460 | |
| 461 | def spawnl(mode, file, *args): |
Guido van Rossum | e0cd291 | 2000-04-21 18:35:36 +0000 | [diff] [blame] | 462 | """spawnl(mode, file, *args) -> integer |
| 463 | |
| 464 | Execute file with arguments from args in a subprocess. |
| 465 | If mode == P_NOWAIT return the pid of the process. |
| 466 | If mode == P_WAIT return the process's exit code if it exits normally; |
| 467 | otherwise return -SIG, where SIG is the signal that killed it. """ |
Guido van Rossum | 5a2ca93 | 1999-11-02 13:27:32 +0000 | [diff] [blame] | 468 | return spawnv(mode, file, args) |
| 469 | |
| 470 | def spawnle(mode, file, *args): |
Guido van Rossum | e0cd291 | 2000-04-21 18:35:36 +0000 | [diff] [blame] | 471 | """spawnle(mode, file, *args, env) -> integer |
| 472 | |
| 473 | Execute file with arguments from args in a subprocess with the |
| 474 | supplied environment. |
| 475 | If mode == P_NOWAIT return the pid of the process. |
| 476 | If mode == P_WAIT return the process's exit code if it exits normally; |
| 477 | otherwise return -SIG, where SIG is the signal that killed it. """ |
Guido van Rossum | 5a2ca93 | 1999-11-02 13:27:32 +0000 | [diff] [blame] | 478 | env = args[-1] |
| 479 | return spawnve(mode, file, args[:-1], env) |
| 480 | |
Guido van Rossum | dd7cbbf | 1999-11-02 20:44:07 +0000 | [diff] [blame] | 481 | if _exists("spawnvp"): |
| 482 | # At the moment, Windows doesn't implement spawnvp[e], |
| 483 | # so it won't have spawnlp[e] either. |
Guido van Rossum | 5a2ca93 | 1999-11-02 13:27:32 +0000 | [diff] [blame] | 484 | def spawnlp(mode, file, *args): |
Guido van Rossum | e0cd291 | 2000-04-21 18:35:36 +0000 | [diff] [blame] | 485 | """spawnlp(mode, file, *args, env) -> integer |
| 486 | |
| 487 | Execute file (which is looked for along $PATH) with arguments from |
| 488 | args in a subprocess with the supplied environment. |
| 489 | If mode == P_NOWAIT return the pid of the process. |
| 490 | If mode == P_WAIT return the process's exit code if it exits normally; |
| 491 | otherwise return -SIG, where SIG is the signal that killed it. """ |
Guido van Rossum | 5a2ca93 | 1999-11-02 13:27:32 +0000 | [diff] [blame] | 492 | return spawnvp(mode, file, args) |
| 493 | |
| 494 | def spawnlpe(mode, file, *args): |
Guido van Rossum | e0cd291 | 2000-04-21 18:35:36 +0000 | [diff] [blame] | 495 | """spawnlpe(mode, file, *args, env) -> integer |
| 496 | |
| 497 | Execute file (which is looked for along $PATH) with arguments from |
| 498 | args in a subprocess with the supplied environment. |
| 499 | If mode == P_NOWAIT return the pid of the process. |
| 500 | If mode == P_WAIT return the process's exit code if it exits normally; |
| 501 | otherwise return -SIG, where SIG is the signal that killed it. """ |
Guido van Rossum | 5a2ca93 | 1999-11-02 13:27:32 +0000 | [diff] [blame] | 502 | env = args[-1] |
| 503 | return spawnvpe(mode, file, args[:-1], env) |
Guido van Rossum | e0cd291 | 2000-04-21 18:35:36 +0000 | [diff] [blame] | 504 | |
| 505 | |
Skip Montanaro | 269b83b | 2001-02-06 01:07:02 +0000 | [diff] [blame^] | 506 | __all__.extend(["spawnlp","spawnlpe","spawnv", "spawnve","spawnvp", |
| 507 | "spawnvpe","spawnl","spawnle",]) |
| 508 | |
| 509 | |
Guido van Rossum | d9a8e96 | 2000-09-19 03:04:52 +0000 | [diff] [blame] | 510 | # Supply popen2 etc. (for Unix) |
| 511 | if _exists("fork"): |
| 512 | if not _exists("popen2"): |
| 513 | def popen2(cmd, mode="t", bufsize=-1): |
Guido van Rossum | d9a8e96 | 2000-09-19 03:04:52 +0000 | [diff] [blame] | 514 | import popen2 |
| 515 | stdout, stdin = popen2.popen2(cmd, bufsize) |
| 516 | return stdin, stdout |
Skip Montanaro | 269b83b | 2001-02-06 01:07:02 +0000 | [diff] [blame^] | 517 | __all__.append("popen2") |
Fred Drake | 31f182e | 2000-08-28 17:20:05 +0000 | [diff] [blame] | 518 | |
Guido van Rossum | d9a8e96 | 2000-09-19 03:04:52 +0000 | [diff] [blame] | 519 | if not _exists("popen3"): |
| 520 | def popen3(cmd, mode="t", bufsize=-1): |
Guido van Rossum | d9a8e96 | 2000-09-19 03:04:52 +0000 | [diff] [blame] | 521 | import popen2 |
| 522 | stdout, stdin, stderr = popen2.popen3(cmd, bufsize) |
| 523 | return stdin, stdout, stderr |
Skip Montanaro | 269b83b | 2001-02-06 01:07:02 +0000 | [diff] [blame^] | 524 | __all__.append("popen3") |
Fred Drake | 20af317 | 2000-09-28 19:10:56 +0000 | [diff] [blame] | 525 | |
| 526 | if not _exists("popen4"): |
| 527 | def popen4(cmd, mode="t", bufsize=-1): |
| 528 | import popen2 |
| 529 | stdout, stdin = popen2.popen4(cmd, bufsize) |
| 530 | return stdin, stdout |
Skip Montanaro | 269b83b | 2001-02-06 01:07:02 +0000 | [diff] [blame^] | 531 | __all__.append("popen4") |