1#!/usr/bin/env python3 2# Copyright 2020 Google LLC 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# https://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15 16"""Proof of concept license checker. 17 18This is only a demonstration. It will be replaced with other tools. 19""" 20 21import argparse 22import codecs 23import json 24from tools import sbom 25 26TOOL = 'https//github.com/bazelbuild/rules_license/tools:write_sbom' 27 28def _load_package_data(package_info): 29 with codecs.open(package_info, encoding='utf-8') as inp: 30 return json.loads(inp.read()) 31 32def main(): 33 parser = argparse.ArgumentParser( 34 description='Demonstraton license compliance checker') 35 36 parser.add_argument('--licenses_info', 37 help='path to JSON file containing all license data') 38 parser.add_argument('--out', default='sbom.out', help='SBOM output') 39 args = parser.parse_args() 40 41 license_data = _load_package_data(args.licenses_info) 42 target = license_data[0] # we assume only one target for the demo 43 44 top_level_target = target['top_level_target'] 45 dependencies = target['dependencies'] 46 # It's not really packages, but this is close proxy for now 47 licenses = target['licenses'] 48 package_infos = target['packages'] 49 50 # These are similar dicts, so merge them by package. This is not 51 # strictly true, as different licenese can appear in the same 52 # package, but it is good enough for demonstrating the sbom. 53 54 all = {x['bazel_package']: x for x in licenses} 55 for pi in package_infos: 56 p = all.get(pi['bazel_package']) 57 if p: 58 p.update(pi) 59 else: 60 all[pi['bazel_package']] = pi 61 62 with codecs.open(args.out, mode='w', encoding='utf-8') as out: 63 sbom_writer = sbom.SBOMWriter(TOOL, out) 64 sbom_writer.write_header(package=top_level_target) 65 sbom_writer.write_packages(packages=all.values()) 66 67 68if __name__ == '__main__': 69 main() 70