Mercurial > libervia-backend
annotate libervia/backend/tools/common/async_process.py @ 4322:00837fa13e5a default tip @
tools (common/template), cli (call/gui): use font-awesome instead of fontello:
following change in Libervia Media, code has been updated to use font-awesome now instead
of fontello.
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 26 Oct 2024 22:42:17 +0200 |
parents | 9308b2d15fd2 |
children |
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 |
4273
9308b2d15fd2
tools (common/async_process): accept `Path` instances as command path.
Goffi <goffi@goffi.org>
parents:
4270
diff
changeset
|
23 from pathlib import 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
|
24 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
|
25 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
|
26 from twisted.python.failure import Failure |
4071
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
3712
diff
changeset
|
27 from libervia.backend.core.i18n import _ |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
3712
diff
changeset
|
28 from libervia.backend.core import exceptions |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
3712
diff
changeset
|
29 from libervia.backend.core.log import getLogger |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4251
diff
changeset
|
30 |
2793
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
31 log = getLogger(__name__) |
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 |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
34 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
|
35 """handle an external command""" |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4251
diff
changeset
|
36 |
2793
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
37 # 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
|
38 name = None |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
39 # 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
|
40 command = None |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
41 # 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
|
42 log = False |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
43 |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
44 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
|
45 """ |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
46 @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
|
47 @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
|
48 """ |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
49 self._stdin = stdin |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
50 self._deferred = deferred |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
51 self.data = [] |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
52 self.err_data = [] |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4251
diff
changeset
|
53 self.cmd_args: list[str] | None = None |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4251
diff
changeset
|
54 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
|
55 |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
56 @property |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
57 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
|
58 """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
|
59 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
|
60 return self.name |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
61 elif self.command is not None: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4251
diff
changeset
|
62 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
|
63 "utf-8", "ignore" |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4251
diff
changeset
|
64 ) |
2793
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
65 else: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4251
diff
changeset
|
66 return "" |
2793
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
67 |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
68 def connectionMade(self): |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
69 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
|
70 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
|
71 self.transport.closeStdin() |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
72 |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
73 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
|
74 if self.log: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4251
diff
changeset
|
75 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
|
76 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
|
77 |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
78 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
|
79 if self.log: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4251
diff
changeset
|
80 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
|
81 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
|
82 |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
83 def processEnded(self, reason): |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4251
diff
changeset
|
84 data = b"".join(self.data) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4251
diff
changeset
|
85 if reason.value.exitCode == 0: |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4251
diff
changeset
|
86 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
|
87 # 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
|
88 # is not working properly |
3028 | 89 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
|
90 else: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4251
diff
changeset
|
91 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
|
92 |
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
|
93 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
|
94 assert self.cmd_kwargs is not None |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4251
diff
changeset
|
95 msg = _( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4251
diff
changeset
|
96 "Can't complete {name} command (error code: {code}):\n" |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4251
diff
changeset
|
97 "Executed command: {command}\n" |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4251
diff
changeset
|
98 "Keyword arguments:\n" |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4251
diff
changeset
|
99 "{command_kw}\n\n" |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4251
diff
changeset
|
100 "stderr:\n{stderr}\n{stdout}\n" |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4251
diff
changeset
|
101 ).format( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4251
diff
changeset
|
102 name=self.command_name, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4251
diff
changeset
|
103 code=reason.value.exitCode, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4251
diff
changeset
|
104 command=" ".join(self.cmd_args), |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4251
diff
changeset
|
105 command_kw="\n".join( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4251
diff
changeset
|
106 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
|
107 ), |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4251
diff
changeset
|
108 stderr=err_data.decode(errors="replace"), |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4251
diff
changeset
|
109 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
|
110 ) |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4251
diff
changeset
|
111 self._deferred.errback( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4251
diff
changeset
|
112 Failure(exceptions.CommandException(msg, data, err_data)) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4251
diff
changeset
|
113 ) |
2793
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
114 |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
115 @classmethod |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
116 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
|
117 """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
|
118 |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
119 @param *args(unicode): command arguments |
3368
e86b71b1aa31
core: minor typos, docstring/comments update
Goffi <goffi@goffi.org>
parents:
3161
diff
changeset
|
120 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
|
121 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
|
122 @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
|
123 - 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
|
124 - 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
|
125 other keyword arguments will be used in reactor.spawnProcess |
3040 | 126 @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
|
127 @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
|
128 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
|
129 |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
130 """ |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4251
diff
changeset
|
131 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
|
132 if stdin is not None: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4251
diff
changeset
|
133 stdin = stdin.encode("utf-8") |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4251
diff
changeset
|
134 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
|
135 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
|
136 d = defer.Deferred() |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
137 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
|
138 if verbose: |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
139 prot.log = True |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
140 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
|
141 if not args: |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
142 raise ValueError( |
3028 | 143 "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
|
144 "to execute as first argument" |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4251
diff
changeset
|
145 ) |
2793
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
146 command = args.pop(0) |
4273
9308b2d15fd2
tools (common/async_process): accept `Path` instances as command path.
Goffi <goffi@goffi.org>
parents:
4270
diff
changeset
|
147 if isinstance(command, Path): |
9308b2d15fd2
tools (common/async_process): accept `Path` instances as command path.
Goffi <goffi@goffi.org>
parents:
4270
diff
changeset
|
148 command = str(command) |
2793
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
149 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
|
150 name = os.path.splitext(os.path.basename(command))[0] |
3028 | 151 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
|
152 else: |
181735d1b062
plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
153 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
|
154 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
|
155 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
|
156 prot.cmd_kwargs = kwargs |
3712
799d4f6fa7ca
tools (common/async_process): use parent environment by default
Goffi <goffi@goffi.org>
parents:
3603
diff
changeset
|
157 if "env" not in kwargs: |
799d4f6fa7ca
tools (common/async_process): use parent environment by default
Goffi <goffi@goffi.org>
parents:
3603
diff
changeset
|
158 # we pass parent environment by default |
4193
730f542e4ad0
core: add new `init_script_path` option:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
159 # 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
|
160 # checked and reported upstream if confirmed. |
730f542e4ad0
core: add new `init_script_path` option:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
161 kwargs["env"] = os.environ |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4251
diff
changeset
|
162 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
|
163 return d |
3161
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 |
be5fffe34987
tools (common/async_process): fixed stderr handling + added global "run"
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
166 run = CommandProtocol.run |