Skip to content

Commit 1b6013d

Browse files
committed
Address review comments
Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com>
1 parent 3a3bc16 commit 1b6013d

7 files changed

Lines changed: 65 additions & 180 deletions

File tree

src/python_inspector/resolution.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,10 @@ def format_resolution(results, environment, repos, as_tree=False):
510510
def pdt_dfs(mapping, graph, src):
511511
"""
512512
Return a nested mapping of dependencies.
513+
514+
This takes ``mapping`` and ``graph`` as input. And do a dfs
515+
on the ``graph`` to get the dependencies of the given ``src``.
516+
And use the ``mapping`` to get the version of the given dependency.
513517
"""
514518
children = list(graph.iter_children(src))
515519
if not children:

src/python_inspector/resolve_cli.py

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,17 @@
9696
"--json",
9797
"json_output",
9898
type=FileOptionType(mode="w", encoding="utf-8", lazy=True),
99-
required=True,
99+
required=False,
100100
metavar="FILE",
101101
help="Write output as pretty-printed JSON to FILE. "
102102
"Use the special '-' file name to print results on screen/stdout.",
103103
)
104104
@click.option(
105105
"--json-pdt",
106106
"pdt_output",
107-
is_flag=True,
107+
type=FileOptionType(mode="w", encoding="utf-8", lazy=True),
108+
required=False,
109+
metavar="FILE",
108110
help="Write output as pretty-printed JSON to FILE. "
109111
"Use the special '-' file name to print results on screen/stdout.",
110112
)
@@ -166,6 +168,11 @@ def resolve_dependencies(
166168
167169
dad --spec "flask==2.1.2" --json -
168170
"""
171+
if not (json_output or pdt_output):
172+
if debug:
173+
click.secho("No output file specified. Use --json or --json-pdt.", err=True)
174+
return
175+
169176
if debug:
170177
click.secho(f"Resolving dependencies...")
171178

@@ -268,12 +275,22 @@ def resolve_dependencies(
268275
errors=[],
269276
)
270277

271-
write_output(
272-
headers=headers,
273-
requirements=requirements,
274-
resolved_dependencies=resolved_dependencies,
275-
json_output=json_output,
276-
)
278+
if json_output:
279+
write_output(
280+
headers=headers,
281+
requirements=requirements,
282+
resolved_dependencies=resolved_dependencies,
283+
json_output=json_output,
284+
)
285+
286+
if pdt_output:
287+
write_output(
288+
headers=headers,
289+
requirements=requirements,
290+
resolved_dependencies=resolved_dependencies,
291+
json_output=pdt_output,
292+
pdt_output=True,
293+
)
277294

278295
if debug:
279296
click.secho("done!")
@@ -322,16 +339,19 @@ def resolve(
322339
return initial_requirements, resolved_dependencies
323340

324341

325-
def write_output(headers, requirements, resolved_dependencies, json_output):
342+
def write_output(headers, requirements, resolved_dependencies, json_output, pdt_output=False):
326343
"""
327344
Write headers, requirements and resolved_dependencies as JSON to ``json_output``.
328345
Return the output data.
329346
"""
330-
output = dict(
331-
headers=headers,
332-
requirements=requirements,
333-
resolved_dependencies=resolved_dependencies,
334-
)
347+
if not pdt_output:
348+
output = dict(
349+
headers=headers,
350+
requirements=requirements,
351+
resolved_dependencies=resolved_dependencies,
352+
)
353+
else:
354+
output = (resolved_dependencies,)
335355

336356
json.dump(output, json_output, indent=2)
337357
return output

tests/data/pdt-expected.json

Lines changed: 3 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -1,147 +1,5 @@
1-
{
2-
"headers": {
3-
"tool_name": "dad",
4-
"tool_homepageurl": "https://github.com/nexB/python-inspector",
5-
"tool_version": "0.5.0",
6-
"options": [
7-
"--index-url https://pypi.org/simple",
8-
"--python-version 38",
9-
"--operating-system linux",
10-
"--json <file>"
11-
],
12-
"notice": "Dependency tree generated with python-inspector.\npython-inspector is a free software tool from nexB Inc. and others.\nVisit https://github.com/nexB/scancode-toolkit/ for support and download.",
13-
"warnings": [],
14-
"errors": []
15-
},
16-
"requirements": [
17-
{
18-
"purl": "pkg:pypi/click@6.7",
19-
"extracted_requirement": "click==6.7",
20-
"scope": "install",
21-
"is_runtime": true,
22-
"is_optional": false,
23-
"is_resolved": true,
24-
"resolved_package": {},
25-
"extra_data": {
26-
"is_editable": false,
27-
"link": null,
28-
"hash_options": [],
29-
"is_constraint": false,
30-
"is_archive": null,
31-
"is_wheel": false,
32-
"is_url": null,
33-
"is_vcs_url": null,
34-
"is_name_at_url": false,
35-
"is_local_path": null
36-
}
37-
},
38-
{
39-
"purl": "pkg:pypi/flask@1.0",
40-
"extracted_requirement": "Flask==1.0",
41-
"scope": "install",
42-
"is_runtime": true,
43-
"is_optional": false,
44-
"is_resolved": true,
45-
"resolved_package": {},
46-
"extra_data": {
47-
"is_editable": false,
48-
"link": null,
49-
"hash_options": [],
50-
"is_constraint": false,
51-
"is_archive": null,
52-
"is_wheel": false,
53-
"is_url": null,
54-
"is_vcs_url": null,
55-
"is_name_at_url": false,
56-
"is_local_path": null
57-
}
58-
},
59-
{
60-
"purl": "pkg:pypi/itsdangerous@0.24",
61-
"extracted_requirement": "itsdangerous==0.24",
62-
"scope": "install",
63-
"is_runtime": true,
64-
"is_optional": false,
65-
"is_resolved": true,
66-
"resolved_package": {},
67-
"extra_data": {
68-
"is_editable": false,
69-
"link": null,
70-
"hash_options": [],
71-
"is_constraint": false,
72-
"is_archive": null,
73-
"is_wheel": false,
74-
"is_url": null,
75-
"is_vcs_url": null,
76-
"is_name_at_url": false,
77-
"is_local_path": null
78-
}
79-
},
80-
{
81-
"purl": "pkg:pypi/jinja2@2.11.3",
82-
"extracted_requirement": "Jinja2==2.11.3",
83-
"scope": "install",
84-
"is_runtime": true,
85-
"is_optional": false,
86-
"is_resolved": true,
87-
"resolved_package": {},
88-
"extra_data": {
89-
"is_editable": false,
90-
"link": null,
91-
"hash_options": [],
92-
"is_constraint": false,
93-
"is_archive": null,
94-
"is_wheel": false,
95-
"is_url": null,
96-
"is_vcs_url": null,
97-
"is_name_at_url": false,
98-
"is_local_path": null
99-
}
100-
},
101-
{
102-
"purl": "pkg:pypi/markupsafe@1.0",
103-
"extracted_requirement": "MarkupSafe==1.0",
104-
"scope": "install",
105-
"is_runtime": true,
106-
"is_optional": false,
107-
"is_resolved": true,
108-
"resolved_package": {},
109-
"extra_data": {
110-
"is_editable": false,
111-
"link": null,
112-
"hash_options": [],
113-
"is_constraint": false,
114-
"is_archive": null,
115-
"is_wheel": false,
116-
"is_url": null,
117-
"is_vcs_url": null,
118-
"is_name_at_url": false,
119-
"is_local_path": null
120-
}
121-
},
122-
{
123-
"purl": "pkg:pypi/werkzeug@0.15.3",
124-
"extracted_requirement": "Werkzeug==0.15.3",
125-
"scope": "install",
126-
"is_runtime": true,
127-
"is_optional": false,
128-
"is_resolved": true,
129-
"resolved_package": {},
130-
"extra_data": {
131-
"is_editable": false,
132-
"link": null,
133-
"hash_options": [],
134-
"is_constraint": false,
135-
"is_archive": null,
136-
"is_wheel": false,
137-
"is_url": null,
138-
"is_vcs_url": null,
139-
"is_name_at_url": false,
140-
"is_local_path": null
141-
}
142-
}
143-
],
144-
"resolved_dependencies": [
1+
[
2+
[
1453
{
1464
"key": "flask",
1475
"package_name": "flask",
@@ -181,4 +39,4 @@
18139
]
18240
}
18341
]
184-
}
42+
]

tests/data/pinned-requirements.txt-expected.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -939,14 +939,14 @@
939939
"sdist_url": "https://files.pythonhosted.org/packages/10/a7/51953e73828deef2b58ba1604de9167843ee9cd4185d8aaffcb45dd1932d/cryptography-36.0.2.tar.gz"
940940
},
941941
{
942-
"package": "pkg:pypi/cython@0.29.30",
942+
"package": "pkg:pypi/cython@0.29.31",
943943
"dependencies": [],
944944
"wheel_urls": [
945-
"https://files.pythonhosted.org/packages/6e/14/9ce941960f68dd4f530c84b74c176e3020c7b8c9616614f788bcf59c7f00/Cython-0.29.30-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_24_x86_64.whl",
946-
"https://files.pythonhosted.org/packages/60/ac/aeb6bf82cffb63aed057bbfb6eb370d3d7993c014e82a9dca3994cfbb8f3/Cython-0.29.30-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.whl",
947-
"https://files.pythonhosted.org/packages/80/08/1c007f1d571f8f2a67ed6938cc79117fa5ae9c0d9ff633fbd5e52f212062/Cython-0.29.30-py2.py3-none-any.whl"
945+
"https://files.pythonhosted.org/packages/02/da/2ebb4183a3e4d8894a08342122279bd1f732751cd75d784692424f044ef9/Cython-0.29.31-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_24_x86_64.whl",
946+
"https://files.pythonhosted.org/packages/32/73/0f28273dce8c02ec6df0e828db064d7a976d1cb270ab963244aa577776d0/Cython-0.29.31-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.whl",
947+
"https://files.pythonhosted.org/packages/1c/6c/dc9a3bd97ed094b7f24373705b13e66705bf69958f6ace90153202f60836/Cython-0.29.31-py2.py3-none-any.whl"
948948
],
949-
"sdist_url": "https://files.pythonhosted.org/packages/d4/ad/7ce0cccd68824ac9623daf4e973c587aa7e2d23418cd028f8860c80651f5/Cython-0.29.30.tar.gz"
949+
"sdist_url": "https://files.pythonhosted.org/packages/e3/96/ff430960a664e571285365009bf526fa8ec67267a9632b758914adf9206c/Cython-0.29.31.tar.gz"
950950
},
951951
{
952952
"package": "pkg:pypi/debian-inspector@30.0.0",
@@ -1239,7 +1239,7 @@
12391239
{
12401240
"package": "pkg:pypi/lxml@4.8.0",
12411241
"dependencies": [
1242-
"pkg:pypi/cython@0.29.30"
1242+
"pkg:pypi/cython@0.29.31"
12431243
],
12441244
"wheel_urls": [
12451245
"https://files.pythonhosted.org/packages/d4/a7/21bf8234d3f0bae5219c869c2d34d1ac3fcd0b1907ce2afffb0209b15a7f/lxml-4.8.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_24_x86_64.whl",

tests/data/single-url-except-simple-expected.json

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
"pkg:pypi/importlib-metadata@4.12.0",
4444
"pkg:pypi/itsdangerous@2.1.2",
4545
"pkg:pypi/jinja2@3.1.2",
46-
"pkg:pypi/werkzeug@2.1.2"
46+
"pkg:pypi/werkzeug@2.2.1"
4747
],
4848
"wheel_urls": [
4949
"https://files.pythonhosted.org/packages/af/6a/00d144ac1626fbb44c4ff36519712e258128985a5d0ae43344778ae5cbb9/Flask-2.1.3-py3-none-any.whl"
@@ -87,12 +87,14 @@
8787
"sdist_url": "https://files.pythonhosted.org/packages/1d/97/2288fe498044284f39ab8950703e88abbac2abbdf65524d576157af70556/MarkupSafe-2.1.1.tar.gz"
8888
},
8989
{
90-
"package": "pkg:pypi/werkzeug@2.1.2",
91-
"dependencies": [],
90+
"package": "pkg:pypi/werkzeug@2.2.1",
91+
"dependencies": [
92+
"pkg:pypi/markupsafe@2.1.1"
93+
],
9294
"wheel_urls": [
93-
"https://files.pythonhosted.org/packages/c4/44/f50f2d22cdfb6d56c03d1b4cc3cfa03ebee2f21b59a7768f151e43415ba5/Werkzeug-2.1.2-py3-none-any.whl"
95+
"https://files.pythonhosted.org/packages/c0/93/d6d60870e47162ea6a1bbdd787649eea776b2a70618dd66ed87cb2238543/Werkzeug-2.2.1-py3-none-any.whl"
9496
],
95-
"sdist_url": "https://files.pythonhosted.org/packages/10/cf/97eb1a3847c01ae53e8376bc21145555ac95279523a935963dc8ff96c50b/Werkzeug-2.1.2.tar.gz"
97+
"sdist_url": "https://files.pythonhosted.org/packages/32/19/a92cdbd9fb795928dfca1031278ae8a7f051e78a2c057c224ad2d4cdd95e/Werkzeug-2.2.1.tar.gz"
9698
},
9799
{
98100
"package": "pkg:pypi/zipp@3.8.1",

tests/test_cli.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,12 @@ def test_cli_with_default_urls():
4444
def test_pdt_output():
4545
requirements_file = test_env.get_test_loc("pdt.txt")
4646
expected_file = test_env.get_test_loc("pdt-expected.json", must_exist=False)
47-
extra_options = [
48-
"--json-pdt",
49-
]
47+
extra_options = []
5048
check_requirements_resolution(
5149
requirements_file=requirements_file,
5250
expected_file=expected_file,
5351
extra_options=extra_options,
52+
pdt_output=True,
5453
regen=REGEN_TEST_FIXTURES,
5554
)
5655

@@ -182,15 +181,17 @@ def check_requirements_resolution(
182181
expected_file,
183182
extra_options=tuple(),
184183
regen=REGEN_TEST_FIXTURES,
184+
pdt_output=False,
185185
):
186186
result_file = test_env.get_temp_file("json")
187-
options = ["--requirement", requirements_file, "--json", result_file]
187+
if pdt_output:
188+
options = ["--requirement", requirements_file, "--json-pdt", result_file]
189+
else:
190+
options = ["--requirement", requirements_file, "--json", result_file]
188191
options.extend(extra_options)
189192
run_cli(options=options)
190193
check_json_results(
191-
result_file=result_file,
192-
expected_file=expected_file,
193-
regen=regen,
194+
result_file=result_file, expected_file=expected_file, regen=regen, clean=not pdt_output
194195
)
195196

196197

tests/test_resolution.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def test_get_resolved_dependencies_with_flask_and_python_310():
3838
"pkg:pypi/itsdangerous@2.1.2",
3939
"pkg:pypi/jinja2@3.1.2",
4040
"pkg:pypi/markupsafe@2.1.1",
41-
"pkg:pypi/werkzeug@2.1.2",
41+
"pkg:pypi/werkzeug@2.2.1",
4242
]
4343

4444

@@ -63,7 +63,7 @@ def test_get_resolved_dependencies_with_flask_and_python_310_windows():
6363
"pkg:pypi/itsdangerous@2.1.2",
6464
"pkg:pypi/jinja2@3.1.2",
6565
"pkg:pypi/markupsafe@2.1.1",
66-
"pkg:pypi/werkzeug@2.1.2",
66+
"pkg:pypi/werkzeug@2.2.1",
6767
]
6868

6969

@@ -116,7 +116,7 @@ def test_get_resolved_dependencies_with_tilde_requirement_using_json_api():
116116
"pkg:pypi/itsdangerous@2.1.2",
117117
"pkg:pypi/jinja2@3.1.2",
118118
"pkg:pypi/markupsafe@2.1.1",
119-
"pkg:pypi/werkzeug@2.1.2",
119+
"pkg:pypi/werkzeug@2.2.1",
120120
"pkg:pypi/zipp@3.8.1",
121121
]
122122

0 commit comments

Comments
 (0)