@@ -550,38 +550,60 @@ def parse_command_args():
550
550
parser .add_argument ("--no-spdx-validate" , dest = 'spdx_validate' , action = 'store_false' , help = "Disable SPDX validation" )
551
551
return parser .parse_args ()
552
552
553
- def main ():
553
+ # Stub to support invocation as a standalone script
554
+ # Parses the command-line args, creates a BD object, and inokes import_sbom
555
+ def spdx_main_parse_args ():
554
556
args = parse_command_args ()
555
- if (Path (args .spdx_file ).is_file ()):
556
- document = spdx_parse (args .spdx_file )
557
- if (args .spdx_validate ):
558
- spdx_validate (document )
559
- else :
560
- logging .error (f"Could not open SPDX file: { args .spdx_file } " )
561
- sys .exit (1 )
562
-
563
557
with open (args .token_file , 'r' ) as tf :
564
558
access_token = tf .readline ().strip ()
559
+ bdobj = Client (base_url = args .base_url , token = access_token , verify = args .verify )
560
+ import_sbom (bdobj , args .project_name , args .version_name , args .spdx_file , \
561
+ args .out_file , args .license_name , args .spdx_validate )
562
+
563
+ # Main entry point
564
+ #
565
+ # Inputs:
566
+ # bdobj - BD Client Object
567
+ # projname - Name of project
568
+ # vername - Name of version
569
+ # spdxfile - SPDX file location
570
+ # outfile (Optional) - Name of file to write missing component data to in JSON.
571
+ # Default: No file written
572
+ # license_name - Name of license to use for custom components
573
+ # Default: NOASSERTION
574
+ # do_spdx_validate - Validate the SPDX file? (Boolean)
575
+ # Default: True
576
+ def import_sbom (bdobj , projname , vername , spdxfile , outfile = None , \
577
+ license_name = "NOASSERTION" , do_spdx_validate = True ):
565
578
566
579
global bd
567
- bd = Client (base_url = args .base_url , token = access_token , verify = args .verify )
580
+ bd = bdobj
581
+
582
+ if (Path (spdxfile ).is_file ()):
583
+ document = spdx_parse (spdxfile )
584
+ if (do_spdx_validate ):
585
+ spdx_validate (document )
586
+ else :
587
+ logging .error (f"Could not open SPDX file: { spdxfile } " )
588
+ sys .exit (1 )
568
589
569
590
# Validate project/version details
570
- project , version = get_proj_ver (args . project_name , args . version_name )
591
+ project , version = get_proj_ver (projname , vername )
571
592
proj_version_url = version ['_meta' ]['href' ]
572
593
573
594
# Upload the provided SBOM
574
- upload_sbom_file (args . spdx_file , args . project_name , args . version_name )
595
+ upload_sbom_file (spdxfile , projname , vername )
575
596
576
597
# Wait for scan completion. Will exit if it fails.
577
598
poll_for_sbom_complete (document .creation_info .name , proj_version_url )
578
599
579
600
# Open unmatched component file to save name, spdxid, version, and
580
601
# origin/purl for later in json format
581
- try : outfile = open (args .out_file , 'w' )
582
- except :
583
- logging .exception ("Failed to open file for writing: " + args .out_file )
584
- sys .exit (1 )
602
+ if outfile :
603
+ try : outfile = open (outfile , 'w' )
604
+ except :
605
+ logging .exception ("Failed to open file for writing: " + outfile )
606
+ sys .exit (1 )
585
607
586
608
# Stats to track
587
609
bom_matches = 0
@@ -697,13 +719,13 @@ def main():
697
719
# Custom component did not exist, so create it
698
720
cust_comp_count += 1
699
721
comp_ver_url = create_cust_comp (package .name , package .version ,
700
- args . license_name )
722
+ license_name )
701
723
elif comp_url and not comp_ver_url :
702
724
# Custom component existed, but not the version we care about
703
725
cust_ver_count += 1
704
726
print (f" Adding version { package .version } to custom component { package .name } " )
705
727
comp_ver_url = create_cust_comp_ver (comp_url , package .version , \
706
- args . license_name )
728
+ license_name )
707
729
else :
708
730
print (" Custom component already exists, not in SBOM" )
709
731
@@ -714,8 +736,9 @@ def main():
714
736
add_to_sbom (proj_version_url , comp_ver_url )
715
737
716
738
# Save unmatched components
717
- json .dump (comps_out , outfile )
718
- outfile .close ()
739
+ if outfile :
740
+ json .dump (comps_out , outfile )
741
+ outfile .close ()
719
742
720
743
print ("\n Stats: " )
721
744
print ("------" )
@@ -731,4 +754,4 @@ def main():
731
754
print (f" { len (packages )} unique packages processed" )
732
755
733
756
if __name__ == "__main__" :
734
- sys .exit (main ())
757
+ sys .exit (spdx_main_parse_args ())
0 commit comments