diff mbox series

[02/24] moveconfig: Allow regex matches when finding combinations

Message ID 20220208114935.2.I061931956016352181753039d4df732997a7ab15@changeid
State Accepted
Commit 941671a19cfb8e0fad3bd2a11ab5137e43c2ddc5
Delegated to: Simon Glass
Headers show
Series binman: rockchip: Migrate from rockchip SPL_FIT_GENERATOR script | expand

Commit Message

Simon Glass Feb. 8, 2022, 6:49 p.m. UTC
It is useful to be able to search for CONFIG options that match a regex,
such as this, which lists boards which define SPL_FIT_GENERATOR and
anything not starting with ROCKCHIP:

   ./tools/moveconfig.py -f SPL_FIT_GENERATOR ~ROCKCHIP.*

Add support for this.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 tools/moveconfig.py | 24 ++++++++++++++++++++++--
 1 file changed, 22 insertions(+), 2 deletions(-)

Comments

Alper Nebi Yasak Feb. 15, 2022, 11:41 a.m. UTC | #1
On 08/02/2022 21:49, Simon Glass wrote:
> It is useful to be able to search for CONFIG options that match a regex,
> such as this, which lists boards which define SPL_FIT_GENERATOR and
> anything not starting with ROCKCHIP:
> 
>    ./tools/moveconfig.py -f SPL_FIT_GENERATOR ~ROCKCHIP.*
> 
> Add support for this.
> 
> Signed-off-by: Simon Glass <sjg@chromium.org>
> ---
> 
>  tools/moveconfig.py | 24 ++++++++++++++++++++++--
>  1 file changed, 22 insertions(+), 2 deletions(-)
> 
> diff --git a/tools/moveconfig.py b/tools/moveconfig.py
> index 5ef5a95eb6..cff1e30658 100755
> --- a/tools/moveconfig.py
> +++ b/tools/moveconfig.py
> @@ -1606,12 +1606,31 @@ def do_imply_config(config_list, add_imply, imply_flags, skip_added,
>              for linenum in sorted(linenums, reverse=True):
>                  add_imply_rule(config[CONFIG_LEN:], fname, linenum)
>  
> +def defconfig_matches(configs, re_match):
> +    """Check if any CONFIG option matches a regex
> +
> +    The match must be complete, i.e. from the start to end of the CONFIG option.
> +
> +    Args:
> +        configs (dict): Dict of CONFIG options:
> +            key: CONFIG option
> +            value: Value of option
> +        re_match (re.Pattern): Match to check
> +
> +    Returns:
> +        bool: True if any CONFIG matches the regex
> +    """
> +    for cfg in configs:
> +        m_cfg = re_match.match(cfg)
> +        if m_cfg and m_cfg.span()[1] == len(cfg):
> +            return True
> +    return False

Consider re_match.fullmatch() to match the entire string. I think this
can be further reduced into any(map(re_match.fullmatch, configs)) and
used directly below instead of adding this extra function.

>  
>  def do_find_config(config_list):
>      """Find boards with a given combination of CONFIGs
>  
>      Params:
> -        config_list: List of CONFIG options to check (each a string consisting
> +        config_list: List of CONFIG options to check (each a regex consisting
>              of a config option, with or without a CONFIG_ prefix. If an option
>              is preceded by a tilde (~) then it must be false, otherwise it must
>              be true)
> @@ -1643,8 +1662,9 @@ def do_find_config(config_list):
>          # running for the next stage
>          in_list = out
>          out = set()
> +        re_match = re.compile(cfg)
>          for defc in in_list:
> -            has_cfg = cfg in config_db[defc]
> +            has_cfg = defconfig_matches(config_db[defc], re_match)
>              if has_cfg == want:
>                  out.add(defc)
>      if adhoc:
Simon Glass Feb. 23, 2022, 2:35 a.m. UTC | #2
On 08/02/2022 21:49, Simon Glass wrote:
> It is useful to be able to search for CONFIG options that match a regex,
> such as this, which lists boards which define SPL_FIT_GENERATOR and
> anything not starting with ROCKCHIP:
>
>    ./tools/moveconfig.py -f SPL_FIT_GENERATOR ~ROCKCHIP.*
>
> Add support for this.
>
> Signed-off-by: Simon Glass <sjg@chromium.org>
> ---
>
>  tools/moveconfig.py | 24 ++++++++++++++++++++++--
>  1 file changed, 22 insertions(+), 2 deletions(-)
>
Applied to u-boot-dm, thanks!
diff mbox series

Patch

diff --git a/tools/moveconfig.py b/tools/moveconfig.py
index 5ef5a95eb6..cff1e30658 100755
--- a/tools/moveconfig.py
+++ b/tools/moveconfig.py
@@ -1606,12 +1606,31 @@  def do_imply_config(config_list, add_imply, imply_flags, skip_added,
             for linenum in sorted(linenums, reverse=True):
                 add_imply_rule(config[CONFIG_LEN:], fname, linenum)
 
+def defconfig_matches(configs, re_match):
+    """Check if any CONFIG option matches a regex
+
+    The match must be complete, i.e. from the start to end of the CONFIG option.
+
+    Args:
+        configs (dict): Dict of CONFIG options:
+            key: CONFIG option
+            value: Value of option
+        re_match (re.Pattern): Match to check
+
+    Returns:
+        bool: True if any CONFIG matches the regex
+    """
+    for cfg in configs:
+        m_cfg = re_match.match(cfg)
+        if m_cfg and m_cfg.span()[1] == len(cfg):
+            return True
+    return False
 
 def do_find_config(config_list):
     """Find boards with a given combination of CONFIGs
 
     Params:
-        config_list: List of CONFIG options to check (each a string consisting
+        config_list: List of CONFIG options to check (each a regex consisting
             of a config option, with or without a CONFIG_ prefix. If an option
             is preceded by a tilde (~) then it must be false, otherwise it must
             be true)
@@ -1643,8 +1662,9 @@  def do_find_config(config_list):
         # running for the next stage
         in_list = out
         out = set()
+        re_match = re.compile(cfg)
         for defc in in_list:
-            has_cfg = cfg in config_db[defc]
+            has_cfg = defconfig_matches(config_db[defc], re_match)
             if has_cfg == want:
                 out.add(defc)
     if adhoc: