Skip to content

Commit fb42cfb

Browse files
committed
Add API function for using cleanly as a library #39
Add metadata support for API Adjust with the latest changes in output Reference: #39 Signed-off-by: Tushar Goel <[email protected]>
1 parent 34e018d commit fb42cfb

21 files changed

+20128
-41
lines changed

src/python_inspector/resolution.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,7 @@ def get_resolved_dependencies(
743743
verbose: bool = False,
744744
pdt_output: bool = False,
745745
analyze_setup_py_insecurely: bool = False,
746+
ctx=None,
746747
):
747748
"""
748749
Return resolved dependencies of a ``requirements`` list of Requirement for
@@ -771,6 +772,9 @@ def get_resolved_dependencies(
771772
)
772773
except Exception as e:
773774
if verbose:
774-
import click
775+
if ctx:
776+
import click
775777

776-
click.secho(f"{e!r}", err=True)
778+
click.secho(f"{e!r}", err=True)
779+
else:
780+
print(f"{e!r}")

src/python_inspector/resolve_cli.py

Lines changed: 96 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636

3737
TRACE = False
3838

39-
__version__ = "0.8.1"
39+
__version__ = "0.9.0"
4040

4141
DEFAULT_PYTHON_VERSION = "38"
4242
PYPI_SIMPLE_URL = "https://pypi.org/simple"
@@ -227,8 +227,67 @@ def resolve_dependencies(
227227
click.secho("Only one of --json or --json-pdt can be used.", err=True)
228228
ctx.exit(1)
229229

230+
resolver_api(
231+
requirement_files=requirement_files,
232+
setup_py_file=setup_py_file,
233+
specifiers=specifiers,
234+
python_version=python_version,
235+
operating_system=operating_system,
236+
index_urls=index_urls,
237+
json_output=json_output,
238+
pdt_output=pdt_output,
239+
netrc_file=netrc_file,
240+
max_rounds=max_rounds,
241+
use_cached_index=use_cached_index,
242+
use_pypi_json_api=use_pypi_json_api,
243+
verbose=verbose,
244+
analyze_setup_py_insecurely=analyze_setup_py_insecurely,
245+
ctx=ctx,
246+
)
247+
248+
249+
def raise_error(message, ctx):
250+
"""Raise error."""
251+
if ctx:
252+
click.secho(message, err=True)
253+
ctx.exit(1)
254+
else:
255+
raise ValueError(message)
256+
257+
258+
def print_message(message, ctx):
259+
"""Print message."""
260+
if ctx:
261+
click.secho(message)
262+
else:
263+
print(message)
264+
265+
266+
def resolver_api(
267+
requirement_files=[],
268+
setup_py_file=None,
269+
specifiers=[],
270+
python_version=DEFAULT_PYTHON_VERSION,
271+
operating_system="linux",
272+
index_urls=tuple([PYPI_SIMPLE_URL]),
273+
json_output=None,
274+
pdt_output=None,
275+
netrc_file=None,
276+
max_rounds=200000,
277+
use_cached_index=False,
278+
use_pypi_json_api=False,
279+
verbose=False,
280+
analyze_setup_py_insecurely=False,
281+
ctx=None,
282+
):
283+
"""
284+
Resolve the dependencies for the package requirements listed in one or
285+
more ``requirement_files``, one or more ``specifiers`` and one setuptools
286+
``setup_py_file`` file and save the results as JSON to FILE.
287+
"""
288+
230289
if verbose:
231-
click.secho(f"Resolving dependencies...")
290+
print_message("Resolving dependencies...", ctx=ctx)
232291

233292
if netrc_file:
234293
if not os.path.exists(netrc_file):
@@ -243,7 +302,7 @@ def resolve_dependencies(
243302

244303
if netrc_file:
245304
if verbose:
246-
click.secho(f"Using netrc file {netrc_file}")
305+
print_message(f"Using netrc file {netrc_file}", ctx=ctx)
247306
netrc = Netrc(file=netrc_file)
248307
else:
249308
netrc = None
@@ -286,13 +345,12 @@ def resolve_dependencies(
286345
python_version=get_python_version_from_env_tag(python_version),
287346
python_requires=python_requires,
288347
):
289-
click.secho(
348+
raise_error(
290349
f"Python version {get_python_version_from_env_tag(python_version)} "
291350
f"is not compatible with setup.py {setup_py_file} "
292351
f"python_requires {python_requires}",
293-
err=True,
352+
ctx=ctx,
294353
)
295-
ctx.exit(1)
296354

297355
setup_py_file_deps = package_data.dependencies
298356
for dep in package_data.dependencies:
@@ -341,21 +399,20 @@ def resolve_dependencies(
341399
)
342400

343401
if not direct_dependencies:
344-
click.secho("Error: no requirements requested.")
345-
ctx.exit(1)
402+
raise_error("Error: no requirements requested.", ctx=ctx)
346403

347404
if verbose:
348-
click.secho("direct_dependencies:")
405+
print_message("direct_dependencies:", ctx=ctx)
349406
for dep in direct_dependencies:
350-
click.secho(f" {dep}")
407+
print_message(f" {dep}", ctx=ctx)
351408

352409
# create a resolution environments
353410
environment = utils_pypi.Environment.from_pyver_and_os(
354411
python_version=python_version, operating_system=operating_system
355412
)
356413

357414
if verbose:
358-
click.secho(f"environment: {environment}")
415+
print_message(f"environment: {environment}", ctx=ctx)
359416

360417
repos = []
361418
if not use_pypi_json_api:
@@ -381,9 +438,9 @@ def resolve_dependencies(
381438
repos.append(repo)
382439

383440
if verbose:
384-
click.secho("repos:")
441+
print_message("repos:", ctx=ctx)
385442
for repo in repos:
386-
click.secho(f" {repo}")
443+
print_message(f" {repo}", ctx=ctx)
387444

388445
# resolve dependencies proper
389446
resolved_dependencies, purls = resolve(
@@ -395,14 +452,23 @@ def resolve_dependencies(
395452
verbose=verbose,
396453
pdt_output=pdt_output,
397454
analyze_setup_py_insecurely=analyze_setup_py_insecurely,
455+
ctx=ctx,
398456
)
399457

400-
cli_options = [f"--requirement {rf}" for rf in requirement_files]
401-
cli_options += [f"--specifier {sp}" for sp in specifiers]
402-
cli_options += [f"--index-url {iu}" for iu in index_urls]
403-
cli_options += [f"--python-version {python_version}"]
404-
cli_options += [f"--operating-system {operating_system}"]
405-
cli_options += ["--json <file>"]
458+
if ctx:
459+
options = [f"--requirement {rf}" for rf in requirement_files]
460+
options += [f"--specifier {sp}" for sp in specifiers]
461+
options += [f"--index-url {iu}" for iu in index_urls]
462+
options += [f"--python-version {python_version}"]
463+
options += [f"--operating-system {operating_system}"]
464+
options += ["--json <file>"]
465+
else:
466+
options = [f"requirement_files- {rf}" for rf in requirement_files]
467+
options += [f"specifiers- {sp}" for sp in specifiers]
468+
options += [f"index_urls- {iu}" for iu in index_urls]
469+
options += [f"python-version {python_version}"]
470+
options += [f"operating-system {operating_system}"]
471+
options += [f"json "]
406472

407473
notice = (
408474
"Dependency tree generated with python-inspector.\n"
@@ -414,7 +480,7 @@ def resolve_dependencies(
414480
tool_name="python-inspector",
415481
tool_homepageurl="https://github.com/nexB/python-inspector",
416482
tool_version=__version__,
417-
options=cli_options,
483+
options=options,
418484
notice=notice,
419485
warnings=[],
420486
errors=[],
@@ -434,13 +500,16 @@ def resolve_dependencies(
434500
packages=packages,
435501
)
436502

437-
write_output(
438-
json_output=json_output or pdt_output,
439-
output=output,
440-
)
503+
if ctx:
504+
write_output(
505+
json_output=json_output or pdt_output,
506+
output=output,
507+
)
508+
else:
509+
return output
441510

442511
if verbose:
443-
click.secho("done!")
512+
print_message("done!", ctx=ctx)
444513

445514

446515
def resolve(
@@ -452,6 +521,7 @@ def resolve(
452521
verbose=False,
453522
pdt_output=False,
454523
analyze_setup_py_insecurely=False,
524+
ctx=None,
455525
):
456526
"""
457527
Resolve dependencies given a ``direct_dependencies`` list of
@@ -478,6 +548,7 @@ def resolve(
478548
verbose=verbose,
479549
pdt_output=pdt_output,
480550
analyze_setup_py_insecurely=analyze_setup_py_insecurely,
551+
ctx=ctx,
481552
)
482553

