Add ability to run on a remote root based on the config file.
[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 import os as _os
21
22 import bzrlib as _bzrlib
23 import bzrlib.builtins as _bzrlib_builtins
24 import bzrlib.log as _bzrlib_log
25
26 from . import VCSBackend as _VCSBackend
27
28
29 class _LogFormatter (_bzrlib_log.LogFormatter):
30     supports_merge_revisions = True
31     preferred_levels = 0
32     supports_deta = False
33     supports_tags = False
34     supports_diff = False
35
36     def log_revision(self, revision):
37         raise NotImplementedError
38
39
40 class _YearLogFormatter (_LogFormatter):
41     def log_revision(self, revision):
42         self.to_file.write(
43             time.strftime('%Y', time.gmtime(revision.rev.timestamp))
44             +'\n')
45
46
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')
51
52
53 class BazaarBackend (_VCSBackend):
54     name = 'Bazaar'
55
56     def __init__(self, **kwargs):
57         super(BazaarBackend, self).__init__(**kwargs)
58         self._version = _bzrlib.__version__
59
60     def _bzr_cmd(self, cmd, **kwargs):
61         cwd = _os.getcwd()
62         _os.chdir(self._root)
63         try:
64             cmd.run(**kwargs)
65         finally:
66             _os.chdir(cwd)
67
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())
76         return years
77
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())
86         return authors
87
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])
92         return True