]>
git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/m2/tools-src/tidydates.py
49ef8325494bd8734d42b335f265eea81feac6ea
3 # utility to tidy dates and detect lack of copyright.
5 # Copyright (C) 2016-2025 Free Software Foundation, Inc.
7 # This file is part of GNU Modula-2.
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)
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.
19 # You should have received a copy of the GNU General Public License
20 # along with GCC; see the file COPYING3. If not see
21 # <http://www.gnu.org/licenses/>.
30 COPYRIGHT
= 'Copyright (C)'
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
)
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
:
43 elif path
.is_dir(full
):
44 visit_dir(full
, ext
, func
)
48 # is_year - returns True if, year, is legal.
57 def handle_copyright(outfile
, lines
, n
, leader1
, leader2
):
58 # handle_copyright look for Copyright in the comment.
59 global max_line_length
61 c
= i
.find(COPYRIGHT
)+len(COPYRIGHT
)
79 if c
> max_line_length
:
81 outfile
.write(leader1
)
82 outfile
.write(leader2
)
83 outfile
.write(' '*(start
-2))
86 if (e
[-1] == '.') or (e
[-1] == ','):
95 c
+= len(e
) + len(punctuation
)
98 outfile
.write(punctuation
)
103 outfile
.write(leader1
)
104 outfile
.write(leader2
)
105 outfile
.write(' '*(start
-2))
109 outfile
.write(punctuation
)
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()
123 with
open('tmptidy', 'w') as outfile
:
126 if i
.find('Copyright (C)') >= 0:
127 outfile
, n
= handle_copyright(outfile
, lines
,
129 outfile
.writelines(lines
[n
:])
131 print('-> mv tmptidy', filename
)
132 shutil
.move('tmptidy', filename
)
135 outfile
.write(lines
[n
])
137 sys
.stdout
.write('%s:1:1 needs a Copyright notice..\n' % filename
)
140 def bash_tidy(filename
):
141 # bash_tidy - tidy up dates using '#' comment
142 handle_header(filename
, '#', ' ')
145 def c_tidy(filename
):
146 # c_tidy - tidy up dates using '/* */' comments
147 handle_header(filename
, ' ', '*')
150 def m2_tidy(filename
):
151 # m2_tidy - tidy up dates using '(* *)' comments
152 handle_header(filename
, ' ', ' ')
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
)