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
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 _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]
66 years = set(int(year) for year in cmd.outf.getvalue().splitlines())
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]
76 authors = set(cmd.outf.getvalue().splitlines())
79 def is_versioned(self, filename):
80 cmd = _bzrlib_builtins.cmd_log()
81 cmd.outf = StringIO.StringIO()
82 cmd.run(file_list=[filename])