diff options
| author | Tarek Ziade <tarek@ziade.org> | 2010-07-03 16:18:51 +0200 |
|---|---|---|
| committer | Tarek Ziade <tarek@ziade.org> | 2010-07-03 16:18:51 +0200 |
| commit | f61fd745e1bd5a86c7ab2a7717fd1d9cc568c6bd (patch) | |
| tree | e6c3b53395a4966d91fc9107a831a1f668beafad | |
| parent | a737a17d0a5a8b81d21e1f90a2bb01f7bd615188 (diff) | |
| download | disutils2-f61fd745e1bd5a86c7ab2a7717fd1d9cc568c6bd.tar.gz | |
now spawn takes the environment when required (like byte compilation here, was failing)
| -rw-r--r-- | src/distutils2/spawn.py | 41 | ||||
| -rw-r--r-- | src/distutils2/util.py | 12 |
2 files changed, 39 insertions, 14 deletions
diff --git a/src/distutils2/spawn.py b/src/distutils2/spawn.py index a875459..08996fc 100644 --- a/src/distutils2/spawn.py +++ b/src/distutils2/spawn.py @@ -14,7 +14,7 @@ import os from distutils2.errors import DistutilsPlatformError, DistutilsExecError from distutils2 import log -def spawn(cmd, search_path=1, verbose=0, dry_run=0): +def spawn(cmd, search_path=1, verbose=0, dry_run=0, env=None): """Run another program, specified as a command list 'cmd', in a new process. 'cmd' is just the argument list for the new process, ie. @@ -27,15 +27,18 @@ def spawn(cmd, search_path=1, verbose=0, dry_run=0): must be the exact path to the executable. If 'dry_run' is true, the command will not actually be run. + If 'env' is given, it's a environment dictionary used for the execution + environment. + Raise DistutilsExecError if running the program fails in any way; just return on success. """ if os.name == 'posix': - _spawn_posix(cmd, search_path, dry_run=dry_run) + _spawn_posix(cmd, search_path, dry_run=dry_run, env=env) elif os.name == 'nt': - _spawn_nt(cmd, search_path, dry_run=dry_run) + _spawn_nt(cmd, search_path, dry_run=dry_run, env=env) elif os.name == 'os2': - _spawn_os2(cmd, search_path, dry_run=dry_run) + _spawn_os2(cmd, search_path, dry_run=dry_run, env=env) else: raise DistutilsPlatformError, \ "don't know how to spawn programs on platform '%s'" % os.name @@ -56,7 +59,7 @@ def _nt_quote_args(args): args[i] = '"%s"' % arg return args -def _spawn_nt(cmd, search_path=1, verbose=0, dry_run=0): +def _spawn_nt(cmd, search_path=1, verbose=0, dry_run=0, env=None): executable = cmd[0] cmd = _nt_quote_args(cmd) if search_path: @@ -66,7 +69,11 @@ def _spawn_nt(cmd, search_path=1, verbose=0, dry_run=0): if not dry_run: # spawn for NT requires a full path to the .exe try: - rc = os.spawnv(os.P_WAIT, executable, cmd) + if env is None: + rc = os.spawnv(os.P_WAIT, executable, cmd) + else: + rc = os.spawnve(os.P_WAIT, executable, cmd, env) + except OSError, exc: # this seems to happen when the command isn't found raise DistutilsExecError, \ @@ -76,7 +83,7 @@ def _spawn_nt(cmd, search_path=1, verbose=0, dry_run=0): raise DistutilsExecError, \ "command '%s' failed with exit status %d" % (cmd[0], rc) -def _spawn_os2(cmd, search_path=1, verbose=0, dry_run=0): +def _spawn_os2(cmd, search_path=1, verbose=0, dry_run=0, env=None): executable = cmd[0] if search_path: # either we find one or it stays the same @@ -85,7 +92,11 @@ def _spawn_os2(cmd, search_path=1, verbose=0, dry_run=0): if not dry_run: # spawnv for OS/2 EMX requires a full path to the .exe try: - rc = os.spawnv(os.P_WAIT, executable, cmd) + if env is None: + rc = os.spawnv(os.P_WAIT, executable, cmd) + else: + rc = os.spawnve(os.P_WAIT, executable, cmd, env) + except OSError, exc: # this seems to happen when the command isn't found raise DistutilsExecError, \ @@ -97,16 +108,24 @@ def _spawn_os2(cmd, search_path=1, verbose=0, dry_run=0): "command '%s' failed with exit status %d" % (cmd[0], rc) -def _spawn_posix(cmd, search_path=1, verbose=0, dry_run=0): +def _spawn_posix(cmd, search_path=1, verbose=0, dry_run=0, env=None): log.info(' '.join(cmd)) if dry_run: return - exec_fn = search_path and os.execvp or os.execv + + if env is None: + exec_fn = search_path and os.execvp or os.execv + else: + exec_fn = search_path and os.execvpe or os.execve + pid = os.fork() if pid == 0: # in the child try: - exec_fn(cmd[0], cmd) + if env is None: + exec_fn(cmd[0], cmd) + else: + exec_fn(cmd[0], cmd, env) except OSError, e: sys.stderr.write("unable to execute %s: %s\n" % (cmd[0], e.strerror)) diff --git a/src/distutils2/util.py b/src/distutils2/util.py index 0750d31..28f04b0 100644 --- a/src/distutils2/util.py +++ b/src/distutils2/util.py @@ -6,6 +6,7 @@ Miscellaneous utility functions. __revision__ = "$Id: util.py 77761 2010-01-26 22:46:15Z tarek.ziade $" import sys, os, string, re +from copy import copy from fnmatch import fnmatchcase from distutils2.errors import (DistutilsPlatformError, DistutilsFileError, @@ -394,9 +395,14 @@ byte_compile(files, optimize=%r, force=%r, cmd.insert(1, "-O") elif optimize == 2: cmd.insert(1, "-OO") - spawn(cmd, dry_run=dry_run) - execute(os.remove, (script_name,), "removing %s" % script_name, - dry_run=dry_run) + + env = copy(os.environ) + env['PYTHONPATH'] = ':'.join(sys.path) + try: + spawn(cmd, dry_run=dry_run, env=env) + finally: + execute(os.remove, (script_name,), "removing %s" % script_name, + dry_run=dry_run) # "Direct" byte-compilation: use the py_compile module to compile # right here, right now. Note that the script generated in indirect |
