Guido van Rossum | 31104f4 | 1992-01-14 18:28:36 +0000 | [diff] [blame] | 1 | # os.py -- either mac or posix depending on what system we're on. |
| 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 | 31104f4 | 1992-01-14 18:28:36 +0000 | [diff] [blame] | 10 | |
| 11 | # Programs that import and use 'os' stand a better chance of being |
| 12 | # portable between different platforms. Of course, they must then |
| 13 | # only use functions that are defined by all platforms (e.g., unlink |
| 14 | # and opendir), and leave all pathname manipulation to os.path |
| 15 | # (e.g., split and join). |
| 16 | |
Guido van Rossum | 1a76ef2 | 1992-03-31 18:57:28 +0000 | [diff] [blame] | 17 | # XXX This will need to distinguish between real posix and MS-DOS emulation |
| 18 | |
Guido van Rossum | 31104f4 | 1992-01-14 18:28:36 +0000 | [diff] [blame] | 19 | try: |
| 20 | from posix import * |
Guido van Rossum | 35fb82a | 1993-01-26 13:04:43 +0000 | [diff] [blame] | 21 | try: |
| 22 | from posix import _exit |
| 23 | except ImportError: |
| 24 | pass |
Guido van Rossum | 31104f4 | 1992-01-14 18:28:36 +0000 | [diff] [blame] | 25 | name = 'posix' |
| 26 | curdir = '.' |
Guido van Rossum | 1a76ef2 | 1992-03-31 18:57:28 +0000 | [diff] [blame] | 27 | pardir = '..' |
Guido van Rossum | b59cdd4 | 1992-04-06 14:03:45 +0000 | [diff] [blame] | 28 | sep = '/' |
Guido van Rossum | 1a76ef2 | 1992-03-31 18:57:28 +0000 | [diff] [blame] | 29 | import posixpath |
| 30 | path = posixpath |
| 31 | del posixpath |
Guido van Rossum | 31104f4 | 1992-01-14 18:28:36 +0000 | [diff] [blame] | 32 | except ImportError: |
| 33 | from mac import * |
| 34 | name = 'mac' |
| 35 | curdir = ':' |
Guido van Rossum | 1a76ef2 | 1992-03-31 18:57:28 +0000 | [diff] [blame] | 36 | pardir = '::' |
Guido van Rossum | b59cdd4 | 1992-04-06 14:03:45 +0000 | [diff] [blame] | 37 | sep = ':' |
Guido van Rossum | 31104f4 | 1992-01-14 18:28:36 +0000 | [diff] [blame] | 38 | import macpath |
| 39 | path = macpath |
| 40 | del macpath |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 41 | |
| 42 | def execl(file, *args): |
| 43 | execv(file, args) |
| 44 | |
| 45 | def execle(file, *args): |
| 46 | env = args[-1] |
| 47 | execve(file, args[:-1], env) |
| 48 | |
| 49 | def execlp(file, *args): |
| 50 | execvp(file, args) |
| 51 | |
| 52 | def execvp(file, args): |
| 53 | if '/' in file: |
| 54 | execv(file, args) |
| 55 | return |
| 56 | ENOENT = 2 |
| 57 | if environ.has_key('PATH'): |
| 58 | import string |
| 59 | PATH = string.splitfields(environ['PATH'], ':') |
| 60 | else: |
| 61 | PATH = ['', '/bin', '/usr/bin'] |
| 62 | exc, arg = (ENOENT, 'No such file or directory') |
| 63 | for dir in PATH: |
| 64 | fullname = path.join(dir, file) |
| 65 | try: |
| 66 | execv(fullname, args) |
| 67 | except error, (errno, msg): |
| 68 | if errno != ENOENT: |
| 69 | exc, arg = error, (errno, msg) |
| 70 | raise exc, arg |