From 1f243cb227d33788cace911ee1713dd45f2d46a6 Mon Sep 17 00:00:00 2001 From: Tushar Goel Date: Fri, 22 Jul 2022 16:49:08 +0530 Subject: [PATCH 1/2] Do not log anything by default Signed-off-by: Tushar Goel --- src/python_inspector/resolution.py | 27 ++++++++++++++++++++------- src/python_inspector/resolve_cli.py | 13 +++++++++---- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/src/python_inspector/resolution.py b/src/python_inspector/resolution.py index e37d18b1..dea8ebf0 100644 --- a/src/python_inspector/resolution.py +++ b/src/python_inspector/resolution.py @@ -633,6 +633,7 @@ def get_resolved_dependencies( repos: Sequence[PypiSimpleRepository] = tuple(), as_tree: bool = False, max_rounds: int = 200000, + debug: bool = False, ): """ Return resolved dependencies of a ``requirements`` list of Requirement for @@ -642,10 +643,22 @@ def get_resolved_dependencies( Used the provided ``repos`` list of PypiSimpleRepository. If empty, use instead the PyPI.org JSON API exclusively instead """ - resolver = Resolver( - provider=PythonInputProvider(environment=environment, repos=repos), - reporter=BaseReporter(), - ) - results = resolver.resolve(requirements=requirements, max_rounds=max_rounds) - results = format_resolution(results, as_tree=as_tree, environment=environment, repos=repos) - return results + try: + resolved_requirements = { + packaging.utils.canonicalize_name(r.name): r.specifier + for r in requirements + if getattr(r, "is_requirement_resolved", False) + } + resolver = Resolver( + provider=PythonInputProvider( + environment=environment, repos=repos, resolved_requirements=resolved_requirements + ), + reporter=BaseReporter(), + ) + results = resolver.resolve(requirements=requirements, max_rounds=max_rounds) + results = format_resolution(results, as_tree=as_tree, environment=environment, repos=repos) + return results + except Exception as e: + if debug: + click.secho(f"{e!r}", err=True) + return None diff --git a/src/python_inspector/resolve_cli.py b/src/python_inspector/resolve_cli.py index 9babbe17..8d0d9e2c 100644 --- a/src/python_inspector/resolve_cli.py +++ b/src/python_inspector/resolve_cli.py @@ -161,8 +161,8 @@ def resolve_dependencies( dad --spec "flask==2.1.2" --json - """ - - click.secho(f"Resolving dependencies...") + if debug: + click.secho(f"Resolving dependencies...") netrc = None if netrc_file: @@ -184,7 +184,8 @@ def resolve_dependencies( direct_dependencies.append(dep) if not direct_dependencies: - click.secho("Error: no requirements requested.") + if debug: + click.secho("Error: no requirements requested.") sys.exit(1) if debug: @@ -235,6 +236,7 @@ def resolve_dependencies( repos=repos, as_tree=False, max_rounds=max_rounds, + debug=debug, ) cli_options = [f"--requirement {rf}" for rf in requirement_files] @@ -271,7 +273,9 @@ def resolve_dependencies( click.secho("done!") -def resolve(direct_dependencies, environment, repos=tuple(), as_tree=False, max_rounds=200000): +def resolve( + direct_dependencies, environment, repos=tuple(), as_tree=False, max_rounds=200000, debug=False +): """ Resolve dependencies given a ``direct_dependencies`` list of DependentPackage and return a tuple of (initial_requirements, @@ -288,6 +292,7 @@ def resolve(direct_dependencies, environment, repos=tuple(), as_tree=False, max_ repos=repos, as_tree=as_tree, max_rounds=max_rounds, + debug=debug, ) initial_requirements = [d.to_dict() for d in direct_dependencies] From be60160e03ed8086f00bd7c741515e04bd5600ae Mon Sep 17 00:00:00 2001 From: Tushar Goel Date: Fri, 29 Jul 2022 18:16:37 +0530 Subject: [PATCH 2/2] Address review comments Signed-off-by: Tushar Goel --- src/python_inspector/resolution.py | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/src/python_inspector/resolution.py b/src/python_inspector/resolution.py index dea8ebf0..116c7e4d 100644 --- a/src/python_inspector/resolution.py +++ b/src/python_inspector/resolution.py @@ -644,15 +644,8 @@ def get_resolved_dependencies( If empty, use instead the PyPI.org JSON API exclusively instead """ try: - resolved_requirements = { - packaging.utils.canonicalize_name(r.name): r.specifier - for r in requirements - if getattr(r, "is_requirement_resolved", False) - } resolver = Resolver( - provider=PythonInputProvider( - environment=environment, repos=repos, resolved_requirements=resolved_requirements - ), + provider=PythonInputProvider(environment=environment, repos=repos), reporter=BaseReporter(), ) results = resolver.resolve(requirements=requirements, max_rounds=max_rounds) @@ -660,5 +653,6 @@ def get_resolved_dependencies( return results except Exception as e: if debug: + import click + click.secho(f"{e!r}", err=True) - return None