Run update-copyright on itself.
[update-copyright.git] / update_copyright / vcs / bazaar.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 import StringIO as _StringIO
20
21 import bzrlib as _bzrlib
22 import bzrlib.builtins as _bzrlib_builtins
23 import bzrlib.log as _bzrlib_log
24
25 from . import VCSBackend as _VCSBackend
26
27
28 class _LogFormatter (_bzrlib_log.LogFormatter):
29     supports_merge_revisions = True
30     preferred_levels = 0
31     supports_deta = False
32     supports_tags = False
33     supports_diff = False
34
35     def log_revision(self, revision):
36         raise NotImplementedError
37
38
39 class _YearLogFormatter (_LogFormatter):
40     def log_revision(self, revision):
41         self.to_file.write(
42             time.strftime('%Y', time.gmtime(revision.rev.timestamp))
43             +'\n')
44
45
46 class _AuthorLogFormatter (_LogFormatter):
47     def log_revision(self, revision):
48         authors = revision.rev.get_apparent_authors()
49         self.to_file.write('\n'.join(authors)+'\n')
50
51
52 class BazaarBackend (_VCSBackend):
53     name = 'Bazaar'
54
55     def __init__(self, **kwargs):
56         super(BazaarBackend, self).__init__(**kwargs)
57         self._version = _bzrlib.__version__
58
59     def _years(self, filename=None):
60         cmd = _bzrlib_builtins.cmd_log()
61         cmd.outf = _StringIO.StringIO()
62         kwargs = {'log_format':_YearLogFormatter, 'levels':0}
63         if filename is not None:
64             kwargs['file_list'] = [filename]
65         cmd.run(**kwargs)
66         years = set(int(year) for year in cmd.outf.getvalue().splitlines())
67         return years
68
69     def _authors(self, filename=None):
70         cmd = _bzrlib_builtins.cmd_log()
71         cmd.outf = _StringIO.StringIO()
72         kwargs = {'log_format':_AuthorLogFormatter, 'levels':0}
73         if filename is not None:
74             kwargs['file_list'] = [filename]
75         cmd.run(**kwargs)
76         authors = set(cmd.outf.getvalue().splitlines())
77         return authors
78
79     def is_versioned(self, filename):
80         cmd = _bzrlib_builtins.cmd_log()
81         cmd.outf = StringIO.StringIO()
82         cmd.run(file_list=[filename])
83         return True