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'), |
| 23 | 'mac': (':', '::', ':', ' ', ':'), |
| 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 |
| 43 | break |
| 44 | else: |
| 45 | del name |
| 46 | raise ImportError, 'no os specific module found' |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 47 | |
| 48 | def execl(file, *args): |
| 49 | execv(file, args) |
| 50 | |
| 51 | def execle(file, *args): |
| 52 | env = args[-1] |
| 53 | execve(file, args[:-1], env) |
| 54 | |
| 55 | def execlp(file, *args): |
| 56 | execvp(file, args) |
| 57 | |
Guido van Rossum | 2979b01 | 1994-08-01 11:18:30 +0000 | [diff] [blame] | 58 | _notfound = None |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 59 | def execvp(file, args): |
Guido van Rossum | 2979b01 | 1994-08-01 11:18:30 +0000 | [diff] [blame] | 60 | global _notfound |
| 61 | head, tail = path.split(file) |
| 62 | if head: |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 63 | execv(file, args) |
| 64 | return |
| 65 | ENOENT = 2 |
| 66 | if environ.has_key('PATH'): |
Guido van Rossum | 2979b01 | 1994-08-01 11:18:30 +0000 | [diff] [blame] | 67 | envpath = environ['PATH'] |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 68 | else: |
Guido van Rossum | 2979b01 | 1994-08-01 11:18:30 +0000 | [diff] [blame] | 69 | envpath = defpath |
| 70 | import string |
| 71 | PATH = string.splitfields(envpath, pathsep) |
| 72 | if not _notfound: |
| 73 | import tempfile |
| 74 | # Exec a file that is guaranteed not to exist |
| 75 | try: execv(tempfile.mktemp(), ()) |
| 76 | except error, _notfound: pass |
| 77 | exc, arg = error, _notfound |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 78 | for dir in PATH: |
| 79 | fullname = path.join(dir, file) |
| 80 | try: |
| 81 | execv(fullname, args) |
| 82 | except error, (errno, msg): |
Guido van Rossum | 2979b01 | 1994-08-01 11:18:30 +0000 | [diff] [blame] | 83 | if errno != arg[0]: |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 84 | exc, arg = error, (errno, msg) |
| 85 | raise exc, arg |
Guido van Rossum | 2979b01 | 1994-08-01 11:18:30 +0000 | [diff] [blame] | 86 | |
| 87 | # Provide listdir for Windows NT that doesn't have it built in |
| 88 | if name == 'nt': |
| 89 | try: |
| 90 | _tmp = listdir |
| 91 | del _tmp |
| 92 | except NameError: |
| 93 | def listdir(name): |
| 94 | if path.ismount(name): |
| 95 | list = ['.'] |
| 96 | else: |
| 97 | list = ['.', '..'] |
| 98 | f = popen('dir/l/b ' + name, 'r') |
| 99 | line = f.readline() |
| 100 | while line: |
| 101 | list.append(line[:-1]) |
| 102 | line = f.readline() |
| 103 | return list |