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 from __future__ import absolute_import
20 import StringIO as _StringIO
24 import mercurial as _mercurial
25 from mercurial.__version__ import version as _version
26 import mercurial.dispatch as _mercurial_dispatch
28 from . import VCSBackend as _VCSBackend
29 from . import utils as _utils
32 class MercurialBackend (_VCSBackend):
35 def __init__(self, **kwargs):
36 super(MercurialBackend, self).__init__(**kwargs)
37 self._version = _version
43 tmp_stdout = _StringIO.StringIO()
44 tmp_stderr = _StringIO.StringIO()
45 _sys.stdout = tmp_stdout
46 _sys.stderr = tmp_stderr
49 _mercurial_dispatch.dispatch(list(args))
54 return (tmp_stdout.getvalue().rstrip('\n'),
55 tmp_stderr.getvalue().rstrip('\n'))
57 def _years(self, filename=None):
59 '--template', '{date|shortdate}\n',
60 # shortdate filter: YEAR-MONTH-DAY
62 if filename is not None:
63 args.extend(['--follow', filename])
64 output,error = mercurial_cmd('log', *args)
65 years = set(int(line.split('-', 1)[0]) for line in output.splitlines())
68 def _authors(self, filename=None):
69 args = ['--template', '{author}\n']
70 if filename is not None:
71 args.extend(['--follow', filename])
72 output,error = mercurial_cmd('log', *args)
73 authors = set(output.splitlines())
76 def is_versioned(self, filename):
77 output,error = mercurial_cmd('log', '--follow', filename)