install_lib.py 3.8 KB

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