yang.li | 8c78ed6 | 2022-03-08 13:50:52 +0800 | [diff] [blame] | 1 | #!/usr/bin/python3 |
| 2 | #coding:utf-8 |
| 3 | # |
| 4 | # Copyright (c) 2021-2022 Amlogic, Inc. All rights reserved. |
| 5 | # |
| 6 | # SPDX-License-Identifier: MIT |
| 7 | # |
| 8 | |
| 9 | import os |
| 10 | import sys |
yang.li | ba81c32 | 2022-04-25 17:13:37 +0800 | [diff] [blame] | 11 | import getopt |
yang.li | 8c78ed6 | 2022-03-08 13:50:52 +0800 | [diff] [blame] | 12 | import string |
| 13 | import re |
| 14 | |
yang.li | ba81c32 | 2022-04-25 17:13:37 +0800 | [diff] [blame] | 15 | def usage(): |
| 16 | """ |
| 17 | The script is used to check RTOS_SDK public license, |
| 18 | Usage: ./scripts/check_license.py [option] |
| 19 | |
| 20 | Example: ./scripts/module_size_report.py -p patch.diff |
| 21 | |
| 22 | Description |
| 23 | -h --help display help information |
| 24 | -d <directory> directory of checking license |
| 25 | -p <patch file> patch file of checking license |
| 26 | -v --version version information |
| 27 | """ |
| 28 | |
yang.li | 8c78ed6 | 2022-03-08 13:50:52 +0800 | [diff] [blame] | 29 | #file type contains .[chS]/.ld |
| 30 | def src_file_check_license(line): |
| 31 | license =\ |
| 32 | '''/* |
| 33 | * Copyright (c) 2021-2022 Amlogic, Inc. All rights reserved. |
| 34 | * |
| 35 | * SPDX-License-Identifier: MIT |
| 36 | */\n |
| 37 | ''' |
| 38 | if(line.find('\r\n') > 0): |
| 39 | line_new = re.sub("\r\n", "\n", line) |
| 40 | return line_new.find(license) |
| 41 | else: |
| 42 | return line.find(license) |
| 43 | |
| 44 | #file type contains Script/defconfig/prj.conf |
| 45 | def script_file_check_license(line): |
| 46 | license =\ |
| 47 | '''# |
| 48 | # Copyright (c) 2021-2022 Amlogic, Inc. All rights reserved. |
| 49 | # |
| 50 | # SPDX-License-Identifier: MIT |
| 51 | #\n |
| 52 | ''' |
| 53 | if(line.find('\r\n') > 0): |
| 54 | line_new = re.sub("\r\n", "\n", line) |
| 55 | return line_new.find(license) |
| 56 | else: |
| 57 | return line.find(license) |
| 58 | |
| 59 | #file type contains Kconfig/CMakeLists.txt |
| 60 | def cfg_file_check_license(line): |
| 61 | license =\ |
| 62 | '''# Copyright (c) 2021-2022 Amlogic, Inc. All rights reserved. |
| 63 | |
| 64 | # SPDX-License-Identifier: MIT\n |
| 65 | ''' |
| 66 | if(line.find('\r\n') > 0): |
| 67 | line_new = re.sub("\r\n", "\n", line) |
| 68 | return line_new.find(license) |
| 69 | else: |
| 70 | return line.find(license) |
| 71 | |
yang.li | ba81c32 | 2022-04-25 17:13:37 +0800 | [diff] [blame] | 72 | #checking patch file |
| 73 | def check_patch(patch_file): |
| 74 | ret_val = 0 |
| 75 | if os.path.exists(patch_file): |
| 76 | file = open(patch_file, "r", errors='ignore', encoding='utf-8', newline='') |
| 77 | try: |
| 78 | lines = file.readlines() |
| 79 | except: |
| 80 | print("read patch exception!!") |
| 81 | |
| 82 | src_type = ['.c','.h','.S', '.ld'] |
| 83 | cfg_type = ['Kconfig', 'CMakeLists.txt', '.cmake'] |
| 84 | script_type = ['.sh', '.py', '.conf', 'defconfig'] |
| 85 | |
| 86 | new_file_line=[s for s in lines if 'diff --git' in s] |
| 87 | for ch_file in new_file_line: |
| 88 | line_index=lines.index(ch_file) |
yang.li | 8c78ed6 | 2022-03-08 13:50:52 +0800 | [diff] [blame] | 89 | #diff start of file, and get the file name |
yang.li | ba81c32 | 2022-04-25 17:13:37 +0800 | [diff] [blame] | 90 | line=lines[line_index] |
yang.li | 8c78ed6 | 2022-03-08 13:50:52 +0800 | [diff] [blame] | 91 | file = line.split(' ')[-1].strip() |
yang.li | 1092d00 | 2022-04-26 16:12:45 +0800 | [diff] [blame] | 92 | file = file.split('/')[-1] |
yang.li | 8c78ed6 | 2022-03-08 13:50:52 +0800 | [diff] [blame] | 93 | patch_str = '' |
yang.li | ba81c32 | 2022-04-25 17:13:37 +0800 | [diff] [blame] | 94 | |
| 95 | if 'new file mode' in lines[line_index+1]: |
yang.li | 8c78ed6 | 2022-03-08 13:50:52 +0800 | [diff] [blame] | 96 | #new file in patch, and save the patch string |
yang.li | ba81c32 | 2022-04-25 17:13:37 +0800 | [diff] [blame] | 97 | for line in lines[line_index+2:]: |
| 98 | line_index+=1 |
| 99 | if 'diff --git' in line: |
| 100 | break |
| 101 | patch_str += line[1:] |
yang.li | 8c78ed6 | 2022-03-08 13:50:52 +0800 | [diff] [blame] | 102 | |
| 103 | if file.split('/')[-1] in cfg_type: |
| 104 | ret = cfg_file_check_license(patch_str) |
| 105 | if ret < 0: |
| 106 | print(file + ' license error\r\n') |
yang.li | ba81c32 | 2022-04-25 17:13:37 +0800 | [diff] [blame] | 107 | ret_val = 1 |
yang.li | 8c78ed6 | 2022-03-08 13:50:52 +0800 | [diff] [blame] | 108 | continue |
| 109 | |
| 110 | if os.path.splitext(file)[-1] in src_type: |
| 111 | ret = src_file_check_license(patch_str) |
| 112 | if ret < 0: |
| 113 | print(file + ' license error\r\n') |
yang.li | ba81c32 | 2022-04-25 17:13:37 +0800 | [diff] [blame] | 114 | ret_val = 1 |
yang.li | 8c78ed6 | 2022-03-08 13:50:52 +0800 | [diff] [blame] | 115 | continue |
| 116 | |
| 117 | if os.path.splitext(file)[-1] in script_type: |
| 118 | ret = script_file_check_license(patch_str) |
| 119 | if ret < 0: |
| 120 | print(file + ' license error\r\n') |
yang.li | ba81c32 | 2022-04-25 17:13:37 +0800 | [diff] [blame] | 121 | ret_val = 1 |
yang.li | 8c78ed6 | 2022-03-08 13:50:52 +0800 | [diff] [blame] | 122 | continue |
yang.li | ba81c32 | 2022-04-25 17:13:37 +0800 | [diff] [blame] | 123 | else: |
| 124 | print("Not Found Patch file:"+patch_file) |
| 125 | ret_val=2 |
yang.li | 8c78ed6 | 2022-03-08 13:50:52 +0800 | [diff] [blame] | 126 | |
yang.li | ba81c32 | 2022-04-25 17:13:37 +0800 | [diff] [blame] | 127 | return ret_val |
| 128 | |
| 129 | #checking directory |
| 130 | def check_dir(directory): |
| 131 | ret_val=0 |
| 132 | if os.path.exists(directory): |
| 133 | list = os.walk(directory, True) |
| 134 | for dir in list: |
| 135 | files = dir[2] |
| 136 | for file in files: |
| 137 | filepath = os.path.join(dir[0], file) |
| 138 | f = open(filepath, "r", errors='ignore', encoding='utf-8', newline='') |
| 139 | try: |
| 140 | lines = f.read() |
| 141 | except: |
| 142 | print("read patch exception!!") |
| 143 | |
| 144 | src_type = ['.c','.h','.S', '.ld'] |
| 145 | cfg_type = ['Kconfig', 'CMakeLists.txt', '.cmake'] |
| 146 | script_type = ['.sh', '.py', '.conf', 'defconfig'] |
| 147 | |
| 148 | if filepath.split('/')[-1] in cfg_type: |
| 149 | ret = cfg_file_check_license(lines) |
| 150 | if ret < 0: |
| 151 | print(filepath + ' license error\r\n') |
| 152 | ret_val = 1 |
| 153 | continue |
| 154 | |
| 155 | if os.path.splitext(filepath)[-1] in src_type: |
| 156 | ret = src_file_check_license(lines) |
| 157 | if ret < 0: |
| 158 | print(filepath + ' license error\r\n') |
| 159 | ret_val = 1 |
| 160 | continue |
| 161 | |
| 162 | if os.path.splitext(filepath)[-1] in script_type: |
| 163 | ret = script_file_check_license(lines) |
| 164 | if ret < 0: |
| 165 | print(filepath + ' license error\r\n') |
| 166 | ret_val = 1 |
| 167 | continue |
| 168 | else: |
| 169 | print("Not Found dirctory:"+directory) |
| 170 | ret_val=2 |
| 171 | |
| 172 | return ret_val |
| 173 | |
| 174 | if __name__ == '__main__': |
| 175 | check_ret=0 |
| 176 | VERSION="20220425" |
| 177 | try: |
| 178 | opts, args = getopt.getopt(sys.argv[1:], "p:d:hv", ["help","version"]) |
| 179 | except getopt.GetoptError as err: |
| 180 | print(err) |
| 181 | print(usage.__doc__) |
| 182 | sys.exit(2) |
| 183 | if len(opts) == 0: |
| 184 | print(usage.__doc__) |
| 185 | sys.exit(2) |
| 186 | for opt, arg in opts: |
| 187 | if opt in ("-h", "--help"): |
| 188 | print(usage.__doc__) |
| 189 | sys.exit() |
| 190 | elif opt in ("-p"): |
| 191 | check_ret=check_patch(arg) |
| 192 | elif opt in ("-d"): |
| 193 | check_ret=check_dir(arg) |
| 194 | elif opt in ("-v", "--version"): |
| 195 | print(VERSION) |
| 196 | sys.exit() |
| 197 | else: |
| 198 | print("Using the wrong way, please refer the help information!") |
| 199 | assert False, "unhandled option" |
| 200 | |
| 201 | if check_ret == 1: |
yang.li | 8c78ed6 | 2022-03-08 13:50:52 +0800 | [diff] [blame] | 202 | print('\nRTOS Opensource License Check Failed, ') |
| 203 | print('refs http://tee.amlogic.com:8000/Documents/Ecosystem/RTOS/rtos-sdk/licensing.html') |
yang.li | ba81c32 | 2022-04-25 17:13:37 +0800 | [diff] [blame] | 204 | elif check_ret == 2: |
| 205 | print('\nWrong Prameter, Check Failed\n') |
yang.li | 8c78ed6 | 2022-03-08 13:50:52 +0800 | [diff] [blame] | 206 | else: |
yang.li | 1092d00 | 2022-04-26 16:12:45 +0800 | [diff] [blame] | 207 | print('\nRTOS Opensource License Check Success\n') |