Mercurial > libervia-backend
annotate libervia/backend/tools/common/async_process.py @ 4270:0d7bb4df2343
Reformatted code base using black.
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 19 Jun 2024 18:44:57 +0200 |
parents | 601e72332907 |
children | 9308b2d15fd2 |
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 |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4251
diff
changeset
|
29 |
2793
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
30 log = getLogger(__name__) |
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 |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
33 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
|
34 """handle an external command""" |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4251
diff
changeset
|
35 |
2793
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
36 # 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
|
37 name = None |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
38 # 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
|
39 command = None |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
40 # 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
|
41 log = False |
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 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
|
44 """ |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
45 @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
|
46 @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
|
47 """ |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
48 self._stdin = stdin |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
49 self._deferred = deferred |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
50 self.data = [] |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
51 self.err_data = [] |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4251
diff
changeset
|
52 self.cmd_args: list[str] | None = None |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4251
diff
changeset
|
53 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
|
54 |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
55 @property |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
56 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
|
57 """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
|
58 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
|
59 return self.name |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
60 elif self.command is not None: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4251
diff
changeset
|
61 return os.path.splitext(os.path.basename(self.command))[0].decode( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4251
diff
changeset
|
62 "utf-8", "ignore" |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4251
diff
changeset
|
63 ) |
2793
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
64 else: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4251
diff
changeset
|
65 return "" |
2793
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
66 |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
67 def connectionMade(self): |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
68 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
|
69 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
|
70 self.transport.closeStdin() |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
71 |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
72 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
|
73 if self.log: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4251
diff
changeset
|
74 log.info(data.decode("utf-8", "replace")) |
2793
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
75 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
|
76 |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
77 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
|
78 if self.log: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4251
diff
changeset
|
79 log.warning(data.decode("utf-8", "replace")) |
2793
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
80 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
|
81 |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
82 def processEnded(self, reason): |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4251
diff
changeset
|
83 data = b"".join(self.data) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4251
diff
changeset
|
84 if reason.value.exitCode == 0: |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4251
diff
changeset
|
85 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
|
86 # 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
|
87 # is not working properly |
3028 | 88 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
|
89 else: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4251
diff
changeset
|
90 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
|
91 |
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
|
92 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
|
93 assert self.cmd_kwargs is not None |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4251
diff
changeset
|
94 msg = _( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4251
diff
changeset
|
95 "Can't complete {name} command (error code: {code}):\n" |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4251
diff
changeset
|
96 "Executed command: {command}\n" |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4251
diff
changeset
|
97 "Keyword arguments:\n" |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4251
diff
changeset
|
98 "{command_kw}\n\n" |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4251
diff
changeset
|
99 "stderr:\n{stderr}\n{stdout}\n" |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4251
diff
changeset
|
100 ).format( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4251
diff
changeset
|
101 name=self.command_name, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4251
diff
changeset
|
102 code=reason.value.exitCode, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4251
diff
changeset
|
103 command=" ".join(self.cmd_args), |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4251
diff
changeset
|
104 command_kw="\n".join( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4251
diff
changeset
|
105 f" - {k} = {v!r}" for k, v in self.cmd_kwargs.items() |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4251
diff
changeset
|
106 ), |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4251
diff
changeset
|
107 stderr=err_data.decode(errors="replace"), |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4251
diff
changeset
|
108 stdout="stdout: " + data.decode(errors="replace") if data else "", |
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
|
109 ) |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4251
diff
changeset
|
110 self._deferred.errback( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4251
diff
changeset
|
111 Failure(exceptions.CommandException(msg, data, err_data)) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4251
diff
changeset
|
112 ) |
2793
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 """ |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4251
diff
changeset
|
130 stdin = kwargs.pop("stdin", None) |
2793
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: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4251
diff
changeset
|
132 stdin = stdin.encode("utf-8") |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4251
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 " |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4251
diff
changeset
|
143 "to execute as first argument" |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4251
diff
changeset
|
144 ) |
2793
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
145 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
|
146 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
|
147 name = os.path.splitext(os.path.basename(command))[0] |
3028 | 148 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
|
149 else: |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
150 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
|
151 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
|
152 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
|
153 prot.cmd_kwargs = kwargs |
3712
799d4f6fa7ca
tools (common/async_process): use parent environment by default
Goffi <goffi@goffi.org>
parents:
3603
diff
changeset
|
154 if "env" not in kwargs: |
799d4f6fa7ca
tools (common/async_process): use parent environment by default
Goffi <goffi@goffi.org>
parents:
3603
diff
changeset
|
155 # we pass parent environment by default |
4193
730f542e4ad0
core: add new `init_script_path` option:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
156 # 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
|
157 # checked and reported upstream if confirmed. |
730f542e4ad0
core: add new `init_script_path` option:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
158 kwargs["env"] = os.environ |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4251
diff
changeset
|
159 reactor.spawnProcess(prot, command, cmd_args, **kwargs) |
2793
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
160 return d |
3161
be5fffe34987
tools (common/async_process): fixed stderr handling + added global "run"
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
161 |
be5fffe34987
tools (common/async_process): fixed stderr handling + added global "run"
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
162 |
be5fffe34987
tools (common/async_process): fixed stderr handling + added global "run"
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
163 run = CommandProtocol.run |