blob: fa13a2058c6c6ed706999aff476de0d3c3c10a62 [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:
Guido van Rossum61de0ac1997-12-05 21:24:30 +000027 name = 'posix'
Guido van Rossume9387ea1998-05-22 15:26:04 +000028 linesep = '\n'
Guido van Rossum61de0ac1997-12-05 21:24:30 +000029 curdir = '.'; pardir = '..'; sep = '/'; pathsep = ':'
30 defpath = ':/bin:/usr/bin'
31 from posix import *
32 try:
33 from posix import _exit
34 except ImportError:
35 pass
36 import posixpath
37 path = posixpath
38 del posixpath
Guido van Rossuma28dab51997-08-29 22:36:47 +000039elif 'nt' in _names:
Guido van Rossum61de0ac1997-12-05 21:24:30 +000040 name = 'nt'
Guido van Rossume9387ea1998-05-22 15:26:04 +000041 linesep = '\r\n'
Guido van Rossum61de0ac1997-12-05 21:24:30 +000042 curdir = '.'; pardir = '..'; sep = '\\'; pathsep = ';'
43 defpath = '.;C:\\bin'
44 from nt import *
45 try:
46 from nt import _exit
47 except ImportError:
48 pass
49 import ntpath
50 path = ntpath
51 del ntpath
Guido van Rossuma28dab51997-08-29 22:36:47 +000052elif 'dos' in _names:
Guido van Rossum61de0ac1997-12-05 21:24:30 +000053 name = 'dos'
Guido van Rossume9387ea1998-05-22 15:26:04 +000054 linesep = '\r\n'
Guido van Rossum61de0ac1997-12-05 21:24:30 +000055 curdir = '.'; pardir = '..'; sep = '\\'; pathsep = ';'
56 defpath = '.;C:\\bin'
57 from dos import *
58 try:
59 from dos import _exit
60 except ImportError:
61 pass
62 import dospath
63 path = dospath
64 del dospath
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000065elif 'os2' in _names:
Guido van Rossum61de0ac1997-12-05 21:24:30 +000066 name = 'os2'
Guido van Rossume9387ea1998-05-22 15:26:04 +000067 linesep = '\r\n'
Guido van Rossum61de0ac1997-12-05 21:24:30 +000068 curdir = '.'; pardir = '..'; sep = '\\'; pathsep = ';'
69 defpath = '.;C:\\bin'
70 from os2 import *
71 try:
72 from os2 import _exit
73 except ImportError:
74 pass
75 import ntpath
76 path = ntpath
77 del ntpath
Guido van Rossuma28dab51997-08-29 22:36:47 +000078elif 'mac' in _names:
Guido van Rossum61de0ac1997-12-05 21:24:30 +000079 name = 'mac'
Guido van Rossume9387ea1998-05-22 15:26:04 +000080 linesep = '\r'
Guido van Rossum61de0ac1997-12-05 21:24:30 +000081 curdir = ':'; pardir = '::'; sep = ':'; pathsep = '\n'
82 defpath = ':'
83 from mac import *
84 try:
85 from mac import _exit
86 except ImportError:
87 pass
88 import macpath
89 path = macpath
90 del macpath
Guido van Rossum2979b011994-08-01 11:18:30 +000091else:
Guido van Rossum61de0ac1997-12-05 21:24:30 +000092 raise ImportError, 'no os specific module found'
Guido van Rossume65cce51993-11-08 15:05:21 +000093
Guido van Rossuma28dab51997-08-29 22:36:47 +000094del _names
95
96# Make sure os.environ exists, at least
97try:
Guido van Rossum61de0ac1997-12-05 21:24:30 +000098 environ
Guido van Rossuma28dab51997-08-29 22:36:47 +000099except NameError:
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000100 environ = {}
Guido van Rossuma28dab51997-08-29 22:36:47 +0000101
Guido van Rossume65cce51993-11-08 15:05:21 +0000102def execl(file, *args):
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000103 execv(file, args)
Guido van Rossume65cce51993-11-08 15:05:21 +0000104
105def execle(file, *args):
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000106 env = args[-1]
107 execve(file, args[:-1], env)
Guido van Rossume65cce51993-11-08 15:05:21 +0000108
109def execlp(file, *args):
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000110 execvp(file, args)
Guido van Rossume65cce51993-11-08 15:05:21 +0000111
Guido van Rossum030afb11995-03-14 17:27:18 +0000112def execlpe(file, *args):
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000113 env = args[-1]
114 execvpe(file, args[:-1], env)
Guido van Rossum030afb11995-03-14 17:27:18 +0000115
Guido van Rossume65cce51993-11-08 15:05:21 +0000116def execvp(file, args):
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000117 _execvpe(file, args)
Guido van Rossum030afb11995-03-14 17:27:18 +0000118
119def execvpe(file, args, env):
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000120 _execvpe(file, args, env)
Guido van Rossum030afb11995-03-14 17:27:18 +0000121
122_notfound = None
123def _execvpe(file, args, env = None):
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000124 if env:
125 func = execve
126 argrest = (args, env)
127 else:
128 func = execv
129 argrest = (args,)
130 env = environ
131 global _notfound
132 head, tail = path.split(file)
133 if head:
134 apply(func, (file,) + argrest)
135 return
136 if env.has_key('PATH'):
137 envpath = env['PATH']
138 else:
139 envpath = defpath
140 import string
141 PATH = string.splitfields(envpath, pathsep)
142 if not _notfound:
143 import tempfile
144 # Exec a file that is guaranteed not to exist
145 try: execv(tempfile.mktemp(), ())
146 except error, _notfound: pass
147 exc, arg = error, _notfound
148 for dir in PATH:
149 fullname = path.join(dir, file)
150 try:
151 apply(func, (fullname,) + argrest)
152 except error, (errno, msg):
153 if errno != arg[0]:
154 exc, arg = error, (errno, msg)
155 raise exc, arg
Guido van Rossum2979b011994-08-01 11:18:30 +0000156
Guido van Rossum3b8e20d1996-07-24 00:55:17 +0000157# Change environ to automatically call putenv() if it exists
158try:
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000159 # This will fail if there's no putenv
160 putenv
Guido van Rossum3b8e20d1996-07-24 00:55:17 +0000161except NameError:
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000162 pass
Guido van Rossuma28dab51997-08-29 22:36:47 +0000163else:
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000164 import UserDict
Guido van Rossum3b8e20d1996-07-24 00:55:17 +0000165
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000166 if name in ('os2', ): # Where Env Var Names Must Be UPPERCASE
167 import string
168 class _Environ(UserDict.UserDict):
169 def __init__(self, environ):
170 UserDict.UserDict.__init__(self)
171 self.data = environ
172 def __setitem__(self, key, item):
173 key = string.upper(key)
174 putenv(key, item)
175 self.data[key] = item
176 def __getitem__(self, key):
177 return self.data[string.upper(key)]
Guido van Rossum3b8e20d1996-07-24 00:55:17 +0000178
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000179 else: # Where Env Var Names Can Be Mixed Case
180 class _Environ(UserDict.UserDict):
181 def __init__(self, environ):
182 UserDict.UserDict.__init__(self)
183 self.data = environ
184 def __getinitargs__(self):
185 import copy
186 return (copy.copy(self.data),)
187 def __setitem__(self, key, item):
188 putenv(key, item)
189 self.data[key] = item
190 def __copy__(self):
191 return _Environ(self.data.copy())
192
193 environ = _Environ(environ)