blob: e723b075ef32fce77ba58d144e6a8fdcb2c0e555 [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'),
23 'mac': (':', '::', ':', ' ', ':'),
24}
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
29
Guido van Rossum2979b011994-08-01 11:18:30 +000030import sys
31for name in _osindex.keys():
32 if name in sys.builtin_module_names:
33 curdir, pardir, sep, pathsep, defpath = _osindex[name]
34 exec 'from %s import *' % name
35 exec 'import %spath' % name
36 exec 'path = %spath' % name
37 exec 'del %spath' % name
38 try:
39 exec 'from %s import _exit' % name
40 except ImportError:
41 pass
42 break
43else:
44 del name
45 raise ImportError, 'no os specific module found'
Guido van Rossume65cce51993-11-08 15:05:21 +000046
47def execl(file, *args):
48 execv(file, args)
49
50def execle(file, *args):
51 env = args[-1]
52 execve(file, args[:-1], env)
53
54def execlp(file, *args):
55 execvp(file, args)
56
Guido van Rossum2979b011994-08-01 11:18:30 +000057_notfound = None
Guido van Rossume65cce51993-11-08 15:05:21 +000058def execvp(file, args):
Guido van Rossum2979b011994-08-01 11:18:30 +000059 global _notfound
60 head, tail = path.split(file)
61 if head:
Guido van Rossume65cce51993-11-08 15:05:21 +000062 execv(file, args)
63 return
64 ENOENT = 2
65 if environ.has_key('PATH'):
Guido van Rossum2979b011994-08-01 11:18:30 +000066 envpath = environ['PATH']
Guido van Rossume65cce51993-11-08 15:05:21 +000067 else:
Guido van Rossum2979b011994-08-01 11:18:30 +000068 envpath = defpath
69 import string
70 PATH = string.splitfields(envpath, pathsep)
71 if not _notfound:
72 import tempfile
73 # Exec a file that is guaranteed not to exist
74 try: execv(tempfile.mktemp(), ())
75 except error, _notfound: pass
76 exc, arg = error, _notfound
Guido van Rossume65cce51993-11-08 15:05:21 +000077 for dir in PATH:
78 fullname = path.join(dir, file)
79 try:
80 execv(fullname, args)
81 except error, (errno, msg):
Guido van Rossum2979b011994-08-01 11:18:30 +000082 if errno != arg[0]:
Guido van Rossume65cce51993-11-08 15:05:21 +000083 exc, arg = error, (errno, msg)
84 raise exc, arg
Guido van Rossum2979b011994-08-01 11:18:30 +000085
86# Provide listdir for Windows NT that doesn't have it built in
87if name == 'nt':
88 try:
89 _tmp = listdir
90 del _tmp
91 except NameError:
92 def listdir(name):
93 if path.ismount(name):
94 list = ['.']
95 else:
96 list = ['.', '..']
97 f = popen('dir/l/b ' + name, 'r')
98 line = f.readline()
99 while line:
100 list.append(line[:-1])
101 line = f.readline()
102 return list