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.

install_lib.py 3.8KB

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