|
4 | 4 | import subprocess |
5 | 5 | import sys |
6 | 6 |
|
| 7 | + |
| 8 | +from . import namespace_pkgs |
7 | 9 | from .wheel import Wheel |
8 | 10 |
|
9 | 11 | BUILD_TEMPLATE = """\ |
@@ -38,25 +40,31 @@ def sanitise_name(name): |
38 | 40 |
|
39 | 41 |
|
40 | 42 | def _setup_namespace_pkg_compatibility(extracted_whl_directory): |
41 | | - # need dist-info directory for pkg_resources to be able to find the packages |
42 | | - dist_info = glob.glob(os.path.join(extracted_whl_directory, "*.dist-info"))[0] |
43 | | - # fix namespace packages by adding proper __init__.py files |
44 | | - namespace_packages = os.path.join(dist_info, "namespace_packages.txt") |
45 | | - if os.path.exists(namespace_packages): |
46 | | - with open(namespace_packages) as nspkg: |
47 | | - for line in nspkg.readlines(): |
48 | | - namespace = line.strip().replace(".", os.sep) |
49 | | - if namespace: |
50 | | - nspkg_init = os.path.join(extracted_whl_directory, namespace, "__init__.py") |
51 | | - with open(nspkg_init, "w") as nspkg: |
52 | | - nspkg.writelines([ |
53 | | - "# __path__ manipulation added by rules_python_external to support namespace pkgs.\n" |
54 | | - "__path__ = __import__('pkgutil').extend_path(__path__, __name__)\n" |
55 | | - ]) |
56 | | - |
57 | | - |
58 | | - |
59 | | - # return pkginfo.Wheel(dist_info) |
| 43 | + """ |
| 44 | + Namespace packages can be created in one of three ways. The are detailed here: |
| 45 | + https://packaging.python.org/guides/packaging-namespace-packages/#creating-a-namespace-package |
| 46 | +
|
| 47 | + 'pkgutil-style namespace packages' (2) works in Bazel, but 'native namespace packages' (1) and |
| 48 | + 'pkg_resources-style namespace packages' (3) do not. |
| 49 | +
|
| 50 | + We ensure compatibility with Bazel of methods 1 and 3 by converting them into method 2. |
| 51 | + """ |
| 52 | + namespace_pkg_dirs = namespace_pkgs.pkg_resources_style_namespace_packages( |
| 53 | + extracted_whl_directory |
| 54 | + ) |
| 55 | + if ( |
| 56 | + not namespace_pkg_dirs and |
| 57 | + (sys.version_info.major, sys.version_info.minor) >= (3, 3) |
| 58 | + ): |
| 59 | + namespace_pkg_dirs = namespace_pkgs.implicit_namespace_packages( |
| 60 | + extracted_whl_directory, |
| 61 | + ignored_dirnames=[ |
| 62 | + f"{extracted_whl_directory}/bin", |
| 63 | + ] |
| 64 | + ) |
| 65 | + |
| 66 | + for ns_pkg_dir in namespace_pkg_dirs: |
| 67 | + namespace_pkgs.add_pkgutil_style_namespace_pkg_init(ns_pkg_dir) |
60 | 68 |
|
61 | 69 |
|
62 | 70 | def extract_wheel(whl, directory, extras): |
|
0 commit comments