blob: d4cfff987e76e22a55426a2fdf567418b106897f [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 *
Guido van Rossumfb801e71999-02-22 15:40:34 +000045 for i in ['_exit']:
Guido van Rossum67c65b21999-02-01 23:52:29 +000046 try:
47 exec "from nt import " + i
48 except ImportError:
49 pass
Guido van Rossum61de0ac1997-12-05 21:24:30 +000050 import ntpath
51 path = ntpath
52 del ntpath
Guido van Rossuma28dab51997-08-29 22:36:47 +000053elif 'dos' in _names:
Guido van Rossum61de0ac1997-12-05 21:24:30 +000054 name = 'dos'
Guido van Rossume9387ea1998-05-22 15:26:04 +000055 linesep = '\r\n'
Guido van Rossum61de0ac1997-12-05 21:24:30 +000056 curdir = '.'; pardir = '..'; sep = '\\'; pathsep = ';'
57 defpath = '.;C:\\bin'
58 from dos import *
59 try:
60 from dos import _exit
61 except ImportError:
62 pass
63 import dospath
64 path = dospath
65 del dospath
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000066elif 'os2' in _names:
Guido van Rossum61de0ac1997-12-05 21:24:30 +000067 name = 'os2'
Guido van Rossume9387ea1998-05-22 15:26:04 +000068 linesep = '\r\n'
Guido van Rossum61de0ac1997-12-05 21:24:30 +000069 curdir = '.'; pardir = '..'; sep = '\\'; pathsep = ';'
70 defpath = '.;C:\\bin'
71 from os2 import *
72 try:
73 from os2 import _exit
74 except ImportError:
75 pass
76 import ntpath
77 path = ntpath
78 del ntpath
Guido van Rossuma28dab51997-08-29 22:36:47 +000079elif 'mac' in _names:
Guido van Rossum61de0ac1997-12-05 21:24:30 +000080 name = 'mac'
Guido van Rossume9387ea1998-05-22 15:26:04 +000081 linesep = '\r'
Guido van Rossum61de0ac1997-12-05 21:24:30 +000082 curdir = ':'; pardir = '::'; sep = ':'; pathsep = '\n'
83 defpath = ':'
84 from mac import *
85 try:
86 from mac import _exit
87 except ImportError:
88 pass
89 import macpath
90 path = macpath
91 del macpath
Guido van Rossum2979b011994-08-01 11:18:30 +000092else:
Guido van Rossum61de0ac1997-12-05 21:24:30 +000093 raise ImportError, 'no os specific module found'
Guido van Rossume65cce51993-11-08 15:05:21 +000094
Guido van Rossuma28dab51997-08-29 22:36:47 +000095del _names
96
Fred Drake02379091999-01-19 16:05:13 +000097sys.modules['os.path'] = path
98
Guido van Rossum4def7de1998-07-24 20:48:03 +000099# Super directory utilities.
100# (Inspired by Eric Raymond; the doc strings are mostly his)
101
102def makedirs(name, mode=0777):
103 """makedirs(path [, mode=0777]) -> None
104
105 Super-mkdir; create a leaf directory and all intermediate ones.
106 Works like mkdir, except that any intermediate path segment (not
107 just the rightmost) will be created if it does not exist. This is
108 recursive.
109
110 """
111 head, tail = path.split(name)
112 if head and tail and not path.exists(head):
113 makedirs(head, mode)
114 mkdir(name, mode)
115
116def removedirs(name):
117 """removedirs(path) -> None
118
119 Super-rmdir; remove a leaf directory and empty all intermediate
120 ones. Works like rmdir except that, if the leaf directory is
121 successfully removed, directories corresponding to rightmost path
122 segments will be pruned way until either the whole path is
123 consumed or an error occurs. Errors during this latter phase are
124 ignored -- they generally mean that a directory was not empty.
125
126 """
127 rmdir(name)
128 head, tail = path.split(name)
129 while head and tail:
130 try:
131 rmdir(head)
132 except error:
133 break
134 head, tail = path.split(head)
135
136def renames(old, new):
137 """renames(old, new) -> None
138
139 Super-rename; create directories as necessary and delete any left
140 empty. Works like rename, except creation of any intermediate
141 directories needed to make the new pathname good is attempted
142 first. After the rename, directories corresponding to rightmost
143 path segments of the old name will be pruned way until either the
144 whole path is consumed or a nonempty directory is found.
145
146 Note: this function can fail with the new directory structure made
147 if you lack permissions needed to unlink the leaf directory or
148 file.
149
150 """
151 head, tail = path.split(new)
152 if head and tail and not path.exists(head):
153 makedirs(head)
154 rename(old, new)
155 head, tail = path.split(old)
156 if head and tail:
157 try:
158 removedirs(head)
159 except error:
160 pass
161
Guido van Rossuma28dab51997-08-29 22:36:47 +0000162# Make sure os.environ exists, at least
163try:
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000164 environ
Guido van Rossuma28dab51997-08-29 22:36:47 +0000165except NameError:
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000166 environ = {}
Guido van Rossuma28dab51997-08-29 22:36:47 +0000167
Guido van Rossume65cce51993-11-08 15:05:21 +0000168def execl(file, *args):
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000169 execv(file, args)
Guido van Rossume65cce51993-11-08 15:05:21 +0000170
171def execle(file, *args):
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000172 env = args[-1]
173 execve(file, args[:-1], env)
Guido van Rossume65cce51993-11-08 15:05:21 +0000174
175def execlp(file, *args):
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000176 execvp(file, args)
Guido van Rossume65cce51993-11-08 15:05:21 +0000177
Guido van Rossum030afb11995-03-14 17:27:18 +0000178def execlpe(file, *args):
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000179 env = args[-1]
180 execvpe(file, args[:-1], env)
Guido van Rossum030afb11995-03-14 17:27:18 +0000181
Guido van Rossume65cce51993-11-08 15:05:21 +0000182def execvp(file, args):
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000183 _execvpe(file, args)
Guido van Rossum030afb11995-03-14 17:27:18 +0000184
185def execvpe(file, args, env):
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000186 _execvpe(file, args, env)
Guido van Rossum030afb11995-03-14 17:27:18 +0000187
188_notfound = None
189def _execvpe(file, args, env = None):
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000190 if env:
191 func = execve
192 argrest = (args, env)
193 else:
194 func = execv
195 argrest = (args,)
196 env = environ
197 global _notfound
198 head, tail = path.split(file)
199 if head:
200 apply(func, (file,) + argrest)
201 return
202 if env.has_key('PATH'):
203 envpath = env['PATH']
204 else:
205 envpath = defpath
206 import string
207 PATH = string.splitfields(envpath, pathsep)
208 if not _notfound:
209 import tempfile
210 # Exec a file that is guaranteed not to exist
211 try: execv(tempfile.mktemp(), ())
212 except error, _notfound: pass
213 exc, arg = error, _notfound
214 for dir in PATH:
215 fullname = path.join(dir, file)
216 try:
217 apply(func, (fullname,) + argrest)
218 except error, (errno, msg):
219 if errno != arg[0]:
220 exc, arg = error, (errno, msg)
221 raise exc, arg
Guido van Rossum2979b011994-08-01 11:18:30 +0000222
Guido van Rossum3b8e20d1996-07-24 00:55:17 +0000223# Change environ to automatically call putenv() if it exists
224try:
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000225 # This will fail if there's no putenv
226 putenv
Guido van Rossum3b8e20d1996-07-24 00:55:17 +0000227except NameError:
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000228 pass
Guido van Rossuma28dab51997-08-29 22:36:47 +0000229else:
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000230 import UserDict
Guido van Rossum3b8e20d1996-07-24 00:55:17 +0000231
Guido van Rossumda4d6da1998-08-04 16:01:23 +0000232 if name in ('os2', 'nt', 'dos'): # Where Env Var Names Must Be UPPERCASE
233 # But we store them as upper case
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000234 import string
235 class _Environ(UserDict.UserDict):
236 def __init__(self, environ):
237 UserDict.UserDict.__init__(self)
Guido van Rossumda4d6da1998-08-04 16:01:23 +0000238 data = self.data
239 upper = string.upper
240 for k, v in environ.items():
241 data[upper(k)] = v
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000242 def __setitem__(self, key, item):
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000243 putenv(key, item)
Guido van Rossumda4d6da1998-08-04 16:01:23 +0000244 key = string.upper(key)
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000245 self.data[key] = item
246 def __getitem__(self, key):
247 return self.data[string.upper(key)]
Guido van Rossumb46413f1999-05-03 15:23:24 +0000248 def has_key(self, key):
249 return self.data.has_key(string.upper(key))
Guido van Rossum3b8e20d1996-07-24 00:55:17 +0000250
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000251 else: # Where Env Var Names Can Be Mixed Case
252 class _Environ(UserDict.UserDict):
253 def __init__(self, environ):
254 UserDict.UserDict.__init__(self)
255 self.data = environ
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000256 def __setitem__(self, key, item):
257 putenv(key, item)
258 self.data[key] = item
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000259
260 environ = _Environ(environ)