From: W. Trevor King Date: Wed, 25 Aug 2010 12:34:18 +0000 (-0400) Subject: Update update_copyright.py to handle several comment characters. X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=79d10fa69a700c58ef9f35a7f58fcf6830fc6b5e;p=chemdb.git Update update_copyright.py to handle several comment characters. Currently extended from '#' to ['#', '%'] to support % Copyright in LaTeX files, but should be flexible enough to handle any character (or string) by placing the string in the COMMENT_CHARS list. --- diff --git a/update_copyright.py b/update_copyright.py index 60b929b..22f1b9a 100755 --- a/update_copyright.py +++ b/update_copyright.py @@ -2,20 +2,20 @@ # # Copyright (C) 2010 W. Trevor King # -# This file is part of Cookbook. +# This file is part of ChemDB. # -# Cookbook is free software: you can redistribute it and/or modify it +# ChemDB is free software: you can redistribute it and/or modify it # under the terms of the GNU General Public License as published by the # Free Software Foundation, either version 3 of the License, or (at your # option) any later version. # -# Cookbook is distributed in the hope that it will be useful, +# ChemDB is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License -# along with Cookbook. If not, see . +# along with ChemDB. If not, see . """Automatically update copyright boilerplate. @@ -58,6 +58,7 @@ along with %(project)s. If not, see . """.strip() COPY_RIGHT_TAG='-xyz-COPY' + '-RIGHT-zyx-' # unlikely to occur in the wild :p +COMMENT_CHARS=['#', '%'] # allowed comment characters # Convert author names to canonical forms. # ALIASES[] = @@ -441,19 +442,33 @@ def _tag_copyright(contents): >>> print _tag_copyright(contents).replace('COPY-RIGHT', 'CR') Some file bla bla - -xyz-CR-zyx- + -xyz-CR-zyx-# + (copyright ends) + bla bla bla + + >>> contents = contents.replace('#', '%') + >>> print _tag_copyright(contents).replace('COPY-RIGHT', 'CR') + Some file + bla bla + -xyz-CR-zyx-% (copyright ends) bla bla bla """ lines = [] incopy = False + comment_char = None for line in contents.splitlines(): - if incopy == False and line.startswith('# Copyright'): - incopy = True - lines.append(COPY_RIGHT_TAG) - elif incopy == True and not line.startswith('#'): + if incopy == False: + for c in COMMENT_CHARS: + if line.startswith('%s Copyright' % c): + incopy = True + comment_char = c + lines.append(COPY_RIGHT_TAG + c) + continue + elif incopy == True and not line.startswith(comment_char): incopy = False + comment_char = None if incopy == False: lines.append(line.rstrip('\n')) return '\n'.join(lines)+'\n' @@ -479,12 +494,33 @@ def _update_copyright(contents, original_year, authors): (copyright ends) bla bla bla + >>> contents = contents.replace('#', '%') + >>> print _update_copyright(contents, 2008, ['Jack', 'Jill'] + ... ) # doctest: +ELLIPSIS, +REPORT_UDIFF + Some file + bla bla + % Copyright (C) 2008-... Jack + % Jill + % + % This file... + (copyright ends) + bla bla bla + """ current_year = time.gmtime()[0] - copyright_string = _copyright_string( - original_year, current_year, authors, prefix='# ') contents = _tag_copyright(contents) - return contents.replace(COPY_RIGHT_TAG, copyright_string) + lines = [] + for line in contents.splitlines(): + if line.startswith(COPY_RIGHT_TAG): + comment_char = line[len(COPY_RIGHT_TAG):] + copyright_string = _copyright_string( + original_year, current_year, authors, + prefix='%s ' % comment_char) + lines.append(copyright_string.rstrip('\n')) + else: + lines.append(line.rstrip('\n')) + return '\n'.join(lines)+'\n' + def ignored_file(filename, ignored_paths=None, ignored_files=None, check_disk=True, check_vcs=True):