Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 42 additions & 3 deletions src/python_inspector/resolution.py
Original file line number Diff line number Diff line change
Expand Up @@ -627,13 +627,49 @@ def format_resolution(
return dependencies


def pdt_dfs(mapping, graph, src):
"""
Return a nested mapping of dependencies.
Comment thread
pombredanne marked this conversation as resolved.

This takes ``mapping`` and ``graph`` as input. And do a dfs
Comment thread
TG1999 marked this conversation as resolved.
on the ``graph`` to get the dependencies of the given ``src``.
And use the ``mapping`` to get the version of the given dependency.
"""
children = list(graph.iter_children(src))
if not children:
return dict(
key=src, package_name=src, installed_version=str(mapping[src].version), dependencies=[]
)

Comment thread
TG1999 marked this conversation as resolved.
Outdated
return dict(
key=src,
package_name=src,
installed_version=str(mapping[src].version),
dependencies=sorted([pdt_dfs(mapping, graph, c) for c in children], key=lambda d: d["key"]),
Comment thread
TG1999 marked this conversation as resolved.
Outdated
)


def format_pdt_tree(results):
"""
Return a formatted tree of dependencies.
Comment thread
TG1999 marked this conversation as resolved.
Outdated
"""
mapping = results.mapping
graph = results.graph
dependencies = []
for src in get_all_srcs(mapping=mapping, graph=graph):
dependencies.append(pdt_dfs(mapping=mapping, graph=graph, src=src))
dependencies.sort(key=lambda d: d["key"])
Comment thread
TG1999 marked this conversation as resolved.
return dependencies


def get_resolved_dependencies(
requirements: List[Requirement],
environment: Environment = None,
repos: Sequence[PypiSimpleRepository] = tuple(),
as_tree: bool = False,
max_rounds: int = 200000,
debug: bool = False,
pdt_output: bool = False,
):
"""
Return resolved dependencies of a ``requirements`` list of Requirement for
Expand All @@ -648,9 +684,12 @@ def get_resolved_dependencies(
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
resolver_results = resolver.resolve(requirements=requirements, max_rounds=max_rounds)
if pdt_output:
return format_pdt_tree(resolver_results)
return format_resolution(
resolver_results, as_tree=as_tree, environment=environment, repos=repos
)
except Exception as e:
if debug:
import click
Expand Down
66 changes: 52 additions & 14 deletions src/python_inspector/resolve_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,16 @@
"--json",
"json_output",
type=FileOptionType(mode="w", encoding="utf-8", lazy=True),
required=True,
required=False,
metavar="FILE",
help="Write output as pretty-printed JSON to FILE. "
"Use the special '-' file name to print results on screen/stdout.",
)
@click.option(
"--json-pdt",
"pdt_output",
type=FileOptionType(mode="w", encoding="utf-8", lazy=True),
required=False,
metavar="FILE",
help="Write output as pretty-printed JSON to FILE. "
Comment thread
TG1999 marked this conversation as resolved.
Outdated
"Use the special '-' file name to print results on screen/stdout.",
Expand Down Expand Up @@ -138,6 +147,7 @@ def resolve_dependencies(
operating_system,
index_urls,
json_output,
pdt_output,
max_rounds,
use_cached_index=False,
use_pypi_json_api=False,
Expand All @@ -161,6 +171,11 @@ def resolve_dependencies(

dad --spec "flask==2.1.2" --json -
"""
if not (json_output or pdt_output):
if debug:
Comment thread
TG1999 marked this conversation as resolved.
Outdated
click.secho("No output file specified. Use --json or --json-pdt.", err=True)
return
Comment thread
TG1999 marked this conversation as resolved.
Outdated

if debug:
Comment thread
TG1999 marked this conversation as resolved.
Outdated
click.secho(f"Resolving dependencies...")

Expand Down Expand Up @@ -237,6 +252,7 @@ def resolve_dependencies(
as_tree=False,
max_rounds=max_rounds,
debug=debug,
pdt_output=pdt_output,
)

cli_options = [f"--requirement {rf}" for rf in requirement_files]
Expand All @@ -262,19 +278,35 @@ def resolve_dependencies(
errors=[],
)

write_output(
headers=headers,
requirements=requirements,
resolved_dependencies=resolved_dependencies,
json_output=json_output,
)
if json_output:
write_output(
headers=headers,
requirements=requirements,
resolved_dependencies=resolved_dependencies,
json_output=json_output,
)

if pdt_output:
Comment thread
TG1999 marked this conversation as resolved.
Outdated
write_output(
headers=headers,
requirements=requirements,
resolved_dependencies=resolved_dependencies,
json_output=pdt_output,
pdt_output=True,
)

if debug:
click.secho("done!")


def resolve(
direct_dependencies, environment, repos=tuple(), as_tree=False, max_rounds=200000, debug=False
direct_dependencies,
environment,
repos=tuple(),
as_tree=False,
max_rounds=200000,
debug=False,
pdt_output=False,
):
"""
Resolve dependencies given a ``direct_dependencies`` list of
Expand All @@ -293,8 +325,11 @@ def resolve(
as_tree=as_tree,
max_rounds=max_rounds,
debug=debug,
pdt_output=pdt_output,
)

print(resolved_dependencies)
Comment thread
TG1999 marked this conversation as resolved.
Outdated

initial_requirements = [d.to_dict() for d in direct_dependencies]

return initial_requirements, resolved_dependencies
Expand All @@ -312,16 +347,19 @@ def get_requirements_from_direct_dependencies(
yield Requirement(requirement_string=dependency.extracted_requirement)


def write_output(headers, requirements, resolved_dependencies, json_output):
def write_output(headers, requirements, resolved_dependencies, json_output, pdt_output=False):
"""
Write headers, requirements and resolved_dependencies as JSON to ``json_output``.
Return the output data.
"""
output = dict(
headers=headers,
requirements=requirements,
resolved_dependencies=resolved_dependencies,
)
if not pdt_output:
output = dict(
headers=headers,
requirements=requirements,
resolved_dependencies=resolved_dependencies,
)
else:
output = resolved_dependencies

json.dump(output, json_output, indent=2)
return output
Expand Down
Loading