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