copymodules.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import os
  2. import shutil
  3. import sys
  4. import zipfile, zipimport
  5. PY2 = sys.version_info[0] == 2
  6. running_python = '.'.join(str(x) for x in sys.version_info[:2])
  7. class ExtensionModuleMismatch(ImportError):
  8. pass
  9. extensionmod_errmsg = """Found an extension module that will not be usable on %s:
  10. %s
  11. Put Windows packages in pynsist_pkgs/ to avoid this."""
  12. def check_ext_mod(path, target_python):
  13. if path.endswith('.so'):
  14. raise ExtensionModuleMismatch(extensionmod_errmsg % ('Windows', path))
  15. elif path.endswith('.pyd') and not target_python.startswith(running_python):
  16. # TODO: From Python 3.2, extension modules can restrict themselves
  17. # to a stable ABI. Can we detect this?
  18. raise ExtensionModuleMismatch(extensionmod_errmsg % ('Python '+target_python, path))
  19. def check_package_for_ext_mods(path, target_python):
  20. for dirpath, dirnames, filenames in os.walk(path):
  21. for filename in filenames:
  22. check_ext_mod(os.path.join(path, dirpath, filename), target_python)
  23. def copy_zipmodule(loader, modname, target):
  24. file = loader.get_filename(modname)
  25. prefix = loader.archive + '/' + loader.prefix
  26. assert file.startswith(prefix)
  27. path_in_zip = file[len(prefix):]
  28. zf = zipfile.ZipFile(loader.archive)
  29. if loader.is_package(modname):
  30. pkgdir, basename = path_in_zip.rsplit('/', 1)
  31. assert basename.startswith('__init__')
  32. pkgfiles = [f for f in zf.namelist() if f.startswith(pkgdir)]
  33. zf.extractall(target, pkgfiles)
  34. else:
  35. zf.extract(path_in_zip, target)
  36. if not PY2:
  37. import importlib
  38. import importlib.abc
  39. import importlib.machinery
  40. class ModuleCopier:
  41. """Finds and copies importable Python modules and packages.
  42. This is the Python >3.3 version and uses the `importlib` package to
  43. locate modules.
  44. """
  45. def __init__(self, py_version, path=None):
  46. self.py_version = py_version
  47. self.path = path if (path is not None) else ([''] + sys.path)
  48. def copy(self, modname, target):
  49. """Copy the importable module 'modname' to the directory 'target'.
  50. modname should be a top-level import, i.e. without any dots.
  51. Packages are always copied whole.
  52. This can currently copy regular filesystem files and directories,
  53. and extract modules and packages from appropriately structured zip
  54. files.
  55. """
  56. loader = importlib.find_loader(modname, self.path)
  57. if loader is None:
  58. raise ImportError('Could not find %s' % modname)
  59. pkg = loader.is_package(modname)
  60. if isinstance(loader, importlib.machinery.ExtensionFileLoader):
  61. check_ext_mod(loader.path, self.py_version)
  62. shutil.copy2(loader.path, target)
  63. elif isinstance(loader, importlib.abc.FileLoader):
  64. file = loader.get_filename(modname)
  65. if pkg:
  66. pkgdir, basename = os.path.split(file)
  67. assert basename.startswith('__init__')
  68. check_package_for_ext_mods(pkgdir, self.py_version)
  69. dest = os.path.join(target, modname)
  70. shutil.copytree(pkgdir, dest,
  71. ignore=shutil.ignore_patterns('*.pyc'))
  72. else:
  73. shutil.copy2(file, target)
  74. elif isinstance(loader, zipimport.zipimporter):
  75. copy_zipmodule(loader, modname, target)
  76. else:
  77. import imp
  78. class ModuleCopier:
  79. """Finds and copies importable Python modules and packages.
  80. This is the Python 2.7 version and uses the `imp` package to locate
  81. modules.
  82. """
  83. def __init__(self, py_version, path=None):
  84. self.py_version = py_version
  85. self.path = path if (path is not None) else ([''] + sys.path)
  86. self.zip_paths = [p for p in self.path if zipfile.is_zipfile(p)]
  87. def copy(self, modname, target):
  88. """Copy the importable module 'modname' to the directory 'target'.
  89. modname should be a top-level import, i.e. without any dots.
  90. Packages are always copied whole.
  91. This can currently copy regular filesystem files and directories,
  92. and extract modules and packages from appropriately structured zip
  93. files.
  94. """
  95. info = imp.find_module(modname, self.path)
  96. path = info[1]
  97. modtype = info[2][2]
  98. if modtype == imp.C_EXTENSION:
  99. check_ext_mod(path, self.py_version)
  100. if modtype in (imp.PY_SOURCE, imp.C_EXTENSION):
  101. shutil.copy2(path, target)
  102. elif modtype == imp.PKG_DIRECTORY:
  103. check_package_for_ext_mods(path, self.py_version)
  104. dest = os.path.join(target, modname)
  105. shutil.copytree(path, dest,
  106. ignore=shutil.ignore_patterns('*.pyc'))
  107. else:
  108. # Search all ZIP files in self.path for the module name
  109. # NOTE: `imp.find_module(...)` will *not* find modules in ZIP
  110. # files, so we have to check each file for ourselves
  111. for zpath in self.zip_path:
  112. loader = zipimport.zipimporter(zpath)
  113. if loader.find_module(modname) is None:
  114. continue
  115. copy_zipmodule(loader, modname, target)
  116. def copy_modules(modnames, target, py_version, path=None):
  117. """Copy the specified importable modules to the target directory.
  118. By default, it finds modules in sys.path - this can be overridden by passing
  119. the path parameter.
  120. """
  121. mc = ModuleCopier(py_version, path)
  122. files_in_target_noext = [os.path.splitext(f)[0] for f in os.listdir(target)]
  123. for modname in modnames:
  124. if modname in files_in_target_noext:
  125. # Already there, no need to copy it.
  126. continue
  127. mc.copy(modname, target)
  128. if not modnames:
  129. # NSIS abhors an empty folder, so give it a file to find.
  130. with open(os.path.join(target, 'placeholder'), 'w') as f:
  131. f.write('This file only exists so NSIS finds something in this directory.')