forked from anabrid/pyanalog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLICENSE.py
executable file
·97 lines (83 loc) · 3.97 KB
/
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
#!/usr/bin/env python3
#
# This script updates/inserts copyright statements into any (preferably text)
# file. This works as following: The file should contain somet text like
#
# Copyright (c) 2020 anabrid GmbH
# Contact: https://www.anabrid.com/licensing/
# This file is part of the @MODULE of the @SOFTWARE.
#
# Followed by a line containing something like "ANABRID_BEGIN_LICENSE:GPL",
# where the text after ":" is an identifier for the actual license.
# We have a dictionary of license texts below.
#
magic_begin_license="ANABRID_BEGIN_LICENSE"
magic_end_license ="ANABRID_END_LICENSE"
license_text = {}
license_text["GPL"] = """
Commercial License Usage
Licensees holding valid commercial anabrid licenses may use this file in
accordance with the commercial license agreement provided with the
Software or, alternatively, in accordance with the terms contained in
a written agreement between you and Anabrid GmbH. For licensing terms
and conditions see https://www.anabrid.com/licensing. For further
information use the contact form at https://www.anabrid.com/contact.
GNU General Public License Usage
Alternatively, this file may be used under the terms of the GNU
General Public License version 3 as published by the Free Software
Foundation and appearing in the file LICENSE.GPL3 included in the
packaging of this file. Please review the following information to
ensure the GNU General Public License version 3 requirements
will be met: https://www.gnu.org/licenses/gpl-3.0.html.
For Germany, additional rules exist. Please consult /LICENSE.DE
for further agreements.
"""
import sys, os, re, pathlib, collections
files = os.popen("git ls-files").read().strip().split("\n")
counters = collections.defaultdict(int)
class MalformedTagsError(Exception): pass
for fname in files:
#if pathlib.Path(fname) == pathlib.Path(__file__):
# continue # do not process this script itself
groups = []
with open(fname, "r") as fh:
try:
for i, line in enumerate(fh):
start = re.match(f"^(?P<prefix>.*){magic_begin_license}:(?P<license>[a-zA-Z0-9]+)\s*$", line)
end = re.match(f"^(?P<prefix>.*){magic_end_license}\s*$", line)
if start:
if not start.group("license") in license_text:
raise MalformedTagsError(f"{fname}: Skipped because license {license} not known. Available licenses are {list(keys(license_text))}.")
else:
groups.append({ "start": i, **start.groupdict() })
if end:
if len(groups) == 0:
raise MalformedTagsError(f"{fname}: Encountered end license tag on line {i} but missed start license tag.")
else:
if not groups[-1]["prefix"] == end.group("prefix"):
raise MalformedTagsError(f"{fname}: Start line {groups[-1]['start']} has incompatible prefix to end line {i}")
else:
groups[-1]["end"] = i
except MalformedTagsError as e:
counters["Skipped (errnous tags)"] += 1
print(e.message, file=sys.stderr)
continue
except UnicodeDecodeError:
counters["Skipped (binary files)"] += 1
continue
if not groups:
counters["Skipped (no license tag)"] += 1
continue
with open(fname, "r+") as fh:
content = list(fh)
for group in groups:
amended_license = [ f"{group['prefix']}{line}\n" for line in license_text[group["license"]].strip().split("\n")]
content[group["start"]+1 : group["end"]] = amended_license
fh.seek(0)
fh.writelines(content)
fh.truncate()
counters["Processed"] += 1
print("Statistics on number of processed files:")
for k,v in counters.items(): print(f"* {k}: {v}")
# Exit with error exit code if any error counter is > 0.
sys.exit(1 if any([ v for k,v in counters.items() if "err" in k]) else 0)