Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
34 changes: 33 additions & 1 deletion src/python_inspector/resolve_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
#

import json
import os
from typing import List

import click
from packaging.requirements import Requirement
from tinynetrc import Netrc

from _packagedcode.models import DependentPackage
from _packagedcode.pypi import PipRequirementsFileHandler
from _packagedcode.pypi import can_process_dependent_package
from python_inspector import dependencies
from python_inspector import utils
Expand Down Expand Up @@ -183,15 +185,45 @@ def resolve_dependencies(
if verbose:
click.secho(f"Resolving dependencies...")

netrc = None
if netrc_file:
if not os.path.exists(netrc_file):
raise Exception(f"Missing netrc file {netrc_file}")

if not netrc_file:
netrc_file = os.path.join(os.path.expanduser("~"), ".netrc")
if not os.path.exists(netrc_file):
netrc_file = os.path.join(os.path.expanduser("~"), "_netrc")
if not os.path.exists(netrc_file):
netrc_file = None

if netrc_file:
if verbose:
click.secho(f"Using netrc file {netrc_file}")
netrc = Netrc(file=netrc_file)
else:
netrc = None

# TODO: deduplicate me
direct_dependencies = []

if PYPI_SIMPLE_URL not in index_urls:
index_urls = tuple([PYPI_SIMPLE_URL]) + tuple(index_urls)

invalid_requirement_files = []

for req_file in requirement_files:
if not PipRequirementsFileHandler.is_datafile(location=req_file):
invalid_requirement_files.append(req_file)

if invalid_requirement_files:
invalid_requirement_files = "\n".join(invalid_requirement_files)
click.secho(
"The following requirement files are not valid pip "
f"requirement file names: \n{invalid_requirement_files}",
err=True,
)
ctx.exit(1)

for req_file in requirement_files:
deps = dependencies.get_dependencies_from_requirements(requirements_file=req_file)
for extra_data in dependencies.get_extra_data_from_requirements(requirements_file=req_file):
Expand Down
2 changes: 1 addition & 1 deletion tests/data/frozen-requirements-pdt-expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@
{
"key": "pip",
"package_name": "pip",
"installed_version": "22.2.1",
"installed_version": "22.2.2",
"dependencies": []
}
]
Expand Down
File renamed without changes.
6 changes: 3 additions & 3 deletions tests/data/single-url-except-simple-expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"sdist_url": "https://files.pythonhosted.org/packages/59/87/84326af34517fca8c58418d148f2403df25303e02736832403587318e9e8/click-8.1.3.tar.gz"
},
{
"package": "pkg:pypi/flask@2.1.3",
"package": "pkg:pypi/flask@2.2.1",
"dependencies": [
"pkg:pypi/click@8.1.3",
"pkg:pypi/importlib-metadata@4.12.0",
Expand All @@ -46,9 +46,9 @@
"pkg:pypi/werkzeug@2.2.1"
],
"wheel_urls": [
"https://files.pythonhosted.org/packages/af/6a/00d144ac1626fbb44c4ff36519712e258128985a5d0ae43344778ae5cbb9/Flask-2.1.3-py3-none-any.whl"
"https://files.pythonhosted.org/packages/3c/96/6c896f80f466b7f5e2cfd6d632fe5b0464dcb412757c595a663e59589a93/Flask-2.2.1-py3-none-any.whl"
],
"sdist_url": "https://files.pythonhosted.org/packages/5b/77/3accd62b8771954e9584beb03f080385b32ddcad30009d2a4fe4068a05d9/Flask-2.1.3.tar.gz"
"sdist_url": "https://files.pythonhosted.org/packages/4b/4f/50888944490f2263bd70171e8298c9626675fd3dfd750694a7beaa3484fb/Flask-2.2.1.tar.gz"
},
{
"package": "pkg:pypi/importlib-metadata@4.12.0",
Expand Down
20 changes: 19 additions & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def test_cli_with_default_urls():

@pytest.mark.online
def test_pdt_output():
requirements_file = test_env.get_test_loc("pdt.txt")
requirements_file = test_env.get_test_loc("pdt-requirements.txt")
expected_file = test_env.get_test_loc("pdt-expected.json", must_exist=False)
extra_options = []
check_requirements_resolution(
Expand Down Expand Up @@ -212,6 +212,24 @@ def test_passing_of_json_pdt_and_json_flags():
run_cli(options=options, expected_rc=1)


def test_passing_of_netrc_file_that_does_not_exist():
options = ["--specifier", "foo", "--netrc", "bar.txt", "--json", "-"]
run_cli(options=options, expected_rc=2)


def test_passing_of_wrong_requiremts_file():
test_file = test_env.get_temp_file(file_name="pdt.txt", extension="")
with open(test_file, "w") as f:
f.write("")
test_file_2 = test_env.get_temp_file(file_name="setup.py", extension="")
with open(test_file_2, "w") as f:
f.write("")
options = ["--requirement", test_file, "--json", "-", "--requirement", test_file_2]
result = run_cli(options=options, expected_rc=1)
assert "pdt.txt" in result.output
assert "setup.py" in result.output


def test_passing_of_no_json_output_flag():
options = ["--specifier", "foo"]
run_cli(options=options, expected_rc=1)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_resolution.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def test_without_supported_wheels():
"pkg:pypi/hyperlink@21.0.0",
"pkg:pypi/idna@3.3",
"pkg:pypi/pycparser@2.21",
"pkg:pypi/setuptools@63.3.0",
"pkg:pypi/setuptools@63.4.1",
"pkg:pypi/txaio@22.2.1",
]

Expand Down