os.py revision a28dab5ea2649b244a62ada83d017be39d520099
1# os.py -- either mac, dos 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.
5# - os.path is either module posixpath or macpath
6# - os.name is either 'posix' or 'mac'
7# - os.curdir is a string representing the current directory ('.' or ':')
8# - os.pardir is a string representing the parent directory ('..' or '::')
9# - os.sep is the (or a most common) pathname separator ('/' or ':' or '\\')
10# - os.altsep is the alternatte pathname separator (None or '/')
11# - os.pathsep is the component separator used in $PATH etc
12# - os.defpath is the default search path for executables
13
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
20import sys
21
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
74else:
75	raise ImportError, 'no os specific module found'
76
77del _names
78
79# Make sure os.environ exists, at least
80try:
81	environ
82except NameError:
83	environ = {}
84
85def 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
95def execlpe(file, *args):
96	env = args[-1]
97	execvpe(file, args[:-1], env)
98
99def execvp(file, args):
100	_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
114	global _notfound
115	head, tail = path.split(file)
116	if head:
117		apply(func, (file,) + argrest)
118		return
119	if env.has_key('PATH'):
120		envpath = env['PATH']
121	else:
122		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
131	for dir in PATH:
132		fullname = path.join(dir, file)
133		try:
134			apply(func, (fullname,) + argrest)
135		except error, (errno, msg):
136			if errno != arg[0]:
137				exc, arg = error, (errno, msg)
138	raise exc, arg
139
140# Change environ to automatically call putenv() if it exists
141try:
142	# This will fail if there's no putenv
143	putenv
144except NameError:
145	pass
146else:
147	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)
158