Allow '-' characters in section names.
[update-copyright.git] / update_copyright / project.py
1 # Copyright (C) 2012 W. Trevor King
2 #
3 # This file is part of update-copyright.
4 #
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.
9 #
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.
14 #
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/>.
18
19 """Project-specific configuration.
20
21 # Convert author names to canonical forms.
22 # ALIASES[<canonical name>] = <list of aliases>
23 # for example,
24 # ALIASES = {
25 #     'John Doe <jdoe@a.com>':
26 #         ['John Doe', 'jdoe', 'J. Doe <j@doe.net>'],
27 #     }
28 # Git-based projects are encouraged to use .mailmap instead of
29 # ALIASES.  See git-shortlog(1) for details.
30
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']
37
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:
42 # AUTHOR_HACKS = {
43 #     ('path', 'to', 'module.py'):['John Doe'],
44 #     }
45 AUTHOR_HACKS = {}
46
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:
51 # YEAR_HACKS = {
52 #     ('path', 'to', 'module.py'):2008,
53 #     }
54 YEAR_HACKS = {}
55 """
56
57 import ConfigParser as _configparser
58 import fnmatch as _fnmatch
59 import os.path as _os_path
60 import sys
61 import time as _time
62
63 from . import LOG as _LOG
64 from . import utils as _utils
65 from .vcs.git import GitBackend as _GitBackend
66 try:
67     from .vcs.bazaar import BazaarBackend as _BazaarBackend
68 except ImportError, _bazaar_import_error:
69     _BazaarBackend = None
70 try:
71     from .vcs.mercurial import MercurialBackend as _MercurialBackend
72 except ImportError, _mercurial_import_error:
73     _MercurialBackend = None
74
75
76 class Project (object):
77     def __init__(self, name=None, vcs=None, copyright=None,
78                  short_copyright=None):
79         self._name = name
80         self._vcs = vcs
81         self._copyright = None
82         self._short_copyright = None
83         self.with_authors = False
84         self.with_files = False
85         self._ignored_paths = None
86         self._pyfile = None
87         self._encoding = None
88         self._width = 79
89
90         # unlikely to occur in the wild :p
91         self._copyright_tag = u'-xyz-COPY' + u'-RIGHT-zyx-'
92
93     def load_config(self, stream):
94         parser = _configparser.RawConfigParser()
95         parser.readfp(stream)
96         for section in parser.sections():
97             clean_section = section.replace('-', '_')
98             try:
99                 loader = getattr(self, '_load_{}_conf'.format(clean_section))
100             except AttributeError, e:
101                 _LOG.error('invalid {} section'.format(section))
102                 raise
103             loader(parser=parser)
104
105     def _load_project_conf(self, parser):
106         try:
107             self._name = parser.get('project', 'name')
108         except _configparser.NoOptionError:
109             pass
110         try:
111             vcs = parser.get('project', 'vcs')
112         except _configparser.NoOptionError:
113             pass
114         else:
115             if vcs == 'Git':
116                 self._vcs = _GitBackend()
117             elif vcs == 'Bazaar':
118                 if _BazaarBackend is None:
119                     raise _bazaar_import_error
120                 self._vcs = _BazaarBackend()
121             elif vcs == 'Mercurial':
122                 if _MercurialBackend is None:
123                     raise _mercurial_import_error
124                 self._vcs = _MercurialBackend()
125             else:
126                 raise NotImplementedError('vcs: {}'.format(vcs))
127
128     def _load_copyright_conf(self, parser):
129         try:
130             self._copyright = parser.get('copyright', 'long').splitlines()
131         except _configparser.NoOptionError:
132             pass
133         try:
134             self._short_copyright = parser.get(
135                 'copyright', 'short').splitlines()
136         except _configparser.NoOptionError:
137             pass
138
139     def _load_files_conf(self, parser):
140         try:
141             self.with_authors = parser.get('files', 'authors')
142         except _configparser.NoOptionError:
143             pass
144         try:
145             self.with_files = parser.get('files', 'files')
146         except _configparser.NoOptionError:
147             pass
148         try:
149             ignored = parser.get('files', 'ignored')
150         except _configparser.NoOptionError:
151             pass
152         else:
153             self._ignored_paths = [pth.strip() for pth in ignored.split(',')]
154         try:
155             self._pyfile = parser.get('files', 'pyfile')
156         except _configparser.NoOptionError:
157             pass
158
159     def _info(self):
160         return {
161             'project': self._name,
162             'vcs': self._vcs.name,
163             }
164
165     def update_authors(self, dry_run=False):
166         _LOG.info('update AUTHORS')
167         authors = self._vcs.authors()
168         new_contents = u'{} was written by:\n{}\n'.format(
169             self._name, u'\n'.join(authors))
170         _utils.set_contents(
171             'AUTHORS', new_contents, unicode=True, encoding=self._encoding,
172             dry_run=dry_run)
173
174     def update_file(self, filename, dry_run=False):
175         _LOG.info('update {}'.format(filename))
176         contents = _utils.get_contents(
177             filename=filename, unicode=True, encoding=self._encoding)
178         original_year = self._vcs.original_year(filename=filename)
179         authors = self._vcs.authors(filename=filename)
180         new_contents = _utils.update_copyright(
181             contents=contents, original_year=original_year, authors=authors,
182             text=self._copyright, info=self._info(), prefix='# ',
183             width=self._width, tag=self._copyright_tag)
184         _utils.set_contents(
185             filename=filename, contents=new_contents,
186             original_contents=contents, unicode=True, encoding=self._encoding,
187             dry_run=dry_run)
188
189     def update_files(self, files=None, dry_run=False):
190         if files is None or len(files) == 0:
191             files = _utils.list_files(root='.')
192         for filename in files:
193             if self._ignored_file(filename=filename):
194                 continue
195             self.update_file(filename=filename, dry_run=dry_run)
196
197     def update_pyfile(self, dry_run=False):
198         if self._pyfile is None:
199             _LOG.info('no pyfile location configured, skip `update_pyfile`')
200             return
201         _LOG.info('update pyfile at {}'.format(self._pyfile))
202         current_year = _time.gmtime()[0]
203         original_year = self._vcs.original_year()
204         authors = self._vcs.authors()
205         lines = [
206             _utils.copyright_string(
207                 original_year=original_year, final_year=current_year,
208                 authors=authors, text=self._copyright, info=self._info(),
209                 prefix=u'# ', width=self._width),
210             u'', u'import textwrap as _textwrap', u'', u'',
211             u'LICENSE = """',
212             _utils.copyright_string(
213                 original_year=original_year, final_year=current_year,
214                 authors=authors, text=self._copyright, info=self._info(),
215                 prefix=u'', width=self._width),
216             u'""".strip()',
217             u'',
218             u'def short_license(info, wrap=True, **kwargs):',
219             u'    paragraphs = [',
220             ]
221         paragraphs = _utils.copyright_string(
222             original_year=original_year, final_year=current_year,
223             authors=authors, text=self._short_copyright, info=self._info(),
224             author_format_fn=_utils.short_author_formatter, wrap=False,
225             ).split(u'\n\n')
226         for p in paragraphs:
227             lines.append(u"        '{}' % info,".format(
228                     p.replace(u"'", ur"\'")))
229         lines.extend([
230                 u'        ]',
231                 u'    if wrap:',
232                 u'        for i,p in enumerate(paragraphs):',
233                 u'            paragraphs[i] = _textwrap.fill(p, **kwargs)',
234                 ur"    return '\n\n'.join(paragraphs)",
235                 u'',  # for terminal endline
236                 ])
237         new_contents = u'\n'.join(lines)
238         _utils.set_contents(
239             filename=self._pyfile, contents=new_contents, unicode=True,
240             encoding=self._encoding, dry_run=dry_run)
241
242     def _ignored_file(self, filename):
243         """
244         >>> ignored_paths = ['./a/', './b/']
245         >>> ignored_files = ['x', 'y']
246         >>> ignored_file('./a/z', ignored_paths, ignored_files, False, False)
247         True
248         >>> ignored_file('./ab/z', ignored_paths, ignored_files, False, False)
249         False
250         >>> ignored_file('./ab/x', ignored_paths, ignored_files, False, False)
251         True
252         >>> ignored_file('./ab/xy', ignored_paths, ignored_files, False, False)
253         False
254         >>> ignored_file('./z', ignored_paths, ignored_files, False, False)
255         False
256         """
257         if self._ignored_paths is not None:
258             for path in self._ignored_paths:
259                 if _fnmatch.fnmatch(filename, path):
260                     _LOG.debug('ignoring {} (matched {})'.format(
261                             filename, path))
262                     return True
263         if self._vcs and not self._vcs.is_versioned(filename):
264             _LOG.debug('ignoring {} (not versioned))'.format(filename))
265             return True
266         return False