Use py_resource module
[python/dscho.git] / Lib / os.py
blobd974c59fa0f3e748d26056d985ebb0d85256884f
1 # os.py -- either mac, dos or posix depending on what system we're on.
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 ':')
10 # - os.pathsep is the component separator used in $PATH etc
11 # - os.defpath is the default search path for executables
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).
19 _osindex = {
20 'posix': ('.', '..', '/', ':', ':/bin:/usr/bin'),
21 'dos': ('.', '..', '\\', ';', '.;C:\\bin'),
22 'nt': ('.', '..', '\\', ';', '.;C:\\bin'),
23 'mac': (':', '::', ':', '\n', ':'),
26 # For freeze.py script:
27 if 0:
28 import posix
29 import posixpath
31 import sys
32 for 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 try:
44 environ
45 except:
46 environ = {} # Make sure os.environ exists, at least
47 break
48 else:
49 del name
50 raise ImportError, 'no os specific module found'
52 def execl(file, *args):
53 execv(file, args)
55 def execle(file, *args):
56 env = args[-1]
57 execve(file, args[:-1], env)
59 def execlp(file, *args):
60 execvp(file, args)
62 def execlpe(file, *args):
63 env = args[-1]
64 execvpe(file, args[:-1], env)
66 def execvp(file, args):
67 _execvpe(file, args)
69 def execvpe(file, args, env):
70 _execvpe(file, args, env)
72 _notfound = None
73 def _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
81 global _notfound
82 head, tail = path.split(file)
83 if head:
84 apply(func, (file,) + argrest)
85 return
86 if env.has_key('PATH'):
87 envpath = env['PATH']
88 else:
89 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
98 for dir in PATH:
99 fullname = path.join(dir, file)
100 try:
101 apply(func, (fullname,) + argrest)
102 except error, (errno, msg):
103 if errno != arg[0]:
104 exc, arg = error, (errno, msg)
105 raise exc, arg
107 # Provide listdir for Windows NT that doesn't have it built in
108 if 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