[PATCH rtems-tools] rtemstoolkit: add support for executing pipe command

Chris Johns chrisj at rtems.org
Tue Jun 20 00:48:24 UTC 2023


Hi,

Thanks for this. I like the change.

Could you please shorten the long line? This code will be run through yapf after
6 branches but until then we limit the line length. :)

Could you please add a unit test for this to the end of this file?

Thanks
Chris

On 20/6/2023 8:44 am, Muhammad Sulthan Mazaya wrote:
> The previous implementation of execute command on rtems-tools config does
> not support command line pipe operation. So something like:
> 
> %execute %{command} | %{trim_command}
> 
> Would not work, since the "| %{trim_command}" part is treated as an 
> additional command option. This patch is intended to fix that issue.
> 
> ---
>  rtemstoolkit/execute.py | 28 +++++++++++++++++++++++-----
>  1 file changed, 23 insertions(+), 5 deletions(-)
> 
> diff --git a/rtemstoolkit/execute.py b/rtemstoolkit/execute.py
> index ed81589..9bb7434 100755
> --- a/rtemstoolkit/execute.py
> +++ b/rtemstoolkit/execute.py
> @@ -389,11 +389,29 @@ class execute(object):
>                      r, e = os.path.splitext(command[0])
>                      if e not in ['.exe', '.com', '.bat']:
>                          command[0] = command[0] + '.exe'
> -            proc = subprocess.Popen(command, shell = shell,
> -                                    cwd = cwd, env = env,
> -                                    stdin = stdin, stdout = stdout,
> -                                    stderr = stderr,
> -                                    close_fds = False)
> +                        
> +            pipe_commands = []
> +            current_command = []
> +            for cmd in command:
> +                if cmd == '|':
> +                    pipe_commands.append(current_command)
> +                    current_command = []
> +                else:
> +                    current_command.append(cmd)
> +            pipe_commands.append(current_command)
> +
> +            proc = None
> +            for i, cmd in enumerate(pipe_commands):
> +                if i == 0:
> +                    proc = subprocess.Popen(
> +                        cmd, shell=shell, cwd=cwd, env=env, stdin=stdin, stdout=subprocess.PIPE)
> +                elif i == len(pipe_commands) - 1:
> +                    proc = subprocess.Popen(
> +                        cmd, shell=shell, cwd=cwd, env=env, stdin=proc.stdout, stdout=stdout, stderr=stderr, close_fds=False)
> +                else:
> +                    proc = subprocess.Popen(
> +                        cmd, shell=shell, cwd=cwd, env=env, stdin=proc.stdout, stdout=subprocess.PIPE)
> +
>              if not capture:
>                  return (0, proc)
>              if self.output is None:


More information about the devel mailing list