77# format
88
99import json
10- import sys
1110import hashlib
11+ import argparse
12+ import os
1213from collections import defaultdict
1314
1415
@@ -32,7 +33,6 @@ def group_by_source_package(data):
3233 "copyrights" : {},
3334 "source_version" : None
3435 })
35-
3636 for artifact in data .get ("artifacts" , []):
3737 metadata = artifact .get ("metadata" , {})
3838 binary = metadata .get ("package" , "unknown" )
@@ -41,40 +41,37 @@ def group_by_source_package(data):
4141 source_version = metadata .get ("sourceVersion" ) or version
4242 grouped [source ]["binaries" ].add (binary )
4343 grouped [source ]["source_version" ] = source_version
44-
4544 for lic in artifact .get ("licenses" , []):
4645 grouped [source ]["licenses" ].add (lic .get ("value" , "unknown" ))
47-
4846 for loc in artifact .get ("locations" , []):
4947 path = loc .get ("path" , "" )
5048 if "copyright" in path :
5149 grouped [source ]["copyrights" ][binary ] = path
52-
5350 return grouped
5451
5552
56- def print_table (grouped ):
53+ def print_table (grouped , rootfs_path ):
5754 print ("source,version,binaries,licenses,copyright_sha256" )
5855 for source , data in grouped .items ():
5956 binaries = " " .join (sorted (data ["binaries" ]))
6057 licenses = " " .join (sorted (data ["licenses" ]))
6158 version = data ["source_version" ] or "unknown"
62-
63- # Compute SHA256 hashes
6459 hashes = set ()
6560 for path in data ["copyrights" ].values ():
66- hashes .add (sha256_of_file (path .lstrip ('/' )))
61+ full_path = os .path .join (rootfs_path , path .lstrip ('/' ))
62+ hashes .add (sha256_of_file (full_path ))
6763 hash_summary = " " .join (sorted (hashes ))
68-
6964 print (f"{ source } ,{ version } ,{ binaries } ,{ licenses } ,{ hash_summary } " )
7065
7166
7267if __name__ == "__main__" :
73- if len (sys .argv ) != 2 :
74- print ("Usage: syft-license-summary.py <syft-json-file>" )
75- sys .exit (1 )
76-
77- syft_file = sys .argv [1 ]
78- syft_data = load_syft_json (syft_file )
68+ parser = argparse .ArgumentParser (
69+ description = "Summarize Syft license data." )
70+ parser .add_argument ("syft_json" , help = "Path to the Syft JSON file" )
71+ parser .add_argument ("--rootfs" , required = True ,
72+ help = "Base path to the root filesystem" )
73+ args = parser .parse_args ()
74+
75+ syft_data = load_syft_json (args .syft_json )
7976 syft_grouped = group_by_source_package (syft_data )
80- print_table (syft_grouped )
77+ print_table (syft_grouped , args . rootfs )
0 commit comments