scripts/jlib.py: system(): added support for multiline commands.
authorJulian Smith <[email protected]>
Fri, 14 Jun 2024 20:55:41 +0000 (21:55 +0100)
committerRobin Watts <[email protected]>
Tue, 23 Jul 2024 19:02:02 +0000 (20:02 +0100)
scripts/jlib.py

index eec9af3269f308115f780237202c11be8dbf188c..421371aaf2b3fe29472583ac9bb536438747c3df 100644 (file)
@@ -1420,6 +1420,7 @@ def system(
         caller=1,
         bufsize=-1,
         env_extra=None,
+        multiline=True,
         ):
     '''
     Runs a command like `os.system()` or `subprocess.*`, but with more
@@ -1494,6 +1495,10 @@ def system(
         env_extra:
             If not `None`, a `dict` with extra items that are added to the
             environment passed to the child process.
+        multiline:
+            If true (the default) we convert a multiline command into a single
+            command, but preserve the multiline representation in verbose
+            diagnostics.
 
     Returns:
 
@@ -1613,6 +1618,19 @@ def system(
     else:
         assert 0, f'Inconsistent out: {out}'
 
+    if multiline and '\n' in command:
+        command = textwrap.dedent(command)
+        lines = list()
+        for line in command.split( '\n'):
+            h = 0 if line.startswith( '#') else line.find(' #')
+            if h >= 0:
+                line = line[:h]
+            if line.strip():
+                line = line.rstrip()
+                lines.append(line)
+        sep = ' ' if platform.system() == 'Windows' else ' \\\n'
+        command = sep.join(lines)
+
     if verbose:
         log(f'running: {command_env_text( command, env_extra)}', nv=0, caller=caller+1)