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