@@ -550,38 +550,60 @@ def parse_command_args():
550550 parser .add_argument ("--no-spdx-validate" , dest = 'spdx_validate' , action = 'store_false' , help = "Disable SPDX validation" )
551551 return parser .parse_args ()
552552
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 ():
554556 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-
563557 with open (args .token_file , 'r' ) as tf :
564558 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 ):
565578
566579 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 )
568589
569590 # Validate project/version details
570- project , version = get_proj_ver (args . project_name , args . version_name )
591+ project , version = get_proj_ver (projname , vername )
571592 proj_version_url = version ['_meta' ]['href' ]
572593
573594 # Upload the provided SBOM
574- upload_sbom_file (args . spdx_file , args . project_name , args . version_name )
595+ upload_sbom_file (spdxfile , projname , vername )
575596
576597 # Wait for scan completion. Will exit if it fails.
577598 poll_for_sbom_complete (document .creation_info .name , proj_version_url )
578599
579600 # Open unmatched component file to save name, spdxid, version, and
580601 # 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 )
585607
586608 # Stats to track
587609 bom_matches = 0
@@ -697,13 +719,13 @@ def main():
697719 # Custom component did not exist, so create it
698720 cust_comp_count += 1
699721 comp_ver_url = create_cust_comp (package .name , package .version ,
700- args . license_name )
722+ license_name )
701723 elif comp_url and not comp_ver_url :
702724 # Custom component existed, but not the version we care about
703725 cust_ver_count += 1
704726 print (f" Adding version { package .version } to custom component { package .name } " )
705727 comp_ver_url = create_cust_comp_ver (comp_url , package .version , \
706- args . license_name )
728+ license_name )
707729 else :
708730 print (" Custom component already exists, not in SBOM" )
709731
@@ -714,8 +736,9 @@ def main():
714736 add_to_sbom (proj_version_url , comp_ver_url )
715737
716738 # Save unmatched components
717- json .dump (comps_out , outfile )
718- outfile .close ()
739+ if outfile :
740+ json .dump (comps_out , outfile )
741+ outfile .close ()
719742
720743 print ("\n Stats: " )
721744 print ("------" )
@@ -731,4 +754,4 @@ def main():
731754 print (f" { len (packages )} unique packages processed" )
732755
733756if __name__ == "__main__" :
734- sys .exit (main ())
757+ sys .exit (spdx_main_parse_args ())
0 commit comments