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