1 r
"""OS routines for Mac, DOS, NT, or Posix depending on what system we're on.
4 - all functions from posix, nt, os2, mac, or ce, e.g. unlink, stat, etc.
5 - os.path is one of the modules posixpath, ntpath, or macpath
6 - os.name is 'posix', 'nt', 'os2', 'mac', 'ce' or 'riscos'
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 '\\')
10 - os.extsep is the extension separator ('.' or '/')
11 - os.altsep is the alternate pathname separator (None or '/')
12 - os.pathsep is the component separator used in $PATH etc
13 - os.linesep is the line separator in text files ('\r' or '\n' or '\r\n')
14 - os.defpath is the default search path for executables
16 Programs that import and use 'os' stand a better chance of being
17 portable between different platforms. Of course, they must then
18 only use functions that are defined by all platforms (e.g., unlink
19 and opendir), and leave all pathname manipulation to os.path
20 (e.g., split and join).
27 _names
= sys
.builtin_module_names
29 __all__
= ["altsep", "curdir", "pardir", "sep", "pathsep", "linesep",
30 "defpath", "name", "path"]
32 def _get_exports_list(module
):
34 return list(module
.__all
__)
35 except AttributeError:
36 return [n
for n
in dir(module
) if n
[0] != '_']
43 from posix
import _exit
46 import posixpath
as path
49 __all__
.extend(_get_exports_list(posix
))
63 __all__
.extend(_get_exports_list(nt
))
74 if sys
.version
.find('EMX GCC') == -1:
77 import os2emxpath
as path
80 __all__
.extend(_get_exports_list(os2
))
91 import macpath
as path
94 __all__
.extend(_get_exports_list(mac
))
105 # We can use the standard Windows path.
106 import ntpath
as path
109 __all__
.extend(_get_exports_list(ce
))
112 elif 'riscos' in _names
:
117 from riscos
import _exit
120 import riscospath
as path
123 __all__
.extend(_get_exports_list(riscos
))
127 raise ImportError, 'no os specific module found'
129 sys
.modules
['os.path'] = path
130 from os
.path
import curdir
, pardir
, sep
, pathsep
, defpath
, extsep
, altsep
136 # Super directory utilities.
137 # (Inspired by Eric Raymond; the doc strings are mostly his)
139 def makedirs(name
, mode
=0777):
140 """makedirs(path [, mode=0777])
142 Super-mkdir; create a leaf directory and all intermediate ones.
143 Works like mkdir, except that any intermediate path segment (not
144 just the rightmost) will be created if it does not exist. This is
148 head
, tail
= path
.split(name
)
150 head
, tail
= path
.split(head
)
151 if head
and tail
and not path
.exists(head
):
155 def removedirs(name
):
158 Super-rmdir; remove a leaf directory and empty all intermediate
159 ones. Works like rmdir except that, if the leaf directory is
160 successfully removed, directories corresponding to rightmost path
161 segments will be pruned way until either the whole path is
162 consumed or an error occurs. Errors during this latter phase are
163 ignored -- they generally mean that a directory was not empty.
167 head
, tail
= path
.split(name
)
169 head
, tail
= path
.split(head
)
175 head
, tail
= path
.split(head
)
177 def renames(old
, new
):
180 Super-rename; create directories as necessary and delete any left
181 empty. Works like rename, except creation of any intermediate
182 directories needed to make the new pathname good is attempted
183 first. After the rename, directories corresponding to rightmost
184 path segments of the old name will be pruned way until either the
185 whole path is consumed or a nonempty directory is found.
187 Note: this function can fail with the new directory structure made
188 if you lack permissions needed to unlink the leaf directory or
192 head
, tail
= path
.split(new
)
193 if head
and tail
and not path
.exists(head
):
196 head
, tail
= path
.split(old
)
203 __all__
.extend(["makedirs", "removedirs", "renames"])
205 # Make sure os.environ exists, at least
211 def execl(file, *args
):
212 """execl(file, *args)
214 Execute the executable file with argument list args, replacing the
218 def execle(file, *args
):
219 """execle(file, *args, env)
221 Execute the executable file with argument list args and
222 environment env, replacing the current process. """
224 execve(file, args
[:-1], env
)
226 def execlp(file, *args
):
227 """execlp(file, *args)
229 Execute the executable file (which is searched for along $PATH)
230 with argument list args, replacing the current process. """
233 def execlpe(file, *args
):
234 """execlpe(file, *args, env)
236 Execute the executable file (which is searched for along $PATH)
237 with argument list args and environment env, replacing the current
240 execvpe(file, args
[:-1], env
)
242 def execvp(file, args
):
245 Execute the executable file (which is searched for along $PATH)
246 with argument list args, replacing the current process.
247 args may be a list or tuple of strings. """
250 def execvpe(file, args
, env
):
251 """execvpe(file, args, env)
253 Execute the executable file (which is searched for along $PATH)
254 with argument list args and environment env , replacing the
256 args may be a list or tuple of strings. """
257 _execvpe(file, args
, env
)
259 __all__
.extend(["execl","execle","execlp","execlpe","execvp","execvpe"])
261 def _execvpe(file, args
, env
=None):
262 from errno
import ENOENT
, ENOTDIR
266 argrest
= (args
, env
)
272 head
, tail
= path
.split(file)
277 envpath
= env
['PATH']
280 PATH
= envpath
.split(pathsep
)
284 fullname
= path
.join(dir, file)
286 func(fullname
, *argrest
)
288 tb
= sys
.exc_info()[2]
289 if (e
.errno
!= ENOENT
and e
.errno
!= ENOTDIR
290 and saved_exc
is None):
294 raise error
, saved_exc
, saved_tb
297 # Change environ to automatically call putenv() if it exists
299 # This will fail if there's no putenv
306 # Fake unsetenv() for Windows
307 # not sure about os2 here but
308 # I'm guessing they are the same.
310 if name
in ('os2', 'nt'):
315 # On RISC OS, all env access goes through getenv and putenv
316 from riscosenviron
import _Environ
317 elif name
in ('os2', 'nt'): # Where Env Var Names Must Be UPPERCASE
318 # But we store them as upper case
319 class _Environ(UserDict
.IterableUserDict
):
320 def __init__(self
, environ
):
321 UserDict
.UserDict
.__init
__(self
)
323 for k
, v
in environ
.items():
325 def __setitem__(self
, key
, item
):
327 self
.data
[key
.upper()] = item
328 def __getitem__(self
, key
):
329 return self
.data
[key
.upper()]
333 def __delitem__(self
, key
):
334 del self
.data
[key
.upper()]
336 def __delitem__(self
, key
):
338 del self
.data
[key
.upper()]
339 def has_key(self
, key
):
340 return key
.upper() in self
.data
341 def __contains__(self
, key
):
342 return key
.upper() in self
.data
343 def get(self
, key
, failobj
=None):
344 return self
.data
.get(key
.upper(), failobj
)
345 def update(self
, dict):
346 for k
, v
in dict.items():
351 else: # Where Env Var Names Can Be Mixed Case
352 class _Environ(UserDict
.IterableUserDict
):
353 def __init__(self
, environ
):
354 UserDict
.UserDict
.__init
__(self
)
356 def __setitem__(self
, key
, item
):
358 self
.data
[key
] = item
359 def update(self
, dict):
360 for k
, v
in dict.items():
367 def __delitem__(self
, key
):
374 environ
= _Environ(environ
)
376 def getenv(key
, default
=None):
377 """Get an environment variable, return None if it doesn't exist.
378 The optional second argument can specify an alternate default."""
379 return environ
.get(key
, default
)
380 __all__
.append("getenv")
389 # Supply spawn*() (probably only for Unix)
390 if _exists("fork") and not _exists("spawnv") and _exists("execv"):
393 P_NOWAIT
= P_NOWAITO
= 1
395 # XXX Should we support P_DETACH? I suppose it could fork()**2
396 # and close the std I/O streams. Also, P_OVERLAY is the same
399 def _spawnvef(mode
, file, args
, env
, func
):
400 # Internal helper; func is the exec*() function to use
408 func(file, args
, env
)
414 return pid
# Caller is responsible for waiting!
416 wpid
, sts
= waitpid(pid
, 0)
419 elif WIFSIGNALED(sts
):
420 return -WTERMSIG(sts
)
422 return WEXITSTATUS(sts
)
424 raise error
, "Not stopped, signaled or exited???"
426 def spawnv(mode
, file, args
):
427 """spawnv(mode, file, args) -> integer
429 Execute file with arguments from args in a subprocess.
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. """
433 return _spawnvef(mode
, file, args
, None, execv
)
435 def spawnve(mode
, file, args
, env
):
436 """spawnve(mode, file, args, env) -> integer
438 Execute file with arguments from args in a subprocess with the
439 specified environment.
440 If mode == P_NOWAIT return the pid of the process.
441 If mode == P_WAIT return the process's exit code if it exits normally;
442 otherwise return -SIG, where SIG is the signal that killed it. """
443 return _spawnvef(mode
, file, args
, env
, execve
)
445 # Note: spawnvp[e] is't currently supported on Windows
447 def spawnvp(mode
, file, args
):
448 """spawnvp(mode, file, args) -> integer
450 Execute file (which is looked for along $PATH) with arguments from
451 args in a subprocess.
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. """
455 return _spawnvef(mode
, file, args
, None, execvp
)
457 def spawnvpe(mode
, file, args
, env
):
458 """spawnvpe(mode, file, args, env) -> integer
460 Execute file (which is looked for along $PATH) with arguments from
461 args in a subprocess with the supplied environment.
462 If mode == P_NOWAIT return the pid of the process.
463 If mode == P_WAIT return the process's exit code if it exits normally;
464 otherwise return -SIG, where SIG is the signal that killed it. """
465 return _spawnvef(mode
, file, args
, env
, execvpe
)
467 if _exists("spawnv"):
468 # These aren't supplied by the basic Windows code
469 # but can be easily implemented in Python
471 def spawnl(mode
, file, *args
):
472 """spawnl(mode, file, *args) -> integer
474 Execute file with arguments from args in a subprocess.
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. """
478 return spawnv(mode
, file, args
)
480 def spawnle(mode
, file, *args
):
481 """spawnle(mode, file, *args, env) -> integer
483 Execute file with arguments from args in a subprocess with the
484 supplied environment.
485 If mode == P_NOWAIT return the pid of the process.
486 If mode == P_WAIT return the process's exit code if it exits normally;
487 otherwise return -SIG, where SIG is the signal that killed it. """
489 return spawnve(mode
, file, args
[:-1], env
)
491 if _exists("spawnvp"):
492 # At the moment, Windows doesn't implement spawnvp[e],
493 # so it won't have spawnlp[e] either.
494 def spawnlp(mode
, file, *args
):
495 """spawnlp(mode, file, *args, env) -> integer
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. """
502 return spawnvp(mode
, file, args
)
504 def spawnlpe(mode
, file, *args
):
505 """spawnlpe(mode, file, *args, env) -> integer
507 Execute file (which is looked for along $PATH) with arguments from
508 args in a subprocess with the supplied environment.
509 If mode == P_NOWAIT return the pid of the process.
510 If mode == P_WAIT return the process's exit code if it exits normally;
511 otherwise return -SIG, where SIG is the signal that killed it. """
513 return spawnvpe(mode
, file, args
[:-1], env
)
516 __all__
.extend(["spawnlp","spawnlpe","spawnv", "spawnve","spawnvp",
517 "spawnvpe","spawnl","spawnle",])
520 # Supply popen2 etc. (for Unix)
522 if not _exists("popen2"):
523 def popen2(cmd
, mode
="t", bufsize
=-1):
525 stdout
, stdin
= popen2
.popen2(cmd
, bufsize
)
527 __all__
.append("popen2")
529 if not _exists("popen3"):
530 def popen3(cmd
, mode
="t", bufsize
=-1):
532 stdout
, stdin
, stderr
= popen2
.popen3(cmd
, bufsize
)
533 return stdin
, stdout
, stderr
534 __all__
.append("popen3")
536 if not _exists("popen4"):
537 def popen4(cmd
, mode
="t", bufsize
=-1):
539 stdout
, stdin
= popen2
.popen4(cmd
, bufsize
)
541 __all__
.append("popen4")
543 import copy_reg
as _copy_reg
545 def _make_stat_result(tup
, dict):
546 return stat_result(tup
, dict)
548 def _pickle_stat_result(sr
):
549 (type, args
) = sr
.__reduce
__()
550 return (_make_stat_result
, args
)
553 _copy_reg
.pickle(stat_result
, _pickle_stat_result
, _make_stat_result
)
554 except NameError: # stat_result may not exist
557 def _make_statvfs_result(tup
, dict):
558 return statvfs_result(tup
, dict)
560 def _pickle_statvfs_result(sr
):
561 (type, args
) = sr
.__reduce
__()
562 return (_make_statvfs_result
, args
)
565 _copy_reg
.pickle(statvfs_result
, _pickle_statvfs_result
,
566 _make_statvfs_result
)
567 except NameError: # statvfs_result may not exist