Reformatted code base using black.
This commit is contained in:
parent
56319cbad3
commit
53fdaa9d09
1 changed files with 12604 additions and 8966 deletions
|
|
@ -26,11 +26,13 @@ from twisted.python.failure import Failure
|
|||
from libervia.backend.core.i18n import _
|
||||
from libervia.backend.core import exceptions
|
||||
from libervia.backend.core.log import getLogger
|
||||
|
||||
log = getLogger(__name__)
|
||||
|
||||
|
||||
class CommandProtocol(protocol.ProcessProtocol):
|
||||
"""handle an external command"""
|
||||
|
||||
# name of the command (unicode)
|
||||
name = None
|
||||
# full path to the command (bytes)
|
||||
|
|
@ -47,8 +49,8 @@ class CommandProtocol(protocol.ProcessProtocol):
|
|||
self._deferred = deferred
|
||||
self.data = []
|
||||
self.err_data = []
|
||||
self.cmd_args: list[str]|None = None
|
||||
self.cmd_kwargs: dict[str, Any]|None = None
|
||||
self.cmd_args: list[str] | None = None
|
||||
self.cmd_kwargs: dict[str, Any] | None = None
|
||||
|
||||
@property
|
||||
def command_name(self):
|
||||
|
|
@ -56,10 +58,11 @@ class CommandProtocol(protocol.ProcessProtocol):
|
|||
if self.name is not None:
|
||||
return self.name
|
||||
elif self.command is not None:
|
||||
return os.path.splitext(os.path.basename(self.command))[0].decode('utf-8',
|
||||
'ignore')
|
||||
return os.path.splitext(os.path.basename(self.command))[0].decode(
|
||||
"utf-8", "ignore"
|
||||
)
|
||||
else:
|
||||
return ''
|
||||
return ""
|
||||
|
||||
def connectionMade(self):
|
||||
if self._stdin is not None:
|
||||
|
|
@ -68,48 +71,45 @@ class CommandProtocol(protocol.ProcessProtocol):
|
|||
|
||||
def outReceived(self, data):
|
||||
if self.log:
|
||||
log.info(data.decode('utf-8', 'replace'))
|
||||
log.info(data.decode("utf-8", "replace"))
|
||||
self.data.append(data)
|
||||
|
||||
def errReceived(self, data):
|
||||
if self.log:
|
||||
log.warning(data.decode('utf-8', 'replace'))
|
||||
log.warning(data.decode("utf-8", "replace"))
|
||||
self.err_data.append(data)
|
||||
|
||||
def processEnded(self, reason):
|
||||
data = b''.join(self.data)
|
||||
if (reason.value.exitCode == 0):
|
||||
log.debug(f'{self.command_name!r} command succeed')
|
||||
data = b"".join(self.data)
|
||||
if reason.value.exitCode == 0:
|
||||
log.debug(f"{self.command_name!r} command succeed")
|
||||
# we don't use "replace" on purpose, we want an exception if decoding
|
||||
# is not working properly
|
||||
self._deferred.callback(data)
|
||||
else:
|
||||
err_data = b''.join(self.err_data)
|
||||
err_data = b"".join(self.err_data)
|
||||
|
||||
assert self.cmd_args is not None
|
||||
assert self.cmd_kwargs is not None
|
||||
msg = (
|
||||
_(
|
||||
"Can't complete {name} command (error code: {code}):\n"
|
||||
"Executed command: {command}\n"
|
||||
"Keyword arguments:\n"
|
||||
"{command_kw}\n\n"
|
||||
"stderr:\n{stderr}\n{stdout}\n"
|
||||
)
|
||||
.format(
|
||||
name = self.command_name,
|
||||
code = reason.value.exitCode,
|
||||
command = " ".join(self.cmd_args),
|
||||
command_kw = "\n".join(
|
||||
f" - {k} = {v!r}" for k,v in self.cmd_kwargs.items()
|
||||
),
|
||||
stderr= err_data.decode(errors='replace'),
|
||||
stdout = "stdout: " + data.decode(errors='replace')
|
||||
if data else '',
|
||||
)
|
||||
msg = _(
|
||||
"Can't complete {name} command (error code: {code}):\n"
|
||||
"Executed command: {command}\n"
|
||||
"Keyword arguments:\n"
|
||||
"{command_kw}\n\n"
|
||||
"stderr:\n{stderr}\n{stdout}\n"
|
||||
).format(
|
||||
name=self.command_name,
|
||||
code=reason.value.exitCode,
|
||||
command=" ".join(self.cmd_args),
|
||||
command_kw="\n".join(
|
||||
f" - {k} = {v!r}" for k, v in self.cmd_kwargs.items()
|
||||
),
|
||||
stderr=err_data.decode(errors="replace"),
|
||||
stdout="stdout: " + data.decode(errors="replace") if data else "",
|
||||
)
|
||||
self._deferred.errback(
|
||||
Failure(exceptions.CommandException(msg, data, err_data))
|
||||
)
|
||||
self._deferred.errback(Failure(exceptions.CommandException(
|
||||
msg, data, err_data)))
|
||||
|
||||
@classmethod
|
||||
def run(cls, *args, **kwargs):
|
||||
|
|
@ -127,10 +127,10 @@ class CommandProtocol(protocol.ProcessProtocol):
|
|||
stdin and stdout will be given as arguments
|
||||
|
||||
"""
|
||||
stdin = kwargs.pop('stdin', None)
|
||||
stdin = kwargs.pop("stdin", None)
|
||||
if stdin is not None:
|
||||
stdin = stdin.encode('utf-8')
|
||||
verbose = kwargs.pop('verbose', False)
|
||||
stdin = stdin.encode("utf-8")
|
||||
verbose = kwargs.pop("verbose", False)
|
||||
args = list(args)
|
||||
d = defer.Deferred()
|
||||
prot = cls(d, stdin=stdin)
|
||||
|
|
@ -140,7 +140,8 @@ class CommandProtocol(protocol.ProcessProtocol):
|
|||
if not args:
|
||||
raise ValueError(
|
||||
"You must either specify cls.command or use a full path to command "
|
||||
"to execute as first argument")
|
||||
"to execute as first argument"
|
||||
)
|
||||
command = args.pop(0)
|
||||
if prot.name is None:
|
||||
name = os.path.splitext(os.path.basename(command))[0]
|
||||
|
|
@ -155,10 +156,7 @@ class CommandProtocol(protocol.ProcessProtocol):
|
|||
# FIXME: `None` doesn't seem to work, despite what documentation says, to be
|
||||
# checked and reported upstream if confirmed.
|
||||
kwargs["env"] = os.environ
|
||||
reactor.spawnProcess(prot,
|
||||
command,
|
||||
cmd_args,
|
||||
**kwargs)
|
||||
reactor.spawnProcess(prot, command, cmd_args, **kwargs)
|
||||
return d
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue