1 # Copyright (C) 2009-2012 W. Trevor King <wking@drexel.edu>
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 """Project-specific configuration.
21 # Convert author names to canonical forms.
22 # ALIASES[<canonical name>] = <list of aliases>
25 # 'John Doe <jdoe@a.com>':
26 # ['John Doe', 'jdoe', 'J. Doe <j@doe.net>'],
28 # Git-based projects are encouraged to use .mailmap instead of
29 # ALIASES. See git-shortlog(1) for details.
31 # List of paths that should not be scanned for copyright updates.
32 # IGNORED_PATHS = ['./.git/']
33 IGNORED_PATHS = ['./.git']
34 # List of files that should not be scanned for copyright updates.
35 # IGNORED_FILES = ['COPYING']
36 IGNORED_FILES = ['COPYING']
38 # Work around missing author holes in the VCS history.
39 # AUTHOR_HACKS[<path tuple>] = [<missing authors]
40 # for example, if John Doe contributed to module.py but wasn't listed
41 # in the VCS history of that file:
43 # ('path', 'to', 'module.py'):['John Doe'],
47 # Work around missing year holes in the VCS history.
48 # YEAR_HACKS[<path tuple>] = <original year>
49 # for example, if module.py was published in 2008 but the VCS history
50 # only goes back to 2010:
52 # ('path', 'to', 'module.py'):2008,
57 import ConfigParser as _configparser
58 import fnmatch as _fnmatch
59 import os.path as _os_path
63 from . import LOG as _LOG
64 from . import utils as _utils
65 from .vcs.git import GitBackend as _GitBackend
67 from .vcs.bazaar import BazaarBackend as _BazaarBackend
68 except ImportError, _bazaar_import_error:
71 from .vcs.mercurial import MercurialBackend as _MercurialBackend
72 except ImportError, _mercurial_import_error:
73 _MercurialBackend = None
76 class Project (object):
77 def __init__(self, name=None, vcs=None, copyright=None,
78 short_copyright=None):
81 self._copyright = None
82 self._short_copyright = None
83 self.with_authors = False
84 self.with_files = False
85 self._ignored_paths = None
88 # unlikely to occur in the wild :p
89 self._copyright_tag = '-xyz-COPY' + '-RIGHT-zyx-'
91 def load_config(self, stream):
92 p = _configparser.RawConfigParser()
95 self._name = p.get('project', 'name')
96 except _configparser.NoOptionError:
99 vcs = p.get('project', 'vcs')
100 except _configparser.NoOptionError:
104 self._vcs = _GitBackend()
105 elif vcs == 'Bazaar':
106 self._vcs = _BazaarBackend()
107 elif vcs == 'Mercurial':
108 self._vcs = _MercurialBackend()
110 raise NotImplementedError('vcs: {}'.format(vcs))
112 self._copyright = p.get('copyright', 'long').splitlines()
113 except _configparser.NoOptionError:
116 self._short_copyright = p.get('copyright', 'short').splitlines()
117 except _configparser.NoOptionError:
120 self.with_authors = p.get('files', 'authors')
121 except _configparser.NoOptionError:
124 self.with_files = p.get('files', 'files')
125 except _configparser.NoOptionError:
128 self._ignored_paths = p.get('files', 'ignored')
129 except _configparser.NoOptionError:
132 self._pyfile = p.get('files', 'pyfile')
133 except _configparser.NoOptionError:
138 'project': self._name,
139 'vcs': self._vcs.name,
142 def update_authors(self, dry_run=False):
143 _LOG.info('update AUTHORS')
144 authors = self._vcs.authors()
145 new_contents = u'{} was written by:\n{}\n'.format(
146 self._name, u'\n'.join(authors))
147 _utils.set_contents('AUTHORS', new_contents, dry_run=dry_run)
149 def update_file(self, filename, dry_run=False):
150 _LOG.info('update {}'.format(filename))
151 contents = _utils.get_contents(filename=filename)
152 original_year = self._vcs.original_year(filename=filename)
153 authors = self._vcs.authors(filename=filename)
154 new_contents = _utils.update_copyright(
155 contents=contents, original_year=original_year, authors=authors,
156 text=self._copyright, info=self._info(), prefix='# ',
157 tag=self._copyright_tag)
159 filename=filename, contents=new_contents,
160 original_contents=contents, dry_run=dry_run)
162 def update_files(self, files=None, dry_run=False):
163 if files is None or len(files) == 0:
164 files = _utils.list_files(root='.')
165 for filename in files:
166 if self._ignored_file(filename=filename):
168 self.update_file(filename=filename, dry_run=dry_run)
170 def update_pyfile(self, dry_run=False):
171 if self._pyfile is None:
172 _LOG.info('no pyfile location configured, skip `update_pyfile`')
174 _LOG.info('update pyfile at {}'.format(self._pyfile))
175 current_year = _time.gmtime()[0]
176 original_year = self._vcs.original_year()
177 authors = self._vcs.authors()
179 _utils.copyright_string(
180 original_year=original_year, final_year=current_year,
181 authors=authors, text=self._copyright, info=self._info(),
183 '', 'import textwrap as _textwrap', '', '',
185 _utils.copyright_string(
186 original_year=original_year, final_year=current_year,
187 authors=authors, text=self._copyright, info=self._info(),
191 'def short_license(info, wrap=True, **kwargs):',
194 paragraphs = _utils.copyright_string(
195 original_year=original_year, final_year=current_year,
196 authors=authors, text=self._short_copyright, info=self._info(),
197 author_format_fn=_utils.short_author_formatter, wrap=False,
200 lines.append(" '{}' % info,".format(
201 p.replace("'", r"\'")))
205 ' for i,p in enumerate(paragraphs):',
206 ' paragraphs[i] = _textwrap.fill(p, **kwargs)',
207 r" return '\n\n'.join(paragraphs)",
208 '', # for terminal endline
210 new_contents = '\n'.join(lines)
212 filename=self._pyfile, contents=new_contents, dry_run=dry_run)
214 def _ignored_file(self, filename):
216 >>> ignored_paths = ['./a/', './b/']
217 >>> ignored_files = ['x', 'y']
218 >>> ignored_file('./a/z', ignored_paths, ignored_files, False, False)
220 >>> ignored_file('./ab/z', ignored_paths, ignored_files, False, False)
222 >>> ignored_file('./ab/x', ignored_paths, ignored_files, False, False)
224 >>> ignored_file('./ab/xy', ignored_paths, ignored_files, False, False)
226 >>> ignored_file('./z', ignored_paths, ignored_files, False, False)
229 if self._ignored_paths is not None:
230 for path in self._ignored_paths:
231 if _fnmatch.fnmatch(filename, path):
233 if self._vcs and not self._vcs.is_versioned(filename):