blob: 7322fa56f93f5221580a42f176285cbbcb215c1f [file] [log] [blame]
Guido van Rossum31104f41992-01-14 18:28:36 +00001# 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 Rossum1a76ef21992-03-31 18:57:28 +00005# - os.path is either module posixpath or macpath
Guido van Rossum31104f41992-01-14 18:28:36 +00006# - os.name is either 'posix' or 'mac'
7# - os.curdir is a string representing the current directory ('.' or ':')
Guido van Rossum1a76ef21992-03-31 18:57:28 +00008# - os.pardir is a string representing the parent directory ('..' or '::')
Guido van Rossumb59cdd41992-04-06 14:03:45 +00009# - os.sep is the (or a most common) pathname separator ('/' or ':')
Guido van Rossum31104f41992-01-14 18:28:36 +000010
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 Rossum1a76ef21992-03-31 18:57:28 +000017# XXX This will need to distinguish between real posix and MS-DOS emulation
18
Guido van Rossum31104f41992-01-14 18:28:36 +000019try:
20 from posix import *
Guido van Rossum35fb82a1993-01-26 13:04:43 +000021 try:
22 from posix import _exit
23 except ImportError:
24 pass
Guido van Rossum31104f41992-01-14 18:28:36 +000025 name = 'posix'
26 curdir = '.'
Guido van Rossum1a76ef21992-03-31 18:57:28 +000027 pardir = '..'
Guido van Rossumb59cdd41992-04-06 14:03:45 +000028 sep = '/'
Guido van Rossum1a76ef21992-03-31 18:57:28 +000029 import posixpath
30 path = posixpath
31 del posixpath
Guido van Rossum31104f41992-01-14 18:28:36 +000032except ImportError:
33 from mac import *
34 name = 'mac'
35 curdir = ':'
Guido van Rossum1a76ef21992-03-31 18:57:28 +000036 pardir = '::'
Guido van Rossumb59cdd41992-04-06 14:03:45 +000037 sep = ':'
Guido van Rossum31104f41992-01-14 18:28:36 +000038 import macpath
39 path = macpath
40 del macpath
Guido van Rossume65cce51993-11-08 15:05:21 +000041
42def execl(file, *args):
43 execv(file, args)
44
45def execle(file, *args):
46 env = args[-1]
47 execve(file, args[:-1], env)
48
49def execlp(file, *args):
50 execvp(file, args)
51
52def 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