Mercurial > libervia-backend
annotate sat/plugins/plugin_merge_req_mercurial.py @ 2620:72f6f37ab648
core: some more line limiting
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 27 Jun 2018 07:23:13 +0200 |
parents | 26edcf3a30eb |
children | 2f75830a8228 |
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 | 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: |
2620 | 74 msg = (_(u"Can't complete Mercurial command (error code: {code}): {message}") |
75 .format(code = reason.value.exitCode, message = data)) | |
2544
a64887289931
plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
76 log.warning(msg) |
a64887289931
plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
77 self._deferred.errback(Failure(RuntimeError(msg))) |
2449
67942ba2ee55
plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
78 |
67942ba2ee55
plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
79 @classmethod |
2544
a64887289931
plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
80 def run(cls, path, command, *args, **kwargs): |
2620 | 81 """Create a new MercurialRegisterProtocol and execute the given mercurial command. |
2449
67942ba2ee55
plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
82 |
67942ba2ee55
plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
83 @param path(unicode): path to the repository |
67942ba2ee55
plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
84 @param command(unicode): command to run |
67942ba2ee55
plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
85 @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
|
86 @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
|
87 can only be: |
a64887289931
plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
88 - stdin(unicode, None): data to push to standard input |
2449
67942ba2ee55
plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
89 @return ((D)): |
67942ba2ee55
plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
90 """ |
2544
a64887289931
plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
91 stdin = kwargs.pop('stdin', None) |
a64887289931
plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
92 if kwargs: |
a64887289931
plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
93 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
|
94 if stdin is not None: |
a64887289931
plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
95 stdin = stdin.encode('utf-8') |
2449
67942ba2ee55
plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
96 d = defer.Deferred() |
2544
a64887289931
plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
97 mercurial_prot = MercurialProtocol(d, stdin=stdin) |
2449
67942ba2ee55
plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
98 cmd_args = [cls.hg, command.encode('utf-8')] |
67942ba2ee55
plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
99 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
|
100 reactor.spawnProcess(mercurial_prot, |
67942ba2ee55
plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
101 cls.hg, |
67942ba2ee55
plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
102 cmd_args, |
67942ba2ee55
plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
103 path=path.encode('utf-8')) |
67942ba2ee55
plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
104 return d |
67942ba2ee55
plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
105 |
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 class MercurialHandler(object): |
2544
a64887289931
plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
108 data_types = (u'mercurial_changeset',) |
2449
67942ba2ee55
plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
109 |
67942ba2ee55
plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
110 def __init__(self, host): |
67942ba2ee55
plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
111 log.info(_(u"Mercurial merge request handler initialization")) |
67942ba2ee55
plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
112 try: |
67942ba2ee55
plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
113 MercurialProtocol.hg = which('hg')[0] |
67942ba2ee55
plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
114 except IndexError: |
2620 | 115 raise exceptions.NotFound(_(u"Mercurial executable (hg) not found, " |
116 u"can't use Mercurial handler")) | |
2449
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): |
2620 | 128 return MercurialProtocol.run(repository, 'export', '-g', '-r', 'outgoing()', |
129 '--encoding=utf-8') | |
2449
67942ba2ee55
plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
130 |
2544
a64887289931
plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
131 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
|
132 parsed_data = self.parse(data) |
a64887289931
plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
133 try: |
a64887289931
plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
134 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
|
135 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
|
136 except Exception: |
a64887289931
plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
137 parsed_name = u'' |
2620 | 138 name = u'mr_{item_id}_{parsed_name}'.format(item_id=item_id, |
139 parsed_name=parsed_name) | |
140 return MercurialProtocol.run(repository, 'qimport', '-g', '--name', name, | |
141 '--encoding=utf-8', '-', stdin=data) | |
2544
a64887289931
plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
142 |
2449
67942ba2ee55
plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
143 def parse(self, data, data_type=None): |
67942ba2ee55
plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
144 lines = data.splitlines() |
67942ba2ee55
plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
145 total_lines = len(lines) |
67942ba2ee55
plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
146 patches = [] |
67942ba2ee55
plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
147 while lines: |
67942ba2ee55
plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
148 patch = {} |
67942ba2ee55
plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
149 commit_msg = [] |
67942ba2ee55
plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
150 diff = [] |
67942ba2ee55
plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
151 state = 'init' |
67942ba2ee55
plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
152 if lines[0] != '# HG changeset patch': |
67942ba2ee55
plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
153 raise exceptions.DataError(_(u'invalid changeset signature')) |
67942ba2ee55
plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
154 # line index of this patch in the whole data |
67942ba2ee55
plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
155 patch_idx = total_lines - len(lines) |
67942ba2ee55
plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
156 del lines[0] |
67942ba2ee55
plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
157 |
67942ba2ee55
plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
158 for idx, line in enumerate(lines): |
67942ba2ee55
plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
159 if state == 'init': |
67942ba2ee55
plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
160 if line.startswith(u'# '): |
67942ba2ee55
plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
161 if line.startswith(u'# User '): |
67942ba2ee55
plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
162 elems = line[7:].split() |
67942ba2ee55
plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
163 if not elems: |
67942ba2ee55
plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
164 continue |
67942ba2ee55
plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
165 last = elems[-1] |
2620 | 166 if (last.startswith(u'<') and last.endswith(u'>') |
167 and u'@' in last): | |
2449
67942ba2ee55
plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
168 patch[self._m.META_EMAIL] = elems.pop()[1:-1] |
67942ba2ee55
plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
169 patch[self._m.META_AUTHOR] = u' '.join(elems) |
67942ba2ee55
plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
170 elif line.startswith(u'# Date '): |
67942ba2ee55
plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
171 time_data = line[7:].split() |
67942ba2ee55
plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
172 if len(time_data) != 2: |
2620 | 173 log.warning(_(u'unexpected time data: {data}') |
174 .format(data=line[7:])) | |
2449
67942ba2ee55
plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
175 continue |
2620 | 176 patch[self._m.META_TIMESTAMP] = (int(time_data[0]) |
177 + int(time_data[1])) | |
2449
67942ba2ee55
plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
178 elif line.startswith(u'# Node ID '): |
67942ba2ee55
plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
179 patch[self._m.META_HASH] = line[10:] |
67942ba2ee55
plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
180 elif line.startswith(u'# Parent '): |
67942ba2ee55
plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
181 patch[self._m.META_PARENT_HASH] = line[10:] |
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 state = 'commit_msg' |
67942ba2ee55
plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
184 if state == 'commit_msg': |
67942ba2ee55
plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
185 if line.startswith(u'diff --git a/'): |
67942ba2ee55
plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
186 state = 'diff' |
67942ba2ee55
plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
187 patch[self._m.META_DIFF_IDX] = patch_idx + idx + 1 |
67942ba2ee55
plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
188 else: |
67942ba2ee55
plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
189 commit_msg.append(line) |
67942ba2ee55
plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
190 if state == 'diff': |
67942ba2ee55
plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
191 if line.startswith(u'# ') or idx == len(lines)-1: |
67942ba2ee55
plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
192 # 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
|
193 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
|
194 patch[self._m.META_DIFF] = u'\n'.join(diff) |
67942ba2ee55
plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
195 patches.append(patch) |
67942ba2ee55
plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
196 if idx == len(lines)-1: |
67942ba2ee55
plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
197 del lines[:] |
67942ba2ee55
plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
198 else: |
67942ba2ee55
plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
199 del lines[:idx] |
67942ba2ee55
plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
200 break |
67942ba2ee55
plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
201 else: |
67942ba2ee55
plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
202 diff.append(line) |
67942ba2ee55
plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
203 return patches |