]>
Commit | Line | Data |
---|---|---|
d11973f1 DM |
1 | \r |
2 | # Module 'os' -- OS routines for NT, Posix, or UEFI depending on what system we're on.\r | |
3 | #\r | |
4 | # Copyright (c) 2015, Daryl McDaniel. All rights reserved.<BR>\r | |
5 | # Copyright (c) 2011 - 2012, Intel Corporation. All rights reserved.<BR>\r | |
6 | # This program and the accompanying materials are licensed and made available under\r | |
7 | # the terms and conditions of the BSD License that accompanies this distribution.\r | |
8 | # The full text of the license may be found at\r | |
9 | # http://opensource.org/licenses/bsd-license.\r | |
10 | #\r | |
11 | # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
12 | # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
13 | \r | |
14 | r"""OS routines for NT, Posix, or UEFI depending on what system we're on.\r | |
3ec97ca4 DM |
15 | \r |
16 | This exports:\r | |
d11973f1 DM |
17 | - all functions from edk2, posix, nt, os2, or ce, e.g. unlink, stat, etc.\r |
18 | - os.path is one of the modules uefipath, posixpath, or ntpath\r | |
19 | - os.name is 'edk2', 'posix', 'nt', 'os2', 'ce' or 'riscos'\r | |
3ec97ca4 DM |
20 | - os.curdir is a string representing the current directory ('.' or ':')\r |
21 | - os.pardir is a string representing the parent directory ('..' or '::')\r | |
22 | - os.sep is the (or a most common) pathname separator ('/' or ':' or '\\')\r | |
23 | - os.extsep is the extension separator ('.' or '/')\r | |
24 | - os.altsep is the alternate pathname separator (None or '/')\r | |
25 | - os.pathsep is the component separator used in $PATH etc\r | |
26 | - os.linesep is the line separator in text files ('\r' or '\n' or '\r\n')\r | |
27 | - os.defpath is the default search path for executables\r | |
28 | - os.devnull is the file path of the null device ('/dev/null', etc.)\r | |
29 | \r | |
30 | Programs that import and use 'os' stand a better chance of being\r | |
31 | portable between different platforms. Of course, they must then\r | |
32 | only use functions that are defined by all platforms (e.g., unlink\r | |
33 | and opendir), and leave all pathname manipulation to os.path\r | |
34 | (e.g., split and join).\r | |
35 | """\r | |
36 | \r | |
37 | #'\r | |
38 | \r | |
39 | import sys, errno\r | |
40 | \r | |
41 | _names = sys.builtin_module_names\r | |
42 | \r | |
43 | # Note: more names are added to __all__ later.\r | |
44 | __all__ = ["altsep", "curdir", "pardir", "sep", "extsep", "pathsep", "linesep",\r | |
45 | "defpath", "name", "path", "devnull",\r | |
46 | "SEEK_SET", "SEEK_CUR", "SEEK_END"]\r | |
47 | \r | |
48 | def _get_exports_list(module):\r | |
49 | try:\r | |
50 | return list(module.__all__)\r | |
51 | except AttributeError:\r | |
52 | return [n for n in dir(module) if n[0] != '_']\r | |
53 | \r | |
54 | if 'posix' in _names:\r | |
55 | name = 'posix'\r | |
56 | linesep = '\n'\r | |
57 | from posix import *\r | |
58 | try:\r | |
59 | from posix import _exit\r | |
60 | except ImportError:\r | |
61 | pass\r | |
62 | import posixpath as path\r | |
63 | \r | |
64 | import posix\r | |
65 | __all__.extend(_get_exports_list(posix))\r | |
66 | del posix\r | |
67 | \r | |
68 | elif 'nt' in _names:\r | |
69 | name = 'nt'\r | |
70 | linesep = '\r\n'\r | |
71 | from nt import *\r | |
72 | try:\r | |
73 | from nt import _exit\r | |
74 | except ImportError:\r | |
75 | pass\r | |
76 | import ntpath as path\r | |
77 | \r | |
78 | import nt\r | |
79 | __all__.extend(_get_exports_list(nt))\r | |
80 | del nt\r | |
81 | \r | |
82 | elif 'os2' in _names:\r | |
83 | name = 'os2'\r | |
84 | linesep = '\r\n'\r | |
85 | from os2 import *\r | |
86 | try:\r | |
87 | from os2 import _exit\r | |
88 | except ImportError:\r | |
89 | pass\r | |
90 | if sys.version.find('EMX GCC') == -1:\r | |
91 | import ntpath as path\r | |
92 | else:\r | |
93 | import os2emxpath as path\r | |
94 | from _emx_link import link\r | |
95 | \r | |
96 | import os2\r | |
97 | __all__.extend(_get_exports_list(os2))\r | |
98 | del os2\r | |
99 | \r | |
100 | elif 'ce' in _names:\r | |
101 | name = 'ce'\r | |
102 | linesep = '\r\n'\r | |
103 | from ce import *\r | |
104 | try:\r | |
105 | from ce import _exit\r | |
106 | except ImportError:\r | |
107 | pass\r | |
108 | # We can use the standard Windows path.\r | |
109 | import ntpath as path\r | |
110 | \r | |
111 | import ce\r | |
112 | __all__.extend(_get_exports_list(ce))\r | |
113 | del ce\r | |
114 | \r | |
115 | elif 'riscos' in _names:\r | |
116 | name = 'riscos'\r | |
117 | linesep = '\n'\r | |
118 | from riscos import *\r | |
119 | try:\r | |
120 | from riscos import _exit\r | |
121 | except ImportError:\r | |
122 | pass\r | |
123 | import riscospath as path\r | |
124 | \r | |
125 | import riscos\r | |
126 | __all__.extend(_get_exports_list(riscos))\r | |
127 | del riscos\r | |
128 | \r | |
d11973f1 DM |
129 | elif 'edk2' in _names:\r |
130 | name = 'edk2'\r | |
131 | linesep = '\n'\r | |
132 | from edk2 import *\r | |
133 | try:\r | |
134 | from edk2 import _exit\r | |
135 | except ImportError:\r | |
136 | pass\r | |
137 | import ntpath as path\r | |
138 | \r | |
139 | import edk2\r | |
140 | __all__.extend(_get_exports_list(edk2))\r | |
141 | del edk2\r | |
142 | \r | |
3ec97ca4 DM |
143 | else:\r |
144 | raise ImportError, 'no os specific module found'\r | |
145 | \r | |
146 | sys.modules['os.path'] = path\r | |
147 | from os.path import (curdir, pardir, sep, pathsep, defpath, extsep, altsep,\r | |
148 | devnull)\r | |
149 | \r | |
150 | del _names\r | |
151 | \r | |
152 | # Python uses fixed values for the SEEK_ constants; they are mapped\r | |
153 | # to native constants if necessary in posixmodule.c\r | |
154 | SEEK_SET = 0\r | |
155 | SEEK_CUR = 1\r | |
156 | SEEK_END = 2\r | |
157 | \r | |
158 | #'\r | |
159 | \r | |
160 | # Super directory utilities.\r | |
161 | # (Inspired by Eric Raymond; the doc strings are mostly his)\r | |
162 | \r | |
163 | def makedirs(name, mode=0777):\r | |
164 | """makedirs(path [, mode=0777])\r | |
165 | \r | |
166 | Super-mkdir; create a leaf directory and all intermediate ones.\r | |
167 | Works like mkdir, except that any intermediate path segment (not\r | |
168 | just the rightmost) will be created if it does not exist. This is\r | |
169 | recursive.\r | |
170 | \r | |
171 | """\r | |
172 | head, tail = path.split(name)\r | |
173 | if not tail:\r | |
174 | head, tail = path.split(head)\r | |
175 | if head and tail and not path.exists(head):\r | |
176 | try:\r | |
177 | makedirs(head, mode)\r | |
178 | except OSError, e:\r | |
179 | # be happy if someone already created the path\r | |
180 | if e.errno != errno.EEXIST:\r | |
181 | raise\r | |
182 | if tail == curdir: # xxx/newdir/. exists if xxx/newdir exists\r | |
183 | return\r | |
184 | mkdir(name, mode)\r | |
185 | \r | |
186 | def removedirs(name):\r | |
187 | """removedirs(path)\r | |
188 | \r | |
189 | Super-rmdir; remove a leaf directory and all empty intermediate\r | |
190 | ones. Works like rmdir except that, if the leaf directory is\r | |
191 | successfully removed, directories corresponding to rightmost path\r | |
192 | segments will be pruned away until either the whole path is\r | |
193 | consumed or an error occurs. Errors during this latter phase are\r | |
194 | ignored -- they generally mean that a directory was not empty.\r | |
195 | \r | |
196 | """\r | |
197 | rmdir(name)\r | |
198 | head, tail = path.split(name)\r | |
199 | if not tail:\r | |
200 | head, tail = path.split(head)\r | |
201 | while head and tail:\r | |
202 | try:\r | |
203 | rmdir(head)\r | |
204 | except error:\r | |
205 | break\r | |
206 | head, tail = path.split(head)\r | |
207 | \r | |
208 | def renames(old, new):\r | |
209 | """renames(old, new)\r | |
210 | \r | |
211 | Super-rename; create directories as necessary and delete any left\r | |
212 | empty. Works like rename, except creation of any intermediate\r | |
213 | directories needed to make the new pathname good is attempted\r | |
214 | first. After the rename, directories corresponding to rightmost\r | |
215 | path segments of the old name will be pruned until either the\r | |
216 | whole path is consumed or a nonempty directory is found.\r | |
217 | \r | |
218 | Note: this function can fail with the new directory structure made\r | |
219 | if you lack permissions needed to unlink the leaf directory or\r | |
220 | file.\r | |
221 | \r | |
222 | """\r | |
223 | head, tail = path.split(new)\r | |
224 | if head and tail and not path.exists(head):\r | |
225 | makedirs(head)\r | |
226 | rename(old, new)\r | |
227 | head, tail = path.split(old)\r | |
228 | if head and tail:\r | |
229 | try:\r | |
230 | removedirs(head)\r | |
231 | except error:\r | |
232 | pass\r | |
233 | \r | |
234 | __all__.extend(["makedirs", "removedirs", "renames"])\r | |
235 | \r | |
236 | def walk(top, topdown=True, onerror=None, followlinks=False):\r | |
237 | """Directory tree generator.\r | |
238 | \r | |
239 | For each directory in the directory tree rooted at top (including top\r | |
240 | itself, but excluding '.' and '..'), yields a 3-tuple\r | |
241 | \r | |
242 | dirpath, dirnames, filenames\r | |
243 | \r | |
244 | dirpath is a string, the path to the directory. dirnames is a list of\r | |
245 | the names of the subdirectories in dirpath (excluding '.' and '..').\r | |
246 | filenames is a list of the names of the non-directory files in dirpath.\r | |
247 | Note that the names in the lists are just names, with no path components.\r | |
248 | To get a full path (which begins with top) to a file or directory in\r | |
249 | dirpath, do os.path.join(dirpath, name).\r | |
250 | \r | |
251 | If optional arg 'topdown' is true or not specified, the triple for a\r | |
252 | directory is generated before the triples for any of its subdirectories\r | |
253 | (directories are generated top down). If topdown is false, the triple\r | |
254 | for a directory is generated after the triples for all of its\r | |
255 | subdirectories (directories are generated bottom up).\r | |
256 | \r | |
257 | When topdown is true, the caller can modify the dirnames list in-place\r | |
258 | (e.g., via del or slice assignment), and walk will only recurse into the\r | |
259 | subdirectories whose names remain in dirnames; this can be used to prune the\r | |
260 | search, or to impose a specific order of visiting. Modifying dirnames when\r | |
261 | topdown is false is ineffective, since the directories in dirnames have\r | |
262 | already been generated by the time dirnames itself is generated. No matter\r | |
263 | the value of topdown, the list of subdirectories is retrieved before the\r | |
264 | tuples for the directory and its subdirectories are generated.\r | |
265 | \r | |
266 | By default errors from the os.listdir() call are ignored. If\r | |
267 | optional arg 'onerror' is specified, it should be a function; it\r | |
268 | will be called with one argument, an os.error instance. It can\r | |
269 | report the error to continue with the walk, or raise the exception\r | |
270 | to abort the walk. Note that the filename is available as the\r | |
271 | filename attribute of the exception object.\r | |
272 | \r | |
273 | By default, os.walk does not follow symbolic links to subdirectories on\r | |
274 | systems that support them. In order to get this functionality, set the\r | |
275 | optional argument 'followlinks' to true.\r | |
276 | \r | |
277 | Caution: if you pass a relative pathname for top, don't change the\r | |
278 | current working directory between resumptions of walk. walk never\r | |
279 | changes the current directory, and assumes that the client doesn't\r | |
280 | either.\r | |
281 | \r | |
282 | Example:\r | |
283 | \r | |
284 | import os\r | |
285 | from os.path import join, getsize\r | |
286 | for root, dirs, files in os.walk('python/Lib/email'):\r | |
287 | print root, "consumes",\r | |
288 | print sum([getsize(join(root, name)) for name in files]),\r | |
289 | print "bytes in", len(files), "non-directory files"\r | |
290 | if 'CVS' in dirs:\r | |
291 | dirs.remove('CVS') # don't visit CVS directories\r | |
292 | \r | |
293 | """\r | |
294 | \r | |
295 | islink, join, isdir = path.islink, path.join, path.isdir\r | |
296 | \r | |
297 | # We may not have read permission for top, in which case we can't\r | |
298 | # get a list of the files the directory contains. os.path.walk\r | |
299 | # always suppressed the exception then, rather than blow up for a\r | |
300 | # minor reason when (say) a thousand readable directories are still\r | |
301 | # left to visit. That logic is copied here.\r | |
302 | try:\r | |
303 | # Note that listdir and error are globals in this module due\r | |
304 | # to earlier import-*.\r | |
305 | names = listdir(top)\r | |
306 | except error, err:\r | |
307 | if onerror is not None:\r | |
308 | onerror(err)\r | |
309 | return\r | |
310 | \r | |
311 | dirs, nondirs = [], []\r | |
312 | for name in names:\r | |
313 | if isdir(join(top, name)):\r | |
314 | dirs.append(name)\r | |
315 | else:\r | |
316 | nondirs.append(name)\r | |
317 | \r | |
318 | if topdown:\r | |
319 | yield top, dirs, nondirs\r | |
320 | for name in dirs:\r | |
321 | new_path = join(top, name)\r | |
322 | if followlinks or not islink(new_path):\r | |
323 | for x in walk(new_path, topdown, onerror, followlinks):\r | |
324 | yield x\r | |
325 | if not topdown:\r | |
326 | yield top, dirs, nondirs\r | |
327 | \r | |
328 | __all__.append("walk")\r | |
329 | \r | |
330 | # Make sure os.environ exists, at least\r | |
331 | try:\r | |
332 | environ\r | |
333 | except NameError:\r | |
334 | environ = {}\r | |
335 | \r | |
336 | def execl(file, *args):\r | |
337 | """execl(file, *args)\r | |
338 | \r | |
339 | Execute the executable file with argument list args, replacing the\r | |
340 | current process. """\r | |
341 | execv(file, args)\r | |
342 | \r | |
343 | def execle(file, *args):\r | |
344 | """execle(file, *args, env)\r | |
345 | \r | |
346 | Execute the executable file with argument list args and\r | |
347 | environment env, replacing the current process. """\r | |
348 | env = args[-1]\r | |
349 | execve(file, args[:-1], env)\r | |
350 | \r | |
351 | def execlp(file, *args):\r | |
352 | """execlp(file, *args)\r | |
353 | \r | |
354 | Execute the executable file (which is searched for along $PATH)\r | |
355 | with argument list args, replacing the current process. """\r | |
356 | execvp(file, args)\r | |
357 | \r | |
358 | def execlpe(file, *args):\r | |
359 | """execlpe(file, *args, env)\r | |
360 | \r | |
361 | Execute the executable file (which is searched for along $PATH)\r | |
362 | with argument list args and environment env, replacing the current\r | |
363 | process. """\r | |
364 | env = args[-1]\r | |
365 | execvpe(file, args[:-1], env)\r | |
366 | \r | |
367 | def execvp(file, args):\r | |
368 | """execvp(file, args)\r | |
369 | \r | |
370 | Execute the executable file (which is searched for along $PATH)\r | |
371 | with argument list args, replacing the current process.\r | |
372 | args may be a list or tuple of strings. """\r | |
373 | _execvpe(file, args)\r | |
374 | \r | |
375 | def execvpe(file, args, env):\r | |
376 | """execvpe(file, args, env)\r | |
377 | \r | |
378 | Execute the executable file (which is searched for along $PATH)\r | |
379 | with argument list args and environment env , replacing the\r | |
380 | current process.\r | |
381 | args may be a list or tuple of strings. """\r | |
382 | _execvpe(file, args, env)\r | |
383 | \r | |
384 | __all__.extend(["execl","execle","execlp","execlpe","execvp","execvpe"])\r | |
385 | \r | |
386 | def _execvpe(file, args, env=None):\r | |
387 | if env is not None:\r | |
388 | func = execve\r | |
389 | argrest = (args, env)\r | |
390 | else:\r | |
391 | func = execv\r | |
392 | argrest = (args,)\r | |
393 | env = environ\r | |
394 | \r | |
395 | head, tail = path.split(file)\r | |
396 | if head:\r | |
397 | func(file, *argrest)\r | |
398 | return\r | |
399 | if 'PATH' in env:\r | |
400 | envpath = env['PATH']\r | |
401 | else:\r | |
402 | envpath = defpath\r | |
403 | PATH = envpath.split(pathsep)\r | |
404 | saved_exc = None\r | |
405 | saved_tb = None\r | |
406 | for dir in PATH:\r | |
407 | fullname = path.join(dir, file)\r | |
408 | try:\r | |
409 | func(fullname, *argrest)\r | |
410 | except error, e:\r | |
411 | tb = sys.exc_info()[2]\r | |
412 | if (e.errno != errno.ENOENT and e.errno != errno.ENOTDIR\r | |
413 | and saved_exc is None):\r | |
414 | saved_exc = e\r | |
415 | saved_tb = tb\r | |
416 | if saved_exc:\r | |
417 | raise error, saved_exc, saved_tb\r | |
418 | raise error, e, tb\r | |
419 | \r | |
420 | # Change environ to automatically call putenv() if it exists\r | |
421 | try:\r | |
422 | # This will fail if there's no putenv\r | |
423 | putenv\r | |
424 | except NameError:\r | |
425 | pass\r | |
426 | else:\r | |
427 | import UserDict\r | |
428 | \r | |
429 | # Fake unsetenv() for Windows\r | |
430 | # not sure about os2 here but\r | |
431 | # I'm guessing they are the same.\r | |
432 | \r | |
433 | if name in ('os2', 'nt'):\r | |
434 | def unsetenv(key):\r | |
435 | putenv(key, "")\r | |
436 | \r | |
437 | if name == "riscos":\r | |
438 | # On RISC OS, all env access goes through getenv and putenv\r | |
439 | from riscosenviron import _Environ\r | |
440 | elif name in ('os2', 'nt'): # Where Env Var Names Must Be UPPERCASE\r | |
441 | # But we store them as upper case\r | |
442 | class _Environ(UserDict.IterableUserDict):\r | |
443 | def __init__(self, environ):\r | |
444 | UserDict.UserDict.__init__(self)\r | |
445 | data = self.data\r | |
446 | for k, v in environ.items():\r | |
447 | data[k.upper()] = v\r | |
448 | def __setitem__(self, key, item):\r | |
449 | putenv(key, item)\r | |
450 | self.data[key.upper()] = item\r | |
451 | def __getitem__(self, key):\r | |
452 | return self.data[key.upper()]\r | |
453 | try:\r | |
454 | unsetenv\r | |
455 | except NameError:\r | |
456 | def __delitem__(self, key):\r | |
457 | del self.data[key.upper()]\r | |
458 | else:\r | |
459 | def __delitem__(self, key):\r | |
460 | unsetenv(key)\r | |
461 | del self.data[key.upper()]\r | |
462 | def clear(self):\r | |
463 | for key in self.data.keys():\r | |
464 | unsetenv(key)\r | |
465 | del self.data[key]\r | |
466 | def pop(self, key, *args):\r | |
467 | unsetenv(key)\r | |
468 | return self.data.pop(key.upper(), *args)\r | |
469 | def has_key(self, key):\r | |
470 | return key.upper() in self.data\r | |
471 | def __contains__(self, key):\r | |
472 | return key.upper() in self.data\r | |
473 | def get(self, key, failobj=None):\r | |
474 | return self.data.get(key.upper(), failobj)\r | |
475 | def update(self, dict=None, **kwargs):\r | |
476 | if dict:\r | |
477 | try:\r | |
478 | keys = dict.keys()\r | |
479 | except AttributeError:\r | |
480 | # List of (key, value)\r | |
481 | for k, v in dict:\r | |
482 | self[k] = v\r | |
483 | else:\r | |
484 | # got keys\r | |
485 | # cannot use items(), since mappings\r | |
486 | # may not have them.\r | |
487 | for k in keys:\r | |
488 | self[k] = dict[k]\r | |
489 | if kwargs:\r | |
490 | self.update(kwargs)\r | |
491 | def copy(self):\r | |
492 | return dict(self)\r | |
493 | \r | |
494 | else: # Where Env Var Names Can Be Mixed Case\r | |
495 | class _Environ(UserDict.IterableUserDict):\r | |
496 | def __init__(self, environ):\r | |
497 | UserDict.UserDict.__init__(self)\r | |
498 | self.data = environ\r | |
499 | def __setitem__(self, key, item):\r | |
500 | putenv(key, item)\r | |
501 | self.data[key] = item\r | |
502 | def update(self, dict=None, **kwargs):\r | |
503 | if dict:\r | |
504 | try:\r | |
505 | keys = dict.keys()\r | |
506 | except AttributeError:\r | |
507 | # List of (key, value)\r | |
508 | for k, v in dict:\r | |
509 | self[k] = v\r | |
510 | else:\r | |
511 | # got keys\r | |
512 | # cannot use items(), since mappings\r | |
513 | # may not have them.\r | |
514 | for k in keys:\r | |
515 | self[k] = dict[k]\r | |
516 | if kwargs:\r | |
517 | self.update(kwargs)\r | |
518 | try:\r | |
519 | unsetenv\r | |
520 | except NameError:\r | |
521 | pass\r | |
522 | else:\r | |
523 | def __delitem__(self, key):\r | |
524 | unsetenv(key)\r | |
525 | del self.data[key]\r | |
526 | def clear(self):\r | |
527 | for key in self.data.keys():\r | |
528 | unsetenv(key)\r | |
529 | del self.data[key]\r | |
530 | def pop(self, key, *args):\r | |
531 | unsetenv(key)\r | |
532 | return self.data.pop(key, *args)\r | |
533 | def copy(self):\r | |
534 | return dict(self)\r | |
535 | \r | |
536 | \r | |
537 | environ = _Environ(environ)\r | |
538 | \r | |
539 | def getenv(key, default=None):\r | |
540 | """Get an environment variable, return None if it doesn't exist.\r | |
541 | The optional second argument can specify an alternate default."""\r | |
542 | return environ.get(key, default)\r | |
543 | __all__.append("getenv")\r | |
544 | \r | |
545 | def _exists(name):\r | |
546 | return name in globals()\r | |
547 | \r | |
548 | # Supply spawn*() (probably only for Unix)\r | |
549 | if _exists("fork") and not _exists("spawnv") and _exists("execv"):\r | |
550 | \r | |
551 | P_WAIT = 0\r | |
552 | P_NOWAIT = P_NOWAITO = 1\r | |
553 | \r | |
554 | # XXX Should we support P_DETACH? I suppose it could fork()**2\r | |
555 | # and close the std I/O streams. Also, P_OVERLAY is the same\r | |
556 | # as execv*()?\r | |
557 | \r | |
558 | def _spawnvef(mode, file, args, env, func):\r | |
559 | # Internal helper; func is the exec*() function to use\r | |
560 | pid = fork()\r | |
561 | if not pid:\r | |
562 | # Child\r | |
563 | try:\r | |
564 | if env is None:\r | |
565 | func(file, args)\r | |
566 | else:\r | |
567 | func(file, args, env)\r | |
568 | except:\r | |
569 | _exit(127)\r | |
570 | else:\r | |
571 | # Parent\r | |
572 | if mode == P_NOWAIT:\r | |
573 | return pid # Caller is responsible for waiting!\r | |
574 | while 1:\r | |
575 | wpid, sts = waitpid(pid, 0)\r | |
576 | if WIFSTOPPED(sts):\r | |
577 | continue\r | |
578 | elif WIFSIGNALED(sts):\r | |
579 | return -WTERMSIG(sts)\r | |
580 | elif WIFEXITED(sts):\r | |
581 | return WEXITSTATUS(sts)\r | |
582 | else:\r | |
583 | raise error, "Not stopped, signaled or exited???"\r | |
584 | \r | |
585 | def spawnv(mode, file, args):\r | |
586 | """spawnv(mode, file, args) -> integer\r | |
587 | \r | |
588 | Execute file with arguments from args in a subprocess.\r | |
589 | If mode == P_NOWAIT return the pid of the process.\r | |
590 | If mode == P_WAIT return the process's exit code if it exits normally;\r | |
591 | otherwise return -SIG, where SIG is the signal that killed it. """\r | |
592 | return _spawnvef(mode, file, args, None, execv)\r | |
593 | \r | |
594 | def spawnve(mode, file, args, env):\r | |
595 | """spawnve(mode, file, args, env) -> integer\r | |
596 | \r | |
597 | Execute file with arguments from args in a subprocess with the\r | |
598 | specified environment.\r | |
599 | If mode == P_NOWAIT return the pid of the process.\r | |
600 | If mode == P_WAIT return the process's exit code if it exits normally;\r | |
601 | otherwise return -SIG, where SIG is the signal that killed it. """\r | |
602 | return _spawnvef(mode, file, args, env, execve)\r | |
603 | \r | |
604 | # Note: spawnvp[e] is't currently supported on Windows\r | |
605 | \r | |
606 | def spawnvp(mode, file, args):\r | |
607 | """spawnvp(mode, file, args) -> integer\r | |
608 | \r | |
609 | Execute file (which is looked for along $PATH) with arguments from\r | |
610 | args in a subprocess.\r | |
611 | If mode == P_NOWAIT return the pid of the process.\r | |
612 | If mode == P_WAIT return the process's exit code if it exits normally;\r | |
613 | otherwise return -SIG, where SIG is the signal that killed it. """\r | |
614 | return _spawnvef(mode, file, args, None, execvp)\r | |
615 | \r | |
616 | def spawnvpe(mode, file, args, env):\r | |
617 | """spawnvpe(mode, file, args, env) -> integer\r | |
618 | \r | |
619 | Execute file (which is looked for along $PATH) with arguments from\r | |
620 | args in a subprocess with the supplied environment.\r | |
621 | If mode == P_NOWAIT return the pid of the process.\r | |
622 | If mode == P_WAIT return the process's exit code if it exits normally;\r | |
623 | otherwise return -SIG, where SIG is the signal that killed it. """\r | |
624 | return _spawnvef(mode, file, args, env, execvpe)\r | |
625 | \r | |
626 | if _exists("spawnv"):\r | |
627 | # These aren't supplied by the basic Windows code\r | |
628 | # but can be easily implemented in Python\r | |
629 | \r | |
630 | def spawnl(mode, file, *args):\r | |
631 | """spawnl(mode, file, *args) -> integer\r | |
632 | \r | |
633 | Execute file with arguments from args in a subprocess.\r | |
634 | If mode == P_NOWAIT return the pid of the process.\r | |
635 | If mode == P_WAIT return the process's exit code if it exits normally;\r | |
636 | otherwise return -SIG, where SIG is the signal that killed it. """\r | |
637 | return spawnv(mode, file, args)\r | |
638 | \r | |
639 | def spawnle(mode, file, *args):\r | |
640 | """spawnle(mode, file, *args, env) -> integer\r | |
641 | \r | |
642 | Execute file with arguments from args in a subprocess with the\r | |
643 | supplied environment.\r | |
644 | If mode == P_NOWAIT return the pid of the process.\r | |
645 | If mode == P_WAIT return the process's exit code if it exits normally;\r | |
646 | otherwise return -SIG, where SIG is the signal that killed it. """\r | |
647 | env = args[-1]\r | |
648 | return spawnve(mode, file, args[:-1], env)\r | |
649 | \r | |
650 | \r | |
651 | __all__.extend(["spawnv", "spawnve", "spawnl", "spawnle",])\r | |
652 | \r | |
653 | \r | |
654 | if _exists("spawnvp"):\r | |
655 | # At the moment, Windows doesn't implement spawnvp[e],\r | |
656 | # so it won't have spawnlp[e] either.\r | |
657 | def spawnlp(mode, file, *args):\r | |
658 | """spawnlp(mode, file, *args) -> integer\r | |
659 | \r | |
660 | Execute file (which is looked for along $PATH) with arguments from\r | |
661 | args in a subprocess with the supplied environment.\r | |
662 | If mode == P_NOWAIT return the pid of the process.\r | |
663 | If mode == P_WAIT return the process's exit code if it exits normally;\r | |
664 | otherwise return -SIG, where SIG is the signal that killed it. """\r | |
665 | return spawnvp(mode, file, args)\r | |
666 | \r | |
667 | def spawnlpe(mode, file, *args):\r | |
668 | """spawnlpe(mode, file, *args, env) -> integer\r | |
669 | \r | |
670 | Execute file (which is looked for along $PATH) with arguments from\r | |
671 | args in a subprocess with the supplied environment.\r | |
672 | If mode == P_NOWAIT return the pid of the process.\r | |
673 | If mode == P_WAIT return the process's exit code if it exits normally;\r | |
674 | otherwise return -SIG, where SIG is the signal that killed it. """\r | |
675 | env = args[-1]\r | |
676 | return spawnvpe(mode, file, args[:-1], env)\r | |
677 | \r | |
678 | \r | |
679 | __all__.extend(["spawnvp", "spawnvpe", "spawnlp", "spawnlpe",])\r | |
680 | \r | |
681 | \r | |
682 | # Supply popen2 etc. (for Unix)\r | |
683 | if _exists("fork"):\r | |
684 | if not _exists("popen2"):\r | |
685 | def popen2(cmd, mode="t", bufsize=-1):\r | |
686 | """Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd'\r | |
687 | may be a sequence, in which case arguments will be passed directly to\r | |
688 | the program without shell intervention (as with os.spawnv()). If 'cmd'\r | |
689 | is a string it will be passed to the shell (as with os.system()). If\r | |
690 | 'bufsize' is specified, it sets the buffer size for the I/O pipes. The\r | |
691 | file objects (child_stdin, child_stdout) are returned."""\r | |
692 | import warnings\r | |
693 | msg = "os.popen2 is deprecated. Use the subprocess module."\r | |
694 | warnings.warn(msg, DeprecationWarning, stacklevel=2)\r | |
695 | \r | |
696 | import subprocess\r | |
697 | PIPE = subprocess.PIPE\r | |
698 | p = subprocess.Popen(cmd, shell=isinstance(cmd, basestring),\r | |
699 | bufsize=bufsize, stdin=PIPE, stdout=PIPE,\r | |
700 | close_fds=True)\r | |
701 | return p.stdin, p.stdout\r | |
702 | __all__.append("popen2")\r | |
703 | \r | |
704 | if not _exists("popen3"):\r | |
705 | def popen3(cmd, mode="t", bufsize=-1):\r | |
706 | """Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd'\r | |
707 | may be a sequence, in which case arguments will be passed directly to\r | |
708 | the program without shell intervention (as with os.spawnv()). If 'cmd'\r | |
709 | is a string it will be passed to the shell (as with os.system()). If\r | |
710 | 'bufsize' is specified, it sets the buffer size for the I/O pipes. The\r | |
711 | file objects (child_stdin, child_stdout, child_stderr) are returned."""\r | |
712 | import warnings\r | |
713 | msg = "os.popen3 is deprecated. Use the subprocess module."\r | |
714 | warnings.warn(msg, DeprecationWarning, stacklevel=2)\r | |
715 | \r | |
716 | import subprocess\r | |
717 | PIPE = subprocess.PIPE\r | |
718 | p = subprocess.Popen(cmd, shell=isinstance(cmd, basestring),\r | |
719 | bufsize=bufsize, stdin=PIPE, stdout=PIPE,\r | |
720 | stderr=PIPE, close_fds=True)\r | |
721 | return p.stdin, p.stdout, p.stderr\r | |
722 | __all__.append("popen3")\r | |
723 | \r | |
724 | if not _exists("popen4"):\r | |
725 | def popen4(cmd, mode="t", bufsize=-1):\r | |
726 | """Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd'\r | |
727 | may be a sequence, in which case arguments will be passed directly to\r | |
728 | the program without shell intervention (as with os.spawnv()). If 'cmd'\r | |
729 | is a string it will be passed to the shell (as with os.system()). If\r | |
730 | 'bufsize' is specified, it sets the buffer size for the I/O pipes. The\r | |
731 | file objects (child_stdin, child_stdout_stderr) are returned."""\r | |
732 | import warnings\r | |
733 | msg = "os.popen4 is deprecated. Use the subprocess module."\r | |
734 | warnings.warn(msg, DeprecationWarning, stacklevel=2)\r | |
735 | \r | |
736 | import subprocess\r | |
737 | PIPE = subprocess.PIPE\r | |
738 | p = subprocess.Popen(cmd, shell=isinstance(cmd, basestring),\r | |
739 | bufsize=bufsize, stdin=PIPE, stdout=PIPE,\r | |
740 | stderr=subprocess.STDOUT, close_fds=True)\r | |
741 | return p.stdin, p.stdout\r | |
742 | __all__.append("popen4")\r | |
743 | \r | |
744 | import copy_reg as _copy_reg\r | |
745 | \r | |
746 | def _make_stat_result(tup, dict):\r | |
747 | return stat_result(tup, dict)\r | |
748 | \r | |
749 | def _pickle_stat_result(sr):\r | |
750 | (type, args) = sr.__reduce__()\r | |
751 | return (_make_stat_result, args)\r | |
752 | \r | |
753 | try:\r | |
754 | _copy_reg.pickle(stat_result, _pickle_stat_result, _make_stat_result)\r | |
755 | except NameError: # stat_result may not exist\r | |
756 | pass\r | |
757 | \r | |
758 | def _make_statvfs_result(tup, dict):\r | |
759 | return statvfs_result(tup, dict)\r | |
760 | \r | |
761 | def _pickle_statvfs_result(sr):\r | |
762 | (type, args) = sr.__reduce__()\r | |
763 | return (_make_statvfs_result, args)\r | |
764 | \r | |
765 | try:\r | |
766 | _copy_reg.pickle(statvfs_result, _pickle_statvfs_result,\r | |
767 | _make_statvfs_result)\r | |
768 | except NameError: # statvfs_result may not exist\r | |
769 | pass\r |