Debian Codegraph @

script/tools/check_copyright_headers.py

raw symbols SCIP
1 #!/usr/bin/env python3
2
3 ###########################################################################
4 # fheroes2: https://github.com/ihhub/fheroes2 #
5 # Copyright (C) 2022 - 2024 #
6 # #
7 # This program is free software; you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation; either version 2 of the License, or #
10 # (at your option) any later version. #
11 # #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
16 # #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program; if not, write to the #
19 # Free Software Foundation, Inc., #
20 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #
21 ###########################################################################
22
23 # pylint: disable=missing-module-docstring
24
25 import argparse
26 import datetime
27 import os
28 import re
29 import subprocess
30 import sys
31
32 CURRENT_YEAR = datetime.datetime.now().date().year
33
34
35 class CustomArgumentParser(
36 argparse.ArgumentParser
37 ): # pylint: disable=missing-class-docstring
38 def error(self, message):
39 self.print_usage(sys.stderr)
40 self.exit(
41 1,
42 "%(prog)s: error: %(message)s\n" % {"prog": self.prog, "message": message},
43 )
44
45
46 def parse_arguments(): # pylint: disable=missing-function-docstring
47 parser = CustomArgumentParser()
48
49 parser.add_argument(
50 "--handle-shebang",
51 action="store_true",
52 help="handle the shebang at the beginning of the source file(s)",
53 )
54 parser.add_argument("full_header_file", help="full header file")
55 parser.add_argument("header_template_file", help="header template file")
56 parser.add_argument(
57 "source_file", help="source files (at least one, can be multiple)", nargs="+"
58 )
59
60 return parser.parse_args()
61
62
63 def main(): # pylint: disable=missing-function-docstring,too-many-locals,too-many-branches
64 args = parse_arguments()
65
66 with open(args.full_header_file, "r", encoding="latin_1") as full_header_file:
67 copyright_hdr_full = (
68 full_header_file.read().strip().replace("{YR}", f"{CURRENT_YEAR}")
69 )
70
71 with open(
72 args.header_template_file, "r", encoding="latin_1"
73 ) as header_template_file:
74 copyright_hdr_tmpl = header_template_file.read().strip()
75
76 copyright_hdr_re_tmpl = copyright_hdr_tmpl
77
78 for ch_to_replace in "*()":
79 copyright_hdr_re_tmpl = copyright_hdr_re_tmpl.replace(
80 ch_to_replace, "\\" + ch_to_replace
81 )
82
83 copyright_hdr_year_re = re.compile(
84 "^" + copyright_hdr_re_tmpl.replace("{Y1} - {Y2}", "([0-9]{4}) ")
85 )
86 copyright_hdr_yrng_re = re.compile(
87 "^"
88 + copyright_hdr_re_tmpl.replace("{Y1}", "([0-9]{4})").replace(
89 "{Y2}", "([0-9]{4})"
90 )
91 )
92
93 for file_name in args.source_file:
94 if not os.path.exists(file_name):
95 continue
96
97 with open(file_name, "r", encoding="latin_1") as src_file:
98 src = src_file.read()
99
100 if args.handle_shebang and src.startswith("#!"):
101 partition = src.partition("\n")
102
103 shebang = partition[0]
104 src = partition[2].lstrip("\n")
105 else:
106 shebang = ""
107
108 year_re_match = copyright_hdr_year_re.match(src)
109 yrng_re_match = copyright_hdr_yrng_re.match(src)
110
111 src_is_ok = False
112
113 if year_re_match:
114 if year_re_match.group(1) == f"{CURRENT_YEAR}":
115 src_is_ok = True
116 elif yrng_re_match:
117 if yrng_re_match.group(2) == f"{CURRENT_YEAR}":
118 src_is_ok = True
119
120 if not src_is_ok:
121 if year_re_match:
122 hdr = copyright_hdr_tmpl.replace(
123 "{Y1}", f"{year_re_match.group(1)}"
124 ).replace("{Y2}", f"{CURRENT_YEAR}")
125 src = copyright_hdr_year_re.sub(hdr, src)
126 elif yrng_re_match:
127 hdr = copyright_hdr_tmpl.replace(
128 "{Y1}", f"{yrng_re_match.group(1)}"
129 ).replace("{Y2}", f"{CURRENT_YEAR}")
130 src = copyright_hdr_yrng_re.sub(hdr, src)
131 else:
132 src = copyright_hdr_full + "\n\n" + src.lstrip("\n")
133
134 if shebang:
135 src = shebang + "\n\n" + src
136
137 with open(file_name + ".tmp", "x", encoding="latin_1") as tmp_file:
138 tmp_file.write(src)
139
140 with subprocess.Popen(
141 ["diff", "-u", file_name, file_name + ".tmp"]
142 ) as diff_proc:
143 diff_proc.wait()
144
145 os.remove(file_name + ".tmp")
146
147
148 if __name__ == "__main__":
149 main()