install_lib.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import distutils.command.install_lib as orig
  2. import os
  3. class install_lib(orig.install_lib):
  4. """Don't add compiled flags to filenames of non-Python files"""
  5. def run(self):
  6. self.build()
  7. outfiles = self.install()
  8. if outfiles is not None:
  9. # always compile, in case we have any extension stubs to deal with
  10. self.byte_compile(outfiles)
  11. def get_exclusions(self):
  12. exclude = {}
  13. nsp = self.distribution.namespace_packages
  14. svem = (nsp and self.get_finalized_command('install')
  15. .single_version_externally_managed)
  16. if svem:
  17. for pkg in nsp:
  18. parts = pkg.split('.')
  19. while parts:
  20. pkgdir = os.path.join(self.install_dir, *parts)
  21. for f in '__init__.py', '__init__.pyc', '__init__.pyo':
  22. exclude[os.path.join(pkgdir,f)] = 1
  23. parts.pop()
  24. return exclude
  25. def copy_tree(
  26. self, infile, outfile,
  27. preserve_mode=1, preserve_times=1, preserve_symlinks=0, level=1
  28. ):
  29. assert preserve_mode and preserve_times and not preserve_symlinks
  30. exclude = self.get_exclusions()
  31. if not exclude:
  32. return orig.install_lib.copy_tree(self, infile, outfile)
  33. # Exclude namespace package __init__.py* files from the output
  34. from setuptools.archive_util import unpack_directory
  35. from distutils import log
  36. outfiles = []
  37. def pf(src, dst):
  38. if dst in exclude:
  39. log.warn("Skipping installation of %s (namespace package)",dst)
  40. return False
  41. log.info("copying %s -> %s", src, os.path.dirname(dst))
  42. outfiles.append(dst)
  43. return dst
  44. unpack_directory(infile, outfile, pf)
  45. return outfiles
  46. def get_outputs(self):
  47. outputs = orig.install_lib.get_outputs(self)
  48. exclude = self.get_exclusions()
  49. if exclude:
  50. return [f for f in outputs if f not in exclude]
  51. return outputs