setuptools_build.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import sys
  2. from typing import List, Optional, Sequence
  3. # Shim to wrap setup.py invocation with setuptools
  4. #
  5. # We set sys.argv[0] to the path to the underlying setup.py file so
  6. # setuptools / distutils don't take the path to the setup.py to be "-c" when
  7. # invoking via the shim. This avoids e.g. the following manifest_maker
  8. # warning: "warning: manifest_maker: standard file '-c' not found".
  9. _SETUPTOOLS_SHIM = (
  10. "import io, os, sys, setuptools, tokenize; sys.argv[0] = {0!r}; __file__={0!r};"
  11. "f = getattr(tokenize, 'open', open)(__file__) "
  12. "if os.path.exists(__file__) "
  13. "else io.StringIO('from setuptools import setup; setup()');"
  14. "code = f.read().replace('\\r\\n', '\\n');"
  15. "f.close();"
  16. "exec(compile(code, __file__, 'exec'))"
  17. )
  18. def make_setuptools_shim_args(
  19. setup_py_path, # type: str
  20. global_options=None, # type: Sequence[str]
  21. no_user_config=False, # type: bool
  22. unbuffered_output=False, # type: bool
  23. ):
  24. # type: (...) -> List[str]
  25. """
  26. Get setuptools command arguments with shim wrapped setup file invocation.
  27. :param setup_py_path: The path to setup.py to be wrapped.
  28. :param global_options: Additional global options.
  29. :param no_user_config: If True, disables personal user configuration.
  30. :param unbuffered_output: If True, adds the unbuffered switch to the
  31. argument list.
  32. """
  33. args = [sys.executable]
  34. if unbuffered_output:
  35. args += ["-u"]
  36. args += ["-c", _SETUPTOOLS_SHIM.format(setup_py_path)]
  37. if global_options:
  38. args += global_options
  39. if no_user_config:
  40. args += ["--no-user-cfg"]
  41. return args
  42. def make_setuptools_bdist_wheel_args(
  43. setup_py_path, # type: str
  44. global_options, # type: Sequence[str]
  45. build_options, # type: Sequence[str]
  46. destination_dir, # type: str
  47. ):
  48. # type: (...) -> List[str]
  49. # NOTE: Eventually, we'd want to also -S to the flags here, when we're
  50. # isolating. Currently, it breaks Python in virtualenvs, because it
  51. # relies on site.py to find parts of the standard library outside the
  52. # virtualenv.
  53. args = make_setuptools_shim_args(
  54. setup_py_path, global_options=global_options, unbuffered_output=True
  55. )
  56. args += ["bdist_wheel", "-d", destination_dir]
  57. args += build_options
  58. return args
  59. def make_setuptools_clean_args(
  60. setup_py_path, # type: str
  61. global_options, # type: Sequence[str]
  62. ):
  63. # type: (...) -> List[str]
  64. args = make_setuptools_shim_args(
  65. setup_py_path, global_options=global_options, unbuffered_output=True
  66. )
  67. args += ["clean", "--all"]
  68. return args
  69. def make_setuptools_develop_args(
  70. setup_py_path, # type: str
  71. global_options, # type: Sequence[str]
  72. install_options, # type: Sequence[str]
  73. no_user_config, # type: bool
  74. prefix, # type: Optional[str]
  75. home, # type: Optional[str]
  76. use_user_site, # type: bool
  77. ):
  78. # type: (...) -> List[str]
  79. assert not (use_user_site and prefix)
  80. args = make_setuptools_shim_args(
  81. setup_py_path,
  82. global_options=global_options,
  83. no_user_config=no_user_config,
  84. )
  85. args += ["develop", "--no-deps"]
  86. args += install_options
  87. if prefix:
  88. args += ["--prefix", prefix]
  89. if home is not None:
  90. args += ["--install-dir", home]
  91. if use_user_site:
  92. args += ["--user", "--prefix="]
  93. return args
  94. def make_setuptools_egg_info_args(
  95. setup_py_path, # type: str
  96. egg_info_dir, # type: Optional[str]
  97. no_user_config, # type: bool
  98. ):
  99. # type: (...) -> List[str]
  100. args = make_setuptools_shim_args(setup_py_path, no_user_config=no_user_config)
  101. args += ["egg_info"]
  102. if egg_info_dir:
  103. args += ["--egg-base", egg_info_dir]
  104. return args
  105. def make_setuptools_install_args(
  106. setup_py_path, # type: str
  107. global_options, # type: Sequence[str]
  108. install_options, # type: Sequence[str]
  109. record_filename, # type: str
  110. root, # type: Optional[str]
  111. prefix, # type: Optional[str]
  112. header_dir, # type: Optional[str]
  113. home, # type: Optional[str]
  114. use_user_site, # type: bool
  115. no_user_config, # type: bool
  116. pycompile, # type: bool
  117. ):
  118. # type: (...) -> List[str]
  119. assert not (use_user_site and prefix)
  120. assert not (use_user_site and root)
  121. args = make_setuptools_shim_args(
  122. setup_py_path,
  123. global_options=global_options,
  124. no_user_config=no_user_config,
  125. unbuffered_output=True,
  126. )
  127. args += ["install", "--record", record_filename]
  128. args += ["--single-version-externally-managed"]
  129. if root is not None:
  130. args += ["--root", root]
  131. if prefix is not None:
  132. args += ["--prefix", prefix]
  133. if home is not None:
  134. args += ["--home", home]
  135. if use_user_site:
  136. args += ["--user", "--prefix="]
  137. if pycompile:
  138. args += ["--compile"]
  139. else:
  140. args += ["--no-compile"]
  141. if header_dir:
  142. args += ["--install-headers", header_dir]
  143. args += install_options
  144. return args