annotate sat/plugins/plugin_merge_req_mercurial.py @ 2594:59ba387c17ea

core (xmpp): use "phone" type in identities when backend is started from Android
author Goffi <goffi@goffi.org>
date Fri, 25 May 2018 10:52:01 +0200
parents 26edcf3a30eb
children 72f6f37ab648
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2449
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python2
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
3
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # SàT plugin for import external blogs
2483
0046283a285d dates update
Goffi <goffi@goffi.org>
parents: 2449
diff changeset
5 # Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org)
2449
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
6
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # (at your option) any later version.
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
11
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
16
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
19
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
20 from sat.core.i18n import _, D_
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
21 from sat.core.constants import Const as C
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from sat.core import exceptions
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from twisted.internet import reactor, defer, protocol
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from twisted.python.failure import Failure
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from twisted.python.procutils import which
2559
1362cabad225 plugin merge-request mercurial: better name sanitization for import
Goffi <goffi@goffi.org>
parents: 2544
diff changeset
26 import re
2449
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from sat.core.log import getLogger
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
28 log = getLogger(__name__)
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
29
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
30
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
31 PLUGIN_INFO = {
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
32 C.PI_NAME: "Mercurial Merge Request handler",
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 C.PI_IMPORT_NAME: "MERGE_REQUEST_MERCURIAL",
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
34 C.PI_TYPE: C.PLUG_TYPE_MISC,
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 C.PI_DEPENDENCIES: ["MERGE_REQUESTS"],
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
36 C.PI_MAIN: "MercurialHandler",
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 C.PI_HANDLER: "no",
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 C.PI_DESCRIPTION: _(u"""Merge request handler for Mercurial""")
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 }
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
40
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 SHORT_DESC = D_(u"handle Mercurial repository")
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
42
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
43
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
44 class MercurialProtocol(protocol.ProcessProtocol):
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 """handle hg commands"""
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
46 hg = None
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
47
2544
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
48 def __init__(self, deferred, stdin=None):
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
49 """
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
50 @param deferred(defer.Deferred): will be called when command is completed
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
51 @param stdin(str, None): if not None, will be push to standard input
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
52 """
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
53 self._stdin = stdin
2449
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 self._deferred = deferred
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
55 self.data = []
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
56
2544
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
57 def connectionMade(self):
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
58 if self._stdin is not None:
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
59 self.transport.write(self._stdin)
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
60 self.transport.closeStdin()
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
61
2449
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
62 def outReceived(self, data):
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
63 self.data.append(data)
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
64
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
65 def errReceived(self, data):
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 self.data.append(data)
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
67
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 def processEnded(self, reason):
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
69 data = u''.join([d.decode('utf-8') for d in self.data])
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 if (reason.value.exitCode == 0):
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 log.debug(_('Mercurial command succeed'))
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 self._deferred.callback(data)
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 else:
2544
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
74 msg = _(u"Can't complete Mercurial command (error code: {code}): {message}").format(
2449
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
75 code = reason.value.exitCode,
2544
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
76 message = data)
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
77 log.warning(msg)
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
78 self._deferred.errback(Failure(RuntimeError(msg)))
2449
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
79
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
80 @classmethod
2544
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
81 def run(cls, path, command, *args, **kwargs):
2449
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
82 """Create a new MercurialRegisterProtocol and execute the given mercurialctl command.
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
83
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
84 @param path(unicode): path to the repository
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
85 @param command(unicode): command to run
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
86 @param *args(unicode): command arguments
2544
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
87 @param **kwargs: used because Python2 doesn't handle normal kw args after *args
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
88 can only be:
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
89 - stdin(unicode, None): data to push to standard input
2449
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
90 @return ((D)):
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
91 """
2544
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
92 stdin = kwargs.pop('stdin', None)
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
93 if kwargs:
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
94 raise exceptions.InternalError(u'only stdin is allowed as keyword argument')
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
95 if stdin is not None:
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
96 stdin = stdin.encode('utf-8')
2449
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
97 d = defer.Deferred()
2544
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
98 mercurial_prot = MercurialProtocol(d, stdin=stdin)
2449
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
99 cmd_args = [cls.hg, command.encode('utf-8')]
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
100 cmd_args.extend([a.encode('utf-8') for a in args])
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
101 reactor.spawnProcess(mercurial_prot,
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
102 cls.hg,
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
103 cmd_args,
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
104 path=path.encode('utf-8'))
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
105 return d
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
106
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
107
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
108 class MercurialHandler(object):
2544
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
109 data_types = (u'mercurial_changeset',)
2449
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
110
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
111 def __init__(self, host):
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
112 log.info(_(u"Mercurial merge request handler initialization"))
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
113 try:
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
114 MercurialProtocol.hg = which('hg')[0]
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
115 except IndexError:
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
116 raise exceptions.NotFound(_(u"Mercurial executable (hg) not found, can't use Mercurial handler"))
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
117 self.host = host
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
118 self._m = host.plugins['MERGE_REQUESTS']
2544
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
119 self._m.register('mercurial', self, self.data_types, SHORT_DESC)
2449
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
120
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
121 def check(self, repository):
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
122 d = MercurialProtocol.run(repository, 'identify')
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
123 d.addCallback(lambda dummy: True)
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
124 d.addErrback(lambda dummy: False)
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
125 return d
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
126
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
127 def export(self, repository):
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
128 return MercurialProtocol.run(repository, 'export', '-g', '-r', 'outgoing()', '--encoding=utf-8')
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
129
2544
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
130 def import_(self, repository, data, data_type, item_id, service, node, extra):
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
131 parsed_data = self.parse(data)
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
132 try:
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
133 parsed_name = parsed_data[0][u'commit_msg'].split(u'\n')[0]
2561
bd30dc3ffe5a plugin merge-request mercurial: keeps more chars for import name
Goffi <goffi@goffi.org>
parents: 2560
diff changeset
134 parsed_name = re.sub(ur'[^\w -.]', u'', parsed_name, flags=re.UNICODE)[:40]
2544
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
135 except Exception:
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
136 parsed_name = u''
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
137 name = u'mr_{item_id}_{parsed_name}'.format(item_id=item_id, parsed_name=parsed_name)
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
138 return MercurialProtocol.run(repository, 'qimport', '-g', '--name', name, '--encoding=utf-8', '-', stdin=data)
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
139
2449
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
140 def parse(self, data, data_type=None):
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
141 lines = data.splitlines()
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
142 total_lines = len(lines)
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
143 patches = []
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
144 while lines:
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
145 patch = {}
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
146 commit_msg = []
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
147 diff = []
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
148 state = 'init'
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
149 if lines[0] != '# HG changeset patch':
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
150 raise exceptions.DataError(_(u'invalid changeset signature'))
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
151 # line index of this patch in the whole data
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
152 patch_idx = total_lines - len(lines)
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
153 del lines[0]
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
154
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
155 for idx, line in enumerate(lines):
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
156 if state == 'init':
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
157 if line.startswith(u'# '):
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
158 if line.startswith(u'# User '):
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
159 elems = line[7:].split()
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
160 if not elems:
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
161 continue
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
162 last = elems[-1]
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
163 if last.startswith(u'<') and last.endswith(u'>') and u'@' in last:
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
164 patch[self._m.META_EMAIL] = elems.pop()[1:-1]
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
165 patch[self._m.META_AUTHOR] = u' '.join(elems)
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
166 elif line.startswith(u'# Date '):
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
167 time_data = line[7:].split()
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
168 if len(time_data) != 2:
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
169 log.warning(_(u'unexpected time data: {data}').format(data=line[7:]))
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
170 continue
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
171 patch[self._m.META_TIMESTAMP] = int(time_data[0]) + int(time_data[1])
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
172 elif line.startswith(u'# Node ID '):
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
173 patch[self._m.META_HASH] = line[10:]
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
174 elif line.startswith(u'# Parent '):
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
175 patch[self._m.META_PARENT_HASH] = line[10:]
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
176 else:
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
177 state = 'commit_msg'
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
178 if state == 'commit_msg':
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
179 if line.startswith(u'diff --git a/'):
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
180 state = 'diff'
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
181 patch[self._m.META_DIFF_IDX] = patch_idx + idx + 1
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
182 else:
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
183 commit_msg.append(line)
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
184 if state == 'diff':
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
185 if line.startswith(u'# ') or idx == len(lines)-1:
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
186 # a new patch is starting or we have reached end of patches
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
187 patch[self._m.META_COMMIT_MSG] = u'\n'.join(commit_msg)
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
188 patch[self._m.META_DIFF] = u'\n'.join(diff)
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
189 patches.append(patch)
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
190 if idx == len(lines)-1:
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
191 del lines[:]
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
192 else:
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
193 del lines[:idx]
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
194 break
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
195 else:
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
196 diff.append(line)
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
197 return patches