blob: 5c8f3dcd1b8970a474d526573fe4198e5576f031 [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
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
43 break
44else:
45 del name
46 raise ImportError, 'no os specific module found'
Guido van Rossume65cce51993-11-08 15:05:21 +000047
48def execl(file, *args):
49 execv(file, args)
50
51def execle(file, *args):
52 env = args[-1]
53 execve(file, args[:-1], env)
54
55def execlp(file, *args):
56 execvp(file, args)
57
Guido van Rossum2979b011994-08-01 11:18:30 +000058_notfound = None
Guido van Rossume65cce51993-11-08 15:05:21 +000059def execvp(file, args):
Guido van Rossum2979b011994-08-01 11:18:30 +000060 global _notfound
61 head, tail = path.split(file)
62 if head:
Guido van Rossume65cce51993-11-08 15:05:21 +000063 execv(file, args)
64 return
65 ENOENT = 2
66 if environ.has_key('PATH'):
Guido van Rossum2979b011994-08-01 11:18:30 +000067 envpath = environ['PATH']
Guido van Rossume65cce51993-11-08 15:05:21 +000068 else:
Guido van Rossum2979b011994-08-01 11:18:30 +000069 envpath = defpath
70 import string
71 PATH = string.splitfields(envpath, pathsep)
72 if not _notfound:
73 import tempfile
74 # Exec a file that is guaranteed not to exist
75 try: execv(tempfile.mktemp(), ())
76 except error, _notfound: pass
77 exc, arg = error, _notfound
Guido van Rossume65cce51993-11-08 15:05:21 +000078 for dir in PATH:
79 fullname = path.join(dir, file)
80 try:
81 execv(fullname, args)
82 except error, (errno, msg):
Guido van Rossum2979b011994-08-01 11:18:30 +000083 if errno != arg[0]:
Guido van Rossume65cce51993-11-08 15:05:21 +000084 exc, arg = error, (errno, msg)
85 raise exc, arg
Guido van Rossum2979b011994-08-01 11:18:30 +000086
87# Provide listdir for Windows NT that doesn't have it built in
88if name == 'nt':
89 try:
90 _tmp = listdir
91 del _tmp
92 except NameError:
93 def listdir(name):
94 if path.ismount(name):
95 list = ['.']
96 else:
97 list = ['.', '..']
98 f = popen('dir/l/b ' + name, 'r')
99 line = f.readline()
100 while line:
101 list.append(line[:-1])
102 line = f.readline()
103 return list