1edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepr"""OS routines for Mac, NT, or Posix depending on what system we're on.
2edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepThis exports:
4edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  - all functions from posix, nt, os2, or ce, e.g. unlink, stat, etc.
5edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  - os.path is one of the modules posixpath, or ntpath
6edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  - os.name is 'posix', 'nt', 'os2', 'ce' or 'riscos'
7edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  - os.curdir is a string representing the current directory ('.' or ':')
8edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  - os.pardir is a string representing the parent directory ('..' or '::')
9edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  - os.sep is the (or a most common) pathname separator ('/' or ':' or '\\')
10edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  - os.extsep is the extension separator ('.' or '/')
11edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  - os.altsep is the alternate pathname separator (None or '/')
12edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  - os.pathsep is the component separator used in $PATH etc
13edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  - os.linesep is the line separator in text files ('\r' or '\n' or '\r\n')
14edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  - os.defpath is the default search path for executables
15edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  - os.devnull is the file path of the null device ('/dev/null', etc.)
16edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
17edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepPrograms that import and use 'os' stand a better chance of being
18edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepportable between different platforms.  Of course, they must then
19edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoeponly use functions that are defined by all platforms (e.g., unlink
20edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepand opendir), and leave all pathname manipulation to os.path
21edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep(e.g., split and join).
22edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep"""
23edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
24edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep#'
25edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
26edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport sys, errno
27edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
28edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep_names = sys.builtin_module_names
29edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
30edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# Note:  more names are added to __all__ later.
31edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep__all__ = ["altsep", "curdir", "pardir", "sep", "extsep", "pathsep", "linesep",
32edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep           "defpath", "name", "path", "devnull",
33edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep           "SEEK_SET", "SEEK_CUR", "SEEK_END"]
34edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
35edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef _get_exports_list(module):
36edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    try:
37edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        return list(module.__all__)
38edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    except AttributeError:
39edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        return [n for n in dir(module) if n[0] != '_']
40edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
41edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepif 'posix' in _names:
42edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    name = 'posix'
43edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    linesep = '\n'
44edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    from posix import *
45edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    try:
46edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        from posix import _exit
47edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    except ImportError:
48edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        pass
49edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    import posixpath as path
50edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
51edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    import posix
52edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    __all__.extend(_get_exports_list(posix))
53edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    del posix
54edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
55edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepelif 'nt' in _names:
56edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    name = 'nt'
57edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    linesep = '\r\n'
58edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    from nt import *
59edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    try:
60edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        from nt import _exit
61edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    except ImportError:
62edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        pass
63edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    import ntpath as path
64edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
65edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    import nt
66edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    __all__.extend(_get_exports_list(nt))
67edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    del nt
68edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
69edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepelif 'os2' in _names:
70edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    name = 'os2'
71edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    linesep = '\r\n'
72edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    from os2 import *
73edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    try:
74edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        from os2 import _exit
75edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    except ImportError:
76edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        pass
77edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    if sys.version.find('EMX GCC') == -1:
78edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        import ntpath as path
79edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    else:
80edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        import os2emxpath as path
81edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        from _emx_link import link
82edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
83edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    import os2
84edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    __all__.extend(_get_exports_list(os2))
85edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    del os2
86edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
87edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepelif 'ce' in _names:
88edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    name = 'ce'
89edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    linesep = '\r\n'
90edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    from ce import *
91edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    try:
92edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        from ce import _exit
93edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    except ImportError:
94edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        pass
95edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # We can use the standard Windows path.
96edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    import ntpath as path
97edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
98edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    import ce
99edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    __all__.extend(_get_exports_list(ce))
100edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    del ce
101edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
102edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepelif 'riscos' in _names:
103edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    name = 'riscos'
104edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    linesep = '\n'
105edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    from riscos import *
106edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    try:
107edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        from riscos import _exit
108edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    except ImportError:
109edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        pass
110edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    import riscospath as path
111edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
112edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    import riscos
113edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    __all__.extend(_get_exports_list(riscos))
114edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    del riscos
115edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
116edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepelse:
117edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    raise ImportError, 'no os specific module found'
118edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
119edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepsys.modules['os.path'] = path
120edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepfrom os.path import (curdir, pardir, sep, pathsep, defpath, extsep, altsep,
121edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    devnull)
122edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
123edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdel _names
124edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
125edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# Python uses fixed values for the SEEK_ constants; they are mapped
126edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# to native constants if necessary in posixmodule.c
127edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepSEEK_SET = 0
128edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepSEEK_CUR = 1
129edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepSEEK_END = 2
130edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
131edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep#'
132edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
133edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# Super directory utilities.
134edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# (Inspired by Eric Raymond; the doc strings are mostly his)
135edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
136edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef makedirs(name, mode=0777):
137edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """makedirs(path [, mode=0777])
138edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
139edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Super-mkdir; create a leaf directory and all intermediate ones.
140edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Works like mkdir, except that any intermediate path segment (not
141edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    just the rightmost) will be created if it does not exist.  This is
142edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    recursive.
143edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
144edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
145edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    head, tail = path.split(name)
146edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    if not tail:
147edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        head, tail = path.split(head)
148edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    if head and tail and not path.exists(head):
149edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
150edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            makedirs(head, mode)
151edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except OSError, e:
152edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            # be happy if someone already created the path
153edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            if e.errno != errno.EEXIST:
154edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                raise
155edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if tail == curdir:           # xxx/newdir/. exists if xxx/newdir exists
156edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            return
157edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    mkdir(name, mode)
158edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
159edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef removedirs(name):
160edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """removedirs(path)
161edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
162edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Super-rmdir; remove a leaf directory and all empty intermediate
163edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ones.  Works like rmdir except that, if the leaf directory is
164edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    successfully removed, directories corresponding to rightmost path
165edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    segments will be pruned away until either the whole path is
166edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    consumed or an error occurs.  Errors during this latter phase are
167edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ignored -- they generally mean that a directory was not empty.
168edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
169edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
170edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    rmdir(name)
171edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    head, tail = path.split(name)
172edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    if not tail:
173edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        head, tail = path.split(head)
174edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    while head and tail:
175edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
176edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            rmdir(head)
177edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except error:
178edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            break
179edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        head, tail = path.split(head)
180edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
181edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef renames(old, new):
182edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """renames(old, new)
183edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
184edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Super-rename; create directories as necessary and delete any left
185edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    empty.  Works like rename, except creation of any intermediate
186edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    directories needed to make the new pathname good is attempted
187edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    first.  After the rename, directories corresponding to rightmost
188edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    path segments of the old name will be pruned way until either the
189edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    whole path is consumed or a nonempty directory is found.
190edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
191edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Note: this function can fail with the new directory structure made
192edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    if you lack permissions needed to unlink the leaf directory or
193edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    file.
194edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
195edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
196edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    head, tail = path.split(new)
197edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    if head and tail and not path.exists(head):
198edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        makedirs(head)
199edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    rename(old, new)
200edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    head, tail = path.split(old)
201edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    if head and tail:
202edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
203edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            removedirs(head)
204edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except error:
205edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
206edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
207edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep__all__.extend(["makedirs", "removedirs", "renames"])
208edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
209edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef walk(top, topdown=True, onerror=None, followlinks=False):
210edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """Directory tree generator.
211edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
212edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    For each directory in the directory tree rooted at top (including top
213edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    itself, but excluding '.' and '..'), yields a 3-tuple
214edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
215edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        dirpath, dirnames, filenames
216edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
217edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    dirpath is a string, the path to the directory.  dirnames is a list of
218edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    the names of the subdirectories in dirpath (excluding '.' and '..').
219edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    filenames is a list of the names of the non-directory files in dirpath.
220edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Note that the names in the lists are just names, with no path components.
221edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    To get a full path (which begins with top) to a file or directory in
222edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    dirpath, do os.path.join(dirpath, name).
223edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
224edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    If optional arg 'topdown' is true or not specified, the triple for a
225edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    directory is generated before the triples for any of its subdirectories
226edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    (directories are generated top down).  If topdown is false, the triple
227edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    for a directory is generated after the triples for all of its
228edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    subdirectories (directories are generated bottom up).
229edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
230edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    When topdown is true, the caller can modify the dirnames list in-place
231edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    (e.g., via del or slice assignment), and walk will only recurse into the
232edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    subdirectories whose names remain in dirnames; this can be used to prune
233edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    the search, or to impose a specific order of visiting.  Modifying
234edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    dirnames when topdown is false is ineffective, since the directories in
235edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    dirnames have already been generated by the time dirnames itself is
236edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    generated.
237edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
238edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    By default errors from the os.listdir() call are ignored.  If
239edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    optional arg 'onerror' is specified, it should be a function; it
240edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    will be called with one argument, an os.error instance.  It can
241edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    report the error to continue with the walk, or raise the exception
242edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    to abort the walk.  Note that the filename is available as the
243edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    filename attribute of the exception object.
244edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
245edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    By default, os.walk does not follow symbolic links to subdirectories on
246edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    systems that support them.  In order to get this functionality, set the
247edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    optional argument 'followlinks' to true.
248edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
249edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Caution:  if you pass a relative pathname for top, don't change the
250edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    current working directory between resumptions of walk.  walk never
251edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    changes the current directory, and assumes that the client doesn't
252edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    either.
253edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
254edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Example:
255edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
256edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    import os
257edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    from os.path import join, getsize
258edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    for root, dirs, files in os.walk('python/Lib/email'):
259edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        print root, "consumes",
260edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        print sum([getsize(join(root, name)) for name in files]),
261edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        print "bytes in", len(files), "non-directory files"
262edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if 'CVS' in dirs:
263edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            dirs.remove('CVS')  # don't visit CVS directories
264edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """
265edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
266edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    islink, join, isdir = path.islink, path.join, path.isdir
267edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
268edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # We may not have read permission for top, in which case we can't
269edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # get a list of the files the directory contains.  os.path.walk
270edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # always suppressed the exception then, rather than blow up for a
271edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # minor reason when (say) a thousand readable directories are still
272edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # left to visit.  That logic is copied here.
273edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    try:
274edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Note that listdir and error are globals in this module due
275edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # to earlier import-*.
276edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        names = listdir(top)
277edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    except error, err:
278edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if onerror is not None:
279edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            onerror(err)
280edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        return
281edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
282edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    dirs, nondirs = [], []
283edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    for name in names:
284edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if isdir(join(top, name)):
285edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            dirs.append(name)
286edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
287edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            nondirs.append(name)
288edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
289edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    if topdown:
290edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        yield top, dirs, nondirs
291edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    for name in dirs:
292edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        new_path = join(top, name)
293edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if followlinks or not islink(new_path):
294edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            for x in walk(new_path, topdown, onerror, followlinks):
295edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                yield x
296edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    if not topdown:
297edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        yield top, dirs, nondirs
298edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
299edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep__all__.append("walk")
300edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
301edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# Make sure os.environ exists, at least
302edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoeptry:
303edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    environ
304edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepexcept NameError:
305edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    environ = {}
306edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
307edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef execl(file, *args):
308edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """execl(file, *args)
309edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
310edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Execute the executable file with argument list args, replacing the
311edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    current process. """
312edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    execv(file, args)
313edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
314edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef execle(file, *args):
315edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """execle(file, *args, env)
316edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
317edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Execute the executable file with argument list args and
318edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    environment env, replacing the current process. """
319edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    env = args[-1]
320edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    execve(file, args[:-1], env)
321edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
322edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef execlp(file, *args):
323edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """execlp(file, *args)
324edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
325edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Execute the executable file (which is searched for along $PATH)
326edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    with argument list args, replacing the current process. """
327edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    execvp(file, args)
328edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
329edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef execlpe(file, *args):
330edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """execlpe(file, *args, env)
331edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
332edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Execute the executable file (which is searched for along $PATH)
333edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    with argument list args and environment env, replacing the current
334edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    process. """
335edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    env = args[-1]
336edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    execvpe(file, args[:-1], env)
337edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
338edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef execvp(file, args):
339edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """execvp(file, args)
340edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
341edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Execute the executable file (which is searched for along $PATH)
342edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    with argument list args, replacing the current process.
343edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    args may be a list or tuple of strings. """
344edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    _execvpe(file, args)
345edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
346edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef execvpe(file, args, env):
347edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """execvpe(file, args, env)
348edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
349edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    Execute the executable file (which is searched for along $PATH)
350edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    with argument list args and environment env , replacing the
351edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    current process.
352edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    args may be a list or tuple of strings. """
353edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    _execvpe(file, args, env)
354edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
355edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep__all__.extend(["execl","execle","execlp","execlpe","execvp","execvpe"])
356edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
357edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef _execvpe(file, args, env=None):
358edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    if env is not None:
359edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        func = execve
360edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        argrest = (args, env)
361edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    else:
362edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        func = execv
363edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        argrest = (args,)
364edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        env = environ
365edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
366edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    head, tail = path.split(file)
367edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    if head:
368edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        func(file, *argrest)
369edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        return
370edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    if 'PATH' in env:
371edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        envpath = env['PATH']
372edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    else:
373edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        envpath = defpath
374edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    PATH = envpath.split(pathsep)
375edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    saved_exc = None
376edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    saved_tb = None
377edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    for dir in PATH:
378edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        fullname = path.join(dir, file)
379edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
380edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            func(fullname, *argrest)
381edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except error, e:
382edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            tb = sys.exc_info()[2]
383edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            if (e.errno != errno.ENOENT and e.errno != errno.ENOTDIR
384edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                and saved_exc is None):
385edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                saved_exc = e
386edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                saved_tb = tb
387edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    if saved_exc:
388edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        raise error, saved_exc, saved_tb
389edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    raise error, e, tb
390edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
391edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# Change environ to automatically call putenv() if it exists
392edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoeptry:
393edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # This will fail if there's no putenv
394edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    putenv
395edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepexcept NameError:
396edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    pass
397edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepelse:
398edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    import UserDict
399edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
400edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # Fake unsetenv() for Windows
401edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # not sure about os2 here but
402edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # I'm guessing they are the same.
403edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
404edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    if name in ('os2', 'nt'):
405edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        def unsetenv(key):
406edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            putenv(key, "")
407edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
408edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    if name == "riscos":
409edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # On RISC OS, all env access goes through getenv and putenv
410edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        from riscosenviron import _Environ
411edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    elif name in ('os2', 'nt'):  # Where Env Var Names Must Be UPPERCASE
412edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # But we store them as upper case
413edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class _Environ(UserDict.IterableUserDict):
414edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __init__(self, environ):
415edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                UserDict.UserDict.__init__(self)
416edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                data = self.data
417edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                for k, v in environ.items():
418edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    data[k.upper()] = v
419edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __setitem__(self, key, item):
420edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                putenv(key, item)
421edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.data[key.upper()] = item
422edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __getitem__(self, key):
423edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return self.data[key.upper()]
424edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            try:
425edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                unsetenv
426edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            except NameError:
427edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                def __delitem__(self, key):
428edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    del self.data[key.upper()]
429edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            else:
430edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                def __delitem__(self, key):
431edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    unsetenv(key)
432edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    del self.data[key.upper()]
433edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                def clear(self):
434edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    for key in self.data.keys():
435edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        unsetenv(key)
436edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        del self.data[key]
437edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                def pop(self, key, *args):
438edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    unsetenv(key)
439edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    return self.data.pop(key.upper(), *args)
440edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def has_key(self, key):
441edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return key.upper() in self.data
442edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __contains__(self, key):
443edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return key.upper() in self.data
444edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def get(self, key, failobj=None):
445edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return self.data.get(key.upper(), failobj)
446edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def update(self, dict=None, **kwargs):
447edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                if dict:
448edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    try:
449edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        keys = dict.keys()
450edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    except AttributeError:
451edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        # List of (key, value)
452edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        for k, v in dict:
453edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                            self[k] = v
454edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    else:
455edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        # got keys
456edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        # cannot use items(), since mappings
457edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        # may not have them.
458edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        for k in keys:
459edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                            self[k] = dict[k]
460edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                if kwargs:
461edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    self.update(kwargs)
462edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def copy(self):
463edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return dict(self)
464edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
465edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    else:  # Where Env Var Names Can Be Mixed Case
466edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class _Environ(UserDict.IterableUserDict):
467edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __init__(self, environ):
468edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                UserDict.UserDict.__init__(self)
469edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.data = environ
470edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __setitem__(self, key, item):
471edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                putenv(key, item)
472edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.data[key] = item
473edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def update(self,  dict=None, **kwargs):
474edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                if dict:
475edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    try:
476edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        keys = dict.keys()
477edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    except AttributeError:
478edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        # List of (key, value)
479edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        for k, v in dict:
480edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                            self[k] = v
481edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    else:
482edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        # got keys
483edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        # cannot use items(), since mappings
484edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        # may not have them.
485edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        for k in keys:
486edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                            self[k] = dict[k]
487edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                if kwargs:
488edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    self.update(kwargs)
489edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            try:
490edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                unsetenv
491edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            except NameError:
492edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                pass
493edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            else:
494edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                def __delitem__(self, key):
495edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    unsetenv(key)
496edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    del self.data[key]
497edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                def clear(self):
498edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    for key in self.data.keys():
499edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        unsetenv(key)
500edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        del self.data[key]
501edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                def pop(self, key, *args):
502edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    unsetenv(key)
503edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    return self.data.pop(key, *args)
504edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def copy(self):
505edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return dict(self)
506edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
507edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
508edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    environ = _Environ(environ)
509edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
510edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef getenv(key, default=None):
511edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """Get an environment variable, return None if it doesn't exist.
512edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    The optional second argument can specify an alternate default."""
513edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    return environ.get(key, default)
514edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep__all__.append("getenv")
515edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
516edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef _exists(name):
517edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    return name in globals()
518edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
519edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# Supply spawn*() (probably only for Unix)
520edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepif _exists("fork") and not _exists("spawnv") and _exists("execv"):
521edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
522edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    P_WAIT = 0
523edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    P_NOWAIT = P_NOWAITO = 1
524edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
525edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # XXX Should we support P_DETACH?  I suppose it could fork()**2
526edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # and close the std I/O streams.  Also, P_OVERLAY is the same
527edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # as execv*()?
528edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
529edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def _spawnvef(mode, file, args, env, func):
530edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Internal helper; func is the exec*() function to use
531edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        pid = fork()
532edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if not pid:
533edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            # Child
534edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            try:
535edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                if env is None:
536edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    func(file, args)
537edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                else:
538edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    func(file, args, env)
539edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            except:
540edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                _exit(127)
541edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
542edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            # Parent
543edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            if mode == P_NOWAIT:
544edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return pid # Caller is responsible for waiting!
545edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            while 1:
546edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                wpid, sts = waitpid(pid, 0)
547edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                if WIFSTOPPED(sts):
548edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    continue
549edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                elif WIFSIGNALED(sts):
550edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    return -WTERMSIG(sts)
551edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                elif WIFEXITED(sts):
552edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    return WEXITSTATUS(sts)
553edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                else:
554edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    raise error, "Not stopped, signaled or exited???"
555edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
556edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def spawnv(mode, file, args):
557edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        """spawnv(mode, file, args) -> integer
558edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
559edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepExecute file with arguments from args in a subprocess.
560edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepIf mode == P_NOWAIT return the pid of the process.
561edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepIf mode == P_WAIT return the process's exit code if it exits normally;
562edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepotherwise return -SIG, where SIG is the signal that killed it. """
563edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        return _spawnvef(mode, file, args, None, execv)
564edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
565edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def spawnve(mode, file, args, env):
566edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        """spawnve(mode, file, args, env) -> integer
567edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
568edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepExecute file with arguments from args in a subprocess with the
569edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepspecified environment.
570edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepIf mode == P_NOWAIT return the pid of the process.
571edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepIf mode == P_WAIT return the process's exit code if it exits normally;
572edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepotherwise return -SIG, where SIG is the signal that killed it. """
573edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        return _spawnvef(mode, file, args, env, execve)
574edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
575edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # Note: spawnvp[e] is't currently supported on Windows
576edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
577edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def spawnvp(mode, file, args):
578edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        """spawnvp(mode, file, args) -> integer
579edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
580edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepExecute file (which is looked for along $PATH) with arguments from
581edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepargs in a subprocess.
582edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepIf mode == P_NOWAIT return the pid of the process.
583edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepIf mode == P_WAIT return the process's exit code if it exits normally;
584edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepotherwise return -SIG, where SIG is the signal that killed it. """
585edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        return _spawnvef(mode, file, args, None, execvp)
586edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
587edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def spawnvpe(mode, file, args, env):
588edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        """spawnvpe(mode, file, args, env) -> integer
589edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
590edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepExecute file (which is looked for along $PATH) with arguments from
591edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepargs in a subprocess with the supplied environment.
592edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepIf mode == P_NOWAIT return the pid of the process.
593edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepIf mode == P_WAIT return the process's exit code if it exits normally;
594edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepotherwise return -SIG, where SIG is the signal that killed it. """
595edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        return _spawnvef(mode, file, args, env, execvpe)
596edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
597edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepif _exists("spawnv"):
598edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # These aren't supplied by the basic Windows code
599edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # but can be easily implemented in Python
600edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
601edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def spawnl(mode, file, *args):
602edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        """spawnl(mode, file, *args) -> integer
603edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
604edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepExecute file with arguments from args in a subprocess.
605edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepIf mode == P_NOWAIT return the pid of the process.
606edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepIf mode == P_WAIT return the process's exit code if it exits normally;
607edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepotherwise return -SIG, where SIG is the signal that killed it. """
608edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        return spawnv(mode, file, args)
609edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
610edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def spawnle(mode, file, *args):
611edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        """spawnle(mode, file, *args, env) -> integer
612edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
613edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepExecute file with arguments from args in a subprocess with the
614edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepsupplied environment.
615edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepIf mode == P_NOWAIT return the pid of the process.
616edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepIf mode == P_WAIT return the process's exit code if it exits normally;
617edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepotherwise return -SIG, where SIG is the signal that killed it. """
618edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        env = args[-1]
619edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        return spawnve(mode, file, args[:-1], env)
620edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
621edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
622edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    __all__.extend(["spawnv", "spawnve", "spawnl", "spawnle",])
623edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
624edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
625edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepif _exists("spawnvp"):
626edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # At the moment, Windows doesn't implement spawnvp[e],
627edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # so it won't have spawnlp[e] either.
628edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def spawnlp(mode, file, *args):
629edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        """spawnlp(mode, file, *args) -> integer
630edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
631edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepExecute file (which is looked for along $PATH) with arguments from
632edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepargs in a subprocess with the supplied environment.
633edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepIf mode == P_NOWAIT return the pid of the process.
634edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepIf mode == P_WAIT return the process's exit code if it exits normally;
635edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepotherwise return -SIG, where SIG is the signal that killed it. """
636edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        return spawnvp(mode, file, args)
637edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
638edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def spawnlpe(mode, file, *args):
639edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        """spawnlpe(mode, file, *args, env) -> integer
640edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
641edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepExecute file (which is looked for along $PATH) with arguments from
642edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepargs in a subprocess with the supplied environment.
643edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepIf mode == P_NOWAIT return the pid of the process.
644edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepIf mode == P_WAIT return the process's exit code if it exits normally;
645edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepotherwise return -SIG, where SIG is the signal that killed it. """
646edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        env = args[-1]
647edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        return spawnvpe(mode, file, args[:-1], env)
648edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
649edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
650edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    __all__.extend(["spawnvp", "spawnvpe", "spawnlp", "spawnlpe",])
651edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
652edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
653edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# Supply popen2 etc. (for Unix)
654edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepif _exists("fork"):
655edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    if not _exists("popen2"):
656edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        def popen2(cmd, mode="t", bufsize=-1):
657edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            """Execute the shell command 'cmd' in a sub-process.  On UNIX, 'cmd'
658edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            may be a sequence, in which case arguments will be passed directly to
659edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            the program without shell intervention (as with os.spawnv()).  If 'cmd'
660edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            is a string it will be passed to the shell (as with os.system()). If
661edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            'bufsize' is specified, it sets the buffer size for the I/O pipes.  The
662edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            file objects (child_stdin, child_stdout) are returned."""
663edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            import warnings
664edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            msg = "os.popen2 is deprecated.  Use the subprocess module."
665edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            warnings.warn(msg, DeprecationWarning, stacklevel=2)
666edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
667edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            import subprocess
668edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            PIPE = subprocess.PIPE
669edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            p = subprocess.Popen(cmd, shell=isinstance(cmd, basestring),
670edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                 bufsize=bufsize, stdin=PIPE, stdout=PIPE,
671edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                 close_fds=True)
672edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            return p.stdin, p.stdout
673edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        __all__.append("popen2")
674edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
675edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    if not _exists("popen3"):
676edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        def popen3(cmd, mode="t", bufsize=-1):
677edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            """Execute the shell command 'cmd' in a sub-process.  On UNIX, 'cmd'
678edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            may be a sequence, in which case arguments will be passed directly to
679edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            the program without shell intervention (as with os.spawnv()).  If 'cmd'
680edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            is a string it will be passed to the shell (as with os.system()). If
681edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            'bufsize' is specified, it sets the buffer size for the I/O pipes.  The
682edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            file objects (child_stdin, child_stdout, child_stderr) are returned."""
683edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            import warnings
684edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            msg = "os.popen3 is deprecated.  Use the subprocess module."
685edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            warnings.warn(msg, DeprecationWarning, stacklevel=2)
686edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
687edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            import subprocess
688edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            PIPE = subprocess.PIPE
689edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            p = subprocess.Popen(cmd, shell=isinstance(cmd, basestring),
690edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                 bufsize=bufsize, stdin=PIPE, stdout=PIPE,
691edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                 stderr=PIPE, close_fds=True)
692edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            return p.stdin, p.stdout, p.stderr
693edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        __all__.append("popen3")
694edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
695edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    if not _exists("popen4"):
696edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        def popen4(cmd, mode="t", bufsize=-1):
697edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            """Execute the shell command 'cmd' in a sub-process.  On UNIX, 'cmd'
698edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            may be a sequence, in which case arguments will be passed directly to
699edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            the program without shell intervention (as with os.spawnv()).  If 'cmd'
700edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            is a string it will be passed to the shell (as with os.system()). If
701edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            'bufsize' is specified, it sets the buffer size for the I/O pipes.  The
702edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            file objects (child_stdin, child_stdout_stderr) are returned."""
703edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            import warnings
704edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            msg = "os.popen4 is deprecated.  Use the subprocess module."
705edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            warnings.warn(msg, DeprecationWarning, stacklevel=2)
706edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
707edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            import subprocess
708edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            PIPE = subprocess.PIPE
709edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            p = subprocess.Popen(cmd, shell=isinstance(cmd, basestring),
710edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                 bufsize=bufsize, stdin=PIPE, stdout=PIPE,
711edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                 stderr=subprocess.STDOUT, close_fds=True)
712edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            return p.stdin, p.stdout
713edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        __all__.append("popen4")
714edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
715edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport copy_reg as _copy_reg
716edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
717edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef _make_stat_result(tup, dict):
718edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    return stat_result(tup, dict)
719edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
720edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef _pickle_stat_result(sr):
721edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    (type, args) = sr.__reduce__()
722edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    return (_make_stat_result, args)
723edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
724edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoeptry:
725edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    _copy_reg.pickle(stat_result, _pickle_stat_result, _make_stat_result)
726edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepexcept NameError: # stat_result may not exist
727edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    pass
728edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
729edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef _make_statvfs_result(tup, dict):
730edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    return statvfs_result(tup, dict)
731edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
732edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef _pickle_statvfs_result(sr):
733edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    (type, args) = sr.__reduce__()
734edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    return (_make_statvfs_result, args)
735edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
736edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoeptry:
737edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    _copy_reg.pickle(statvfs_result, _pickle_statvfs_result,
738edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                     _make_statvfs_result)
739edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepexcept NameError: # statvfs_result may not exist
740edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    pass
741