From 695bdaa486bc3b44ea7ec7ad81d315e8e72a4dac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ciro=20Santilli=20=E5=85=AD=E5=9B=9B=E4=BA=8B=E4=BB=B6=20?= =?UTF-8?q?=E6=B3=95=E8=BD=AE=E5=8A=9F?= Date: Thu, 7 May 2020 03:00:02 +0000 Subject: [PATCH] --print-cmd-oneline --- common.py | 9 +++++++++ shell_helpers.py | 21 ++++++++++++++------- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/common.py b/common.py index f515440..50dd19d 100644 --- a/common.py +++ b/common.py @@ -573,6 +573,14 @@ when building examples that rely on it or running tests for those examples. Indicate that all packages used by our userland/ examples with --package are available. ''', + ) + self.add_argument( + '--print-cmd-oneline', + action='store_true', + help='''\ +Print generated commands in a single line: +https://cirosantilli.com/linux-kernel-module-cheat#dry-run +''' ) self.add_argument( '--static', @@ -1482,6 +1490,7 @@ lunch aosp_{}-eng self.env = env.copy() self.sh = shell_helpers.ShellHelpers( dry_run=self.env['dry_run'], + force_oneline=self.env['print_cmd_oneline'], quiet=(not show_cmds), ) self._init_env(self.env) diff --git a/shell_helpers.py b/shell_helpers.py index ddf19c4..056c46a 100644 --- a/shell_helpers.py +++ b/shell_helpers.py @@ -35,7 +35,7 @@ class ShellHelpers: _print_lock = threading.Lock() - def __init__(self, dry_run=False, quiet=False): + def __init__(self, dry_run=False, quiet=False, force_oneline=False): ''' :param dry_run: don't run the commands, just potentially print them. Debug aid. :type dry_run: Bool @@ -44,6 +44,7 @@ class ShellHelpers: :type dry_run: Bool ''' self.dry_run = dry_run + self.force_oneline_default = force_oneline self.quiet = quiet @classmethod @@ -108,13 +109,19 @@ class ShellHelpers: new_mode = old_mode & ~mode_delta os.chmod(path, new_mode) - @staticmethod + def force_oneline(self, force_oneline): + if force_oneline is not None: + return force_oneline + else: + return self.force_oneline_default + def cmd_to_string( + self, cmd: List[Union[str, LF]], cwd=None, extra_env=None, extra_paths=None, - force_oneline: bool =False, + force_oneline: Union[bool,None] =None, *, stdin_path: Union[str,None] =None ): @@ -143,12 +150,12 @@ class ShellHelpers: newline_count = 0 for arg in cmd: if arg == LF: - if not force_oneline: + if not self.force_oneline(force_oneline): cmd_quote.append(arg) newline_count += 1 else: cmd_quote.append(shlex.quote(arg)) - if force_oneline or newline_count > 0: + if self.force_oneline(force_oneline) or newline_count > 0: cmd_quote = [ ' '.join(list(y)) for x, y in itertools.groupby( @@ -160,7 +167,7 @@ class ShellHelpers: out.extend(cmd_quote) if stdin_path is not None: out.append('< {}'.format(shlex.quote(stdin_path))) - if force_oneline or newline_count == 1 and cmd[-1] == LF: + if self.force_oneline(force_oneline) or newline_count == 1 and cmd[-1] == LF: ending = '' else: ending = last_newline + ';' @@ -246,7 +253,7 @@ class ShellHelpers: cmd_files=None, extra_env=None, extra_paths=None, - force_oneline=False, + force_oneline: Union[bool,None] =None, *, stdin_path: Union[str,None] =None ):