|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# Copyright (c) 2020 LG Electronics Inc. |
| 4 | +# SPDX-License-Identifier: Apache-2.0 |
| 5 | + |
| 6 | +import os |
| 7 | +import logging |
| 8 | +import fosslight_util.constant as constant |
| 9 | + |
| 10 | +logger = logging.getLogger(constant.LOGGER_NAME) |
| 11 | +replace_word = ["-only", "-old-style", "-or-later", "licenseref-scancode-"] |
| 12 | +_exclude_filename = ["changelog", "config.guess", "config.sub", |
| 13 | + "config.h.in", "changes", "ltmain.sh", |
| 14 | + "aclocal.m4", "configure", "configure.ac", |
| 15 | + "depcomp", "compile", "missing", "libtool.m4", |
| 16 | + "makefile"] |
| 17 | +_exclude_directory = ["test", "tests", "doc", "docs"] |
| 18 | +_exclude_directory = [os.path.sep + dir_name + |
| 19 | + os.path.sep for dir_name in _exclude_directory] |
| 20 | +_exclude_directory.append("/.") |
| 21 | + |
| 22 | + |
| 23 | +class ScanItem: |
| 24 | + file = "" |
| 25 | + licenses = [] |
| 26 | + copyright = "" |
| 27 | + exclude = False |
| 28 | + is_license_text = False |
| 29 | + oss_name = "" |
| 30 | + oss_version = "" |
| 31 | + download_location = "" |
| 32 | + |
| 33 | + def __init__(self, value): |
| 34 | + self.file = value |
| 35 | + self.copyright = [] |
| 36 | + self.licenses = [] |
| 37 | + self.comment = "" |
| 38 | + self.exclude = False |
| 39 | + self.is_license_text = False |
| 40 | + |
| 41 | + def __del__(self): |
| 42 | + pass |
| 43 | + |
| 44 | + def set_comment(self, value): |
| 45 | + self.comment = value |
| 46 | + |
| 47 | + def set_file(self, value): |
| 48 | + self.file = value |
| 49 | + |
| 50 | + def set_copyright(self, value): |
| 51 | + self.copyright.extend(value) |
| 52 | + if len(self.copyright) > 0: |
| 53 | + self.copyright = list(set(self.copyright)) |
| 54 | + |
| 55 | + def set_licenses(self, value): |
| 56 | + self.licenses.extend(value) |
| 57 | + if len(self.licenses) > 0: |
| 58 | + self.licenses = list(set(self.licenses)) |
| 59 | + |
| 60 | + def set_exclude(self, value): |
| 61 | + self.exclude = value |
| 62 | + |
| 63 | + def set_is_license_text(self, value): |
| 64 | + self.is_license_text = value |
| 65 | + |
| 66 | + def set_oss_name(self, value): |
| 67 | + self.oss_name = value |
| 68 | + |
| 69 | + def set_oss_version(self, value): |
| 70 | + self.oss_version = value |
| 71 | + |
| 72 | + def set_download_location(self, value): |
| 73 | + self.download_location = value |
| 74 | + |
| 75 | + def get_row_to_print(self): |
| 76 | + if not self.download_location: |
| 77 | + print_rows = [self.file, "", "", ','.join(self.licenses), "", "", |
| 78 | + ','.join(self.copyright), |
| 79 | + "Exclude" if self.exclude else "", |
| 80 | + self.comment] |
| 81 | + else: |
| 82 | + print_rows = [self.file, self.oss_name, self.oss_version, ','.join(self.licenses), self.download_location, "", |
| 83 | + ','.join(self.copyright), |
| 84 | + "Exclude" if self.exclude else "", |
| 85 | + self.comment] |
| 86 | + return print_rows |
| 87 | + |
| 88 | + def merge_scan_item(self, other): |
| 89 | + """ |
| 90 | + Merge two ScanItem instance into one. |
| 91 | +
|
| 92 | + TODO: define how to merge comments and implement. |
| 93 | + """ |
| 94 | + self.licenses = list(set(self.licenses + other.licenses)) |
| 95 | + |
| 96 | + if len(self.copyright) > 0: |
| 97 | + self.copyright = list(set(self.copyright)) |
| 98 | + |
| 99 | + if self.exclude and other.exclude: |
| 100 | + self.exclude = True |
| 101 | + else: |
| 102 | + self.exclude = False |
| 103 | + |
| 104 | + if not self.oss_name: |
| 105 | + self.oss_name = other.oss_name |
| 106 | + if not self.oss_version: |
| 107 | + self.oss_version = other.oss_version |
| 108 | + if not self.download_location: |
| 109 | + self.download_location = other.download_location |
| 110 | + |
| 111 | + def __eq__(self, other): |
| 112 | + return self.file == other.file |
| 113 | + |
| 114 | + |
| 115 | +def is_exclude_dir(dir_path): |
| 116 | + if dir_path != "": |
| 117 | + dir_path = dir_path.lower() |
| 118 | + dir_path = dir_path if dir_path.endswith( |
| 119 | + os.path.sep) else dir_path + os.path.sep |
| 120 | + dir_path = dir_path if dir_path.startswith( |
| 121 | + os.path.sep) else os.path.sep + dir_path |
| 122 | + return any(dir_name in dir_path for dir_name in _exclude_directory) |
| 123 | + return False |
| 124 | + |
| 125 | + |
| 126 | +def is_exclude_file(file_path, prev_dir=None, prev_dir_exclude_value=None): |
| 127 | + file_path = file_path.lower() |
| 128 | + filename = os.path.basename(file_path) |
| 129 | + if filename in _exclude_filename: |
| 130 | + return True |
| 131 | + |
| 132 | + dir_path = os.path.dirname(file_path) |
| 133 | + if prev_dir is not None: # running ScanCode |
| 134 | + if dir_path == prev_dir: |
| 135 | + return prev_dir_exclude_value |
| 136 | + else: |
| 137 | + # There will be no execution of this else statement. |
| 138 | + # Because scancode json output results are sorted by path, |
| 139 | + # most of them will match the previous if statement. |
| 140 | + return is_exclude_dir(dir_path) |
| 141 | + else: # running SCANOSS |
| 142 | + return is_exclude_dir(dir_path) |
| 143 | + return False |
0 commit comments