setuptools_build.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import sys
  2. from pip._internal.utils.typing import MYPY_CHECK_RUNNING
  3. if MYPY_CHECK_RUNNING:
  4. from typing import List, Sequence
  5. # Shim to wrap setup.py invocation with setuptools
  6. #
  7. # We set sys.argv[0] to the path to the underlying setup.py file so
  8. # setuptools / distutils don't take the path to the setup.py to be "-c" when
  9. # invoking via the shim. This avoids e.g. the following manifest_maker
  10. # warning: "warning: manifest_maker: standard file '-c' not found".
  11. _SETUPTOOLS_SHIM = (
  12. "import sys, setuptools, tokenize; sys.argv[0] = {0!r}; __file__={0!r};"
  13. "f=getattr(tokenize, 'open', open)(__file__);"
  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.append('-u')
  36. args.extend(['-c', _SETUPTOOLS_SHIM.format(setup_py_path)])
  37. if global_options:
  38. args.extend(global_options)
  39. if no_user_config:
  40. args.append('--no-user-cfg')
  41. return args