Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

build_meta.py 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. """A PEP 517 interface to setuptools
  2. Previously, when a user or a command line tool (let's call it a "frontend")
  3. needed to make a request of setuptools to take a certain action, for
  4. example, generating a list of installation requirements, the frontend would
  5. would call "setup.py egg_info" or "setup.py bdist_wheel" on the command line.
  6. PEP 517 defines a different method of interfacing with setuptools. Rather
  7. than calling "setup.py" directly, the frontend should:
  8. 1. Set the current directory to the directory with a setup.py file
  9. 2. Import this module into a safe python interpreter (one in which
  10. setuptools can potentially set global variables or crash hard).
  11. 3. Call one of the functions defined in PEP 517.
  12. What each function does is defined in PEP 517. However, here is a "casual"
  13. definition of the functions (this definition should not be relied on for
  14. bug reports or API stability):
  15. - `build_wheel`: build a wheel in the folder and return the basename
  16. - `get_requires_for_build_wheel`: get the `setup_requires` to build
  17. - `prepare_metadata_for_build_wheel`: get the `install_requires`
  18. - `build_sdist`: build an sdist in the folder and return the basename
  19. - `get_requires_for_build_sdist`: get the `setup_requires` to build
  20. Again, this is not a formal definition! Just a "taste" of the module.
  21. """
  22. import os
  23. import sys
  24. import tokenize
  25. import shutil
  26. import contextlib
  27. import setuptools
  28. import distutils
  29. class SetupRequirementsError(BaseException):
  30. def __init__(self, specifiers):
  31. self.specifiers = specifiers
  32. class Distribution(setuptools.dist.Distribution):
  33. def fetch_build_eggs(self, specifiers):
  34. raise SetupRequirementsError(specifiers)
  35. @classmethod
  36. @contextlib.contextmanager
  37. def patch(cls):
  38. """
  39. Replace
  40. distutils.dist.Distribution with this class
  41. for the duration of this context.
  42. """
  43. orig = distutils.core.Distribution
  44. distutils.core.Distribution = cls
  45. try:
  46. yield
  47. finally:
  48. distutils.core.Distribution = orig
  49. def _to_str(s):
  50. """
  51. Convert a filename to a string (on Python 2, explicitly
  52. a byte string, not Unicode) as distutils checks for the
  53. exact type str.
  54. """
  55. if sys.version_info[0] == 2 and not isinstance(s, str):
  56. # Assume it's Unicode, as that's what the PEP says
  57. # should be provided.
  58. return s.encode(sys.getfilesystemencoding())
  59. return s
  60. def _run_setup(setup_script='setup.py'):
  61. # Note that we can reuse our build directory between calls
  62. # Correctness comes first, then optimization later
  63. __file__ = setup_script
  64. __name__ = '__main__'
  65. f = getattr(tokenize, 'open', open)(__file__)
  66. code = f.read().replace('\\r\\n', '\\n')
  67. f.close()
  68. exec(compile(code, __file__, 'exec'), locals())
  69. def _fix_config(config_settings):
  70. config_settings = config_settings or {}
  71. config_settings.setdefault('--global-option', [])
  72. return config_settings
  73. def _get_build_requires(config_settings):
  74. config_settings = _fix_config(config_settings)
  75. requirements = ['setuptools', 'wheel']
  76. sys.argv = sys.argv[:1] + ['egg_info'] + \
  77. config_settings["--global-option"]
  78. try:
  79. with Distribution.patch():
  80. _run_setup()
  81. except SetupRequirementsError as e:
  82. requirements += e.specifiers
  83. return requirements
  84. def _get_immediate_subdirectories(a_dir):
  85. return [name for name in os.listdir(a_dir)
  86. if os.path.isdir(os.path.join(a_dir, name))]
  87. def get_requires_for_build_wheel(config_settings=None):
  88. config_settings = _fix_config(config_settings)
  89. return _get_build_requires(config_settings)
  90. def get_requires_for_build_sdist(config_settings=None):
  91. config_settings = _fix_config(config_settings)
  92. return _get_build_requires(config_settings)
  93. def prepare_metadata_for_build_wheel(metadata_directory, config_settings=None):
  94. sys.argv = sys.argv[:1] + ['dist_info', '--egg-base', _to_str(metadata_directory)]
  95. _run_setup()
  96. dist_info_directory = metadata_directory
  97. while True:
  98. dist_infos = [f for f in os.listdir(dist_info_directory)
  99. if f.endswith('.dist-info')]
  100. if len(dist_infos) == 0 and \
  101. len(_get_immediate_subdirectories(dist_info_directory)) == 1:
  102. dist_info_directory = os.path.join(
  103. dist_info_directory, os.listdir(dist_info_directory)[0])
  104. continue
  105. assert len(dist_infos) == 1
  106. break
  107. # PEP 517 requires that the .dist-info directory be placed in the
  108. # metadata_directory. To comply, we MUST copy the directory to the root
  109. if dist_info_directory != metadata_directory:
  110. shutil.move(
  111. os.path.join(dist_info_directory, dist_infos[0]),
  112. metadata_directory)
  113. shutil.rmtree(dist_info_directory, ignore_errors=True)
  114. return dist_infos[0]
  115. def build_wheel(wheel_directory, config_settings=None,
  116. metadata_directory=None):
  117. config_settings = _fix_config(config_settings)
  118. wheel_directory = os.path.abspath(wheel_directory)
  119. sys.argv = sys.argv[:1] + ['bdist_wheel'] + \
  120. config_settings["--global-option"]
  121. _run_setup()
  122. if wheel_directory != 'dist':
  123. shutil.rmtree(wheel_directory)
  124. shutil.copytree('dist', wheel_directory)
  125. wheels = [f for f in os.listdir(wheel_directory)
  126. if f.endswith('.whl')]
  127. assert len(wheels) == 1
  128. return wheels[0]
  129. def build_sdist(sdist_directory, config_settings=None):
  130. config_settings = _fix_config(config_settings)
  131. sdist_directory = os.path.abspath(sdist_directory)
  132. sys.argv = sys.argv[:1] + ['sdist'] + \
  133. config_settings["--global-option"]
  134. _run_setup()
  135. if sdist_directory != 'dist':
  136. shutil.rmtree(sdist_directory)
  137. shutil.copytree('dist', sdist_directory)
  138. sdists = [f for f in os.listdir(sdist_directory)
  139. if f.endswith('.tar.gz')]
  140. assert len(sdists) == 1
  141. return sdists[0]