blob: e3774e534101d696ba7cd1a9759a8f3d8b2340af [file] [log] [blame]
yang.li8c78ed62022-03-08 13:50:52 +08001#!/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
9import os
10import sys
yang.liba81c322022-04-25 17:13:37 +080011import getopt
yang.li8c78ed62022-03-08 13:50:52 +080012import string
13import re
14
yang.liba81c322022-04-25 17:13:37 +080015def usage():
16 """
17The script is used to check RTOS_SDK public license,
18Usage: ./scripts/check_license.py [option]
19
20Example: ./scripts/module_size_report.py -p patch.diff
21
22Description
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.li8c78ed62022-03-08 13:50:52 +080029#file type contains .[chS]/.ld
30def 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
45def 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
60def 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.liba81c322022-04-25 17:13:37 +080072#checking patch file
73def 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.li8c78ed62022-03-08 13:50:52 +080089 #diff start of file, and get the file name
yang.liba81c322022-04-25 17:13:37 +080090 line=lines[line_index]
yang.li8c78ed62022-03-08 13:50:52 +080091 file = line.split(' ')[-1].strip()
yang.li1092d002022-04-26 16:12:45 +080092 file = file.split('/')[-1]
yang.li8c78ed62022-03-08 13:50:52 +080093 patch_str = ''
yang.liba81c322022-04-25 17:13:37 +080094
95 if 'new file mode' in lines[line_index+1]:
yang.li8c78ed62022-03-08 13:50:52 +080096 #new file in patch, and save the patch string
yang.liba81c322022-04-25 17:13:37 +080097 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.li8c78ed62022-03-08 13:50:52 +0800102
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.liba81c322022-04-25 17:13:37 +0800107 ret_val = 1
yang.li8c78ed62022-03-08 13:50:52 +0800108 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.liba81c322022-04-25 17:13:37 +0800114 ret_val = 1
yang.li8c78ed62022-03-08 13:50:52 +0800115 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.liba81c322022-04-25 17:13:37 +0800121 ret_val = 1
yang.li8c78ed62022-03-08 13:50:52 +0800122 continue
yang.liba81c322022-04-25 17:13:37 +0800123 else:
124 print("Not Found Patch file:"+patch_file)
125 ret_val=2
yang.li8c78ed62022-03-08 13:50:52 +0800126
yang.liba81c322022-04-25 17:13:37 +0800127 return ret_val
128
129#checking directory
130def 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
174if __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.li8c78ed62022-03-08 13:50:52 +0800202 print('\nRTOS Opensource License Check Failed, ')
203 print('refs http://tee.amlogic.com:8000/Documents/Ecosystem/RTOS/rtos-sdk/licensing.html')
yang.liba81c322022-04-25 17:13:37 +0800204 elif check_ret == 2:
205 print('\nWrong Prameter, Check Failed\n')
yang.li8c78ed62022-03-08 13:50:52 +0800206 else:
yang.li1092d002022-04-26 16:12:45 +0800207 print('\nRTOS Opensource License Check Success\n')