]>
Commit | Line | Data |
---|---|---|
1eee94d3 GM |
1 | #!/usr/bin/env python3 |
2 | ||
3 | # utility to tidy dates and detect lack of copyright. | |
4 | ||
6441eb6d | 5 | # Copyright (C) 2016-2025 Free Software Foundation, Inc. |
1eee94d3 GM |
6 | # |
7 | # This file is part of GNU Modula-2. | |
8 | # | |
9 | # GNU Modula-2 is free software; you can redistribute it and/or modify | |
10 | # it under the terms of the GNU General Public License as published by | |
11 | # the Free Software Foundation; either version 3, or (at your option) | |
12 | # any later version. | |
13 | # | |
14 | # GNU Modula-2 is distributed in the hope that it will be useful, | |
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | # GNU General Public License for more details. | |
18 | # | |
19 | # You should have received a copy of the GNU General Public License | |
78dc2e25 GM |
20 | # along with GCC; see the file COPYING3. If not see |
21 | # <http://www.gnu.org/licenses/>. | |
1eee94d3 GM |
22 | |
23 | import os | |
24 | import pathlib | |
25 | import shutil | |
26 | import sys | |
27 | ||
28 | max_line_length = 60 | |
29 | ||
30 | COPYRIGHT = 'Copyright (C)' | |
31 | ||
32 | ||
33 | def visit_dir(directory, ext, func): | |
34 | # visit_dir - call func for each file below, dir, matching extension, ext. | |
35 | list_of_files = os.listdir(directory) | |
36 | list_of_files.sort() | |
37 | for filename in list_of_files: | |
38 | path = pathlib.PurePath(filename) | |
39 | full = os.path.join(directory, filename) | |
40 | if path.is_file(full): | |
41 | if path.suffix == ext: | |
42 | func(full) | |
43 | elif path.is_dir(full): | |
44 | visit_dir(full, ext, func) | |
45 | ||
46 | ||
47 | def is_year(year): | |
48 | # is_year - returns True if, year, is legal. | |
49 | if len(year) == 5: | |
50 | year = year[:-1] | |
51 | for c in year: | |
52 | if not c.isdigit(): | |
53 | return False | |
54 | return True | |
55 | ||
56 | ||
57 | def handle_copyright(outfile, lines, n, leader1, leader2): | |
58 | # handle_copyright look for Copyright in the comment. | |
59 | global max_line_length | |
60 | i = lines[n] | |
61 | c = i.find(COPYRIGHT)+len(COPYRIGHT) | |
62 | outfile.write(i[:c]) | |
63 | d = i[c:].split() | |
64 | start = c | |
65 | seen_date = True | |
66 | years = [] | |
67 | while seen_date: | |
68 | if d == []: | |
69 | n += 1 | |
70 | i = lines[n] | |
71 | d = i[2:].split() | |
72 | else: | |
73 | e = d[0] | |
74 | punctuation = '' | |
75 | if len(d) == 1: | |
76 | d = [] | |
77 | else: | |
78 | d = d[1:] | |
79 | if c > max_line_length: | |
80 | outfile.write('\n') | |
81 | outfile.write(leader1) | |
82 | outfile.write(leader2) | |
83 | outfile.write(' '*(start-2)) | |
84 | c = start | |
85 | if is_year(e): | |
86 | if (e[-1] == '.') or (e[-1] == ','): | |
87 | punctuation = e[-1] | |
88 | e = e[:-1] | |
89 | else: | |
90 | punctuation = '' | |
91 | else: | |
92 | seen_date = False | |
93 | if seen_date: | |
94 | if not (e in years): | |
95 | c += len(e) + len(punctuation) | |
96 | outfile.write(' ') | |
97 | outfile.write(e) | |
98 | outfile.write(punctuation) | |
99 | years += [e] | |
100 | else: | |
101 | if start < c: | |
102 | outfile.write('\n') | |
103 | outfile.write(leader1) | |
104 | outfile.write(leader2) | |
105 | outfile.write(' '*(start-2)) | |
106 | ||
107 | outfile.write(' ') | |
108 | outfile.write(e) | |
109 | outfile.write(punctuation) | |
110 | for w in d: | |
111 | outfile.write(' ') | |
112 | outfile.write(w) | |
113 | outfile.write('\n') | |
114 | return outfile, n+1 | |
115 | ||
116 | ||
117 | def handle_header(filename, leader1, leader2): | |
118 | # handle_header reads in the header of a file and inserts | |
119 | # a line break around the Copyright dates. | |
120 | print('------------------------------') | |
121 | lines = open(filename).readlines() | |
122 | if len(lines) > 20: | |
123 | with open('tmptidy', 'w') as outfile: | |
124 | n = 0 | |
125 | for i in lines: | |
126 | if i.find('Copyright (C)') >= 0: | |
127 | outfile, n = handle_copyright(outfile, lines, | |
128 | n, leader1, leader2) | |
129 | outfile.writelines(lines[n:]) | |
130 | outfile.close() | |
131 | print('-> mv tmptidy', filename) | |
132 | shutil.move('tmptidy', filename) | |
133 | return | |
134 | else: | |
135 | outfile.write(lines[n]) | |
136 | n += 1 | |
137 | sys.stdout.write('%s:1:1 needs a Copyright notice..\n' % filename) | |
138 | ||
139 | ||
140 | def bash_tidy(filename): | |
141 | # bash_tidy - tidy up dates using '#' comment | |
142 | handle_header(filename, '#', ' ') | |
143 | ||
144 | ||
145 | def c_tidy(filename): | |
146 | # c_tidy - tidy up dates using '/* */' comments | |
147 | handle_header(filename, ' ', '*') | |
148 | ||
149 | ||
150 | def m2_tidy(filename): | |
151 | # m2_tidy - tidy up dates using '(* *)' comments | |
152 | handle_header(filename, ' ', ' ') | |
153 | ||
154 | ||
155 | def main(): | |
156 | # main - for each file extension call the appropriate tidy routine. | |
157 | visit_dir('.', '.in', bash_tidy) | |
158 | visit_dir('.', '.py', bash_tidy) | |
159 | visit_dir('.', '.c', c_tidy) | |
160 | visit_dir('.', '.h', c_tidy) | |
161 | visit_dir('.', '.def', m2_tidy) | |
162 | visit_dir('.', '.mod', m2_tidy) | |
163 | ||
164 | ||
165 | main() |