spawn.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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):
  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 = None
  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 = dict(os.environ,
  59. MACOSX_DEPLOYMENT_TARGET=cur_target)
  60. proc = subprocess.Popen(cmd, env=env)
  61. proc.wait()
  62. exitcode = proc.returncode
  63. if exitcode:
  64. if not DEBUG:
  65. cmd = cmd[0]
  66. raise DistutilsExecError(
  67. "command %r failed with exit code %s" % (cmd, exitcode))
  68. def find_executable(executable, path=None):
  69. """Tries to find 'executable' in the directories listed in 'path'.
  70. A string listing directories separated by 'os.pathsep'; defaults to
  71. os.environ['PATH']. Returns the complete filename or None if not found.
  72. """
  73. _, ext = os.path.splitext(executable)
  74. if (sys.platform == 'win32') and (ext != '.exe'):
  75. executable = executable + '.exe'
  76. if os.path.isfile(executable):
  77. return executable
  78. if path is None:
  79. path = os.environ.get('PATH', None)
  80. if path is None:
  81. try:
  82. path = os.confstr("CS_PATH")
  83. except (AttributeError, ValueError):
  84. # os.confstr() or CS_PATH is not available
  85. path = os.defpath
  86. # bpo-35755: Don't use os.defpath if the PATH environment variable is
  87. # set to an empty string
  88. # PATH='' doesn't match, whereas PATH=':' looks in the current directory
  89. if not path:
  90. return None
  91. paths = path.split(os.pathsep)
  92. for p in paths:
  93. f = os.path.join(p, executable)
  94. if os.path.isfile(f):
  95. # the file exists, we have a shot at spawn working
  96. return f
  97. return None