8b589080cb0ff182286cc424811d6c8aa78a9fd8
[update-copyright.git] / update_copyright / vcs / __init__.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 """Backends for version control systems."""
20
21 import os.path as _os_path
22
23 from . import utils as _utils
24
25
26 class VCSBackend (object):
27     name = None
28
29     def __init__(self, root='.', author_hacks=None, year_hacks=None,
30                  aliases=None):
31         self._root = root
32         if author_hacks is None:
33             author_hacks = {}
34         self._author_hacks = author_hacks
35         if year_hacks is None:
36             year_hacks = {}
37         self._year_hacks = year_hacks
38         if aliases is None:
39             aliases = {}
40         self._aliases = aliases
41
42     def _years(self, filename=None):
43         raise NotImplementedError()
44
45     def original_year(self, filename=None):
46         years = self._years(filename=filename)
47         if filename is None:
48             years.update(self._year_hacks.values())
49         else:
50             filename = _os_path.relpath(filename, self._root)
51             if _utils.splitpath(filename) in self._year_hacks:
52                 years.add(self._year_hacks[_utils.splitpath(filename)])
53         years = sorted(years)
54         return years[0]
55
56     def _authors(self, filename=None):
57         raise NotImplementedError()
58
59     def authors(self, filename=None, with_emails=True):
60         authors = self._authors(filename=filename)
61         if filename is None:
62             for path,_authors in self._author_hacks.items():
63                 authors.update(_authors)
64         elif _utils.splitpath(filename) in self._author_hacks:
65             authors.update(self._author_hacks[_utils.splitpath(filename)])
66         return _utils.replace_aliases(
67             authors, with_email=with_emails, aliases=self._aliases)
68
69     def is_versioned(self, filename=None):
70         raise NotImplementedError()