From 32332d6f5bbbdfc38d014904a0e7d0a4fc67f3f8 Mon Sep 17 00:00:00 2001 From: Shivansh-007 Date: Sat, 29 Jan 2022 07:26:27 +0530 Subject: [PATCH 1/4] Move mindeps generation logic to separate function --- resolver/mindeps/__main__.py | 56 +++++++++++++++++++++++------------- 1 file changed, 36 insertions(+), 20 deletions(-) diff --git a/resolver/mindeps/__main__.py b/resolver/mindeps/__main__.py index 98d2c37..dce1973 100644 --- a/resolver/mindeps/__main__.py +++ b/resolver/mindeps/__main__.py @@ -6,7 +6,7 @@ import pathlib import tempfile -from typing import Iterable, Sequence, Set +from typing import Iterable, Sequence, Set, List, Optional, Dict import packaging.markers import packaging.requirements @@ -91,16 +91,12 @@ def main_parser() -> argparse.ArgumentParser: return parser -def task() -> None: # noqa: C901 - parser = main_parser() - args = parser.parse_args() - - if args.requirements: - for bad_arg in _MARKER_KEYS + ('extras',): - if bad_arg in args: - resolver.__main__._error(f'Option --{bad_arg} not supported when specifying bare requirements') - - reporter = resolver.__main__.VerboseReporter if args.verbose else resolvelib.BaseReporter +def get_min_deps( + reporter: resolvelib.BaseReporter, + requirements: List[str], + extras: Optional[Set[str]] = None, + markers: Optional[Dict[str, str]] = None +) -> Dict[str, str]: package_resolver = resolvelib.Resolver( resolver.mindeps.MinimumDependencyProvider( '/tmp/resolver-cache' if os.name == 'posix' else None @@ -109,21 +105,20 @@ def task() -> None: # noqa: C901 ) requirements: Iterable[packaging.requirements.Requirement] = map( - packaging.requirements.Requirement, - args.requirements or _project_requirements(), + packaging.requirements.Requirement, requirements ) - extras = set(vars(args).get('extras', {})) | {''} - if any(arg in _MARKER_KEYS for arg in vars(args)): + extras = extras.copy() | {''} + if markers is not None and any(marker in _MARKER_KEYS for marker in markers): marker_env = { - k: v for k, v in vars(args).items() + k: v for k, v in markers.items() if k in _MARKER_KEYS } else: marker_env = packaging.markers.default_environment() resolver_requirements: Set[packaging.requirements.Requirement] = set() - for requirement in requirements: + for requirement in list(requirements): for extra in extras: if not requirement.marker: resolver_requirements.add(requirement) @@ -133,13 +128,34 @@ def task() -> None: # noqa: C901 result = package_resolver.resolve(resolver_requirements) - if args.verbose: - print('\n--- Solution ---') - pinned = { candidate.name: candidate.version for candidate in result.mapping.values() } + + return pinned + + +def task() -> None: # noqa: C901 + parser = main_parser() + args = parser.parse_args() + + if args.requirements: + for bad_arg in _MARKER_KEYS + ('extras',): + if bad_arg in args: + resolver.__main__._error(f'Option --{bad_arg} not supported when specifying bare requirements') + + reporter = resolver.__main__.VerboseReporter if args.verbose else resolvelib.BaseReporter + + if args.verbose: + print('\n--- Solution ---') + + pinned = get_min_deps( + reporter=reporter, + requirements=args.requirements or _project_requirements(), + extras=set(vars(args).get('extras', {})), + markers=vars(args) + ) for name, version in pinned.items(): print(f'{name}=={str(version)}') From 535b9efb2986cc75f61d748834ae9111aede5c50 Mon Sep 17 00:00:00 2001 From: Shivansh-007 Date: Sat, 29 Jan 2022 07:27:26 +0530 Subject: [PATCH 2/4] Define mindeps API - allow calling `entrypoint` and `get_min_deps` --- resolver/mindeps/__init__.py | 14 ++------------ resolver/mindeps/__main__.py | 11 ++++++++++- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/resolver/mindeps/__init__.py b/resolver/mindeps/__init__.py index 310c5f0..51de081 100644 --- a/resolver/mindeps/__init__.py +++ b/resolver/mindeps/__init__.py @@ -1,15 +1,5 @@ # SPDX-License-Identifier: MIT -import operator +from resolver.mindeps.__main__ import entrypoint, get_min_deps # noqa: F401 -from typing import Iterable, Sequence - -import resolver - - -class MinimumDependencyProvider(resolver.Provider): - def sort_candidates( - self, - candidates: Iterable[resolver.Candidate], - ) -> Sequence[resolver.Candidate]: - return sorted(candidates, key=operator.attrgetter('version'), reverse=False) +__all__ = ('entrypoint', 'get_min_deps') diff --git a/resolver/mindeps/__main__.py b/resolver/mindeps/__main__.py index dce1973..edc2082 100644 --- a/resolver/mindeps/__main__.py +++ b/resolver/mindeps/__main__.py @@ -1,6 +1,7 @@ # SPDX-License-Identifier: MIT import argparse +import operator import os import os.path import pathlib @@ -91,6 +92,14 @@ def main_parser() -> argparse.ArgumentParser: return parser +class MinimumDependencyProvider(resolver.Provider): + def sort_candidates( + self, + candidates: Iterable[resolver.Candidate], + ) -> Sequence[resolver.Candidate]: + return sorted(candidates, key=operator.attrgetter('version'), reverse=False) + + def get_min_deps( reporter: resolvelib.BaseReporter, requirements: List[str], @@ -98,7 +107,7 @@ def get_min_deps( markers: Optional[Dict[str, str]] = None ) -> Dict[str, str]: package_resolver = resolvelib.Resolver( - resolver.mindeps.MinimumDependencyProvider( + MinimumDependencyProvider( '/tmp/resolver-cache' if os.name == 'posix' else None ), reporter(), From d52ac6ea8e3838af6e41643c7ff2c7d91d4def27 Mon Sep 17 00:00:00 2001 From: Shivansh-007 Date: Sat, 29 Jan 2022 07:33:51 +0530 Subject: [PATCH 3/4] Allow reporter to be `None` --- resolver/mindeps/__main__.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/resolver/mindeps/__main__.py b/resolver/mindeps/__main__.py index edc2082..01ec5af 100644 --- a/resolver/mindeps/__main__.py +++ b/resolver/mindeps/__main__.py @@ -101,11 +101,12 @@ def sort_candidates( def get_min_deps( - reporter: resolvelib.BaseReporter, requirements: List[str], + reporter: Optional[resolvelib.BaseReporter] = None, extras: Optional[Set[str]] = None, markers: Optional[Dict[str, str]] = None ) -> Dict[str, str]: + reporter = reporter or resolvelib.BaseReporter package_resolver = resolvelib.Resolver( MinimumDependencyProvider( '/tmp/resolver-cache' if os.name == 'posix' else None @@ -117,7 +118,7 @@ def get_min_deps( packaging.requirements.Requirement, requirements ) - extras = extras.copy() | {''} + extras = extras.copy() | {''} if extras else {''} if markers is not None and any(marker in _MARKER_KEYS for marker in markers): marker_env = { k: v for k, v in markers.items() @@ -160,8 +161,8 @@ def task() -> None: # noqa: C901 print('\n--- Solution ---') pinned = get_min_deps( + args.requirements or _project_requirements(), reporter=reporter, - requirements=args.requirements or _project_requirements(), extras=set(vars(args).get('extras', {})), markers=vars(args) ) From c44e93e0715d6d7a736db17122e6a606267329b2 Mon Sep 17 00:00:00 2001 From: Shivansh-007 Date: Sat, 29 Jan 2022 07:38:05 +0530 Subject: [PATCH 4/4] Fix linting issues; run pre-commit --- resolver/mindeps/__init__.py | 1 + resolver/mindeps/__main__.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/resolver/mindeps/__init__.py b/resolver/mindeps/__init__.py index 51de081..627f399 100644 --- a/resolver/mindeps/__init__.py +++ b/resolver/mindeps/__init__.py @@ -2,4 +2,5 @@ from resolver.mindeps.__main__ import entrypoint, get_min_deps # noqa: F401 + __all__ = ('entrypoint', 'get_min_deps') diff --git a/resolver/mindeps/__main__.py b/resolver/mindeps/__main__.py index 01ec5af..8e94e81 100644 --- a/resolver/mindeps/__main__.py +++ b/resolver/mindeps/__main__.py @@ -7,7 +7,7 @@ import pathlib import tempfile -from typing import Iterable, Sequence, Set, List, Optional, Dict +from typing import Dict, Iterable, List, Optional, Sequence, Set import packaging.markers import packaging.requirements