1 # Copyright (C) 2012 W. Trevor King
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 import StringIO as _StringIO
22 import bzrlib as _bzrlib
23 import bzrlib.builtins as _bzrlib_builtins
24 import bzrlib.log as _bzrlib_log
26 from . import VCSBackend as _VCSBackend
29 class _LogFormatter (_bzrlib_log.LogFormatter):
30 supports_merge_revisions = True
36 def log_revision(self, revision):
37 raise NotImplementedError
40 class _YearLogFormatter (_LogFormatter):
41 def log_revision(self, revision):
43 time.strftime('%Y', time.gmtime(revision.rev.timestamp))
47 class _AuthorLogFormatter (_LogFormatter):
48 def log_revision(self, revision):
49 authors = revision.rev.get_apparent_authors()
50 self.to_file.write('\n'.join(authors)+'\n')
53 class BazaarBackend (_VCSBackend):
56 def __init__(self, **kwargs):
57 super(BazaarBackend, self).__init__(**kwargs)
58 self._version = _bzrlib.__version__
60 def _bzr_cmd(self, cmd, **kwargs):
68 def _years(self, filename=None):
69 cmd = _bzrlib_builtins.cmd_log()
70 cmd.outf = _StringIO.StringIO()
71 kwargs = {'log_format':_YearLogFormatter, 'levels':0}
72 if filename is not None:
73 kwargs['file_list'] = [filename]
74 self._bzr_cmd(cmd=cmd, **kwargs)
75 years = set(int(year) for year in cmd.outf.getvalue().splitlines())
78 def _authors(self, filename=None):
79 cmd = _bzrlib_builtins.cmd_log()
80 cmd.outf = _StringIO.StringIO()
81 kwargs = {'log_format':_AuthorLogFormatter, 'levels':0}
82 if filename is not None:
83 kwargs['file_list'] = [filename]
84 self._bzr_cmd(cmd=cmd, **kwargs)
85 authors = set(cmd.outf.getvalue().splitlines())
88 def is_versioned(self, filename):
89 cmd = _bzrlib_builtins.cmd_log()
90 cmd.outf = StringIO.StringIO()
91 self._bzr_cmd(cmd=cmd, file_list=[filename])