|
| 1 | +import os |
| 2 | +import glob |
| 3 | +import sys |
| 4 | + |
| 5 | +from typing import Set |
| 6 | + |
| 7 | + |
| 8 | +# See https://packaging.python.org/guides/packaging-namespace-packages/#pkgutil-style-namespace-packages |
| 9 | +PKGUTIL_STYLE_NS_PKG_INIT_CONTENTS = ( |
| 10 | + "# __path__ manipulation added by rules_python_external to support namespace pkgs.\n" |
| 11 | + "__path__ = __import__('pkgutil').extend_path(__path__, __name__)\n" |
| 12 | +) |
| 13 | + |
| 14 | + |
| 15 | +def pkg_resources_style_namespace_packages(extracted_whl_directory) -> Set[str]: |
| 16 | + """ |
| 17 | + Discovers namespace packages implemented using the 'pkg_resources-style namespace packages' method. |
| 18 | +
|
| 19 | + "While this approach is no longer recommended, it is widely present in most existing namespace packages." - PyPA |
| 20 | + See https://packaging.python.org/guides/packaging-namespace-packages/#pkg-resources-style-namespace-packages |
| 21 | + """ |
| 22 | + namespace_pkg_dirs = set() |
| 23 | + |
| 24 | + dist_info_dirs = glob.glob(os.path.join(extracted_whl_directory, "*.dist-info")) |
| 25 | + if not dist_info_dirs: |
| 26 | + raise ValueError(f"No *.dist-info directory found. {extracted_whl_directory} is not a valid Wheel.") |
| 27 | + elif len(dist_info_dirs) > 1: |
| 28 | + raise ValueError(f"Found more than 1 *.dist-info directory. {extracted_whl_directory} is not a valid Wheel.") |
| 29 | + else: |
| 30 | + dist_info = dist_info_dirs[0] |
| 31 | + namespace_packages_record_file = os.path.join(dist_info, "namespace_packages.txt") |
| 32 | + if os.path.exists(namespace_packages_record_file): |
| 33 | + with open(namespace_packages_record_file) as nspkg: |
| 34 | + for line in nspkg.readlines(): |
| 35 | + namespace = line.strip().replace(".", os.sep) |
| 36 | + if namespace: |
| 37 | + namespace_pkg_dirs.add( |
| 38 | + os.path.join(extracted_whl_directory, namespace) |
| 39 | + ) |
| 40 | + return namespace_pkg_dirs |
| 41 | + |
| 42 | + |
| 43 | +def native_namespace_packages_supported() -> bool: |
| 44 | + return (sys.version_info.major, sys.version_info.minor) >= (3, 3) |
| 45 | + |
| 46 | + |
| 47 | +def implicit_namespace_packages(directory, ignored_dirnames=None) -> Set[str]: |
| 48 | + """ |
| 49 | + Discovers namespace packages implemented using the 'native namespace packages' method, |
| 50 | + AKA 'implicit namespace packages', which has been supported since Python 3.3. |
| 51 | +
|
| 52 | + See: https://packaging.python.org/guides/packaging-namespace-packages/#native-namespace-packages |
| 53 | + """ |
| 54 | + namespace_pkg_dirs = set() |
| 55 | + for dirpath, dirnames, filenames in os.walk(directory, topdown=True): |
| 56 | + # We are only interested in dirs with no __init__.py file |
| 57 | + if "__init__.py" in filenames: |
| 58 | + dirnames[:] = [] # Remove dirnames from search |
| 59 | + continue |
| 60 | + |
| 61 | + for ignored_dir in (ignored_dirnames or []): |
| 62 | + if ignored_dir in dirnames: |
| 63 | + dirnames.remove(ignored_dir) |
| 64 | + |
| 65 | + non_empty_directory = (dirnames or filenames) |
| 66 | + if ( |
| 67 | + non_empty_directory and |
| 68 | + # The root of the directory should never be an implicit namespace |
| 69 | + dirpath != directory |
| 70 | + ): |
| 71 | + namespace_pkg_dirs.add(dirpath) |
| 72 | + return namespace_pkg_dirs |
| 73 | + |
| 74 | + |
| 75 | +def add_pkgutil_style_namespace_pkg_init(dir_path: str) -> None: |
| 76 | + """ |
| 77 | + Used to implement the 'pkgutil-style namespace packages' method of |
| 78 | + doing namespace packages. |
| 79 | + See: https://packaging.python.org/guides/packaging-namespace-packages/#pkgutil-style-namespace-packages |
| 80 | + """ |
| 81 | + ns_pkg_init_filepath = os.path.join(dir_path, "__init__.py") |
| 82 | + |
| 83 | + if os.path.isfile(ns_pkg_init_filepath): |
| 84 | + raise ValueError(f"{dir_path} already contains an __init__.py file.") |
| 85 | + with open(ns_pkg_init_filepath, "w") as ns_pkg_init_f: |
| 86 | + ns_pkg_init_f.write(PKGUTIL_STYLE_NS_PKG_INIT_CONTENTS) |
0 commit comments