blob: 14dc908827fe7e8e06234a72fe3eacf503ceb4c0 [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
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 Rossum030afb11995-03-14 17:27:18 +000058def execlpe(file, *args):
59 env = args[-1]
60 execvpe(file, args[:-1], env)
61
Guido van Rossume65cce51993-11-08 15:05:21 +000062def execvp(file, args):
Guido van Rossum030afb11995-03-14 17:27:18 +000063 _execvpe(file, args)
64
65def execvpe(file, args, env):
66 _execvpe(file, args, env)
67
68_notfound = None
69def _execvpe(file, args, env = None):
70 if env:
71 func = execve
72 argrest = (args, env)
73 else:
74 func = execv
75 argrest = (args,)
76 env = environ
Guido van Rossum2979b011994-08-01 11:18:30 +000077 global _notfound
78 head, tail = path.split(file)
79 if head:
Guido van Rossum030afb11995-03-14 17:27:18 +000080 apply(func, (file,) + argrest)
Guido van Rossume65cce51993-11-08 15:05:21 +000081 return
Guido van Rossum030afb11995-03-14 17:27:18 +000082 if env.has_key('PATH'):
83 envpath = env['PATH']
Guido van Rossume65cce51993-11-08 15:05:21 +000084 else:
Guido van Rossum2979b011994-08-01 11:18:30 +000085 envpath = defpath
86 import string
87 PATH = string.splitfields(envpath, pathsep)
88 if not _notfound:
89 import tempfile
90 # Exec a file that is guaranteed not to exist
91 try: execv(tempfile.mktemp(), ())
92 except error, _notfound: pass
93 exc, arg = error, _notfound
Guido van Rossume65cce51993-11-08 15:05:21 +000094 for dir in PATH:
95 fullname = path.join(dir, file)
96 try:
Guido van Rossum030afb11995-03-14 17:27:18 +000097 apply(func, (fullname,) + argrest)
Guido van Rossume65cce51993-11-08 15:05:21 +000098 except error, (errno, msg):
Guido van Rossum2979b011994-08-01 11:18:30 +000099 if errno != arg[0]:
Guido van Rossume65cce51993-11-08 15:05:21 +0000100 exc, arg = error, (errno, msg)
101 raise exc, arg
Guido van Rossum2979b011994-08-01 11:18:30 +0000102
103# Provide listdir for Windows NT that doesn't have it built in
104if name == 'nt':
105 try:
106 _tmp = listdir
107 del _tmp
108 except NameError:
109 def listdir(name):
110 if path.ismount(name):
111 list = ['.']
112 else:
113 list = ['.', '..']
114 f = popen('dir/l/b ' + name, 'r')
115 line = f.readline()
116 while line:
117 list.append(line[:-1])
118 line = f.readline()
119 return list