From 9c86174ff51eb641a97e1e2e9b99d7d8c797749b Mon Sep 17 00:00:00 2001 From: Douglas RAILLARD Date: Tue, 3 Sep 2019 12:44:43 +0100 Subject: [PATCH] target: Add Target.execute(force_locale='C') parameter To avoid locale-specific variations in the output of commands, set LC_ALL=C by default. This can be disabled by using None, or set to another locale. --- devlib/target.py | 11 ++++++++++- doc/target.rst | 5 ++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/devlib/target.py b/devlib/target.py index 8e93e21..d213e97 100644 --- a/devlib/target.py +++ b/devlib/target.py @@ -385,10 +385,19 @@ class Target(object): # execution def execute(self, command, timeout=None, check_exit_code=True, - as_root=False, strip_colors=True, will_succeed=False): + as_root=False, strip_colors=True, will_succeed=False, + force_locale='C'): + + # Force the locale if necessary for more predictable output + if force_locale: + # Use an explicit export so that the command is allowed to be any + # shell statement, rather than just a command invocation + command = 'export LC_ALL={} && {}'.format(quote(force_locale), command) + # Ensure to use deployed command when availables if self.executables_directory: command = "PATH={}:$PATH && {}".format(self.executables_directory, command) + return self.conn.execute(command, timeout=timeout, check_exit_code=check_exit_code, as_root=as_root, strip_colors=strip_colors, will_succeed=will_succeed) diff --git a/doc/target.rst b/doc/target.rst index d7472b6..753053b 100644 --- a/doc/target.rst +++ b/doc/target.rst @@ -232,7 +232,7 @@ Target :param timeout: timeout (in seconds) for the transfer; if the transfer does not complete within this period, an exception will be raised. -.. method:: Target.execute(command [, timeout [, check_exit_code [, as_root [, strip_colors [, will_succeed]]]]]) +.. method:: Target.execute(command [, timeout [, check_exit_code [, as_root [, strip_colors [, will_succeed [, force_locale]]]]]]) Execute the specified command on the target device and return its output. @@ -252,6 +252,9 @@ Target will make the method always raise an instance of a subclass of :class:`DevlibTransientError` when the command fails, instead of a :class:`DevlibStableError`. + :param force_locale: Prepend ``LC_ALL=`` in front of the + command to get predictable output that can be more safely parsed. + If ``None``, no locale is prepended. .. method:: Target.background(command [, stdout [, stderr [, as_root]]])