1 # Copyright (C) 2012 W. Trevor King
3 # This file is part of update-copyright.
5 # update-copyright is free software: you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License as
7 # published by the Free Software Foundation, either version 3 of the
8 # License, or (at your option) any later version.
10 # update-copyright is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with update-copyright. If not, see
17 # <http://www.gnu.org/licenses/>.
19 import difflib as _difflib
21 import os.path as _os_path
22 import textwrap as _textwrap
25 from . import LOG as _LOG
28 def long_author_formatter(copyright_year_string, authors):
30 >>> print '\\n'.join(long_author_formatter(
31 ... copyright_year_string='Copyright (C) 1990-2010',
32 ... authors=['Jack', 'Jill', 'John']))
33 Copyright (C) 1990-2010 Jack
37 lines = ['%s %s' % (copyright_year_string, authors[0])]
38 for author in authors[1:]:
39 lines.append(' '*(len(copyright_year_string)+1) + author)
42 def short_author_formatter(copyright_year_string, authors):
44 >>> print '\\n'.join(short_author_formatter(
45 ... copyright_year_string='Copyright (C) 1990-2010',
46 ... authors=['Jack', 'Jill', 'John']*5))
47 Copyright (C) 1990-2010 Jack, Jill, John, Jack, Jill, John, Jack, Jill, John, Jack, Jill, John, Jack, Jill, John
49 blurb = '%s %s' % (copyright_year_string, ', '.join(authors))
52 def copyright_string(original_year, final_year, authors, text, info={},
53 author_format_fn=long_author_formatter,
54 formatter_kwargs={}, prefix='', wrap=True,
57 >>> print(copyright_string(original_year=2005, final_year=2005,
58 ... authors=['A <a@a.com>', 'B <b@b.edu>'],
59 ... text=['BLURB',], prefix='# '
60 ... )) # doctest: +REPORT_UDIFF
61 # Copyright (C) 2005 A <a@a.com>
65 >>> print(copyright_string(original_year=2005, final_year=2009,
66 ... authors=['A <a@a.com>', 'B <b@b.edu>'],
68 ... )) # doctest: +REPORT_UDIFF
69 Copyright (C) 2005-2009 A <a@a.com>
73 >>> print(copyright_string(original_year=2005, final_year=2005,
74 ... authors=['A <a@a.com>', 'B <b@b.edu>'],
75 ... text=['This file is part of %(program)s.',],
76 ... author_format_fn=short_author_formatter,
77 ... info={'program':'update-copyright'},
79 ... )) # doctest: +REPORT_UDIFF
80 Copyright (C) 2005 A <a@a.com>, B <b@b.edu>
84 >>> print(copyright_string(original_year=2005, final_year=2005,
85 ... authors=['A <a@a.com>', 'B <b@b.edu>'],
86 ... text=[('This file is part of %(program)s. '*3
88 ... info={'program':'update-copyright'},
89 ... author_format_fn=short_author_formatter,
91 ... )) # doctest: +REPORT_UDIFF
92 Copyright (C) 2005 A <a@a.com>, B <b@b.edu>
94 This file is part of update-copyright. This file is part of update-copyright. This file is part of update-copyright.
96 for key in ['initial_indent', 'subsequent_indent']:
97 if key not in wrap_kwargs:
98 wrap_kwargs[key] = prefix
100 if original_year == final_year:
101 date_range = '%s' % original_year
103 date_range = '%s-%s' % (original_year, final_year)
104 copyright_year_string = 'Copyright (C) %s' % date_range
106 lines = author_format_fn(copyright_year_string, authors,
108 for i,line in enumerate(lines):
109 lines[i] = prefix + line
111 for i,paragraph in enumerate(text):
113 text[i] = paragraph % info
114 except ValueError, e:
116 "{}: can't format {} with {}".format(e, paragraph, info))
120 ('{}: copright text must be a list of paragraph strings, '
121 'not {}').format(e, repr(text)))
125 text = [_textwrap.fill(p, **wrap_kwargs) for p in text]
127 assert wrap_kwargs['subsequent_indent'] == '', \
128 wrap_kwargs['subsequent_indent']
129 sep = '\n%s\n' % prefix.rstrip()
130 return sep.join(['\n'.join(lines)] + text)
132 def tag_copyright(contents, tag=None):
134 >>> contents = '''Some file
136 ... # Copyright (copyright begins)
137 ... # (copyright continues)
142 >>> print tag_copyright(contents, tag='-xyz-CR-zyx-')
152 for line in contents.splitlines():
153 if incopy == False and line.startswith('# Copyright'):
156 elif incopy == True and not line.startswith('#'):
159 lines.append(line.rstrip('\n'))
160 return '\n'.join(lines)+'\n'
162 def update_copyright(contents, tag=None, **kwargs):
164 >>> contents = '''Some file
166 ... # Copyright (copyright begins)
167 ... # (copyright continues)
172 >>> print update_copyright(contents, original_year=2008,
173 ... authors=['Jack', 'Jill'],
174 ... text=['BLURB',], prefix='# ', tag='--tag--'
175 ... ) # doctest: +ELLIPSIS, +REPORT_UDIFF
178 # Copyright (C) 2008-... Jack
186 current_year = _time.gmtime()[0]
187 string = copyright_string(final_year=current_year, **kwargs)
188 contents = tag_copyright(contents=contents, tag=tag)
189 return contents.replace(tag, string)
191 def get_contents(filename):
192 if _os_path.isfile(filename):
193 f = open(filename, 'r')
199 def set_contents(filename, contents, original_contents=None, dry_run=False):
200 if original_contents is None:
201 original_contents = get_contents(filename=filename)
202 _LOG.debug('check contents of {}'.format(filename))
203 if contents != original_contents:
204 if original_contents is None:
205 _LOG.info('creating {}'.format(filename))
207 _LOG.info('updating {}'.format(filename))
208 _LOG.debug('\n'.join(
209 _difflib.unified_diff(
210 original_contents.splitlines(), contents.splitlines(),
211 fromfile=_os_path.normpath(
212 _os_path.join('a', filename)),
213 tofile=_os_path.normpath(_os_path.join('b', filename)),
216 f = file(filename, 'w')
219 _LOG.debug('no change in {}'.format(filename))
221 def list_files(root='.'):
222 for dirpath,dirnames,filenames in _os.walk(root):
223 for filename in filenames:
224 yield _os_path.join(root, dirpath, filename)