-
Notifications
You must be signed in to change notification settings - Fork 15
/
cb_put_licenses_on_files.py
177 lines (129 loc) · 5.45 KB
/
cb_put_licenses_on_files.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
165
166
167
168
169
170
171
172
173
174
175
176
177
# -*- coding: utf-8 -*-
# vim: syntax=python ts=4 sw=4 sts=4 sr et columns=100 lines=45
"""
$BeginLicense$
(C) 2020-2021 by Camiel Bouchier ([email protected])
This file is part of cb_thunderlink.
License: Mozilla Public License Version 2.0
(https://github.com/CamielBouchier/cb_thunderlink/blob/main/LICENSE)
$EndLicense$
"""
####################################################################################################
# Want to be asap sure we are running Python 3.6
import sys
assert sys.version_info >= (3,6)
import logging
logger = logging.getLogger(__name__)
import datetime
import os
import re
####################################################################################################
program_name = "cbPutLicenseOnFiles"
log_dir = "logs"
license_txt = [
'',
'(C) 2020-2021 by Camiel Bouchier ([email protected])',
'',
'This file is part of cb_thunderlink.',
'',
'License: Mozilla Public License Version 2.0',
'(https://github.com/CamielBouchier/cb_thunderlink/blob/main/LICENSE)',
''
]
####################################################################################################
def generate_files(root, extensions_to_include=[], regexes_to_exclude=[]) :
for path, dirs, files in os.walk(root) :
for the_file in files :
relative_name = os.path.join(path, the_file).replace('\\', '/')
if extensions_to_include :
(filename, extension) = os.path.splitext(the_file)
if extension not in extensions_to_include :
continue
# logger.info(f"including \'{relative_name}\'")
excluded_due_to_regex = False
for regex in regexes_to_exclude :
if re.match(regex, relative_name) :
# logger.info(f"excluding \'{relative_name}\' due to \'{regex}\'")
excluded_due_to_regex = True
break
if excluded_due_to_regex :
continue
yield os.path.abspath(relative_name).replace('\\', '/')
####################################################################################################
def handle_file(filename) :
logger.info(f"Handling file {filename}")
with open(filename, encoding="utf-8") as f :
file_lines = f.readlines()
has_begin_license = False
has_end_license = False
for line in file_lines :
if not has_begin_license and re.match(r'.{0,5}\$BeginLicense\$', line) :
has_begin_license = True
begin_license_line = line
if has_begin_license and re.match(r'.{0,5}\$EndLicense\$', line) :
has_end_license = True
end_license_line = line
break
if not has_begin_license :
logger.info("No 'BeginLicense' found in '{}'".format(filename))
return
if has_begin_license and not has_end_license :
logger.info("No 'EndLicense' found in '{}'".format(filename))
return
with open(filename, "w", encoding="utf-8") as f :
skipping = False
for line in file_lines :
if not skipping :
f.write(line.rstrip()+'\n') # Implicit trailing blanks removal.
if line == end_license_line :
f.write(line)
skipping = False
if line == begin_license_line :
skipping = True
for X in license_txt :
f.write(begin_license_line.replace('$BeginLicense$', X).rstrip()+'\n')
####################################################################################################
def install_logger() :
now_string = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
log_filename = "{}/{}_{}.log".format(log_dir, program_name, now_string)
root_logger = logging.getLogger()
root_logger.setLevel(logging.DEBUG)
file_handler = logging.FileHandler(log_filename, mode='a', encoding="utf8")
file_handler.setLevel(logging.DEBUG)
file_format = "%(asctime)s - %(levelname)10s - %(filename)32s:%(lineno)5s : %(message)s"
file_formatter = logging.Formatter(file_format)
file_handler.setFormatter(file_formatter)
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.INFO)
console_format = "%(filename)32s:%(lineno)5s : %(message)s"
console_formatter = logging.Formatter(console_format)
console_handler.setFormatter(console_formatter)
root_logger.addHandler(file_handler)
root_logger.addHandler(console_handler)
logger.info("Logging in '{}'".format(log_filename))
####################################################################################################
if __name__ == '__main__':
try :
os.makedirs(log_dir)
except FileExistsError :
pass
install_logger()
logger.info("Starting {}".format(program_name))
dir_todo = '.'
extensions_todo = ['', '.py', '.js', '.spec', '.txt', '.bat', '.css']
regex_exclude = [
r'.*/.hg/.*',
r'.*/.git/.*',
r'.*/__pycache__/.*',
r'.*/build_linux/.*',
r'.*/build_windows/.*',
r'.*/dist_linux/.*',
r'.*/dist_windows/.*',
r'.*/logs/.*',
r'.*/images/.*',
r'.*/references/.*',
]
for the_file in generate_files(dir_todo, extensions_todo, regex_exclude) :
handle_file(f"{the_file}")
logger.info("Ending {}".format(program_name))
####################################################################################################