Browse Source

Merge pull request #147 from davvid/exclude-wheels

pynsist: make the "exclude" option work with pypi_wheels
Thomas Kluyver 7 năm trước cách đây
mục cha
commit
c552d525dc
3 tập tin đã thay đổi với 25 bổ sung6 xóa
  1. 1 1
      doc/cfgfile.rst
  2. 2 1
      nsist/__init__.py
  3. 22 4
      nsist/pypi.py

+ 1 - 1
doc/cfgfile.rst

@@ -271,7 +271,7 @@ the line with the key:
      shell.
    * If you want to exclude whole subfolders, do *not* put a path separator 
      (e.g. ``/``) at their end.
-   * The exclude patterns are only applied to packages and to directories
+   * The exclude patterns are applied to packages, pypi wheels, and directories
      specified using the ``files`` option. If your ``exclude`` option directly 
      contradicts your ``files`` or ``packages`` option, the files in question
      will be included (you can not exclude a full package/extra directory

+ 2 - 1
nsist/__init__.py

@@ -349,7 +349,8 @@ if __name__ == '__main__':
         # 2. Wheels from PyPI
         fetch_pypi_wheels(self.pypi_wheel_reqs, build_pkg_dir,
                           py_version=self.py_version, bitness=self.py_bitness,
-                          extra_sources=self.extra_wheel_sources)
+                          extra_sources=self.extra_wheel_sources,
+                          exclude=self.exclude)
 
         # 3. Copy importable modules
         copy_modules(self.packages, build_pkg_dir,

+ 22 - 4
nsist/pypi.py

@@ -1,5 +1,6 @@
 from distutils.version import LooseVersion
 import errno
+import fnmatch
 import hashlib
 import logging
 try:
@@ -199,13 +200,22 @@ def merge_dir_to(src, dst):
                                    .format(p, dst_p))
             shutil.copy2(str(p), str(dst_p))
 
-def extract_wheel(whl_file, target_dir):
+
+def extract_wheel(whl_file, target_dir, exclude=None):
     """Extract importable modules from a wheel to the target directory
     """
     # Extract to temporary directory
     td = Path(mkdtemp())
     with zipfile.ZipFile(str(whl_file), mode='r') as zf:
-        zf.extractall(str(td))
+        if exclude:
+            basename = Path(Path(target_dir).name)
+            for zpath in zf.namelist():
+                path = basename / zpath
+                if is_excluded(path, exclude):
+                    continue  # Skip excluded paths
+                zf.extract(zpath, path=str(td))
+        else:
+            zf.extractall(str(td))
 
     # Move extra lib files out of the .data subdirectory
     for p in td.iterdir():
@@ -242,8 +252,16 @@ def extract_wheel(whl_file, target_dir):
 
 
 def fetch_pypi_wheels(requirements, target_dir, py_version, bitness,
-                      extra_sources=None):
+                      extra_sources=None, exclude=None):
     for req in requirements:
         wl = WheelLocator(req, py_version, bitness, extra_sources)
         whl_file = wl.fetch()
-        extract_wheel(whl_file, target_dir)
+        extract_wheel(whl_file, target_dir, exclude=exclude)
+
+
+def is_excluded(path, exclude):
+    """Return True if path matches an exclude pattern"""
+    for pattern in (exclude or ()):
+        if fnmatch.fnmatch(path, pattern):
+            return True
+    return False