[v1] dts: strip whitespaces from stdout and stderr

Message ID 20240207121641.22739-1-juraj.linkes@pantheon.tech (mailing list archive)
State Superseded
Delegated to: Thomas Monjalon
Headers
Series [v1] dts: strip whitespaces from stdout and stderr |

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/loongarch-compilation success Compilation OK
ci/loongarch-unit-testing success Unit Testing PASS
ci/github-robot: build success github build: passed
ci/iol-intel-Performance success Performance Testing PASS
ci/iol-broadcom-Functional success Functional Testing PASS
ci/iol-compile-amd64-testing success Testing PASS
ci/iol-intel-Functional success Functional Testing PASS
ci/iol-abi-testing success Testing PASS
ci/Intel-compilation success Compilation OK
ci/iol-unit-amd64-testing success Testing PASS
ci/iol-sample-apps-testing success Testing PASS
ci/intel-Testing success Testing PASS
ci/iol-unit-arm64-testing success Testing PASS
ci/iol-broadcom-Performance success Performance Testing PASS
ci/intel-Functional success Functional PASS
ci/iol-compile-arm64-testing success Testing PASS
ci/iol-mellanox-Performance success Performance Testing PASS

Commit Message

Juraj Linkeš Feb. 7, 2024, 12:16 p.m. UTC
  There could be a newline at the end of stdout or stderr of a remotely
executed command. These cause issues when used later, such as when
joining paths from such commands - a newline in the middle of a path is
not valid.

Fixes: ad80f550dbc5 ("dts: add SSH command verification")
Signed-off-by: Juraj Linkeš <juraj.linkes@pantheon.tech>
---
 .../remote_session/remote_session.py          | 24 +++++++++++++++----
 1 file changed, 20 insertions(+), 4 deletions(-)
  

Comments

Jeremy Spewock Feb. 12, 2024, 4:49 p.m. UTC | #1
On Wed, Feb 7, 2024 at 7:16 AM Juraj Linkeš <juraj.linkes@pantheon.tech> wrote:
>
> There could be a newline at the end of stdout or stderr of a remotely
> executed command. These cause issues when used later, such as when
> joining paths from such commands - a newline in the middle of a path is
> not valid.
>
> Fixes: ad80f550dbc5 ("dts: add SSH command verification")
> Signed-off-by: Juraj Linkeš <juraj.linkes@pantheon.tech>
> ---
>  .../remote_session/remote_session.py          | 24 +++++++++++++++----
>  1 file changed, 20 insertions(+), 4 deletions(-)
>
> diff --git a/dts/framework/remote_session/remote_session.py b/dts/framework/remote_session/remote_session.py
> index 2059f9a981..6bea1a2306 100644
> --- a/dts/framework/remote_session/remote_session.py
> +++ b/dts/framework/remote_session/remote_session.py
> @@ -10,8 +10,8 @@
>  """
>
>
> -import dataclasses
>  from abc import ABC, abstractmethod
> +from dataclasses import InitVar, dataclass, field
>  from pathlib import PurePath
>
>  from framework.config import NodeConfiguration
> @@ -20,7 +20,7 @@
>  from framework.settings import SETTINGS
>
>
> -@dataclasses.dataclass(slots=True, frozen=True)
> +@dataclass(slots=True, frozen=True)
>  class CommandResult:
>      """The result of remote execution of a command.
>
> @@ -34,9 +34,25 @@ class CommandResult:
>
>      name: str
>      command: str
> -    stdout: str
> -    stderr: str
> +    init_stdout: InitVar[str]
> +    init_stderr: InitVar[str]
>      return_code: int
> +    stdout: str = field(init=False)
> +    stderr: str = field(init=False)
> +
> +    def __post_init__(self, init_stdout, init_stderr):

Are the typehints skipped deliberately here because it's redundant? We
might want to include them anyway just for better typehint coverage.

> +        """Strip the whitespaces from stdout and stderr.
> +
> +        The generated __init__ method uses object.__setattr__() when the dataclass is frozen,
> +        so that's what we use here as well.
> +
> +        In order to get access to dataclass fields in the __post_init__ method,
> +        we have to type them as InitVars. These InitVars are included in the __init__ method's
> +        signature, so we have to exclude the actual stdout and stderr fields
> +        from the __init__ method's signature, so that we have the proper number of arguments.
> +        """
> +        object.__setattr__(self, "stdout", init_stdout.strip())
> +        object.__setattr__(self, "stderr", init_stderr.strip())
>
>      def __str__(self) -> str:
>          """Format the command outputs."""
> --
> 2.34.1
>
  
