copymodules.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import os
  2. import shutil
  3. import sys
  4. import zipfile, zipimport
  5. PY2 = sys.version_info[0] == 2
  6. def copy_zipmodule(loader, modname, target):
  7. file = loader.get_filename(modname)
  8. prefix = loader.archive + '/' + loader.prefix
  9. assert file.startswith(prefix)
  10. path_in_zip = file[len(prefix):]
  11. zf = zipfile.ZipFile(loader.archive)
  12. if loader.is_package(modname):
  13. pkgdir, basename = path_in_zip.rsplit('/', 1)
  14. assert basename.startswith('__init__')
  15. pkgfiles = [f for f in zf.namelist() if f.startswith(pkgdir)]
  16. zf.extractall(target, pkgfiles)
  17. else:
  18. zf.extract(path_in_zip, target)
  19. if not PY2:
  20. import importlib
  21. import importlib.abc
  22. import importlib.machinery
  23. class ModuleCopier:
  24. """Finds and copies importable Python modules and packages.
  25. This uses importlib to locate modules.
  26. """
  27. def __init__(self, path=None):
  28. self.path = path if (path is not None) else ([''] + sys.path)
  29. def copy(self, modname, target):
  30. """Copy the importable module 'modname' to the directory 'target'.
  31. modname should be a top-level import, i.e. without any dots.
  32. Packages are always copied whole.
  33. This can currently copy regular filesystem files and directories,
  34. and extract modules and packages from appropriately structured zip
  35. files.
  36. """
  37. loader = importlib.find_loader(modname, self.path)
  38. if loader is None:
  39. raise ImportError('Could not find %s' % modname)
  40. pkg = loader.is_package(modname)
  41. if isinstance(loader, importlib.machinery.ExtensionFileLoader):
  42. shutil.copy2(loader.path, target)
  43. elif isinstance(loader, importlib.abc.FileLoader):
  44. file = loader.get_filename(modname)
  45. if pkg:
  46. pkgdir, basename = os.path.split(file)
  47. assert basename.startswith('__init__')
  48. dest = os.path.join(target, modname)
  49. shutil.copytree(pkgdir, dest,
  50. ignore=shutil.ignore_patterns('*.pyc'))
  51. else:
  52. shutil.copy2(file, target)
  53. elif isinstance(loader, zipimport.zipimporter):
  54. copy_zipmodule(loader, modname, target)
  55. else:
  56. import imp
  57. class ModuleCopier:
  58. """Finds and copies importable Python modules and packages.
  59. This uses importlib to locate modules.
  60. """
  61. def __init__(self, path=None):
  62. self.path = path if (path is not None) else ([''] + sys.path)
  63. def copy(self, modname, target):
  64. """Copy the importable module 'modname' to the directory 'target'.
  65. modname should be a top-level import, i.e. without any dots.
  66. Packages are always copied whole.
  67. This can currently copy regular filesystem files and directories,
  68. and extract modules and packages from appropriately structured zip
  69. files.
  70. """
  71. modinfo = imp.find_module(modname, self.path)
  72. if modinfo[2][2] in (imp.PY_SOURCE, imp.C_EXTENSION):
  73. shutil.copy2(modinfo[1], target)
  74. elif modinfo[2][2] == imp.PKG_DIRECTORY:
  75. dest = os.path.join(target, modname)
  76. shutil.copytree(modinfo[1], dest,
  77. ignore=shutil.ignore_patterns('*.pyc'))
  78. elif any(p.endswith('.zip') for p in self.path):
  79. for fpath in (p for p in self.path if p.endswith('.zip')):
  80. loader = zipimport.zipimporter(fpath)
  81. if loader.find_module(modname) is None:
  82. continue
  83. copy_zipmodule(loader, modname, target)
  84. def copy_modules(modnames, target, path=None):
  85. """Copy the specified importable modules to the target directory.
  86. By default, it finds modules in sys.path - this can be overridden by passing
  87. the path parameter.
  88. """
  89. mc = ModuleCopier(path)
  90. files_in_target_noext = [os.path.splitext(f)[0] for f in os.listdir(target)]
  91. for modname in modnames:
  92. if modname in files_in_target_noext:
  93. # Already there, no need to copy it.
  94. continue
  95. mc.copy(modname, target)
  96. if not modnames:
  97. # NSIS abhors an empty folder, so give it a file to find.
  98. with open(os.path.join(target, 'placeholder'), 'w') as f:
  99. f.write('This file only exists so NSIS finds something in this directory.')