blob: 7f9e696f3447906b38bc073f43c7afbd76ec0f50 [file] [log] [blame]
Guido van Rossumdd8cb441993-12-29 15:33:08 +00001# os.py -- either mac, dos or posix depending on what system we're on.
Guido van Rossum31104f41992-01-14 18:28:36 +00002
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 Rossuma28dab51997-08-29 22:36:47 +00009# - os.sep is the (or a most common) pathname separator ('/' or ':' or '\\')
10# - os.altsep is the alternatte pathname separator (None or '/')
Guido van Rossum2979b011994-08-01 11:18:30 +000011# - os.pathsep is the component separator used in $PATH etc
12# - os.defpath is the default search path for executables
Guido van Rossum31104f41992-01-14 18:28:36 +000013
14# Programs that import and use 'os' stand a better chance of being
15# portable between different platforms. Of course, they must then
16# only use functions that are defined by all platforms (e.g., unlink
17# and opendir), and leave all pathname manipulation to os.path
18# (e.g., split and join).
19
Guido van Rossum2979b011994-08-01 11:18:30 +000020import sys
Guido van Rossuma28dab51997-08-29 22:36:47 +000021
22_names = sys.builtin_module_names
23
24altsep = None
25
26if 'posix' in _names:
27 name = 'posix'
28 curdir = '.'; pardir = '..'; sep = '/'; pathsep = ':'
29 defpath = ':/bin:/usr/bin'
30 from posix import *
31 try:
32 from posix import _exit
33 except ImportError:
34 pass
35 import posixpath
36 path = posixpath
37 del posixpath
38elif 'nt' in _names:
39 name = 'nt'
40 curdir = '.'; pardir = '..'; sep = '\\'; pathsep = ';'
41 defpath = '.;C:\\bin'
42 from nt import *
43 try:
44 from nt import _exit
45 except ImportError:
46 pass
47 import ntpath
48 path = ntpath
49 del ntpath
50elif 'dos' in _names:
51 name = 'dos'
52 curdir = '.'; pardir = '..'; sep = '\\'; pathsep = ';'
53 defpath = '.;C:\\bin'
54 from dos import *
55 try:
56 from dos import _exit
57 except ImportError:
58 pass
59 import dospath
60 path = dospath
61 del dospath
62elif 'mac' in _names:
63 name = 'mac'
64 curdir = ':'; pardir = '::'; sep = ':'; pathsep = '\n'
65 defpath = ':'
66 from mac import *
67 try:
68 from mac import _exit
69 except ImportError:
70 pass
71 import macpath
72 path = macpath
73 del macpath
Guido van Rossum2979b011994-08-01 11:18:30 +000074else:
Guido van Rossum2979b011994-08-01 11:18:30 +000075 raise ImportError, 'no os specific module found'
Guido van Rossume65cce51993-11-08 15:05:21 +000076
Guido van Rossuma28dab51997-08-29 22:36:47 +000077del _names
78
79# Make sure os.environ exists, at least
80try:
81 environ
82except NameError:
83 environ = {}
84
Guido van Rossume65cce51993-11-08 15:05:21 +000085def execl(file, *args):
86 execv(file, args)
87
88def execle(file, *args):
89 env = args[-1]
90 execve(file, args[:-1], env)
91
92def execlp(file, *args):
93 execvp(file, args)
94
Guido van Rossum030afb11995-03-14 17:27:18 +000095def execlpe(file, *args):
96 env = args[-1]
97 execvpe(file, args[:-1], env)
98
Guido van Rossume65cce51993-11-08 15:05:21 +000099def execvp(file, args):
Guido van Rossum030afb11995-03-14 17:27:18 +0000100 _execvpe(file, args)
101
102def execvpe(file, args, env):
103 _execvpe(file, args, env)
104
105_notfound = None
106def _execvpe(file, args, env = None):
107 if env:
108 func = execve
109 argrest = (args, env)
110 else:
111 func = execv
112 argrest = (args,)
113 env = environ
Guido van Rossum2979b011994-08-01 11:18:30 +0000114 global _notfound
115 head, tail = path.split(file)
116 if head:
Guido van Rossum030afb11995-03-14 17:27:18 +0000117 apply(func, (file,) + argrest)
Guido van Rossume65cce51993-11-08 15:05:21 +0000118 return
Guido van Rossum030afb11995-03-14 17:27:18 +0000119 if env.has_key('PATH'):
120 envpath = env['PATH']
Guido van Rossume65cce51993-11-08 15:05:21 +0000121 else:
Guido van Rossum2979b011994-08-01 11:18:30 +0000122 envpath = defpath
123 import string
124 PATH = string.splitfields(envpath, pathsep)
125 if not _notfound:
126 import tempfile
127 # Exec a file that is guaranteed not to exist
128 try: execv(tempfile.mktemp(), ())
129 except error, _notfound: pass
130 exc, arg = error, _notfound
Guido van Rossume65cce51993-11-08 15:05:21 +0000131 for dir in PATH:
132 fullname = path.join(dir, file)
133 try:
Guido van Rossum030afb11995-03-14 17:27:18 +0000134 apply(func, (fullname,) + argrest)
Guido van Rossume65cce51993-11-08 15:05:21 +0000135 except error, (errno, msg):
Guido van Rossum2979b011994-08-01 11:18:30 +0000136 if errno != arg[0]:
Guido van Rossume65cce51993-11-08 15:05:21 +0000137 exc, arg = error, (errno, msg)
138 raise exc, arg
Guido van Rossum2979b011994-08-01 11:18:30 +0000139
Guido van Rossum3b8e20d1996-07-24 00:55:17 +0000140# Change environ to automatically call putenv() if it exists
141try:
Guido van Rossuma28dab51997-08-29 22:36:47 +0000142 # This will fail if there's no putenv
143 putenv
Guido van Rossum3b8e20d1996-07-24 00:55:17 +0000144except NameError:
Guido van Rossuma28dab51997-08-29 22:36:47 +0000145 pass
146else:
Guido van Rossum3b8e20d1996-07-24 00:55:17 +0000147 import UserDict
148
149 class _Environ(UserDict.UserDict):
150 def __init__(self, environ):
151 UserDict.UserDict.__init__(self)
152 self.data = environ
153 def __setitem__(self, key, item):
154 putenv(key, item)
155 self.data[key] = item
156
157 environ = _Environ(environ)