From 359c1d0aa44342773281ffd0e666c444992f8faf Mon Sep 17 00:00:00 2001 From: Christian Heider Nielsen <509350+cnheider@users.noreply.github.com> Date: Fri, 25 Aug 2023 11:02:38 +0200 Subject: [PATCH] bump version and fix import of pip internals --- warg/__init__.py | 2 +- warg/packages/__init__.py | 2 +- warg/packages/editable.py | 5 +- warg/packages/pip_parsing.py | 96 +++++++++++++++++++----------------- warg/packages/reloading.py | 4 +- 5 files changed, 60 insertions(+), 49 deletions(-) diff --git a/warg/__init__.py b/warg/__init__.py index 20af4f2..d115dfc 100644 --- a/warg/__init__.py +++ b/warg/__init__.py @@ -7,7 +7,7 @@ __project__ = "Warg" __author__ = "Christian Heider Nielsen" -__version__ = "1.2.5" +__version__ = "1.2.6" __doc__ = r""" Created on 27/04/2019 diff --git a/warg/packages/__init__.py b/warg/packages/__init__.py index ad8ab78..a265404 100644 --- a/warg/packages/__init__.py +++ b/warg/packages/__init__.py @@ -13,6 +13,6 @@ with open(Path(__file__).parent / "README.md", "r") as this_init_file: __doc__ += this_init_file.read() -from .pip_parsing import * +# from .pip_parsing import * from .reloading import * from .editable import * diff --git a/warg/packages/editable.py b/warg/packages/editable.py index 3e68d8f..0fbeced 100644 --- a/warg/packages/editable.py +++ b/warg/packages/editable.py @@ -1,7 +1,10 @@ import json # noinspection PyProtectedMember -from importlib.metadata import Distribution, PackageNotFoundError, PathDistribution +try: + from importlib.metadata import Distribution, PackageNotFoundError, PathDistribution +except: + from importlib_metadata import Distribution, PackageNotFoundError, PathDistribution __all__ = [ "dist_is_editable", diff --git a/warg/packages/pip_parsing.py b/warg/packages/pip_parsing.py index 1c8e938..f3b3f0f 100644 --- a/warg/packages/pip_parsing.py +++ b/warg/packages/pip_parsing.py @@ -1,55 +1,61 @@ from pathlib import Path from typing import List, Union -from packaging.requirements import Requirement - -# noinspection PyProtectedMember -from pip._internal.network.session import PipSession - -# noinspection PyProtectedMember -from pip._internal.req import parse_requirements - -# noinspection PyProtectedMember -from pip._internal.req.req_file import ParsedRequirement - -# noinspection PyProtectedMember -from pip._internal.utils.packaging import get_requirement - - from urllib.parse import urlparse +from warg.functions import identity, sink __all__ = ["get_requirements_from_file"] - -def get_reqed(req: ParsedRequirement) -> Requirement: - """ - https://packaging.python.org/en/latest/specifications/direct-url/#example-pip-commands-and-their-effect-on-direct-url-json - - :param req: - :type req: ParsedRequirement - :return: - :rtype: Requirement - """ - - req_ = req.requirement - if req.is_editable: # parse out egg=... fragment from VCS URL - parsed = urlparse(req_) - egg_name = parsed.fragment.partition("egg=")[-1] - without_fragment = parsed._replace(fragment="").geturl() - req_parsed = f"{egg_name} @ {without_fragment}" - else: - req_parsed = req_ - return get_requirement(req_parsed) - - -def get_requirements_from_file( - file_path: Union[str, Path], session: Union[str, PipSession] = "test" -) -> List[Requirement]: - """Turn requirements.txt into a list""" - if isinstance(file_path, Path): - file_path = str(file_path) - return [get_reqed(ir) for ir in parse_requirements(file_path, session=session)] - +try: + # from packaging.requirements import Requirement + + # noinspection PyProtectedMember + from pip._internal.network.session import PipSession + + # noinspection PyProtectedMember + from pip._internal.req import parse_requirements + + # noinspection PyProtectedMember + from pip._internal.req.req_file import ParsedRequirement + + # noinspection PyProtectedMember + from pip._internal.utils.packaging import get_requirement + + def get_reqed( + req, + #: ParsedRequirement + ): # -> Requirement: + """ + https://packaging.python.org/en/latest/specifications/direct-url/#example-pip-commands-and-their-effect-on-direct-url-json + + :param req: + :type req: ParsedRequirement + :return: + :rtype: Requirement + """ + + req_ = req.requirement + if req.is_editable: # parse out egg=... fragment from VCS URL + parsed = urlparse(req_) + egg_name = parsed.fragment.partition("egg=")[-1] + without_fragment = parsed._replace(fragment="").geturl() + req_parsed = f"{egg_name} @ {without_fragment}" + else: + req_parsed = req_ + return get_requirement(req_parsed) + + def get_requirements_from_file( + file_path: Union[str, Path], session: Union[str, PipSession] = "test" + ): # -> List[Requirement] + """Turn requirements.txt into a list""" + if isinstance(file_path, Path): + file_path = str(file_path) + return [get_reqed(ir) for ir in parse_requirements(file_path, session=session)] + +except: + get_requirements_from_file = sink + ... + # print('You version of python is to old!') if __name__ == "__main__": print(get_requirements_from_file(Path(__file__).parent.parent.parent / "requirements.txt")) diff --git a/warg/packages/reloading.py b/warg/packages/reloading.py index 9248737..9447c97 100644 --- a/warg/packages/reloading.py +++ b/warg/packages/reloading.py @@ -26,12 +26,12 @@ import sys from importlib import reload from importlib.util import find_spec + from pathlib import Path from typing import Optional, Any, Union, List, Iterable, Callable from warnings import warn -from warg.packages import get_requirements_from_file from warg.decorators import passes_kws_to """ @@ -94,6 +94,8 @@ def reload_requirements(requirements_path: Path, containment_test: Callable = co :param containment_test: :return: """ + from warg.packages.pip_parsing import get_requirements_from_file + for r in get_requirements_from_file(requirements_path): reload_module(r.name, containment_test=containment_test)