blob: bb90f2eed59fe0609e1d634392f773f3496f166f [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 Rossum31104f41992-01-14 18:28:36 +000010
11# Programs that import and use 'os' stand a better chance of being
12# portable between different platforms. Of course, they must then
13# only use functions that are defined by all platforms (e.g., unlink
14# and opendir), and leave all pathname manipulation to os.path
15# (e.g., split and join).
16
Guido van Rossumdd8cb441993-12-29 15:33:08 +000017# XXX This is incorrect if the import *path fails...
Guido van Rossum1a76ef21992-03-31 18:57:28 +000018
Guido van Rossum31104f41992-01-14 18:28:36 +000019try:
20 from posix import *
Guido van Rossum35fb82a1993-01-26 13:04:43 +000021 try:
22 from posix import _exit
23 except ImportError:
24 pass
Guido van Rossum31104f41992-01-14 18:28:36 +000025 name = 'posix'
26 curdir = '.'
Guido van Rossum1a76ef21992-03-31 18:57:28 +000027 pardir = '..'
Guido van Rossumb59cdd41992-04-06 14:03:45 +000028 sep = '/'
Guido van Rossum1a76ef21992-03-31 18:57:28 +000029 import posixpath
30 path = posixpath
31 del posixpath
Guido van Rossum31104f41992-01-14 18:28:36 +000032except ImportError:
Guido van Rossumdd8cb441993-12-29 15:33:08 +000033 try:
34 from mac import *
35 name = 'mac'
36 curdir = ':'
37 pardir = '::'
38 sep = ':'
39 import macpath
40 path = macpath
41 del macpath
42 except ImportError:
43 from dos import *
44 name = 'dos'
45 curdir = '.' # XXX doesn't always work
46 pardir = '..' # XXX doesn't always work
47 sep = '/' # XXX or '\\' ???
48 import dospath
49 path = dospath
50 del dospath
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
62def execvp(file, args):
63 if '/' in file:
64 execv(file, args)
65 return
66 ENOENT = 2
67 if environ.has_key('PATH'):
68 import string
69 PATH = string.splitfields(environ['PATH'], ':')
70 else:
71 PATH = ['', '/bin', '/usr/bin']
72 exc, arg = (ENOENT, 'No such file or directory')
73 for dir in PATH:
74 fullname = path.join(dir, file)
75 try:
76 execv(fullname, args)
77 except error, (errno, msg):
78 if errno != ENOENT:
79 exc, arg = error, (errno, msg)
80 raise exc, arg