forked from Tencent/QMUI_iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
add_license.py
164 lines (127 loc) · 5.39 KB
/
add_license.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# -*- encoding:utf-8 -*-
import os
import re
# 配置参数
root_src_dir = '.' # 代码目录名
ignore_dirs = ['test'] # 要忽略的目录(目录名完全匹配)
rules = [
# Android
# {
# 'suffix': '.java',
# 'new_comment': 'comment_for_java.txt',
# 'old_comment': 'comment_for_java.txt',
# 'ignore_files': [] # 要忽略的文件名(文件名完整匹配)
# },
# {
# 'suffix': '.xml',
# 'new_comment': 'comment_for_xml.txt',
# 'old_comment': 'comment_for_xml.txt',
# 'keep_on_top_lines': [re.compile(r'.*<\?xml version="1\.0".*')] # 要保证在文件前面的行(注释将加在这些行之后)(正则)
# },
# iOS
{
'suffix': '.h',
'new_comment': 'new_license_content.txt', # 要更新的 license 文件
'old_comment': 'old_license_content.txt', # 老的的 license 文件,如果文件没有更新,那么内容要保持跟新文件一样
'delete_lines': [re.compile(r'.*//.*Copyright.*All rights reserved.*')] # 要从源文件中删除的行(正则)
# 'ignore_files': [] # 要忽略的文件名(文件名完整匹配)
},
{
'suffix': '.m',
'new_comment': 'new_license_content.txt',
'old_comment': 'old_license_content.txt',
'delete_lines': [re.compile(r'.*//.*Copyright.*All rights reserved.*')] # 要从源文件中删除的行(正则)
# 'ignore_files': [] # 要忽略的文件名(文件名完整匹配)
},
]
# 全局变量
delete_files = []
def is_match_anyone_dir(path, dir_list):
for d in dir_list:
if "/{dir}/".format(dir=d) in path:
return True
return False
def is_match_anyone_str(filename, str_list):
for s in str_list:
if filename == s:
return True
return False
def is_match_anyone_regex(line, regex_list):
for regex in regex_list:
if regex.match(line):
return True
return False
def find_file(start, suffix, ignore_dirs, ignore_files):
list = []
for relpath, dirs, files in os.walk(start):
for filename in files:
if filename.endswith(suffix):
full_file_name = os.path.join(relpath, filename)
if not is_match_anyone_dir(full_file_name, ignore_dirs) and not is_match_anyone_str(filename, ignore_files):
list.append(full_file_name)
return list
def add_comment(rule):
print('processing with {rule}'.format(rule=rule))
new_comment_filename = rule['new_comment']
old_comment_filename = rule['old_comment']
file_suffix = rule['suffix']
ignore_files = rule.get('ignore_files', [])
keep_on_top_lines = rule.get('keep_on_top_lines', [])
delete_lines = rule.get('delete_lines', [])
delete_count = 0
with open(new_comment_filename, 'r', encoding = "utf-8") as f:
new_comment_content = f.read()
with open(old_comment_filename, 'r', encoding = "utf-8") as f:
old_comment_lines = f.readlines()
old_comment_lines_count = len(old_comment_lines)
files = find_file(root_src_dir, file_suffix, ignore_dirs, ignore_files)
for file in files:
with open(file, 'r', encoding = "utf-8") as f:
src_lines = f.readlines()
with open(file, 'w', encoding = "utf-8") as f:
has_written_comments = False
is_update_license = False
line_index = 0
for line in src_lines:
is_line_exist = False
# 这一行是否存在久的文件中
for old_comment_line in old_comment_lines:
if line == old_comment_line:
is_line_exist = True
break
# 如果存在则不写进去
if is_line_exist and len(line.strip()) > 0:
line_index += 1
delete_count += 1
if line_index <= old_comment_lines_count:
is_update_license = True
continue
# 是否正则删除
if is_match_anyone_regex(line, delete_lines):
print('ignore line: {line}'.format(line=line))
continue
if not has_written_comments:
if is_match_anyone_regex(line, keep_on_top_lines):
f.write(line)
else:
f.writelines(new_comment_content)
has_written_comments = True
f.write(line)
else:
f.write(line)
if delete_count != 0 and delete_count != old_comment_lines_count:
delete_files.append(file)
delete_count = 0
if is_update_license:
print('processing with {file} ({flag})'.format(file=file, flag='update license'))
else:
print('processing with {file} ({flag})'.format(file=file, flag='add license'))
if __name__ == '__main__':
for rule in rules:
add_comment(rule)
if len(delete_files) > 0:
print('==================== 以下文件可能更新遇到问题,建议检查 ====================')
for delete_file in delete_files:
print(delete_file)
print('==================== 以上文件可能更新遇到问题,建议检查 ====================')
delete_files = []