483554
return resolved_dependencies, packages

tests/data/default-url-expected.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"headers": {
33
"tool_name": "python-inspector",
44
"tool_homepageurl": "https://github.com/nexB/python-inspector",
5-
"tool_version": "0.8.1",
5+
"tool_version": "0.9.0",
66
"options": [
77
"--specifier zipp==3.8.0",
88
"--index-url https://pypi.org/simple",

tests/data/environment-marker-test-requirements.txt-expected.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"headers": {
33
"tool_name": "python-inspector",
44
"tool_homepageurl": "https://github.com/nexB/python-inspector",
5-
"tool_version": "0.8.1",
5+
"tool_version": "0.9.0",
66
"options": [
77
"--requirement /home/tg1999/Desktop/python-inspector-1/tests/data/environment-marker-test-requirements.txt",
88
"--index-url https://pypi.org/simple",

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"headers": {
33
"tool_name": "python-inspector",
44
"tool_homepageurl": "https://github.com/nexB/python-inspector",
5-
"tool_version": "0.8.1",
5+
"tool_version": "0.9.0",
66
"options": [
77
"--requirement /home/tg1999/Desktop/python-inspector-1/tests/data/frozen-requirements.txt",
88
"--index-url https://pypi.org/simple",

tests/data/insecure-setup-2/setup.py-expected.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"headers": {
33
"tool_name": "python-inspector",
44
"tool_homepageurl": "https://github.com/nexB/python-inspector",
5-
"tool_version": "0.8.1",
5+
"tool_version": "0.9.0",
66
"options": [
77
"--index-url https://pypi.org/simple",
88
"--python-version 27",

tests/data/insecure-setup/setup.py-expected.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"headers": {
33
"tool_name": "python-inspector",
44
"tool_homepageurl": "https://github.com/nexB/python-inspector",
5-
"tool_version": "0.8.1",
5+
"tool_version": "0.9.0",
66
"options": [
77
"--index-url https://pypi.org/simple",
88
"--python-version 27",

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"headers": {
33
"tool_name": "python-inspector",
44
"tool_homepageurl": "https://github.com/nexB/python-inspector",
5-
"tool_version": "0.8.1",
5+
"tool_version": "0.9.0",
66
"options": [
77
"--requirement /home/tg1999/Desktop/python-inspector-1/tests/data/pdt-requirements.txt",
88
"--index-url https://pypi.org/simple",

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"headers": {
33
"tool_name": "python-inspector",
44
"tool_homepageurl": "https://github.com/nexB/python-inspector",
5-
"tool_version": "0.8.1",
5+
"tool_version": "0.9.0",
66
"options": [
77
"--requirement /home/tg1999/Desktop/python-inspector-1/tests/data/pinned-pdt-requirements.txt",
88
"--index-url https://pypi.org/simple",

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"headers": {
33
"tool_name": "python-inspector",
44
"tool_homepageurl": "https://github.com/nexB/python-inspector",
5-
"tool_version": "0.8.1",
5+
"tool_version": "0.9.0",
66
"options": [
77
"--requirement /home/tg1999/Desktop/python-inspector-1/tests/data/pinned-requirements.txt",
88
"--index-url https://pypi.org/simple",

0 commit comments

Comments
 (0)