Guido van Rossum | dd8cb44 | 1993-12-29 15:33:08 +0000 | [diff] [blame] | 1 | # os.py -- either mac, dos or posix depending on what system we're on. |
Guido van Rossum | 31104f4 | 1992-01-14 18:28:36 +0000 | [diff] [blame] | 2 | |
| 3 | # This exports: |
| 4 | # - all functions from either posix or mac, e.g., os.unlink, os.stat, etc. |
Guido van Rossum | 1a76ef2 | 1992-03-31 18:57:28 +0000 | [diff] [blame] | 5 | # - os.path is either module posixpath or macpath |
Guido van Rossum | 31104f4 | 1992-01-14 18:28:36 +0000 | [diff] [blame] | 6 | # - os.name is either 'posix' or 'mac' |
| 7 | # - os.curdir is a string representing the current directory ('.' or ':') |
Guido van Rossum | 1a76ef2 | 1992-03-31 18:57:28 +0000 | [diff] [blame] | 8 | # - os.pardir is a string representing the parent directory ('..' or '::') |
Guido van Rossum | b59cdd4 | 1992-04-06 14:03:45 +0000 | [diff] [blame] | 9 | # - os.sep is the (or a most common) pathname separator ('/' or ':') |
Guido van Rossum | 2979b01 | 1994-08-01 11:18:30 +0000 | [diff] [blame] | 10 | # - os.pathsep is the component separator used in $PATH etc |
| 11 | # - os.defpath is the default search path for executables |
Guido van Rossum | 31104f4 | 1992-01-14 18:28:36 +0000 | [diff] [blame] | 12 | |
| 13 | # Programs that import and use 'os' stand a better chance of being |
| 14 | # portable between different platforms. Of course, they must then |
| 15 | # only use functions that are defined by all platforms (e.g., unlink |
| 16 | # and opendir), and leave all pathname manipulation to os.path |
| 17 | # (e.g., split and join). |
| 18 | |
Guido van Rossum | 2979b01 | 1994-08-01 11:18:30 +0000 | [diff] [blame] | 19 | _osindex = { |
| 20 | 'posix': ('.', '..', '/', ':', ':/bin:/usr/bin'), |
| 21 | 'dos': ('.', '..', '\\', ';', '.;C:\\bin'), |
| 22 | 'nt': ('.', '..', '\\', ';', '.;C:\\bin'), |
Guido van Rossum | 4c7fa4b | 1995-03-14 17:53:54 +0000 | [diff] [blame] | 23 | 'mac': (':', '::', ':', '\n', ':'), |
Guido van Rossum | 2979b01 | 1994-08-01 11:18:30 +0000 | [diff] [blame] | 24 | } |
Guido van Rossum | 1a76ef2 | 1992-03-31 18:57:28 +0000 | [diff] [blame] | 25 | |
Guido van Rossum | a28f2dc | 1994-08-17 12:33:28 +0000 | [diff] [blame] | 26 | # For freeze.py script: |
| 27 | if 0: |
| 28 | import posix |
Guido van Rossum | ca9321e | 1994-10-05 15:17:55 +0000 | [diff] [blame] | 29 | import posixpath |
Guido van Rossum | a28f2dc | 1994-08-17 12:33:28 +0000 | [diff] [blame] | 30 | |
Guido van Rossum | 2979b01 | 1994-08-01 11:18:30 +0000 | [diff] [blame] | 31 | import sys |
| 32 | for name in _osindex.keys(): |
| 33 | if name in sys.builtin_module_names: |
| 34 | curdir, pardir, sep, pathsep, defpath = _osindex[name] |
| 35 | exec 'from %s import *' % name |
| 36 | exec 'import %spath' % name |
| 37 | exec 'path = %spath' % name |
| 38 | exec 'del %spath' % name |
| 39 | try: |
| 40 | exec 'from %s import _exit' % name |
| 41 | except ImportError: |
| 42 | pass |
Guido van Rossum | b767709 | 1995-08-07 20:15:23 +0000 | [diff] [blame] | 43 | try: |
| 44 | environ |
| 45 | except: |
| 46 | environ = {} # Make sure os.environ exists, at least |
Guido van Rossum | 2979b01 | 1994-08-01 11:18:30 +0000 | [diff] [blame] | 47 | break |
| 48 | else: |
| 49 | del name |
| 50 | raise ImportError, 'no os specific module found' |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 51 | |
| 52 | def execl(file, *args): |
| 53 | execv(file, args) |
| 54 | |
| 55 | def execle(file, *args): |
| 56 | env = args[-1] |
| 57 | execve(file, args[:-1], env) |
| 58 | |
| 59 | def execlp(file, *args): |
| 60 | execvp(file, args) |
| 61 | |
Guido van Rossum | 030afb1 | 1995-03-14 17:27:18 +0000 | [diff] [blame] | 62 | def execlpe(file, *args): |
| 63 | env = args[-1] |
| 64 | execvpe(file, args[:-1], env) |
| 65 | |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 66 | def execvp(file, args): |
Guido van Rossum | 030afb1 | 1995-03-14 17:27:18 +0000 | [diff] [blame] | 67 | _execvpe(file, args) |
| 68 | |
| 69 | def execvpe(file, args, env): |
| 70 | _execvpe(file, args, env) |
| 71 | |
| 72 | _notfound = None |
| 73 | def _execvpe(file, args, env = None): |
| 74 | if env: |
| 75 | func = execve |
| 76 | argrest = (args, env) |
| 77 | else: |
| 78 | func = execv |
| 79 | argrest = (args,) |
| 80 | env = environ |
Guido van Rossum | 2979b01 | 1994-08-01 11:18:30 +0000 | [diff] [blame] | 81 | global _notfound |
| 82 | head, tail = path.split(file) |
| 83 | if head: |
Guido van Rossum | 030afb1 | 1995-03-14 17:27:18 +0000 | [diff] [blame] | 84 | apply(func, (file,) + argrest) |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 85 | return |
Guido van Rossum | 030afb1 | 1995-03-14 17:27:18 +0000 | [diff] [blame] | 86 | if env.has_key('PATH'): |
| 87 | envpath = env['PATH'] |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 88 | else: |
Guido van Rossum | 2979b01 | 1994-08-01 11:18:30 +0000 | [diff] [blame] | 89 | envpath = defpath |
| 90 | import string |
| 91 | PATH = string.splitfields(envpath, pathsep) |
| 92 | if not _notfound: |
| 93 | import tempfile |
| 94 | # Exec a file that is guaranteed not to exist |
| 95 | try: execv(tempfile.mktemp(), ()) |
| 96 | except error, _notfound: pass |
| 97 | exc, arg = error, _notfound |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 98 | for dir in PATH: |
| 99 | fullname = path.join(dir, file) |
| 100 | try: |
Guido van Rossum | 030afb1 | 1995-03-14 17:27:18 +0000 | [diff] [blame] | 101 | apply(func, (fullname,) + argrest) |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 102 | except error, (errno, msg): |
Guido van Rossum | 2979b01 | 1994-08-01 11:18:30 +0000 | [diff] [blame] | 103 | if errno != arg[0]: |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 104 | exc, arg = error, (errno, msg) |
| 105 | raise exc, arg |
Guido van Rossum | 2979b01 | 1994-08-01 11:18:30 +0000 | [diff] [blame] | 106 | |
| 107 | # Provide listdir for Windows NT that doesn't have it built in |
| 108 | if name == 'nt': |
| 109 | try: |
| 110 | _tmp = listdir |
| 111 | del _tmp |
| 112 | except NameError: |
| 113 | def listdir(name): |
| 114 | if path.ismount(name): |
| 115 | list = ['.'] |
| 116 | else: |
| 117 | list = ['.', '..'] |
| 118 | f = popen('dir/l/b ' + name, 'r') |
| 119 | line = f.readline() |
| 120 | while line: |
| 121 | list.append(line[:-1]) |
| 122 | line = f.readline() |
| 123 | return list |
Guido van Rossum | 3b8e20d | 1996-07-24 00:55:17 +0000 | [diff] [blame] | 124 | |
| 125 | |
| 126 | # Change environ to automatically call putenv() if it exists |
| 127 | try: |
| 128 | _putenv = putenv |
| 129 | except NameError: |
| 130 | _putenv = None |
| 131 | if _putenv: |
| 132 | import UserDict |
| 133 | |
| 134 | class _Environ(UserDict.UserDict): |
| 135 | def __init__(self, environ): |
| 136 | UserDict.UserDict.__init__(self) |
| 137 | self.data = environ |
| 138 | def __setitem__(self, key, item): |
| 139 | putenv(key, item) |
| 140 | self.data[key] = item |
| 141 | |
| 142 | environ = _Environ(environ) |