spawn.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. """distutils.spawn
  2. Provides the 'spawn()' function, a front-end to various platform-
  3. specific functions for launching another program in a sub-process.
  4. Also provides the 'find_executable()' to search the path for a given
  5. executable name.
  6. """
  7. import sys
  8. import os
  9. import subprocess
  10. from distutils.errors import DistutilsPlatformError, DistutilsExecError
  11. from distutils.debug import DEBUG
  12. from distutils import log
  13. if sys.platform == 'darwin':
  14. _cfg_target = None
  15. _cfg_target_split = None
  16. def spawn(cmd, search_path=1, verbose=0, dry_run=0, env=None):
  17. """Run another program, specified as a command list 'cmd', in a new process.
  18. 'cmd' is just the argument list for the new process, ie.
  19. cmd[0] is the program to run and cmd[1:] are the rest of its arguments.
  20. There is no way to run a program with a name different from that of its
  21. executable.
  22. If 'search_path' is true (the default), the system's executable
  23. search path will be used to find the program; otherwise, cmd[0]
  24. must be the exact path to the executable. If 'dry_run' is true,
  25. the command will not actually be run.
  26. Raise DistutilsExecError if running the program fails in any way; just
  27. return on success.
  28. """
  29. # cmd is documented as a list, but just in case some code passes a tuple
  30. # in, protect our %-formatting code against horrible death
  31. cmd = list(cmd)
  32. log.info(' '.join(cmd))
  33. if dry_run:
  34. return
  35. if search_path:
  36. executable = find_executable(cmd[0])
  37. if executable is not None:
  38. cmd[0] = executable
  39. env = env if env is not None else dict(os.environ)
  40. if sys.platform == 'darwin':
  41. global _cfg_target, _cfg_target_split
  42. if _cfg_target is None:
  43. from distutils import sysconfig
  44. _cfg_target = sysconfig.get_config_var(
  45. 'MACOSX_DEPLOYMENT_TARGET') or ''
  46. if _cfg_target:
  47. _cfg_target_split = [int(x) for x in _cfg_target.split('.')]
  48. if _cfg_target:
  49. # ensure that the deployment target of build process is not less
  50. # than that used when the interpreter was built. This ensures
  51. # extension modules are built with correct compatibility values
  52. cur_target = os.environ.get('MACOSX_DEPLOYMENT_TARGET', _cfg_target)
  53. if _cfg_target_split > [int(x) for x in cur_target.split('.')]:
  54. my_msg = ('$MACOSX_DEPLOYMENT_TARGET mismatch: '
  55. 'now "%s" but "%s" during configure'
  56. % (cur_target, _cfg_target))
  57. raise DistutilsPlatformError(my_msg)
  58. env.update(MACOSX_DEPLOYMENT_TARGET=cur_target)
  59. try:
  60. proc = subprocess.Popen(cmd, env=env)
  61. proc.wait()
  62. exitcode = proc.returncode
  63. except OSError as exc:
  64. if not DEBUG:
  65. cmd = cmd[0]
  66. raise DistutilsExecError(
  67. "command %r failed: %s" % (cmd, exc.args[-1])) from exc
  68. if exitcode:
  69. if not DEBUG:
  70. cmd = cmd[0]
  71. raise DistutilsExecError(
  72. "command %r failed with exit code %s" % (cmd, exitcode))
  73. def find_executable(executable, path=None):
  74. """Tries to find 'executable' in the directories listed in 'path'.
  75. A string listing directories separated by 'os.pathsep'; defaults to
  76. os.environ['PATH']. Returns the complete filename or None if not found.
  77. """
  78. _, ext = os.path.splitext(executable)
  79. if (sys.platform == 'win32') and (ext != '.exe'):
  80. executable = executable + '.exe'
  81. if os.path.isfile(executable):
  82. return executable
  83. if path is None:
  84. path = os.environ.get('PATH', None)
  85. if path is None:
  86. try:
  87. path = os.confstr("CS_PATH")
  88. except (AttributeError, ValueError):
  89. # os.confstr() or CS_PATH is not available
  90. path = os.defpath
  91. # bpo-35755: Don't use os.defpath if the PATH environment variable is
  92. # set to an empty string
  93. # PATH='' doesn't match, whereas PATH=':' looks in the current directory
  94. if not path:
  95. return None
  96. paths = path.split(os.pathsep)
  97. for p in paths:
  98. f = os.path.join(p, executable)
  99. if os.path.isfile(f):
  100. # the file exists, we have a shot at spawn working
  101. return f
  102. return None