install_lib.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. from __future__ import annotations
  2. import os
  3. import sys
  4. from itertools import product, starmap
  5. from .._path import StrPath
  6. from ..dist import Distribution
  7. import distutils.command.install_lib as orig
  8. class install_lib(orig.install_lib):
  9. """Don't add compiled flags to filenames of non-Python files"""
  10. distribution: Distribution # override distutils.dist.Distribution with setuptools.dist.Distribution
  11. def run(self) -> None:
  12. self.build()
  13. outfiles = self.install()
  14. if outfiles is not None:
  15. # always compile, in case we have any extension stubs to deal with
  16. self.byte_compile(outfiles)
  17. def get_exclusions(self):
  18. """
  19. Return a collections.Sized collections.Container of paths to be
  20. excluded for single_version_externally_managed installations.
  21. """
  22. all_packages = (
  23. pkg
  24. for ns_pkg in self._get_SVEM_NSPs()
  25. for pkg in self._all_packages(ns_pkg)
  26. )
  27. excl_specs = product(all_packages, self._gen_exclusion_paths())
  28. return set(starmap(self._exclude_pkg_path, excl_specs))
  29. def _exclude_pkg_path(self, pkg, exclusion_path):
  30. """
  31. Given a package name and exclusion path within that package,
  32. compute the full exclusion path.
  33. """
  34. parts = pkg.split('.') + [exclusion_path]
  35. return os.path.join(self.install_dir, *parts)
  36. @staticmethod
  37. def _all_packages(pkg_name):
  38. """
  39. >>> list(install_lib._all_packages('foo.bar.baz'))
  40. ['foo.bar.baz', 'foo.bar', 'foo']
  41. """
  42. while pkg_name:
  43. yield pkg_name
  44. pkg_name, _sep, _child = pkg_name.rpartition('.')
  45. def _get_SVEM_NSPs(self):
  46. """
  47. Get namespace packages (list) but only for
  48. single_version_externally_managed installations and empty otherwise.
  49. """
  50. # TODO: is it necessary to short-circuit here? i.e. what's the cost
  51. # if get_finalized_command is called even when namespace_packages is
  52. # False?
  53. if not self.distribution.namespace_packages:
  54. return []
  55. install_cmd = self.get_finalized_command('install')
  56. svem = install_cmd.single_version_externally_managed
  57. return self.distribution.namespace_packages if svem else []
  58. @staticmethod
  59. def _gen_exclusion_paths():
  60. """
  61. Generate file paths to be excluded for namespace packages (bytecode
  62. cache files).
  63. """
  64. # always exclude the package module itself
  65. yield '__init__.py'
  66. yield '__init__.pyc'
  67. yield '__init__.pyo'
  68. if not hasattr(sys, 'implementation'):
  69. return
  70. base = os.path.join('__pycache__', '__init__.' + sys.implementation.cache_tag)
  71. yield base + '.pyc'
  72. yield base + '.pyo'
  73. yield base + '.opt-1.pyc'
  74. yield base + '.opt-2.pyc'
  75. def copy_tree(
  76. self,
  77. infile: StrPath,
  78. outfile: str,
  79. # override: Using actual booleans
  80. preserve_mode: bool = True, # type: ignore[override]
  81. preserve_times: bool = True, # type: ignore[override]
  82. preserve_symlinks: bool = False, # type: ignore[override]
  83. level: object = 1,
  84. ) -> list[str]:
  85. assert preserve_mode
  86. assert preserve_times
  87. assert not preserve_symlinks
  88. exclude = self.get_exclusions()
  89. if not exclude:
  90. return orig.install_lib.copy_tree(self, infile, outfile)
  91. # Exclude namespace package __init__.py* files from the output
  92. from setuptools.archive_util import unpack_directory
  93. from distutils import log
  94. outfiles: list[str] = []
  95. def pf(src: str, dst: str):
  96. if dst in exclude:
  97. log.warn("Skipping installation of %s (namespace package)", dst)
  98. return False
  99. log.info("copying %s -> %s", src, os.path.dirname(dst))
  100. outfiles.append(dst)
  101. return dst
  102. unpack_directory(infile, outfile, pf)
  103. return outfiles
  104. def get_outputs(self):
  105. outputs = orig.install_lib.get_outputs(self)
  106. exclude = self.get_exclusions()
  107. if exclude:
  108. return [f for f in outputs if f not in exclude]
  109. return outputs