@@ -207,16 +207,70 @@ def resolve_dependencies(
207
207
208
208
python-inspector --spec "flask==2.1.2" --json -
209
209
"""
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 )
212
232
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 )
213
268
214
269
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 )
217
271
218
272
if verbose :
219
- click . secho ( f "Resolving dependencies..." )
273
+ print_message ( "Resolving dependencies..." , ctx = ctx )
220
274
221
275
if netrc_file :
222
276
if not os .path .exists (netrc_file ):
@@ -231,7 +285,7 @@ def resolve_dependencies(
231
285
232
286
if netrc_file :
233
287
if verbose :
234
- click . secho (f"Using netrc file { netrc_file } " )
288
+ print_message (f"Using netrc file { netrc_file } " , ctx = ctx )
235
289
netrc = Netrc (file = netrc_file )
236
290
else :
237
291
netrc = None
@@ -262,35 +316,33 @@ def resolve_dependencies(
262
316
python_version = get_python_version_from_env_tag (python_version ),
263
317
python_requires = python_requires ,
264
318
):
265
- click . secho (
319
+ raise_error (
266
320
f"Python version { get_python_version_from_env_tag (python_version )} "
267
321
f"is not compatible with setup.py { setup_py_file } "
268
322
f"python_requires { python_requires } " ,
269
- err = True ,
323
+ ctx = ctx ,
270
324
)
271
- ctx .exit (1 )
272
325
273
326
for dep in package_data .dependencies :
274
327
# TODO : we need to handle to all the scopes
275
328
if dep .scope == "install" :
276
329
direct_dependencies .append (dep )
277
330
278
331
if not direct_dependencies :
279
- click .secho ("Error: no requirements requested." )
280
- ctx .exit (1 )
332
+ raise_error ("Error: no requirements requested." , ctx = ctx )
281
333
282
334
if verbose :
283
- click . secho ("direct_dependencies:" )
335
+ print_message ("direct_dependencies:" , ctx = ctx )
284
336
for dep in direct_dependencies :
285
- click . secho (f" { dep } " )
337
+ print_message (f" { dep } " , ctx = ctx )
286
338
287
339
# create a resolution environments
288
340
environment = utils_pypi .Environment .from_pyver_and_os (
289
341
python_version = python_version , operating_system = operating_system
290
342
)
291
343
292
344
if verbose :
293
- click . secho (f"environment: { environment } " )
345
+ print_message (f"environment: { environment } " , ctx = ctx )
294
346
295
347
repos = []
296
348
if not use_pypi_json_api :
@@ -316,9 +368,9 @@ def resolve_dependencies(
316
368
repos .append (repo )
317
369
318
370
if verbose :
319
- click . secho ("repos:" )
371
+ print_message ("repos:" , ctx = ctx )
320
372
for repo in repos :
321
- click . secho (f" { repo } " )
373
+ print_message (f" { repo } " , ctx = ctx )
322
374
323
375
# resolve dependencies proper
324
376
requirements , resolved_dependencies = resolve (
@@ -331,12 +383,20 @@ def resolve_dependencies(
331
383
pdt_output = pdt_output ,
332
384
)
333
385
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 " ]
340
400
341
401
notice = (
342
402
"Dependency tree generated with python-inspector.\n "
@@ -348,12 +408,18 @@ def resolve_dependencies(
348
408
tool_name = "python-inspector" ,
349
409
tool_homepageurl = "https://github.com/nexB/python-inspector" ,
350
410
tool_version = __version__ ,
351
- options = cli_options ,
411
+ options = options ,
352
412
notice = notice ,
353
413
warnings = [],
354
414
errors = [],
355
415
)
356
416
417
+ if not ctx :
418
+ if json_output :
419
+ json_output = open (file = json_output , mode = "w" , encoding = "utf-8" )
420
+ if pdt_output :
421
+ pdt_output = open (file = pdt_output , mode = "w" , encoding = "utf-8" )
422
+
357
423
if json_output :
358
424
write_output (
359
425
headers = headers ,
@@ -372,7 +438,7 @@ def resolve_dependencies(
372
438
)
373
439
374
440
if verbose :
375
- click . secho ("done!" )
441
+ print_message ("done!" , ctx = ctx )
376
442
377
443
378
444
def resolve (
@@ -445,7 +511,6 @@ def write_output(headers, requirements, resolved_dependencies, json_output, pdt_
445
511
)
446
512
else :
447
513
output = resolved_dependencies
448
-
449
514
json .dump (output , json_output , indent = 2 )
450
515
return output
451
516
0 commit comments