ยปCore Development>Code coverage>Lib/distutils/spawn.py

Python code coverage for Lib/distutils/spawn.py

#countcontent
1n/a"""distutils.spawn
2n/a
3n/aProvides the 'spawn()' function, a front-end to various platform-
4n/aspecific functions for launching another program in a sub-process.
5n/aAlso provides the 'find_executable()' to search the path for a given
6n/aexecutable name.
7n/a"""
8n/a
9n/aimport sys
10n/aimport os
11n/a
12n/afrom distutils.errors import DistutilsPlatformError, DistutilsExecError
13n/afrom distutils.debug import DEBUG
14n/afrom distutils import log
15n/a
16n/adef spawn(cmd, search_path=1, verbose=0, dry_run=0):
17n/a """Run another program, specified as a command list 'cmd', in a new process.
18n/a
19n/a 'cmd' is just the argument list for the new process, ie.
20n/a cmd[0] is the program to run and cmd[1:] are the rest of its arguments.
21n/a There is no way to run a program with a name different from that of its
22n/a executable.
23n/a
24n/a If 'search_path' is true (the default), the system's executable
25n/a search path will be used to find the program; otherwise, cmd[0]
26n/a must be the exact path to the executable. If 'dry_run' is true,
27n/a the command will not actually be run.
28n/a
29n/a Raise DistutilsExecError if running the program fails in any way; just
30n/a return on success.
31n/a """
32n/a # cmd is documented as a list, but just in case some code passes a tuple
33n/a # in, protect our %-formatting code against horrible death
34n/a cmd = list(cmd)
35n/a if os.name == 'posix':
36n/a _spawn_posix(cmd, search_path, dry_run=dry_run)
37n/a elif os.name == 'nt':
38n/a _spawn_nt(cmd, search_path, dry_run=dry_run)
39n/a else:
40n/a raise DistutilsPlatformError(
41n/a "don't know how to spawn programs on platform '%s'" % os.name)
42n/a
43n/adef _nt_quote_args(args):
44n/a """Quote command-line arguments for DOS/Windows conventions.
45n/a
46n/a Just wraps every argument which contains blanks in double quotes, and
47n/a returns a new argument list.
48n/a """
49n/a # XXX this doesn't seem very robust to me -- but if the Windows guys
50n/a # say it'll work, I guess I'll have to accept it. (What if an arg
51n/a # contains quotes? What other magic characters, other than spaces,
52n/a # have to be escaped? Is there an escaping mechanism other than
53n/a # quoting?)
54n/a for i, arg in enumerate(args):
55n/a if ' ' in arg:
56n/a args[i] = '"%s"' % arg
57n/a return args
58n/a
59n/adef _spawn_nt(cmd, search_path=1, verbose=0, dry_run=0):
60n/a executable = cmd[0]
61n/a cmd = _nt_quote_args(cmd)
62n/a if search_path:
63n/a # either we find one or it stays the same
64n/a executable = find_executable(executable) or executable
65n/a log.info(' '.join([executable] + cmd[1:]))
66n/a if not dry_run:
67n/a # spawn for NT requires a full path to the .exe
68n/a try:
69n/a rc = os.spawnv(os.P_WAIT, executable, cmd)
70n/a except OSError as exc:
71n/a # this seems to happen when the command isn't found
72n/a if not DEBUG:
73n/a cmd = executable
74n/a raise DistutilsExecError(
75n/a "command %r failed: %s" % (cmd, exc.args[-1]))
76n/a if rc != 0:
77n/a # and this reflects the command running but failing
78n/a if not DEBUG:
79n/a cmd = executable
80n/a raise DistutilsExecError(
81n/a "command %r failed with exit status %d" % (cmd, rc))
82n/a
83n/aif sys.platform == 'darwin':
84n/a from distutils import sysconfig
85n/a _cfg_target = None
86n/a _cfg_target_split = None
87n/a
88n/adef _spawn_posix(cmd, search_path=1, verbose=0, dry_run=0):
89n/a log.info(' '.join(cmd))
90n/a if dry_run:
91n/a return
92n/a executable = cmd[0]
93n/a exec_fn = search_path and os.execvp or os.execv
94n/a env = None
95n/a if sys.platform == 'darwin':
96n/a global _cfg_target, _cfg_target_split
97n/a if _cfg_target is None:
98n/a _cfg_target = sysconfig.get_config_var(
99n/a 'MACOSX_DEPLOYMENT_TARGET') or ''
100n/a if _cfg_target:
101n/a _cfg_target_split = [int(x) for x in _cfg_target.split('.')]
102n/a if _cfg_target:
103n/a # ensure that the deployment target of build process is not less
104n/a # than that used when the interpreter was built. This ensures
105n/a # extension modules are built with correct compatibility values
106n/a cur_target = os.environ.get('MACOSX_DEPLOYMENT_TARGET', _cfg_target)
107n/a if _cfg_target_split > [int(x) for x in cur_target.split('.')]:
108n/a my_msg = ('$MACOSX_DEPLOYMENT_TARGET mismatch: '
109n/a 'now "%s" but "%s" during configure'
110n/a % (cur_target, _cfg_target))
111n/a raise DistutilsPlatformError(my_msg)
112n/a env = dict(os.environ,
113n/a MACOSX_DEPLOYMENT_TARGET=cur_target)
114n/a exec_fn = search_path and os.execvpe or os.execve
115n/a pid = os.fork()
116n/a if pid == 0: # in the child
117n/a try:
118n/a if env is None:
119n/a exec_fn(executable, cmd)
120n/a else:
121n/a exec_fn(executable, cmd, env)
122n/a except OSError as e:
123n/a if not DEBUG:
124n/a cmd = executable
125n/a sys.stderr.write("unable to execute %r: %s\n"
126n/a % (cmd, e.strerror))
127n/a os._exit(1)
128n/a
129n/a if not DEBUG:
130n/a cmd = executable
131n/a sys.stderr.write("unable to execute %r for unknown reasons" % cmd)
132n/a os._exit(1)
133n/a else: # in the parent
134n/a # Loop until the child either exits or is terminated by a signal
135n/a # (ie. keep waiting if it's merely stopped)
136n/a while True:
137n/a try:
138n/a pid, status = os.waitpid(pid, 0)
139n/a except OSError as exc:
140n/a if not DEBUG:
141n/a cmd = executable
142n/a raise DistutilsExecError(
143n/a "command %r failed: %s" % (cmd, exc.args[-1]))
144n/a if os.WIFSIGNALED(status):
145n/a if not DEBUG:
146n/a cmd = executable
147n/a raise DistutilsExecError(
148n/a "command %r terminated by signal %d"
149n/a % (cmd, os.WTERMSIG(status)))
150n/a elif os.WIFEXITED(status):
151n/a exit_status = os.WEXITSTATUS(status)
152n/a if exit_status == 0:
153n/a return # hey, it succeeded!
154n/a else:
155n/a if not DEBUG:
156n/a cmd = executable
157n/a raise DistutilsExecError(
158n/a "command %r failed with exit status %d"
159n/a % (cmd, exit_status))
160n/a elif os.WIFSTOPPED(status):
161n/a continue
162n/a else:
163n/a if not DEBUG:
164n/a cmd = executable
165n/a raise DistutilsExecError(
166n/a "unknown error executing %r: termination status %d"
167n/a % (cmd, status))
168n/a
169n/adef find_executable(executable, path=None):
170n/a """Tries to find 'executable' in the directories listed in 'path'.
171n/a
172n/a A string listing directories separated by 'os.pathsep'; defaults to
173n/a os.environ['PATH']. Returns the complete filename or None if not found.
174n/a """
175n/a if path is None:
176n/a path = os.environ['PATH']
177n/a
178n/a paths = path.split(os.pathsep)
179n/a base, ext = os.path.splitext(executable)
180n/a
181n/a if (sys.platform == 'win32') and (ext != '.exe'):
182n/a executable = executable + '.exe'
183n/a
184n/a if not os.path.isfile(executable):
185n/a for p in paths:
186n/a f = os.path.join(p, executable)
187n/a if os.path.isfile(f):
188n/a # the file exists, we have a shot at spawn working
189n/a return f
190n/a return None
191n/a else:
192n/a return executable