Skip to content

nashif/scancode-action

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 

Repository files navigation

#!/usr/bin/env python3

# Copyright (c) 2018 Intel Corporation
# SPDX-License-Identifier: Apache-2.0

import argparse
import sys
import json
import yaml

def analyze_file(config_file, scancode_file, scanned_files_dir):

    with open(config_file, 'r') as f:
        config = yaml.safe_load(f.read())

    report = ""
    exclude = config.get("exclude")
    if exclude:
        never_check_ext =  exclude.get("extensions", [])
        never_check_langs = exclude.get("langs", [])
    else:
        never_check_ext = []
        never_check_langs = []

    lic_config = config.get("license")
    lic_main = lic_config.get("main")
    lic_cat = lic_config.get("category")

    check_langs = ['CMake']
    with open(scancode_file, 'r') as json_fp:
        scancode_results = json.load(json_fp)
        for file in scancode_results['files']:
            if file['type'] == 'directory':
                continue

            orig_path = str(file['path']).replace(scanned_files_dir, '')
            licenses = file['licenses']
            file_type = file.get("file_type")
            kconfig = "Kconfig" in orig_path and file_type in ['ASCII text']
            check = False

            if file.get("extension")[1:] in never_check_ext:
                check = False
            elif file.get("programming_language") in never_check_langs:
                check = False
            elif kconfig:
                check = True
            elif file.get("programming_language") in check_langs:
                check = True
            elif file.get("is_script"):
                check = True
            elif file.get("is_source"):
                check = True

            if check:
                if not licenses:
                    report += ("* {} missing license.\n".format(orig_path))
                else:
                    for lic in licenses:
                        if lic['key'] != lic_main:
                            report += ("* {} is not {} licensed: {}\n".format(
                                orig_path, lic_main, lic['key']))
                        if lic['category'] != lic_cat:
                            report += ("* {} has non-permissive license: {}\n".format(
                                orig_path, lic['key']))
                        if lic['key'] == 'unknown-spdx':
                            report += ("* {} has unknown SPDX: {}\n".format(
                                orig_path, lic['key']))

                if not file['copyrights'] and file.get("programming_language") != 'CMake':
                    report += ("* {} missing copyright.\n".format(orig_path))


    return(report)


def parse_args():
    parser = argparse.ArgumentParser(
        description="Analyze licenses...")
    parser.add_argument('-s', '--scancode-output',
                        help='''JSON output from scancode-toolkit''')
    parser.add_argument('-f', '--scanned-files',
                        help="Directory with scanned files")
    parser.add_argument('-c', '--config-file',
                        help="Configuration file")
    parser.add_argument('-o', '--output-file',
                        help="Output report file")
    return parser.parse_args()

if __name__ == "__main__":

    args = parse_args()


    if args.scancode_output and args.scanned_files and args.config_file:
        report = analyze_file(args.config_file, args.scancode_output, args.scanned_files)
        if report:
            with open(args.output_file, "w") as fp:
                fp.write(report)
    else:
        sys.exit("Provide files to analyze")

About

No description, website, or topics provided.

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published