Skip to content

Commit 2366830

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

4 files changed

Lines changed: 86 additions & 203 deletions

File tree

src/python_inspector/resolution.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,10 @@ def format_resolution(
630630
def pdt_dfs(mapping, graph, src):
631631
"""
632632
Return a nested mapping of dependencies.
633+
634+
This takes ``mapping`` and ``graph`` as input. And do a dfs
635+
on the ``graph`` to get the dependencies of the given ``src``.
636+
And use the ``mapping`` to get the version of the given dependency.
633637
"""
634638
children = list(graph.iter_children(src))
635639
if not children:

src/python_inspector/resolve_cli.py

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,17 @@
9999
"--json",
100100
"json_output",
101101
type=FileOptionType(mode="w", encoding="utf-8", lazy=True),
102-
required=True,
102+
required=False,
103103
metavar="FILE",
104104
help="Write output as pretty-printed JSON to FILE. "
105105
"Use the special '-' file name to print results on screen/stdout.",
106106
)
107107
@click.option(
108108
"--json-pdt",
109109
"pdt_output",
110-
is_flag=True,
110+
type=FileOptionType(mode="w", encoding="utf-8", lazy=True),
111+
required=False,
112+
metavar="FILE",
111113
help="Write output as pretty-printed JSON to FILE. "
112114
"Use the special '-' file name to print results on screen/stdout.",
113115
)
@@ -169,6 +171,11 @@ def resolve_dependencies(
169171
170172
dad --spec "flask==2.1.2" --json -
171173
"""
174+
if not (json_output or pdt_output):
175+
if debug:
176+
click.secho("No output file specified. Use --json or --json-pdt.", err=True)
177+
return
178+
172179
if debug:
173180
click.secho(f"Resolving dependencies...")
174181

@@ -271,12 +278,22 @@ def resolve_dependencies(
271278
errors=[],
272279
)
273280

274-
write_output(
275-
headers=headers,
276-
requirements=requirements,
277-
resolved_dependencies=resolved_dependencies,
278-
json_output=json_output,
279-
)
281+
if json_output:
282+
write_output(
283+
headers=headers,
284+
requirements=requirements,
285+
resolved_dependencies=resolved_dependencies,
286+
json_output=json_output,
287+
)
288+
289+
if pdt_output:
290+
write_output(
291+
headers=headers,
292+
requirements=requirements,
293+
resolved_dependencies=resolved_dependencies,
294+
json_output=pdt_output,
295+
pdt_output=True,
296+
)
280297

281298
if debug:
282299
click.secho("done!")
@@ -311,6 +328,8 @@ def resolve(
311328
pdt_output=pdt_output,
312329
)
313330

331+
print(resolved_dependencies)
332+
314333
initial_requirements = [d.to_dict() for d in direct_dependencies]
315334

316335
return initial_requirements, resolved_dependencies
@@ -333,11 +352,14 @@ def write_output(headers, requirements, resolved_dependencies, json_output):
333352
Write headers, requirements and resolved_dependencies as JSON to ``json_output``.
334353
Return the output data.
335354
"""
336-
output = dict(
337-
headers=headers,
338-
requirements=requirements,
339-
resolved_dependencies=resolved_dependencies,
340-
)
355+
if not pdt_output:
356+
output = dict(
357+
headers=headers,
358+
requirements=requirements,
359+
resolved_dependencies=resolved_dependencies,
360+
)
361+
else:
362+
output = resolved_dependencies
341363

342364
json.dump(output, json_output, indent=2)
343365
return output

tests/data/pdt-expected.json

Lines changed: 39 additions & 183 deletions
Original file line numberDiff line numberDiff line change
@@ -1,184 +1,40 @@
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
1+
[
2+
{
3+
"key": "flask",
4+
"package_name": "flask",
5+
"installed_version": "1.0",
6+
"dependencies": [
7+
{
8+
"key": "click",
9+
"package_name": "click",
10+
"installed_version": "6.7",
11+
"dependencies": []
12+
},
13+
{
14+
"key": "itsdangerous",
15+
"package_name": "itsdangerous",
16+
"installed_version": "0.24",
17+
"dependencies": []
18+
},
19+
{
20+
"key": "jinja2",
21+
"package_name": "jinja2",
22+
"installed_version": "2.11.3",
23+
"dependencies": [
24+
{
25+
"key": "markupsafe",
26+
"package_name": "markupsafe",
27+
"installed_version": "1.0",
28+
"dependencies": []
29+
}
30+
]
31+
},
32+
{
33+
"key": "werkzeug",
34+
"package_name": "werkzeug",
35+
"installed_version": "0.15.3",
36+
"dependencies": []
3637
}
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": [
145-
{
146-
"key": "flask",
147-
"package_name": "flask",
148-
"installed_version": "1.0",
149-
"dependencies": [
150-
{
151-
"key": "click",
152-
"package_name": "click",
153-
"installed_version": "6.7",
154-
"dependencies": []
155-
},
156-
{
157-
"key": "itsdangerous",
158-
"package_name": "itsdangerous",
159-
"installed_version": "0.24",
160-
"dependencies": []
161-
},
162-
{
163-
"key": "jinja2",
164-
"package_name": "jinja2",
165-
"installed_version": "2.11.3",
166-
"dependencies": [
167-
{
168-
"key": "markupsafe",
169-
"package_name": "markupsafe",
170-
"installed_version": "1.0",
171-
"dependencies": []
172-
}
173-
]
174-
},
175-
{
176-
"key": "werkzeug",
177-
"package_name": "werkzeug",
178-
"installed_version": "0.15.3",
179-
"dependencies": []
180-
}
181-
]
182-
}
183-
]
184-
}
38+
]
39+
}
40+
]

tests/test_cli.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,12 @@ def test_cli_with_default_urls():
4646
def test_pdt_output():
4747
requirements_file = test_env.get_test_loc("pdt.txt")
4848
expected_file = test_env.get_test_loc("pdt-expected.json", must_exist=False)
49-
extra_options = [
50-
"--json-pdt",
51-
]
49+
extra_options = []
5250
check_requirements_resolution(
5351
requirements_file=requirements_file,
5452
expected_file=expected_file,
5553
extra_options=extra_options,
54+
pdt_output=True,
5655
regen=REGEN_TEST_FIXTURES,
5756
)
5857

@@ -184,15 +183,17 @@ def check_requirements_resolution(
184183
expected_file,
185184
extra_options=tuple(),
186185
regen=REGEN_TEST_FIXTURES,
186+
pdt_output=False,
187187
):
188188
result_file = test_env.get_temp_file("json")
189-
options = ["--requirement", requirements_file, "--json", result_file]
189+
if pdt_output:
190+
options = ["--requirement", requirements_file, "--json-pdt", result_file]
191+
else:
192+
options = ["--requirement", requirements_file, "--json", result_file]
190193
options.extend(extra_options)
191194
run_cli(options=options)
192195
check_json_results(
193-
result_file=result_file,
194-
expected_file=expected_file,
195-
regen=regen,
196+
result_file=result_file, expected_file=expected_file, regen=regen, clean=not pdt_output
196197
)
197198

198199

0 commit comments

Comments
 (0)