@@ -207,16 +207,70 @@ def resolve_dependencies(
207207
208208 python-inspector --spec "flask==2.1.2" --json -
209209 """
210- if not (json_output or pdt_output ):
211- click .secho ("No output file specified. Use --json or --json-pdt." , err = True )
210+ resolver_api (
211+ requirement_files = requirement_files ,
212+ setup_py_file = setup_py_file ,
213+ specifiers = specifiers ,
214+ python_version = python_version ,
215+ operating_system = operating_system ,
216+ index_urls = index_urls ,
217+ json_output = json_output ,
218+ pdt_output = pdt_output ,
219+ netrc_file = netrc_file ,
220+ max_rounds = max_rounds ,
221+ use_cached_index = use_cached_index ,
222+ use_pypi_json_api = use_pypi_json_api ,
223+ verbose = verbose ,
224+ ctx = ctx ,
225+ )
226+
227+
228+ def raise_error (message , ctx ):
229+ """Raise error."""
230+ if ctx :
231+ click .secho (message , err = True )
212232 ctx .exit (1 )
233+ else :
234+ raise ValueError (message )
235+
236+
237+ def print_message (message , ctx ):
238+ """Print message."""
239+ if ctx :
240+ click .secho (message )
241+ else :
242+ print (message )
243+
244+
245+ def resolver_api (
246+ requirement_files = [],
247+ setup_py_file = None ,
248+ specifiers = [],
249+ python_version = DEFAULT_PYTHON_VERSION ,
250+ operating_system = "linux" ,
251+ index_urls = tuple ([PYPI_SIMPLE_URL ]),
252+ json_output = None ,
253+ pdt_output = None ,
254+ netrc_file = None ,
255+ max_rounds = 200000 ,
256+ use_cached_index = False ,
257+ use_pypi_json_api = False ,
258+ verbose = False ,
259+ ctx = None ,
260+ ):
261+ """
262+ Resolve the dependencies for the package requirements listed in one or
263+ more ``requirement_files``, one or more ``specifiers`` and one setuptools
264+ ``setup_py_file`` file and save the results as JSON to FILE.
265+ """
266+ if not (json_output or pdt_output ):
267+ raise_error ("No output file specified. Use --json or --json-pdt." , ctx = ctx )
213268
214269 if json_output and pdt_output :
215- click .secho ("Only one of --json or --json-pdt can be used." , err = True )
216- ctx .exit (1 )
270+ raise_error ("Only one of --json or --json-pdt can be used." , ctx = ctx )
217271
218272 if verbose :
219- click . secho ( f "Resolving dependencies..." )
273+ print_message ( "Resolving dependencies..." , ctx = ctx )
220274
221275 if netrc_file :
222276 if not os .path .exists (netrc_file ):
@@ -231,7 +285,7 @@ def resolve_dependencies(
231285
232286 if netrc_file :
233287 if verbose :
234- click . secho (f"Using netrc file { netrc_file } " )
288+ print_message (f"Using netrc file { netrc_file } " , ctx = ctx )
235289 netrc = Netrc (file = netrc_file )
236290 else :
237291 netrc = None
@@ -262,35 +316,33 @@ def resolve_dependencies(
262316 python_version = get_python_version_from_env_tag (python_version ),
263317 python_requires = python_requires ,
264318 ):
265- click . secho (
319+ raise_error (
266320 f"Python version { get_python_version_from_env_tag (python_version )} "
267321 f"is not compatible with setup.py { setup_py_file } "
268322 f"python_requires { python_requires } " ,
269- err = True ,
323+ ctx = ctx ,
270324 )
271- ctx .exit (1 )
272325
273326 for dep in package_data .dependencies :
274327 # TODO : we need to handle to all the scopes
275328 if dep .scope == "install" :
276329 direct_dependencies .append (dep )
277330
278331 if not direct_dependencies :
279- click .secho ("Error: no requirements requested." )
280- ctx .exit (1 )
332+ raise_error ("Error: no requirements requested." , ctx = ctx )
281333
282334 if verbose :
283- click . secho ("direct_dependencies:" )
335+ print_message ("direct_dependencies:" , ctx = ctx )
284336 for dep in direct_dependencies :
285- click . secho (f" { dep } " )
337+ print_message (f" { dep } " , ctx = ctx )
286338
287339 # create a resolution environments
288340 environment = utils_pypi .Environment .from_pyver_and_os (
289341 python_version = python_version , operating_system = operating_system
290342 )
291343
292344 if verbose :
293- click . secho (f"environment: { environment } " )
345+ print_message (f"environment: { environment } " , ctx = ctx )
294346
295347 repos = []
296348 if not use_pypi_json_api :
@@ -316,9 +368,9 @@ def resolve_dependencies(
316368 repos .append (repo )
317369
318370 if verbose :
319- click . secho ("repos:" )
371+ print_message ("repos:" , ctx = ctx )
320372 for repo in repos :
321- click . secho (f" { repo } " )
373+ print_message (f" { repo } " , ctx = ctx )
322374
323375 # resolve dependencies proper
324376 requirements , resolved_dependencies = resolve (
@@ -331,12 +383,20 @@ def resolve_dependencies(
331383 pdt_output = pdt_output ,
332384 )
333385
334- cli_options = [f"--requirement { rf } " for rf in requirement_files ]
335- cli_options += [f"--specifier { sp } " for sp in specifiers ]
336- cli_options += [f"--index-url { iu } " for iu in index_urls ]
337- cli_options += [f"--python-version { python_version } " ]
338- cli_options += [f"--operating-system { operating_system } " ]
339- cli_options += ["--json <file>" ]
386+ if ctx :
387+ options = [f"--requirement { rf } " for rf in requirement_files ]
388+ options += [f"--specifier { sp } " for sp in specifiers ]
389+ options += [f"--index-url { iu } " for iu in index_urls ]
390+ options += [f"--python-version { python_version } " ]
391+ options += [f"--operating-system { operating_system } " ]
392+ options += ["--json <file>" ]
393+ else :
394+ options = [f"requirement_files- { rf } " for rf in requirement_files ]
395+ options += [f"specifiers- { sp } " for sp in specifiers ]
396+ options += [f"index_urls- { iu } " for iu in index_urls ]
397+ options += [f"python-version { python_version } " ]
398+ options += [f"operating-system { operating_system } " ]
399+ options += [f"json " ]
340400
341401 notice = (
342402 "Dependency tree generated with python-inspector.\n "
@@ -348,12 +408,18 @@ def resolve_dependencies(
348408 tool_name = "python-inspector" ,
349409 tool_homepageurl = "https://github.com/nexB/python-inspector" ,
350410 tool_version = __version__ ,
351- options = cli_options ,
411+ options = options ,
352412 notice = notice ,
353413 warnings = [],
354414 errors = [],
355415 )
356416
417+ if not ctx :
418+ if json_output :
419+ json_output = open (json_output , "w" )
420+ if pdt_output :
421+ pdt_output = open (pdt_output , "w" )
422+
357423 if json_output :
358424 write_output (
359425 headers = headers ,
@@ -372,7 +438,7 @@ def resolve_dependencies(
372438 )
373439
374440 if verbose :
375- click . secho ("done!" )
441+ print_message ("done!" , ctx = ctx )
376442
377443
378444def resolve (
@@ -445,7 +511,6 @@ def write_output(headers, requirements, resolved_dependencies, json_output, pdt_
445511 )
446512 else :
447513 output = resolved_dependencies
448-
449514 json .dump (output , json_output , indent = 2 )
450515 return output
451516
0 commit comments