forked from mariadb-corporation/MaxScale
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd-copyright
executable file
·147 lines (128 loc) · 4.76 KB
/
add-copyright
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
#!/usr/bin/env python3
#
# Copyright (c) 2023 MariaDB plc
#
# Use of this software is governed by the Business Source License included
# in the LICENSE.TXT file and at www.mariadb.com/bsl11.
#
# Change Date: 2024-06-03
#
# On the date above, in accordance with the Business Source License, use
# of this software will be governed by version 2 or later of the General
# Public License.
#
import getopt
import os
import re
import shutil
import sys
import tempfile
# A string literal would be a match and cause an unintended modification.
pattern="Copyright "
pattern+=".*"
pattern+="MariaDB .*"
# The Copyright should be in the blurb at the beginning. We will not
# do modifications after this threshold.
line_threshold=20
recd=re.compile(pattern)
def print_usage_and_exit(name, code):
print("usage: " + name + " -c copyright [path]+")
print()
print("Add a specfic copyright line after the \"MariaDB\"")
print("copyright line. If path is a directory, the copyright will ")
print("recursively be added to all files in that directory hierarchy.")
print()
print("Example: " + name + " -c \"2023 MariaDB plc, Finnish Branch\" somefile.cc")
sys.exit(code)
def update_copyright_in_file(copyright, filename):
print(filename)
tmp=tempfile.mkstemp()
fout=os.fdopen(tmp[0], "w")
with open(filename) as fin:
modified=False
try:
line=fin.readline()
line_number=1
while line:
match=recd.search(line)
if match:
# Some MaxGui files (namely Vue templates) are forced to
# have the license blurb after actual code.
if line_number <= line_threshold or filename.endswith('.vue'):
next_line=''
while True:
fout.write(line)
next_line=fin.readline()
line_number=line_number + 1
if next_line:
next_match=recd.search(next_line)
if next_match:
line=next_line
match=next_match
continue
else:
break
else:
break
# 14 is the length of "Copyright (c) " that an existing MariaDB copyright
# is assumed to begin with.
cutoff=match.span()[0] + 14
prefix=line[0:cutoff]
suffix=line[cutoff:]
if suffix != copyright + "\n":
new_line=prefix + copyright + "\n"
fout.write(new_line)
modified=True
if next_line:
fout.write(next_line)
else:
print("WARNING: Ignoring 'Copyright' encountered on line " + str(line_number) + ".")
fout.write(line)
else:
fout.write(line)
line = fin.readline()
line_number=line_number + 1
except UnicodeDecodeError:
print("WARNING: Could not decode file, ignoring.")
modified=False
except:
print("WARNING: Could not read file, ignoring.")
modified=False
fin.close()
fout.close()
if modified:
shutil.copyfile(tmp[1], filename)
os.remove(tmp[1])
def update_copyright_in_dir(copyright, dir):
for file in os.listdir(dir):
path=dir + "/" + file
update_copyright_in_path(copyright, path)
def update_copyright_in_path(copyright, path):
if not os.path.exists(path):
print("WARNING: " + path + " does not exist.")
elif os.path.isdir(path):
update_copyright_in_dir(copyright, path)
elif os.path.isfile(path):
update_copyright_in_file(copyright, path)
else:
print("WARNING: '" + path + "' is not a regular file or directory, ignoring.")
def update_copyright_in_paths(copyright, paths):
for path in paths:
update_copyright_in_path(copyright, path)
def main(argv):
name=argv[0]
try:
opts, args = getopt.getopt(argv[1:], "hc:")
except getopt.GetoptError:
print_usage_and_exit(name, 1)
copyright=''
for opt, arg in opts:
if opt == '-h':
print_usage_and_exit(name, 0)
elif opt == '-c':
copyright = arg
if copyright == '':
print_usage_and_exit(name, 1)
update_copyright_in_paths(copyright, args)
if __name__ == "__main__":
main(sys.argv)