3 # This file is part of the GROMACS molecular simulation package.
5 # Copyright (c) 2013,2014,2015,2016,2018, by the GROMACS development team, led by
6 # Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7 # and including many others, as listed in the AUTHORS file in the
8 # top-level source directory and at http://www.gromacs.org.
10 # GROMACS is free software; you can redistribute it and/or
11 # modify it under the terms of the GNU Lesser General Public License
12 # as published by the Free Software Foundation; either version 2.1
13 # of the License, or (at your option) any later version.
15 # GROMACS is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 # Lesser General Public License for more details.
20 # You should have received a copy of the GNU Lesser General Public
21 # License along with GROMACS; if not, see
22 # http://www.gnu.org/licenses, or write to the Free Software Foundation,
23 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 # If you want to redistribute modifications to GROMACS, please
26 # consider that scientific software is very special. Version
27 # control is crucial - bugs must be traceable. We will be happy to
28 # consider code for inclusion in the official distribution, but
29 # derived work must not be called official GROMACS. Details are found
30 # in the README & COPYING files - if they are missing, get the
31 # official version at http://www.gromacs.org.
33 # To help us fund GROMACS development, we humbly ask that you cite
34 # the research papers on the package. Check out http://www.gromacs.org.
36 """Checks and/or updates copyright headers in GROMACS source files.
38 It is used internally by several bash scripts to do copyright-relates tasks,
39 but can also be invoked directly for some rare use cases.
41 See docs/dev-manual/uncrustify.rst for more details.
49 from optparse
import OptionParser
51 class CopyrightState(object):
53 """Information about an existing (or non-existing) copyright header."""
55 def __init__(self
, has_copyright
, is_correct
, is_newstyle
, years
, other_copyrights
):
56 self
.has_copyright
= has_copyright
57 self
.is_correct
= is_correct
58 self
.is_newstyle
= is_newstyle
60 self
.other_copyrights
= other_copyrights
62 class CopyrightChecker(object):
64 """Logic for analyzing existing copyright headers and generating new ones."""
66 _header
= ["", "This file is part of the GROMACS molecular simulation package.", ""]
67 _copyright
= "Copyright (c) {0}, by the GROMACS development team, led by"
69 Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
70 and including many others, as listed in the AUTHORS file in the
71 top-level source directory and at http://www.gromacs.org.
73 GROMACS is free software; you can redistribute it and/or
74 modify it under the terms of the GNU Lesser General Public License
75 as published by the Free Software Foundation; either version 2.1
76 of the License, or (at your option) any later version.
78 GROMACS is distributed in the hope that it will be useful,
79 but WITHOUT ANY WARRANTY; without even the implied warranty of
80 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
81 Lesser General Public License for more details.
83 You should have received a copy of the GNU Lesser General Public
84 License along with GROMACS; if not, see
85 http://www.gnu.org/licenses, or write to the Free Software Foundation,
86 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
88 If you want to redistribute modifications to GROMACS, please
89 consider that scientific software is very special. Version
90 control is crucial - bugs must be traceable. We will be happy to
91 consider code for inclusion in the official distribution, but
92 derived work must not be called official GROMACS. Details are found
93 in the README & COPYING files - if they are missing, get the
94 official version at http://www.gromacs.org.
96 To help us fund GROMACS development, we humbly ask that you cite
97 the research papers on the package. Check out http://www.gromacs.org.
98 """.strip().splitlines()
100 def check_copyright(self
, comment_block
):
101 """Analyze existing copyright header for correctness and extract information."""
102 copyright_re
= r
'Copyright \(c\) (([0-9]{4}[,-])*[0-9]{4}),? by the GROMACS development team,'
103 has_copyright
= False
108 append_next_line_to_other_copyrights
= False
110 other_copyrights
= []
111 for line
in comment_block
:
112 if append_next_line_to_other_copyrights
:
113 other_copyrights
[-1] += ' ' + line
114 append_next_line_to_other_copyrights
= False
116 if 'Copyright' in line
:
118 match
= re
.match(copyright_re
, line
)
120 existing_years
= match
.group(1)
121 new_line
= self
._copyright
.format(existing_years
)
125 other_copyrights
.append(line
[line
.find('Copyright'):])
126 if not line
.startswith('Copyright'):
127 append_next_line_to_other_copyrights
= True
128 if next_header_line
!= -1 or next_footer_line
!= 0:
131 if line
.startswith('Written by the Gromacs development team'):
133 if next_header_line
>= 0:
134 if line
== self
._header
[next_header_line
]:
135 next_header_line
+= 1
136 if next_header_line
>= len(self
._header
):
137 next_header_line
= -1
141 elif next_footer_line
>= 0:
142 if line
== self
._footer
[next_footer_line
]:
143 next_footer_line
+= 1
144 if next_footer_line
>= len(self
._footer
):
145 next_footer_line
= -1
150 if next_header_line
!= -1 or next_footer_line
!= -1:
153 return CopyrightState(has_copyright
, is_correct
, is_newstyle
, existing_years
, other_copyrights
)
155 def process_copyright(self
, state
, options
, current_years
, reporter
):
156 """Determine whether a copyrigth header needs to be updated and report issues."""
160 if options
.replace_years
:
161 if state
.years
!= current_years
:
163 reporter
.report('copyright years replaced')
164 new_years
= current_years
166 new_years
= state
.years
167 if not new_years
.endswith(current_years
):
168 if options
.update_year
:
170 new_years
+= ',' + current_years
171 if options
.check
or not need_update
:
172 reporter
.report('copyright year outdated')
174 reporter
.report('copyright year added')
176 new_years
= current_years
178 if not state
.has_copyright
:
179 if options
.add_missing
:
181 if options
.check
or not need_update
:
182 reporter
.report('copyright header missing')
183 elif options
.add_missing
:
184 reporter
.report('copyright header added')
186 if not state
.is_newstyle
:
187 if options
.replace_header
:
189 if options
.check
or not need_update
:
190 reporter
.report('copyright header incorrect')
192 reporter
.report('copyright header replaced')
193 elif not state
.is_correct
:
194 if options
.update_header
:
196 if options
.check
or not need_update
:
197 reporter
.report('copyright header outdated')
199 reporter
.report('copyright header updated')
201 return need_update
, new_years
203 def get_copyright_text(self
, years
, other_copyrights
):
204 """Construct a new copyright header."""
206 output
.extend(self
._header
)
208 for line
in other_copyrights
:
209 outline
= line
.rstrip()
210 if outline
.endswith(','):
211 outline
= outline
[:-1]
212 if not outline
.endswith('.'):
214 output
.append(outline
)
215 output
.append(self
._copyright
.format(years
))
216 output
.extend(self
._footer
)
219 class Reporter(object):
221 """Wrapper for reporting issues in a file."""
223 def __init__(self
, reportfile
, filename
):
224 self
._reportfile
= reportfile
225 self
._filename
= filename
227 def report(self
, text
):
228 self
._reportfile
.write(self
._filename
+ ': ' + text
+ '\n');
230 class CommentHandlerC(object):
232 """Handler for extracting and creating C-style comments."""
234 def extract_first_comment_block(self
, content_lines
):
235 if not content_lines
or not content_lines
[0].startswith('/*'):
237 comment_block
= [content_lines
[0][2:].strip()]
239 while line_index
< len(content_lines
):
240 line
= content_lines
[line_index
]
241 if '*/' in content_lines
[line_index
]:
243 comment_block
.append(line
.lstrip('* ').rstrip())
245 return (comment_block
, line_index
+ 1)
247 def create_comment_block(self
, lines
):
249 output
.append(('/* ' + lines
[0]).rstrip())
250 output
.extend([(' * ' + x
).rstrip() for x
in lines
[1:]])
254 class CommentHandlerSimple(object):
256 """Handler for extracting and creating sh-style comments.
258 Also other comments of the same type, but with a different comment
259 character are supported."""
261 def __init__(self
, comment_char
):
262 self
._comment
_char
= comment_char
264 def extract_first_comment_block(self
, content_lines
):
265 if not content_lines
or not content_lines
[0].startswith(self
._comment
_char
):
269 while line_index
< len(content_lines
):
270 line
= content_lines
[line_index
]
271 if not line
.startswith(self
._comment
_char
):
273 comment_block
.append(line
.lstrip(self
._comment
_char
+ ' ').rstrip())
275 if line
== self
._comment
_char
+ ' the research papers on the package. Check out http://www.gromacs.org.':
277 while line_index
< len(content_lines
):
278 line
= content_lines
[line_index
].rstrip()
279 if len(line
) > 0 and line
!= self
._comment
_char
:
282 return (comment_block
, line_index
)
284 def create_comment_block(self
, lines
):
286 output
.extend([(self
._comment
_char
+ ' ' + x
).rstrip() for x
in lines
])
291 'c': CommentHandlerC(),
292 'tex': CommentHandlerSimple('%'),
293 'sh': CommentHandlerSimple('#')
296 def select_comment_handler(override
, filename
):
297 """Select comment handler for a file based on file name and input options."""
299 if not filetype
and filename
!= '-':
300 basename
= os
.path
.basename(filename
)
301 root
, ext
= os
.path
.splitext(basename
)
302 if ext
== '.cmakein':
303 dummy
, ext2
= os
.path
.splitext(root
)
306 if ext
in ('.c', '.cu', '.cpp', '.cl', '.h', '.cuh', '.clh', '.y', '.l', '.pre', '.bm'):
308 elif ext
in ('.tex',):
310 elif basename
in ('CMakeLists.txt', 'GMXRC', 'git-pre-commit') or \
311 ext
in ('.cmake', '.cmakein', '.py', '.sh', '.bash', '.csh', '.zsh'):
313 if filetype
in comment_handlers
:
314 return comment_handlers
[filetype
]
316 sys
.stderr
.write("Unsupported input format: {0}\n".format(filetype
))
317 elif filename
!= '-':
318 sys
.stderr
.write("Unsupported input format: {0}\n".format(filename
))
320 sys
.stderr
.write("No file name or file type provided.\n")
323 def create_copyright_header(years
, other_copyrights
=None, language
='c'):
324 if language
not in comment_handlers
:
325 sys
.strerr
.write("Unsupported language: {0}\n".format(language
))
327 copyright_checker
= CopyrightChecker()
328 comment_handler
= comment_handlers
[language
]
329 copyright_lines
= copyright_checker
.get_copyright_text(years
, other_copyrights
)
330 comment_lines
= comment_handler
.create_comment_block(copyright_lines
)
331 return '\n'.join(comment_lines
) + '\n'
333 def process_options():
334 """Process input options."""
335 parser
= OptionParser()
336 parser
.add_option('-l', '--lang',
337 help='Comment type to use (c or sh)')
338 parser
.add_option('-y', '--years',
339 help='Comma-separated list of years')
340 parser
.add_option('-F', '--files',
341 help='File to read list of files from')
342 parser
.add_option('--check', action
='store_true',
343 help='Do not modify the files, only check the copyright (default action). ' +
344 'If specified together with --update, do the modifications ' +
345 'but produce output as if only --check was provided.')
346 parser
.add_option('--update-year', action
='store_true',
347 help='Update the copyright year if outdated')
348 parser
.add_option('--replace-years', action
='store_true',
349 help='Replace the copyright years with those given with --years')
350 parser
.add_option('--update-header', action
='store_true',
351 help='Update the copyright header if outdated')
352 parser
.add_option('--replace-header', action
='store_true',
353 help='Replace any copyright header with the current one')
354 parser
.add_option('--remove-old-copyrights', action
='store_true',
355 help='Remove copyright statements not in the new format')
356 parser
.add_option('--add-missing', action
='store_true',
357 help='Add missing copyright headers')
358 options
, args
= parser
.parse_args()
362 with
open(options
.files
, 'r') as filelist
:
363 filenames
= [x
.strip() for x
in filelist
.read().splitlines()]
367 # Default is --check if nothing provided.
368 if not options
.check
and not options
.update_year
and \
369 not options
.update_header
and not options
.replace_header
and \
370 not options
.add_missing
:
373 return options
, filenames
376 """Do processing as a stand-alone script."""
377 options
, filenames
= process_options()
378 years
= options
.years
380 years
= str(datetime
.date
.today().year
)
381 if years
.endswith(','):
384 checker
= CopyrightChecker()
386 # Process each input file in turn.
387 for filename
in filenames
:
388 comment_handler
= select_comment_handler(options
.lang
, filename
)
390 # Read the input file. We are doing an in-place operation, so can't
391 # operate in pass-through mode.
393 contents
= sys
.stdin
.read().splitlines()
394 reporter
= Reporter(sys
.stderr
, '<stdin>')
396 with
open(filename
, 'r') as inputfile
:
397 contents
= inputfile
.read().splitlines()
398 reporter
= Reporter(sys
.stdout
, filename
)
401 # Keep lines that must be at the beginning of the file and skip them in
403 if contents
and (contents
[0].startswith('#!/') or \
404 contents
[0].startswith('%code requires') or \
405 contents
[0].startswith('/* #if')):
406 output
.append(contents
[0])
407 contents
= contents
[1:]
408 # Remove and skip empty lines at the beginning.
409 while contents
and len(contents
[0]) == 0:
410 contents
= contents
[1:]
412 # Analyze the first comment block in the file.
413 comment_block
, line_count
= comment_handler
.extract_first_comment_block(contents
)
414 state
= checker
.check_copyright(comment_block
)
415 need_update
, file_years
= checker
.process_copyright(state
, options
, years
, reporter
)
416 if state
.other_copyrights
and options
.remove_old_copyrights
:
418 state
.other_copyrights
= []
419 reporter
.report('old copyrights removed')
422 # Remove the original comment if it was a copyright comment.
423 if state
.has_copyright
:
424 contents
= contents
[line_count
:]
425 new_block
= checker
.get_copyright_text(file_years
, state
.other_copyrights
)
426 output
.extend(comment_handler
.create_comment_block(new_block
))
428 # Write the output file if required.
429 if need_update
or filename
== '-':
430 # Append the rest of the input file as it was.
431 output
.extend(contents
)
432 output
= '\n'.join(output
) + '\n'
434 sys
.stdout
.write(output
)
436 with
open(filename
, 'w') as outputfile
:
437 outputfile
.write(output
)
439 if __name__
== "__main__":