spawn.py (3994B)
1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\carbon\common\stdlib\distutils\spawn.py 2 __revision__ = '$Id: spawn.py 82110 2010-06-20 13:38:51Z kristjan.jonsson $' 3 import sys 4 import os 5 from distutils.errors import DistutilsPlatformError, DistutilsExecError 6 from distutils import log 7 8 def spawn(cmd, search_path = 1, verbose = 0, dry_run = 0): 9 if os.name == 'posix': 10 _spawn_posix(cmd, search_path, dry_run=dry_run) 11 elif os.name == 'nt': 12 _spawn_nt(cmd, search_path, dry_run=dry_run) 13 elif os.name == 'os2': 14 _spawn_os2(cmd, search_path, dry_run=dry_run) 15 else: 16 raise DistutilsPlatformError, "don't know how to spawn programs on platform '%s'" % os.name 17 18 19 def _nt_quote_args(args): 20 for i, arg in enumerate(args): 21 if ' ' in arg: 22 args[i] = '"%s"' % arg 23 24 return args 25 26 27 def _spawn_nt(cmd, search_path = 1, verbose = 0, dry_run = 0): 28 executable = cmd[0] 29 cmd = _nt_quote_args(cmd) 30 if search_path: 31 executable = find_executable(executable) or executable 32 log.info(' '.join([executable] + cmd[1:])) 33 if not dry_run: 34 try: 35 rc = os.spawnv(os.P_WAIT, executable, cmd) 36 except OSError as exc: 37 raise DistutilsExecError, "command '%s' failed: %s" % (cmd[0], exc[-1]) 38 39 if rc != 0: 40 raise DistutilsExecError, "command '%s' failed with exit status %d" % (cmd[0], rc) 41 42 43 def _spawn_os2(cmd, search_path = 1, verbose = 0, dry_run = 0): 44 executable = cmd[0] 45 if search_path: 46 executable = find_executable(executable) or executable 47 log.info(' '.join([executable] + cmd[1:])) 48 if not dry_run: 49 try: 50 rc = os.spawnv(os.P_WAIT, executable, cmd) 51 except OSError as exc: 52 raise DistutilsExecError, "command '%s' failed: %s" % (cmd[0], exc[-1]) 53 54 if rc != 0: 55 log.debug("command '%s' failed with exit status %d" % (cmd[0], rc)) 56 raise DistutilsExecError, "command '%s' failed with exit status %d" % (cmd[0], rc) 57 58 59 def _spawn_posix(cmd, search_path = 1, verbose = 0, dry_run = 0): 60 log.info(' '.join(cmd)) 61 if dry_run: 62 return 63 exec_fn = search_path and os.execvp or os.execv 64 pid = os.fork() 65 if pid == 0: 66 try: 67 exec_fn(cmd[0], cmd) 68 except OSError as e: 69 sys.stderr.write('unable to execute %s: %s\n' % (cmd[0], e.strerror)) 70 os._exit(1) 71 72 sys.stderr.write('unable to execute %s for unknown reasons' % cmd[0]) 73 os._exit(1) 74 else: 75 while 1: 76 try: 77 pid, status = os.waitpid(pid, 0) 78 except OSError as exc: 79 import errno 80 if exc.errno == errno.EINTR: 81 continue 82 raise DistutilsExecError, "command '%s' failed: %s" % (cmd[0], exc[-1]) 83 84 if os.WIFSIGNALED(status): 85 raise DistutilsExecError, "command '%s' terminated by signal %d" % (cmd[0], os.WTERMSIG(status)) 86 elif os.WIFEXITED(status): 87 exit_status = os.WEXITSTATUS(status) 88 if exit_status == 0: 89 return 90 raise DistutilsExecError, "command '%s' failed with exit status %d" % (cmd[0], exit_status) 91 elif os.WIFSTOPPED(status): 92 continue 93 else: 94 raise DistutilsExecError, "unknown error executing '%s': termination status %d" % (cmd[0], status) 95 96 97 def find_executable(executable, path = None): 98 if path is None: 99 path = os.environ['PATH'] 100 paths = path.split(os.pathsep) 101 base, ext = os.path.splitext(executable) 102 if (sys.platform == 'win32' or os.name == 'os2') and ext != '.exe': 103 executable = executable + '.exe' 104 if not os.path.isfile(executable): 105 for p in paths: 106 f = os.path.join(p, executable) 107 if os.path.isfile(f): 108 return f 109 110 return 111 else: 112 return executable