1 # Copyright (C) 2012 W. Trevor King <wking@tremily.us>
3 # This file is part of update-copyright.
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
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
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/>.
18 import StringIO as _StringIO
21 import bzrlib as _bzrlib
22 import bzrlib.builtins as _bzrlib_builtins
23 import bzrlib.log as _bzrlib_log
25 from . import VCSBackend as _VCSBackend
28 class _LogFormatter (_bzrlib_log.LogFormatter):
29 supports_merge_revisions = True
35 def log_revision(self, revision):
36 raise NotImplementedError
39 class _YearLogFormatter (_LogFormatter):
40 def log_revision(self, revision):
42 time.strftime('%Y', time.gmtime(revision.rev.timestamp))
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')
52 class BazaarBackend (_VCSBackend):
55 def __init__(self, **kwargs):
56 super(BazaarBackend, self).__init__(**kwargs)
57 self._version = _bzrlib.__version__
59 def _bzr_cmd(self, cmd, **kwargs):
67 def _years(self, filename=None):
68 cmd = _bzrlib_builtins.cmd_log()
69 cmd.outf = _StringIO.StringIO()
70 kwargs = {'log_format':_YearLogFormatter, 'levels':0}
71 if filename is not None:
72 kwargs['file_list'] = [filename]
73 self._bzr_cmd(cmd=cmd, **kwargs)
74 years = set(int(year) for year in cmd.outf.getvalue().splitlines())
77 def _authors(self, filename=None):
78 cmd = _bzrlib_builtins.cmd_log()
79 cmd.outf = _StringIO.StringIO()
80 kwargs = {'log_format':_AuthorLogFormatter, 'levels':0}
81 if filename is not None:
82 kwargs['file_list'] = [filename]
83 self._bzr_cmd(cmd=cmd, **kwargs)
84 authors = set(cmd.outf.getvalue().splitlines())
87 def is_versioned(self, filename):
88 cmd = _bzrlib_builtins.cmd_log()
89 cmd.outf = StringIO.StringIO()
90 self._bzr_cmd(cmd=cmd, file_list=[filename])