Skip to content

Commit db7f6e4

Browse files
committed
Do not crash when manifest files are empty
Signed-off-by: Tushar Goel <[email protected]>
1 parent f2eaad4 commit db7f6e4

File tree

6 files changed

+30
-23
lines changed

6 files changed

+30
-23
lines changed

src/python_inspector/api.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,14 @@ def resolver_api(
8383
"""
8484
Resolve the dependencies for the package requirements listed in one or
8585
more ``requirement_files``, one or more ``specifiers`` and one setuptools
86-
``setup_py_file`` file and save the results as JSON to FILE.
86+
``setup_py_file`` file.
87+
88+
Resolve the dependencies for the requested ``python_version`` PYVER and
89+
``operating_system`` OS combination defaulting Python version 3.8 and
90+
linux OS.
91+
92+
Download from the provided PyPI simple index_urls INDEX(s) URLs defaulting
93+
to PyPI.org
8794
"""
8895

8996
if verbose:
@@ -198,7 +205,11 @@ def resolver_api(
198205
)
199206

200207
if not direct_dependencies:
201-
raise Exception("Error: no requirements requested.")
208+
return Resolution(
209+
packages=[],
210+
resolution={},
211+
files=files,
212+
)
202213

203214
if verbose:
204215
printer("direct_dependencies:")
@@ -266,7 +277,7 @@ def resolver_api(
266277
packages=packages,
267278
resolution=resolution,
268279
files=files,
269-
).to_dict()
280+
)
270281

271282

272283
def resolve(
@@ -345,6 +356,9 @@ def get_resolved_dependencies(
345356
def get_requirements_from_direct_dependencies(
346357
direct_dependencies: List[DependentPackage], environment_marker: Dict
347358
) -> List[Requirement]:
359+
"""
360+
Yield Requirements from a list of DependentPackages.
361+
"""
348362
for dependency in direct_dependencies:
349363
# FIXME We are skipping editable requirements
350364
# and other pip options for now

src/python_inspector/resolution.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,9 @@ def get_requirements_from_distribution(
8181
if not os.path.exists(location):
8282
return []
8383
reqs = []
84-
try:
85-
for package_data in handler.parse(location):
86-
dependencies = package_data.dependencies
87-
reqs.extend(get_requirements_from_dependencies(dependencies=dependencies))
88-
except Exception as e:
89-
import subprocess
90-
91-
subprocess.check_call(["zip", "-T", location])
92-
raise Exception(
93-
f"Could not get requirements from pypi package at: {location!r}: {e}"
94-
) from e
84+
for package_data in handler.parse(location):
85+
dependencies = package_data.dependencies
86+
reqs.extend(get_requirements_from_dependencies(dependencies=dependencies))
9587
return reqs
9688

9789

src/python_inspector/resolve_cli.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,9 @@ def resolve_dependencies(
253253
)
254254
output = dict(
255255
headers=headers,
256-
resolved_dependencies_graph=resolution_result.get("resolution"),
257-
files=resolution_result.get("files"),
258-
packages=resolution_result.get("packages"),
256+
resolved_dependencies_graph=resolution_result.resolution,
257+
files=resolution_result.files,
258+
packages=resolution_result.packages,
259259
)
260260
write_output_in_file(
261261
output=output,

src/python_inspector/setup_py_live_eval.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ def iter_requirements(level, extras, setup_file):
6060
with open(setup_file) as sf:
6161
exec(sf.read(), g)
6262
sys.path.pop()
63+
# removing this assertion since it is not true for all cases
64+
# for example when setuptools.setup() is called instead of setup()
6365

6466
mock_args, mock_kwargs = mock_setup.call_args
6567
install_requires = mock_kwargs.get("install_requires", install_requires)

tests/test_api.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def test_api_with_specifier():
3131
specifiers=["flask==2.1.2"],
3232
python_version="3.10",
3333
operating_system="linux",
34-
)
34+
).to_dict()
3535
)
3636
)
3737
check_json_results(
@@ -52,7 +52,7 @@ def test_api_with_specifier_pdt():
5252
python_version="3.10",
5353
operating_system="linux",
5454
pdt_output=True,
55-
)
55+
).to_dict()
5656
)
5757
)
5858
check_json_results(
@@ -73,7 +73,7 @@ def test_api_with_requirement_file():
7373
python_version="3.10",
7474
operating_system="linux",
7575
requirement_files=[requirement_file],
76-
)
76+
).to_dict()
7777
)
7878
)
7979
check_json_results(

tests/test_cli.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -346,16 +346,15 @@ def test_passing_of_netrc_file_that_does_not_exist():
346346
run_cli(options=options, expected_rc=2)
347347

348348

349-
def test_passing_of_wrong_requirements_file():
349+
def test_passing_of_empty_requirements_file():
350350
test_file = test_env.get_temp_file(file_name="pdt.txt", extension="")
351351
with open(test_file, "w") as f:
352352
f.write("")
353353
test_file_2 = test_env.get_temp_file(file_name="setup.py", extension="")
354354
with open(test_file_2, "w") as f:
355355
f.write("")
356356
options = ["--requirement", test_file, "--json", "-", "--requirement", test_file_2]
357-
result = run_cli(options=options, expected_rc=1)
358-
assert "Error: no requirements requested" in result.output
357+
run_cli(options=options, expected_rc=0)
359358

360359

361360
def test_passing_of_no_json_output_flag():

0 commit comments

Comments
 (0)