blob: 51e8a67fca56277f968f24a93231ca2388ddda31 [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 Rossumb59cdd41992-04-06 14:03:45 +00009# - os.sep is the (or a most common) pathname separator ('/' or ':')
Guido van Rossum2979b011994-08-01 11:18:30 +000010# - os.pathsep is the component separator used in $PATH etc
11# - os.defpath is the default search path for executables
Guido van Rossum31104f41992-01-14 18:28:36 +000012
13# Programs that import and use 'os' stand a better chance of being
14# portable between different platforms. Of course, they must then
15# only use functions that are defined by all platforms (e.g., unlink
16# and opendir), and leave all pathname manipulation to os.path
17# (e.g., split and join).
18
Guido van Rossum2979b011994-08-01 11:18:30 +000019_osindex = {
20 'posix': ('.', '..', '/', ':', ':/bin:/usr/bin'),
21 'dos': ('.', '..', '\\', ';', '.;C:\\bin'),
22 'nt': ('.', '..', '\\', ';', '.;C:\\bin'),
Guido van Rossum4c7fa4b1995-03-14 17:53:54 +000023 'mac': (':', '::', ':', '\n', ':'),
Guido van Rossum2979b011994-08-01 11:18:30 +000024}
Guido van Rossum1a76ef21992-03-31 18:57:28 +000025
Guido van Rossuma28f2dc1994-08-17 12:33:28 +000026# For freeze.py script:
27if 0:
28 import posix
Guido van Rossumca9321e1994-10-05 15:17:55 +000029 import posixpath
Guido van Rossuma28f2dc1994-08-17 12:33:28 +000030
Guido van Rossum2979b011994-08-01 11:18:30 +000031import sys
32for name in _osindex.keys():
33 if name in sys.builtin_module_names:
34 curdir, pardir, sep, pathsep, defpath = _osindex[name]
35 exec 'from %s import *' % name
36 exec 'import %spath' % name
37 exec 'path = %spath' % name
38 exec 'del %spath' % name
39 try:
40 exec 'from %s import _exit' % name
41 except ImportError:
42 pass
Guido van Rossumb7677091995-08-07 20:15:23 +000043 try:
44 environ
45 except:
46 environ = {} # Make sure os.environ exists, at least
Guido van Rossum2979b011994-08-01 11:18:30 +000047 break
48else:
49 del name
50 raise ImportError, 'no os specific module found'
Guido van Rossume65cce51993-11-08 15:05:21 +000051
52def execl(file, *args):
53 execv(file, args)
54
55def execle(file, *args):
56 env = args[-1]
57 execve(file, args[:-1], env)
58
59def execlp(file, *args):
60 execvp(file, args)
61
Guido van Rossum030afb11995-03-14 17:27:18 +000062def execlpe(file, *args):
63 env = args[-1]
64 execvpe(file, args[:-1], env)
65
Guido van Rossume65cce51993-11-08 15:05:21 +000066def execvp(file, args):
Guido van Rossum030afb11995-03-14 17:27:18 +000067 _execvpe(file, args)
68
69def execvpe(file, args, env):
70 _execvpe(file, args, env)
71
72_notfound = None
73def _execvpe(file, args, env = None):
74 if env:
75 func = execve
76 argrest = (args, env)
77 else:
78 func = execv
79 argrest = (args,)
80 env = environ
Guido van Rossum2979b011994-08-01 11:18:30 +000081 global _notfound
82 head, tail = path.split(file)
83 if head:
Guido van Rossum030afb11995-03-14 17:27:18 +000084 apply(func, (file,) + argrest)
Guido van Rossume65cce51993-11-08 15:05:21 +000085 return
Guido van Rossum030afb11995-03-14 17:27:18 +000086 if env.has_key('PATH'):
87 envpath = env['PATH']
Guido van Rossume65cce51993-11-08 15:05:21 +000088 else:
Guido van Rossum2979b011994-08-01 11:18:30 +000089 envpath = defpath
90 import string
91 PATH = string.splitfields(envpath, pathsep)
92 if not _notfound:
93 import tempfile
94 # Exec a file that is guaranteed not to exist
95 try: execv(tempfile.mktemp(), ())
96 except error, _notfound: pass
97 exc, arg = error, _notfound
Guido van Rossume65cce51993-11-08 15:05:21 +000098 for dir in PATH:
99 fullname = path.join(dir, file)
100 try:
Guido van Rossum030afb11995-03-14 17:27:18 +0000101 apply(func, (fullname,) + argrest)
Guido van Rossume65cce51993-11-08 15:05:21 +0000102 except error, (errno, msg):
Guido van Rossum2979b011994-08-01 11:18:30 +0000103 if errno != arg[0]:
Guido van Rossume65cce51993-11-08 15:05:21 +0000104 exc, arg = error, (errno, msg)
105 raise exc, arg
Guido van Rossum2979b011994-08-01 11:18:30 +0000106
107# Provide listdir for Windows NT that doesn't have it built in
108if name == 'nt':
109 try:
110 _tmp = listdir
111 del _tmp
112 except NameError:
113 def listdir(name):
114 if path.ismount(name):
115 list = ['.']
116 else:
117 list = ['.', '..']
118 f = popen('dir/l/b ' + name, 'r')
119 line = f.readline()
120 while line:
121 list.append(line[:-1])
122 line = f.readline()
123 return list
Guido van Rossum3b8e20d1996-07-24 00:55:17 +0000124
125
126# Change environ to automatically call putenv() if it exists
127try:
128 _putenv = putenv
129except NameError:
130 _putenv = None
131if _putenv:
132 import UserDict
133
134 class _Environ(UserDict.UserDict):
135 def __init__(self, environ):
136 UserDict.UserDict.__init__(self)
137 self.data = environ
138 def __setitem__(self, key, item):
139 putenv(key, item)
140 self.data[key] = item
141
142 environ = _Environ(environ)