Greg Ward | b4dbfb3 | 1999-08-14 23:57:17 +0000 | [diff] [blame] | 1 | """distutils.spawn |
| 2 | |
| 3 | Provides the 'spawn()' function, a front-end to various platform- |
Greg Ward | 88608ca | 2000-08-02 01:08:02 +0000 | [diff] [blame] | 4 | specific functions for launching another program in a sub-process. |
| 5 | Also provides the 'find_executable()' to search the path for a given |
Greg Ward | a30f7ac | 2000-09-26 02:00:51 +0000 | [diff] [blame] | 6 | executable name. |
| 7 | """ |
Greg Ward | b4dbfb3 | 1999-08-14 23:57:17 +0000 | [diff] [blame] | 8 | |
Tarek Ziadé | 861d644 | 2009-06-02 16:18:55 +0000 | [diff] [blame] | 9 | import sys |
| 10 | import os |
Victor Stinner | 1ec63b6 | 2020-03-04 14:50:19 +0100 | [diff] [blame] | 11 | import subprocess |
Tarek Ziadé | 861d644 | 2009-06-02 16:18:55 +0000 | [diff] [blame] | 12 | |
| 13 | from distutils.errors import DistutilsPlatformError, DistutilsExecError |
Éric Araujo | 45fc871 | 2014-03-13 04:55:35 -0400 | [diff] [blame] | 14 | from distutils.debug import DEBUG |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 15 | from distutils import log |
Greg Ward | b4dbfb3 | 1999-08-14 23:57:17 +0000 | [diff] [blame] | 16 | |
Victor Stinner | 1ec63b6 | 2020-03-04 14:50:19 +0100 | [diff] [blame] | 17 | |
| 18 | if sys.platform == 'darwin': |
| 19 | _cfg_target = None |
| 20 | _cfg_target_split = None |
| 21 | |
| 22 | |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 23 | def spawn(cmd, search_path=1, verbose=0, dry_run=0): |
Tarek Ziadé | 861d644 | 2009-06-02 16:18:55 +0000 | [diff] [blame] | 24 | """Run another program, specified as a command list 'cmd', in a new process. |
| 25 | |
| 26 | 'cmd' is just the argument list for the new process, ie. |
Greg Ward | a30f7ac | 2000-09-26 02:00:51 +0000 | [diff] [blame] | 27 | cmd[0] is the program to run and cmd[1:] are the rest of its arguments. |
| 28 | There is no way to run a program with a name different from that of its |
| 29 | executable. |
Greg Ward | b4dbfb3 | 1999-08-14 23:57:17 +0000 | [diff] [blame] | 30 | |
Andrew M. Kuchling | fec3262 | 2002-11-21 20:41:07 +0000 | [diff] [blame] | 31 | If 'search_path' is true (the default), the system's executable |
| 32 | search path will be used to find the program; otherwise, cmd[0] |
| 33 | must be the exact path to the executable. If 'dry_run' is true, |
Greg Ward | a30f7ac | 2000-09-26 02:00:51 +0000 | [diff] [blame] | 34 | the command will not actually be run. |
Greg Ward | b4dbfb3 | 1999-08-14 23:57:17 +0000 | [diff] [blame] | 35 | |
Greg Ward | a30f7ac | 2000-09-26 02:00:51 +0000 | [diff] [blame] | 36 | Raise DistutilsExecError if running the program fails in any way; just |
| 37 | return on success. |
| 38 | """ |
Éric Araujo | 45fc871 | 2014-03-13 04:55:35 -0400 | [diff] [blame] | 39 | # cmd is documented as a list, but just in case some code passes a tuple |
| 40 | # in, protect our %-formatting code against horrible death |
| 41 | cmd = list(cmd) |
Greg Ward | b4dbfb3 | 1999-08-14 23:57:17 +0000 | [diff] [blame] | 42 | |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 43 | log.info(' '.join(cmd)) |
Greg Ward | b4dbfb3 | 1999-08-14 23:57:17 +0000 | [diff] [blame] | 44 | if dry_run: |
| 45 | return |
Victor Stinner | 1ec63b6 | 2020-03-04 14:50:19 +0100 | [diff] [blame] | 46 | |
| 47 | if search_path: |
| 48 | executable = find_executable(cmd[0]) |
| 49 | if executable is not None: |
| 50 | cmd[0] = executable |
| 51 | |
Éric Araujo | 45fc871 | 2014-03-13 04:55:35 -0400 | [diff] [blame] | 52 | env = None |
Ned Deily | a8f8b50 | 2011-06-28 19:44:24 -0700 | [diff] [blame] | 53 | if sys.platform == 'darwin': |
| 54 | global _cfg_target, _cfg_target_split |
| 55 | if _cfg_target is None: |
Paul Monson | 62dfd7d | 2019-04-25 11:36:45 -0700 | [diff] [blame] | 56 | from distutils import sysconfig |
Ronald Oussoren | 49926cf | 2021-02-01 04:29:44 +0100 | [diff] [blame] | 57 | _cfg_target = sysconfig.get_config_var( |
| 58 | 'MACOSX_DEPLOYMENT_TARGET') or '' |
Ned Deily | a8f8b50 | 2011-06-28 19:44:24 -0700 | [diff] [blame] | 59 | if _cfg_target: |
| 60 | _cfg_target_split = [int(x) for x in _cfg_target.split('.')] |
| 61 | if _cfg_target: |
Ned Deily | 8703178 | 2021-05-02 20:28:43 -0400 | [diff] [blame] | 62 | # Ensure that the deployment target of the build process is not |
| 63 | # less than 10.3 if the interpreter was built for 10.3 or later. |
| 64 | # This ensures extension modules are built with correct |
| 65 | # compatibility values, specifically LDSHARED which can use |
| 66 | # '-undefined dynamic_lookup' which only works on >= 10.3. |
Ned Deily | a8f8b50 | 2011-06-28 19:44:24 -0700 | [diff] [blame] | 67 | cur_target = os.environ.get('MACOSX_DEPLOYMENT_TARGET', _cfg_target) |
Ned Deily | 8703178 | 2021-05-02 20:28:43 -0400 | [diff] [blame] | 68 | cur_target_split = [int(x) for x in cur_target.split('.')] |
| 69 | if _cfg_target_split[:2] >= [10, 3] and cur_target_split[:2] < [10, 3]: |
Ned Deily | a8f8b50 | 2011-06-28 19:44:24 -0700 | [diff] [blame] | 70 | my_msg = ('$MACOSX_DEPLOYMENT_TARGET mismatch: ' |
Ned Deily | 8703178 | 2021-05-02 20:28:43 -0400 | [diff] [blame] | 71 | 'now "%s" but "%s" during configure;' |
| 72 | 'must use 10.3 or later' |
Ned Deily | a8f8b50 | 2011-06-28 19:44:24 -0700 | [diff] [blame] | 73 | % (cur_target, _cfg_target)) |
| 74 | raise DistutilsPlatformError(my_msg) |
| 75 | env = dict(os.environ, |
| 76 | MACOSX_DEPLOYMENT_TARGET=cur_target) |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 77 | |
Jason R. Coombs | 6ae2780 | 2020-07-07 07:11:28 -0400 | [diff] [blame] | 78 | try: |
| 79 | proc = subprocess.Popen(cmd, env=env) |
| 80 | proc.wait() |
| 81 | exitcode = proc.returncode |
| 82 | except OSError as exc: |
| 83 | if not DEBUG: |
| 84 | cmd = cmd[0] |
| 85 | raise DistutilsExecError( |
| 86 | "command %r failed: %s" % (cmd, exc.args[-1])) from exc |
Victor Stinner | 1ec63b6 | 2020-03-04 14:50:19 +0100 | [diff] [blame] | 87 | |
| 88 | if exitcode: |
Éric Araujo | 45fc871 | 2014-03-13 04:55:35 -0400 | [diff] [blame] | 89 | if not DEBUG: |
Victor Stinner | 1ec63b6 | 2020-03-04 14:50:19 +0100 | [diff] [blame] | 90 | cmd = cmd[0] |
| 91 | raise DistutilsExecError( |
| 92 | "command %r failed with exit code %s" % (cmd, exitcode)) |
| 93 | |
Greg Ward | 88608ca | 2000-08-02 01:08:02 +0000 | [diff] [blame] | 94 | |
Greg Ward | 88608ca | 2000-08-02 01:08:02 +0000 | [diff] [blame] | 95 | def find_executable(executable, path=None): |
Tarek Ziadé | 861d644 | 2009-06-02 16:18:55 +0000 | [diff] [blame] | 96 | """Tries to find 'executable' in the directories listed in 'path'. |
| 97 | |
| 98 | A string listing directories separated by 'os.pathsep'; defaults to |
| 99 | os.environ['PATH']. Returns the complete filename or None if not found. |
Greg Ward | 88608ca | 2000-08-02 01:08:02 +0000 | [diff] [blame] | 100 | """ |
Victor Stinner | 228a3c9 | 2019-04-17 16:26:36 +0200 | [diff] [blame] | 101 | _, ext = os.path.splitext(executable) |
Jesus Cea | d17833d | 2012-10-11 01:20:12 +0200 | [diff] [blame] | 102 | if (sys.platform == 'win32') and (ext != '.exe'): |
Greg Ward | 88608ca | 2000-08-02 01:08:02 +0000 | [diff] [blame] | 103 | executable = executable + '.exe' |
Tarek Ziadé | 861d644 | 2009-06-02 16:18:55 +0000 | [diff] [blame] | 104 | |
Victor Stinner | 228a3c9 | 2019-04-17 16:26:36 +0200 | [diff] [blame] | 105 | if os.path.isfile(executable): |
Greg Ward | 88608ca | 2000-08-02 01:08:02 +0000 | [diff] [blame] | 106 | return executable |
Victor Stinner | 228a3c9 | 2019-04-17 16:26:36 +0200 | [diff] [blame] | 107 | |
| 108 | if path is None: |
| 109 | path = os.environ.get('PATH', None) |
| 110 | if path is None: |
| 111 | try: |
| 112 | path = os.confstr("CS_PATH") |
| 113 | except (AttributeError, ValueError): |
| 114 | # os.confstr() or CS_PATH is not available |
| 115 | path = os.defpath |
| 116 | # bpo-35755: Don't use os.defpath if the PATH environment variable is |
Victor Stinner | 197f044 | 2019-04-17 17:44:06 +0200 | [diff] [blame] | 117 | # set to an empty string |
Victor Stinner | 228a3c9 | 2019-04-17 16:26:36 +0200 | [diff] [blame] | 118 | |
| 119 | # PATH='' doesn't match, whereas PATH=':' looks in the current directory |
| 120 | if not path: |
| 121 | return None |
| 122 | |
| 123 | paths = path.split(os.pathsep) |
| 124 | for p in paths: |
| 125 | f = os.path.join(p, executable) |
| 126 | if os.path.isfile(f): |
| 127 | # the file exists, we have a shot at spawn working |
| 128 | return f |
| 129 | return None |