-
-
Notifications
You must be signed in to change notification settings - Fork 43
Vendor pypa/packaging into micropip
#178
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 11 commits
bc4f3fb
e2df367
6a05638
340ffdb
cfc48ad
f2842e5
fa8c092
38c0888
e5d9f7d
1f59b01
8a4dfe2
b30ba38
8c8f0f8
e6932b2
f9b5172
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| # d8e3b31b734926ebbcaff654279f6855a73e052f, for 24.2 release | ||
| # https://github.com/pypa/packaging/releases/tag/24.2 | ||
| [submodule "micropip/_vendored/packaging"] | ||
| path = micropip/_vendored/packaging | ||
| url = https://github.com/pypa/packaging/ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| # micropip/_vendored/__init__.py | ||
|
|
||
| # This is a proxy file that redirects imports from micropip._vendored.packaging | ||
| # to the actual packaging API present in the packaging/src/packaging directory | ||
| # next to this file. | ||
|
|
||
| import importlib.util | ||
| import sys | ||
| import types | ||
| from pathlib import Path | ||
|
|
||
| PACKAGING_PATH = Path(__file__).parent / "packaging" / "src" / "packaging" | ||
|
|
||
|
|
||
| def _create_module(name, package_path=None): | ||
| """ | ||
| Creates a module object for the given name and makes it available both under | ||
| micropip._vendored.packaging and packaging namespaces. | ||
|
|
||
| Args: | ||
| name: The name of the module (without the full package path) | ||
| package_path: Optional path to the module file, if different from default location | ||
| """ | ||
| vendored_name = f"micropip._vendored.packaging.{name}" | ||
| direct_name = f"packaging.{name}" | ||
|
|
||
| # If the module is already in sys.modules, return it, and we | ||
| # add it to sys.modules under both names before executing it. | ||
| if vendored_name in sys.modules: | ||
| return sys.modules[vendored_name] | ||
|
|
||
| module = types.ModuleType(vendored_name) | ||
| module.__package__ = "micropip._vendored.packaging" | ||
|
|
||
| sys.modules[vendored_name] = module | ||
| sys.modules[direct_name] = module | ||
|
|
||
| if package_path is None: | ||
| module_path = PACKAGING_PATH / f"{name}.py" | ||
| else: | ||
| module_path = package_path | ||
|
|
||
| if module_path.exists(): | ||
| spec = importlib.util.spec_from_file_location( | ||
| vendored_name, module_path, submodule_search_locations=[str(PACKAGING_PATH)] | ||
| ) | ||
| module.__spec__ = spec | ||
| module.__file__ = str(module_path) | ||
| loader = spec.loader | ||
| loader.exec_module(module) | ||
|
|
||
| return module | ||
|
|
||
|
|
||
| #################################################### | ||
|
|
||
| packaging_vendored = types.ModuleType("micropip._vendored.packaging") | ||
| packaging_direct = types.ModuleType( | ||
| "packaging" | ||
| ) # this is where we redirect the imports. | ||
|
|
||
| packaging_vendored.__path__ = [str(PACKAGING_PATH)] | ||
| packaging_vendored.__package__ = "micropip._vendored" | ||
| packaging_direct.__path__ = [str(PACKAGING_PATH)] | ||
| packaging_direct.__package__ = "" | ||
|
|
||
| sys.modules["micropip._vendored.packaging"] = packaging_vendored | ||
| sys.modules["packaging"] = packaging_direct | ||
|
|
||
| #################################################### | ||
|
|
||
| # 1. First, handle any packages: these are directories with __init__.py. | ||
| # 2. Then, we load all the internal modules | ||
| # 3. Finally, we'll load all the regular modules (whatever is in the | ||
| # public API) | ||
| # | ||
| # While rudimentary, this order is important because the internal modules | ||
| # may depend on subpackages, and regular modules may depend on internal ones. | ||
| # | ||
| # For example, the metadata.py module imports from licenses, requirements, | ||
| # specifiers, and utils. | ||
| # | ||
| # Similarly, tags.py needs _manylinux and _musllinux to be available. | ||
|
|
||
|
|
||
| for path in PACKAGING_PATH.glob("*/__init__.py"): | ||
| package_name = path.parent.name | ||
| module = _create_module(f"{package_name}/__init__", path) | ||
| setattr(packaging_vendored, package_name, module) | ||
| setattr(packaging_direct, package_name, module) | ||
|
|
||
| internal_modules = [path.stem for path in PACKAGING_PATH.glob("_*.py")] | ||
| for name in internal_modules: | ||
| module = _create_module(name) | ||
| setattr(packaging_vendored, name, module) | ||
| setattr(packaging_direct, name, module) | ||
|
|
||
|
|
||
| for path in PACKAGING_PATH.glob("*.py"): | ||
| if path.stem == "__init__" or path.stem.startswith("_"): | ||
| continue | ||
|
|
||
| module = _create_module(path.stem) | ||
| setattr(packaging_vendored, path.stem, module) | ||
| setattr(packaging_direct, path.stem, module) | ||
|
|
||
|
|
||
| globals()["packaging"] = packaging_vendored |
|
agriyakhetarpal marked this conversation as resolved.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,13 +7,12 @@ | |
| import warnings | ||
| from typing import Any, Dict, List, Optional, Union, Literal, TypeAlias, TypedDict | ||
|
|
||
| import packaging.utils | ||
| import micropip._vendored.packaging.utils as packaging_utils | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (No need to address in this PR) both
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks! Yes, I noticed that. I'll open a follow-up PR after this to merge them. I have no preference on the name of the folder, but it should be
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, I think |
||
|
|
||
|
|
||
| ACCEPT_JSON_V1 = "application/vnd.pypi.simple.v1+json" | ||
|
|
||
|
|
||
|
|
||
| class UnsupportedAPIVersion(Exception): | ||
| """The major version of an API response is not supported.""" | ||
|
|
||
|
|
@@ -92,15 +91,15 @@ class ProjectDetails_1_0(TypedDict): | |
| """A :class:`~typing.TypedDict` for a project details response (:pep:`691`).""" | ||
|
|
||
| meta: _Meta_1_0 | ||
| name: packaging.utils.NormalizedName | ||
| name: packaging_utils.NormalizedName | ||
| files: list[ProjectFileDetails_1_0] | ||
|
|
||
|
|
||
| class ProjectDetails_1_1(TypedDict): | ||
| """A :class:`~typing.TypedDict` for a project details response (:pep:`700`).""" | ||
|
|
||
| meta: _Meta_1_1 | ||
| name: packaging.utils.NormalizedName | ||
| name: packaging_utils.NormalizedName | ||
| files: list[ProjectFileDetails_1_1] | ||
| # PEP 700 | ||
| versions: List[str] | ||
|
|
@@ -235,6 +234,6 @@ def from_project_details_html(html: str, name: str) -> ProjectDetails_1_0: | |
| files.append(details) | ||
| return { | ||
| "meta": {"api-version": "1.0"}, | ||
| "name": packaging.utils.canonicalize_name(name), | ||
| "name": packaging_utils.canonicalize_name(name), | ||
| "files": files, | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,7 +13,8 @@ classifiers = [ | |
| "Operating System :: OS Independent", | ||
| ] | ||
| dynamic = ["version"] | ||
| dependencies = ["packaging>=23.0"] | ||
| dependencies = [] | ||
|
|
||
| [project.optional-dependencies] | ||
| test = [ | ||
| "pytest-httpserver", | ||
|
|
@@ -64,7 +65,12 @@ known-first-party = [ | |
| ] | ||
|
|
||
| [tool.mypy] | ||
| exclude = ["micropip/_vendored/"] | ||
| python_version = "3.12" | ||
| show_error_codes = true | ||
| warn_unreachable = true | ||
| ignore_missing_imports = true | ||
|
|
||
| [[tool.mypy.overrides]] | ||
| module = "micropip._vendored.*" | ||
| warn_unreachable = false | ||
|
Comment on lines
+74
to
+76
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've had to keep this as there is some unreachable code in |
||
Uh oh!
There was an error while loading. Please reload this page.