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.
 
 
 
 

119 lines
3.7 KiB

  1. import os
  2. import imp
  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(imp, 'get_tag'):
  65. return
  66. base = os.path.join('__pycache__', '__init__.' + imp.get_tag())
  67. yield base + '.pyc'
  68. yield base + '.pyo'
  69. def copy_tree(
  70. self, infile, outfile,
  71. preserve_mode=1, preserve_times=1, preserve_symlinks=0, level=1
  72. ):
  73. assert preserve_mode and preserve_times and not preserve_symlinks
  74. exclude = self.get_exclusions()
  75. if not exclude:
  76. return orig.install_lib.copy_tree(self, infile, outfile)
  77. # Exclude namespace package __init__.py* files from the output
  78. from setuptools.archive_util import unpack_directory
  79. from distutils import log
  80. outfiles = []
  81. def pf(src, dst):
  82. if dst in exclude:
  83. log.warn("Skipping installation of %s (namespace package)",
  84. dst)
  85. return False
  86. log.info("copying %s -> %s", src, os.path.dirname(dst))
  87. outfiles.append(dst)
  88. return dst
  89. unpack_directory(infile, outfile, pf)
  90. return outfiles
  91. def get_outputs(self):
  92. outputs = orig.install_lib.get_outputs(self)
  93. exclude = self.get_exclusions()
  94. if exclude:
  95. return [f for f in outputs if f not in exclude]
  96. return outputs