Mercurial > libervia-backend
annotate libervia/backend/tools/common/async_process.py @ 4268:51d004e50786
docker (backend): set `+use_local_shared_tmp` in conf.
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 13 Jun 2024 13:22:41 +0200 |
parents | 601e72332907 |
children | 0d7bb4df2343 |
rev | line source |
---|---|
3028 | 1 #!/usr/bin/env python3 |
3137 | 2 |
2793
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
4 # SAT: a jabber client |
3479 | 5 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org) |
2793
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 # This program is free software: you can redistribute it and/or modify |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 # it under the terms of the GNU Affero General Public License as published by |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 # the Free Software Foundation, either version 3 of the License, or |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 # (at your option) any later version. |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 # This program is distributed in the hope that it will be useful, |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 # GNU Affero General Public License for more details. |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 # You should have received a copy of the GNU Affero General Public License |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
20 """tools to launch process in a async way (using Twisted)""" |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
21 |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
22 import os.path |
4251
601e72332907
tools (common/async_process): show command and arguments used in error message in case a failure.
Goffi <goffi@goffi.org>
parents:
4193
diff
changeset
|
23 from typing import Any |
2793
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
24 from twisted.internet import defer, reactor, protocol |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
25 from twisted.python.failure import Failure |
4071
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
3712
diff
changeset
|
26 from libervia.backend.core.i18n import _ |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
3712
diff
changeset
|
27 from libervia.backend.core import exceptions |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
3712
diff
changeset
|
28 from libervia.backend.core.log import getLogger |
2793
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
29 log = getLogger(__name__) |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
30 |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
31 |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
32 class CommandProtocol(protocol.ProcessProtocol): |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
33 """handle an external command""" |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
34 # name of the command (unicode) |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
35 name = None |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
36 # full path to the command (bytes) |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
37 command = None |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
38 # True to activate logging of command outputs (bool) |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
39 log = False |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
40 |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
41 def __init__(self, deferred, stdin=None): |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
42 """ |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
43 @param deferred(defer.Deferred): will be called when command is completed |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
44 @param stdin(str, None): if not None, will be push to standard input |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
45 """ |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
46 self._stdin = stdin |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
47 self._deferred = deferred |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
48 self.data = [] |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
49 self.err_data = [] |
4251
601e72332907
tools (common/async_process): show command and arguments used in error message in case a failure.
Goffi <goffi@goffi.org>
parents:
4193
diff
changeset
|
50 self.cmd_args: list[str]|None = None |
601e72332907
tools (common/async_process): show command and arguments used in error message in case a failure.
Goffi <goffi@goffi.org>
parents:
4193
diff
changeset
|
51 self.cmd_kwargs: dict[str, Any]|None = None |
2793
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
52 |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
53 @property |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
54 def command_name(self): |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
55 """returns command name or empty string if it can't be guessed""" |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
56 if self.name is not None: |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
57 return self.name |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
58 elif self.command is not None: |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
59 return os.path.splitext(os.path.basename(self.command))[0].decode('utf-8', |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
60 'ignore') |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
61 else: |
3028 | 62 return '' |
2793
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
63 |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
64 def connectionMade(self): |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
65 if self._stdin is not None: |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
66 self.transport.write(self._stdin) |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
67 self.transport.closeStdin() |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
68 |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
69 def outReceived(self, data): |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
70 if self.log: |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
71 log.info(data.decode('utf-8', 'replace')) |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
72 self.data.append(data) |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
73 |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
74 def errReceived(self, data): |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
75 if self.log: |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
76 log.warning(data.decode('utf-8', 'replace')) |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
77 self.err_data.append(data) |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
78 |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
79 def processEnded(self, reason): |
3028 | 80 data = b''.join(self.data) |
2793
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
81 if (reason.value.exitCode == 0): |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
82 log.debug(f'{self.command_name!r} command succeed') |
2793
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
83 # we don't use "replace" on purpose, we want an exception if decoding |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
84 # is not working properly |
3028 | 85 self._deferred.callback(data) |
2793
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
86 else: |
3028 | 87 err_data = b''.join(self.err_data) |
2793
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
88 |
4251
601e72332907
tools (common/async_process): show command and arguments used in error message in case a failure.
Goffi <goffi@goffi.org>
parents:
4193
diff
changeset
|
89 assert self.cmd_args is not None |
601e72332907
tools (common/async_process): show command and arguments used in error message in case a failure.
Goffi <goffi@goffi.org>
parents:
4193
diff
changeset
|
90 assert self.cmd_kwargs is not None |
601e72332907
tools (common/async_process): show command and arguments used in error message in case a failure.
Goffi <goffi@goffi.org>
parents:
4193
diff
changeset
|
91 msg = ( |
601e72332907
tools (common/async_process): show command and arguments used in error message in case a failure.
Goffi <goffi@goffi.org>
parents:
4193
diff
changeset
|
92 _( |
601e72332907
tools (common/async_process): show command and arguments used in error message in case a failure.
Goffi <goffi@goffi.org>
parents:
4193
diff
changeset
|
93 "Can't complete {name} command (error code: {code}):\n" |
601e72332907
tools (common/async_process): show command and arguments used in error message in case a failure.
Goffi <goffi@goffi.org>
parents:
4193
diff
changeset
|
94 "Executed command: {command}\n" |
601e72332907
tools (common/async_process): show command and arguments used in error message in case a failure.
Goffi <goffi@goffi.org>
parents:
4193
diff
changeset
|
95 "Keyword arguments:\n" |
601e72332907
tools (common/async_process): show command and arguments used in error message in case a failure.
Goffi <goffi@goffi.org>
parents:
4193
diff
changeset
|
96 "{command_kw}\n\n" |
601e72332907
tools (common/async_process): show command and arguments used in error message in case a failure.
Goffi <goffi@goffi.org>
parents:
4193
diff
changeset
|
97 "stderr:\n{stderr}\n{stdout}\n" |
601e72332907
tools (common/async_process): show command and arguments used in error message in case a failure.
Goffi <goffi@goffi.org>
parents:
4193
diff
changeset
|
98 ) |
601e72332907
tools (common/async_process): show command and arguments used in error message in case a failure.
Goffi <goffi@goffi.org>
parents:
4193
diff
changeset
|
99 .format( |
601e72332907
tools (common/async_process): show command and arguments used in error message in case a failure.
Goffi <goffi@goffi.org>
parents:
4193
diff
changeset
|
100 name = self.command_name, |
601e72332907
tools (common/async_process): show command and arguments used in error message in case a failure.
Goffi <goffi@goffi.org>
parents:
4193
diff
changeset
|
101 code = reason.value.exitCode, |
601e72332907
tools (common/async_process): show command and arguments used in error message in case a failure.
Goffi <goffi@goffi.org>
parents:
4193
diff
changeset
|
102 command = " ".join(self.cmd_args), |
601e72332907
tools (common/async_process): show command and arguments used in error message in case a failure.
Goffi <goffi@goffi.org>
parents:
4193
diff
changeset
|
103 command_kw = "\n".join( |
601e72332907
tools (common/async_process): show command and arguments used in error message in case a failure.
Goffi <goffi@goffi.org>
parents:
4193
diff
changeset
|
104 f" - {k} = {v!r}" for k,v in self.cmd_kwargs.items() |
601e72332907
tools (common/async_process): show command and arguments used in error message in case a failure.
Goffi <goffi@goffi.org>
parents:
4193
diff
changeset
|
105 ), |
601e72332907
tools (common/async_process): show command and arguments used in error message in case a failure.
Goffi <goffi@goffi.org>
parents:
4193
diff
changeset
|
106 stderr= err_data.decode(errors='replace'), |
601e72332907
tools (common/async_process): show command and arguments used in error message in case a failure.
Goffi <goffi@goffi.org>
parents:
4193
diff
changeset
|
107 stdout = "stdout: " + data.decode(errors='replace') |
601e72332907
tools (common/async_process): show command and arguments used in error message in case a failure.
Goffi <goffi@goffi.org>
parents:
4193
diff
changeset
|
108 if data else '', |
601e72332907
tools (common/async_process): show command and arguments used in error message in case a failure.
Goffi <goffi@goffi.org>
parents:
4193
diff
changeset
|
109 ) |
601e72332907
tools (common/async_process): show command and arguments used in error message in case a failure.
Goffi <goffi@goffi.org>
parents:
4193
diff
changeset
|
110 ) |
2793
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
111 self._deferred.errback(Failure(exceptions.CommandException( |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
112 msg, data, err_data))) |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
113 |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
114 @classmethod |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
115 def run(cls, *args, **kwargs): |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
116 """Create a new CommandProtocol and execute the given command. |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
117 |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
118 @param *args(unicode): command arguments |
3368
e86b71b1aa31
core: minor typos, docstring/comments update
Goffi <goffi@goffi.org>
parents:
3161
diff
changeset
|
119 if cls.command is specified, it will be the path to the command to execute |
2793
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
120 otherwise, first argument must be the path |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
121 @param **kwargs: can be: |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
122 - stdin(unicode, None): data to push to standard input |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
123 - verbose(bool): if True stdout and stderr will be logged |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
124 other keyword arguments will be used in reactor.spawnProcess |
3040 | 125 @return ((D)bytes): stdout in case of success |
2793
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
126 @raise RuntimeError: command returned a non zero status |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
127 stdin and stdout will be given as arguments |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
128 |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
129 """ |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
130 stdin = kwargs.pop('stdin', None) |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
131 if stdin is not None: |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
132 stdin = stdin.encode('utf-8') |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
133 verbose = kwargs.pop('verbose', False) |
3603
25cbaf047728
tools (common/async_process): remove useless encoding + use full path for command:
Goffi <goffi@goffi.org>
parents:
3568
diff
changeset
|
134 args = list(args) |
2793
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
135 d = defer.Deferred() |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
136 prot = cls(d, stdin=stdin) |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
137 if verbose: |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
138 prot.log = True |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
139 if cls.command is None: |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
140 if not args: |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
141 raise ValueError( |
3028 | 142 "You must either specify cls.command or use a full path to command " |
143 "to execute as first argument") | |
2793
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
144 command = args.pop(0) |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
145 if prot.name is None: |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
146 name = os.path.splitext(os.path.basename(command))[0] |
3028 | 147 prot.name = name |
2793
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
148 else: |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
149 command = cls.command |
3603
25cbaf047728
tools (common/async_process): remove useless encoding + use full path for command:
Goffi <goffi@goffi.org>
parents:
3568
diff
changeset
|
150 cmd_args = [command] + args |
4251
601e72332907
tools (common/async_process): show command and arguments used in error message in case a failure.
Goffi <goffi@goffi.org>
parents:
4193
diff
changeset
|
151 prot.cmd_args = cmd_args |
601e72332907
tools (common/async_process): show command and arguments used in error message in case a failure.
Goffi <goffi@goffi.org>
parents:
4193
diff
changeset
|
152 prot.cmd_kwargs = kwargs |
3712
799d4f6fa7ca
tools (common/async_process): use parent environment by default
Goffi <goffi@goffi.org>
parents:
3603
diff
changeset
|
153 if "env" not in kwargs: |
799d4f6fa7ca
tools (common/async_process): use parent environment by default
Goffi <goffi@goffi.org>
parents:
3603
diff
changeset
|
154 # we pass parent environment by default |
4193
730f542e4ad0
core: add new `init_script_path` option:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
155 # FIXME: `None` doesn't seem to work, despite what documentation says, to be |
730f542e4ad0
core: add new `init_script_path` option:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
156 # checked and reported upstream if confirmed. |
730f542e4ad0
core: add new `init_script_path` option:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
157 kwargs["env"] = os.environ |
2793
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
158 reactor.spawnProcess(prot, |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
159 command, |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
160 cmd_args, |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
161 **kwargs) |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
162 return d |
3161
be5fffe34987
tools (common/async_process): fixed stderr handling + added global "run"
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
163 |
be5fffe34987
tools (common/async_process): fixed stderr handling + added global "run"
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
164 |
be5fffe34987
tools (common/async_process): fixed stderr handling + added global "run"
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
165 run = CommandProtocol.run |