diff mbox series

[net-next,3/4] selftests: drv-net: reimplement the config parser

Message ID 20240424221444.4194069-4-kuba@kernel.org (mailing list archive)
State Superseded
Delegated to: Netdev Maintainers
Headers show
Series selftests: drv-net: round some sharp edges | expand

Checks

Context Check Description
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for net-next
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 8 this patch: 8
netdev/build_tools success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers warning 1 maintainers not CCed: shuah@kernel.org
netdev/build_clang success Errors and warnings before: 8 this patch: 8
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 8 this patch: 8
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 38 lines checked
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
netdev/contest success net-next-2024-04-25--09-00 (tests: 995)

Commit Message

Jakub Kicinski April 24, 2024, 10:14 p.m. UTC
The shell lexer is not helping much, do very basic parsing
manually.

Signed-off-by: Jakub Kicinski <kuba@kernel.org>
---
 .../selftests/drivers/net/lib/py/env.py       | 26 ++++++++++---------
 1 file changed, 14 insertions(+), 12 deletions(-)

Comments

Willem de Bruijn April 25, 2024, 1:43 a.m. UTC | #1
Jakub Kicinski wrote:
> The shell lexer is not helping much, do very basic parsing
> manually.
> 
> Signed-off-by: Jakub Kicinski <kuba@kernel.org>
> ---
>  .../selftests/drivers/net/lib/py/env.py       | 26 ++++++++++---------
>  1 file changed, 14 insertions(+), 12 deletions(-)
> 
> diff --git a/tools/testing/selftests/drivers/net/lib/py/env.py b/tools/testing/selftests/drivers/net/lib/py/env.py
> index a3db1bb1afeb..a6a5a5f9c6db 100644
> --- a/tools/testing/selftests/drivers/net/lib/py/env.py
> +++ b/tools/testing/selftests/drivers/net/lib/py/env.py
> @@ -1,7 +1,6 @@
>  # SPDX-License-Identifier: GPL-2.0
>  
>  import os
> -import shlex
>  from pathlib import Path
>  from lib.py import KsftSkipEx
>  from lib.py import cmd, ip
> @@ -16,17 +15,20 @@ from .remote import Remote
>      if not (src_dir / "net.config").exists():
>          return env
>  
> -    lexer = shlex.shlex(open((src_dir / "net.config").as_posix(), 'r').read())
> -    k = None
> -    for token in lexer:
> -        if k is None:
> -            k = token
> -            env[k] = ""
> -        elif token == "=":
> -            pass
> -        else:
> -            env[k] = token
> -            k = None
> +    with open((src_dir / "net.config").as_posix(), 'r') as fp:
> +        for line in fp.readlines():
> +            full_file = line
> +            # Strip comments
> +            pos = line.find("#")
> +            if pos >= 0:
> +                line = line[:pos]
> +            line = line.strip()
> +            if not line:
> +                continue
> +            pos = line.find("=")
> +            if pos <= 0:
> +                raise Exception("Can't parse configuration line:", full_file)
> +            env[line[:pos]] = line[pos+1:]

Or line.split. But bikeshedding python is a deep rabbit hole :)
diff mbox series

Patch

diff --git a/tools/testing/selftests/drivers/net/lib/py/env.py b/tools/testing/selftests/drivers/net/lib/py/env.py
index a3db1bb1afeb..a6a5a5f9c6db 100644
--- a/tools/testing/selftests/drivers/net/lib/py/env.py
+++ b/tools/testing/selftests/drivers/net/lib/py/env.py
@@ -1,7 +1,6 @@ 
 # SPDX-License-Identifier: GPL-2.0
 
 import os
-import shlex
 from pathlib import Path
 from lib.py import KsftSkipEx
 from lib.py import cmd, ip
@@ -16,17 +15,20 @@  from .remote import Remote
     if not (src_dir / "net.config").exists():
         return env
 
-    lexer = shlex.shlex(open((src_dir / "net.config").as_posix(), 'r').read())
-    k = None
-    for token in lexer:
-        if k is None:
-            k = token
-            env[k] = ""
-        elif token == "=":
-            pass
-        else:
-            env[k] = token
-            k = None
+    with open((src_dir / "net.config").as_posix(), 'r') as fp:
+        for line in fp.readlines():
+            full_file = line
+            # Strip comments
+            pos = line.find("#")
+            if pos >= 0:
+                line = line[:pos]
+            line = line.strip()
+            if not line:
+                continue
+            pos = line.find("=")
+            if pos <= 0:
+                raise Exception("Can't parse configuration line:", full_file)
+            env[line[:pos]] = line[pos+1:]
     return env