3636
3737TRACE = False
3838
39- __version__ = "0.8.1 "
39+ __version__ = "0.9.0 "
4040
4141DEFAULT_PYTHON_VERSION = "38"
4242PYPI_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
446515def 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
0 commit comments