Juraj Linkeš Feb. 13, 2024, 11:06 a.m. UTC | #2
On Mon, Feb 12, 2024 at 5:49 PM Jeremy Spewock <jspewock@iol.unh.edu> wrote:
>
> On Wed, Feb 7, 2024 at 7:16 AM Juraj Linkeš <juraj.linkes@pantheon.tech> wrote:
> >
> > There could be a newline at the end of stdout or stderr of a remotely
> > executed command. These cause issues when used later, such as when
> > joining paths from such commands - a newline in the middle of a path is
> > not valid.
> >
> > Fixes: ad80f550dbc5 ("dts: add SSH command verification")
> > Signed-off-by: Juraj Linkeš <juraj.linkes@pantheon.tech>
> > ---
> >  .../remote_session/remote_session.py          | 24 +++++++++++++++----
> >  1 file changed, 20 insertions(+), 4 deletions(-)
> >
> > diff --git a/dts/framework/remote_session/remote_session.py b/dts/framework/remote_session/remote_session.py
> > index 2059f9a981..6bea1a2306 100644
> > --- a/dts/framework/remote_session/remote_session.py
> > +++ b/dts/framework/remote_session/remote_session.py
> > @@ -10,8 +10,8 @@
> >  """
> >
> >
> > -import dataclasses
> >  from abc import ABC, abstractmethod
> > +from dataclasses import InitVar, dataclass, field
> >  from pathlib import PurePath
> >
> >  from framework.config import NodeConfiguration
> > @@ -20,7 +20,7 @@
> >  from framework.settings import SETTINGS
> >
> >
> > -@dataclasses.dataclass(slots=True, frozen=True)
> > +@dataclass(slots=True, frozen=True)
> >  class CommandResult:
> >      """The result of remote execution of a command.
> >
> > @@ -34,9 +34,25 @@ class CommandResult:
> >
> >      name: str
> >      command: str
> > -    stdout: str
> > -    stderr: str
> > +    init_stdout: InitVar[str]
> > +    init_stderr: InitVar[str]
> >      return_code: int
> > +    stdout: str = field(init=False)
> > +    stderr: str = field(init=False)
> > +
> > +    def __post_init__(self, init_stdout, init_stderr):
>
> Are the typehints skipped deliberately here because it's redundant? We
> might want to include them anyway just for better typehint coverage.
>

It's a method defined by the dataclasses module so I guess I didn't
feel the need to include the typehints because of that. No harm in
adding them though, I'll send a new version.

> > +        """Strip the whitespaces from stdout and stderr.
> > +
> > +        The generated __init__ method uses object.__setattr__() when the dataclass is frozen,
> > +        so that's what we use here as well.
> > +
> > +        In order to get access to dataclass fields in the __post_init__ method,
> > +        we have to type them as InitVars. These InitVars are included in the __init__ method's
> > +        signature, so we have to exclude the actual stdout and stderr fields
> > +        from the __init__ method's signature, so that we have the proper number of arguments.
> > +        """
> > +        object.__setattr__(self, "stdout", init_stdout.strip())
> > +        object.__setattr__(self, "stderr", init_stderr.strip())
> >
> >      def __str__(self) -> str:
> >          """Format the command outputs."""
> > --
> > 2.34.1
> >
  

Patch

diff --git a/dts/framework/remote_session/remote_session.py b/dts/framework/remote_session/remote_session.py
index 2059f9a981..6bea1a2306 100644
--- a/dts/framework/remote_session/remote_session.py
+++ b/dts/framework/remote_session/remote_session.py
@@ -10,8 +10,8 @@ 
 """
 
 
-import dataclasses
 from abc import ABC, abstractmethod
+from dataclasses import InitVar, dataclass, field
 from pathlib import PurePath
 
 from framework.config import NodeConfiguration
@@ -20,7 +20,7 @@ 
 from framework.settings import SETTINGS
 
 
-@dataclasses.dataclass(slots=True, frozen=True)
+@dataclass(slots=True, frozen=True)
 class CommandResult:
     """The result of remote execution of a command.
 
@@ -34,9 +34,25 @@  class CommandResult:
 
     name: str
     command: str
-    stdout: str
-    stderr: str
+    init_stdout: InitVar[str]
+    init_stderr: InitVar[str]
     return_code: int
+    stdout: str = field(init=False)
+    stderr: str = field(init=False)
+
+    def __post_init__(self, init_stdout, init_stderr):
+        """Strip the whitespaces from stdout and stderr.
+
+        The generated __init__ method uses object.__setattr__() when the dataclass is frozen,
+        so that's what we use here as well.
+
+        In order to get access to dataclass fields in the __post_init__ method,
+        we have to type them as InitVars. These InitVars are included in the __init__ method's
+        signature, so we have to exclude the actual stdout and stderr fields
+        from the __init__ method's signature, so that we have the proper number of arguments.
+        """
+        object.__setattr__(self, "stdout", init_stdout.strip())
+        object.__setattr__(self, "stderr", init_stderr.strip())
 
     def __str__(self) -> str:
         """Format the command outputs."""