-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcb_put_licenses_on_files.py
177 lines (132 loc) · 6.13 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) 2022 by Camiel Bouchier ([email protected])
This file is part of cb_outlook.
All rights reserved.
You are granted a non-exclusive and non-transferable license to use this
software for personal or internal business purposes.
THIS SOFTWARE IS PROVIDED "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL Camiel Bouchier BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
$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 os
import re
####################################################################################################
program_name = "cb_put_licenses_on_files"
license_txt = [
'',
'(C) 2022 by Camiel Bouchier ([email protected])',
'',
'This file is part of cb_outlook.',
'All rights reserved.',
''
'You are granted a non-exclusive and non-transferable license to use this',
'software for personal or internal business purposes.',
'',
'THIS SOFTWARE IS PROVIDED "AS IS" AND',
'ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED',
'WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE',
'DISCLAIMED. IN NO EVENT SHALL Camiel Bouchier BE LIABLE FOR ANY',
'DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES',
'(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;',
'LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND',
'ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT',
'(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS',
'SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.',
'']
####################################################################################################
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() :
root_logger = logging.getLogger()
root_logger.setLevel(logging.DEBUG)
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(console_handler)
####################################################################################################
if __name__ == '__main__':
install_logger()
logger.info("Starting {}".format(program_name))
dir_todo = '.'
extensions_todo = ['', '.py', '.spec', '.bat']
regex_exclude = [
r'.*/.git.*',
r'.*/__pycache__/.*',
r'.*/Lib/.*',
r'.*/Scripts/.*',
r'.*/build.*/.*',
r'.*/dist.*/.*',
]
for the_file in generate_files(dir_todo, extensions_todo, regex_exclude) :
handle_file(f"{the_file}")
logger.info("Ending {}".format(program_name))
####################################################################################################