Ran update-copyright.
[update-copyright.git] / update_copyright / vcs / __init__.py
1 # Copyright (C) 2012 W. Trevor King <wking@tremily.us>
2 #
3 # This file is part of update-copyright.
4 #
5 # update-copyright is free software: you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by the Free
7 # Software Foundation, either version 3 of the License, or (at your option) any
8 # later version.
9 #
10 # update-copyright is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13 # more details.
14 #
15 # You should have received a copy of the GNU General Public License along with
16 # update-copyright.  If not, see <http://www.gnu.org/licenses/>.
17
18 """Backends for version control systems."""
19
20 import os.path as _os_path
21
22 from . import utils as _utils
23
24
25 class VCSBackend (object):
26     name = None
27
28     def __init__(self, root='.', author_hacks=None, year_hacks=None,
29                  aliases=None):
30         self._root = root
31         if author_hacks is None:
32             author_hacks = {}
33         self._author_hacks = author_hacks
34         if year_hacks is None:
35             year_hacks = {}
36         self._year_hacks = year_hacks
37         if aliases is None:
38             aliases = {}
39         self._aliases = aliases
40
41     def _years(self, filename=None):
42         raise NotImplementedError()
43
44     def original_year(self, filename=None):
45         years = self._years(filename=filename)
46         if filename is None:
47             years.update(self._year_hacks.values())
48         else:
49             filename = _os_path.relpath(filename, self._root)
50             if _utils.splitpath(filename) in self._year_hacks:
51                 years.add(self._year_hacks[_utils.splitpath(filename)])
52         years = sorted(years)
53         return years[0]
54
55     def _authors(self, filename=None):
56         raise NotImplementedError()
57
58     def authors(self, filename=None, with_emails=True):
59         authors = self._authors(filename=filename)
60         if filename is None:
61             for path,_authors in self._author_hacks.items():
62                 authors.update(_authors)
63         elif _utils.splitpath(filename) in self._author_hacks:
64             authors.update(self._author_hacks[_utils.splitpath(filename)])
65         return _utils.replace_aliases(
66             authors, with_email=with_emails, aliases=self._aliases)
67
68     def is_versioned(self, filename=None):
69         raise NotImplementedError()