Update exception handling to use `except X as Y` syntax.
[update-copyright.git] / update_copyright / vcs / bazaar.py
1 # Copyright (C) 2012 W. Trevor King <wking@tremily.us>
2 #
3 # This file is part of update-copyright.
4 #
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
8 # later version.
9 #
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
13 # more details.
14 #
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/>.
17
18 import StringIO as _StringIO
19 import os as _os
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 _bzr_cmd(self, cmd, **kwargs):
60         cwd = _os.getcwd()
61         _os.chdir(self._root)
62         try:
63             cmd.run(**kwargs)
64         finally:
65             _os.chdir(cwd)
66
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())
75         return years
76
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())
85         return authors
86
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])
91         return True