#
# Copyright (C) 2010 W. Trevor King <wking@drexel.edu>
#
-# 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 <http://www.gnu.org/licenses/>.
+# along with ChemDB. If not, see <http://www.gnu.org/licenses/>.
"""Automatically update copyright boilerplate.
""".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[<canonical name>] = <list of aliases>
>>> print _tag_copyright(contents).replace('COPY-RIGHT', 'CR')
Some file
bla bla
- -xyz-CR-zyx-
+ -xyz-CR-zyx-#
+ (copyright ends)
+ bla bla bla
+ <BLANKLINE>
+ >>> contents = contents.replace('#', '%')
+ >>> print _tag_copyright(contents).replace('COPY-RIGHT', 'CR')
+ Some file
+ bla bla
+ -xyz-CR-zyx-%
(copyright ends)
bla bla bla
<BLANKLINE>
"""
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'
(copyright ends)
bla bla bla
<BLANKLINE>
+ >>> 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
+ <BLANKLINE>
"""
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):