diff mbox series

[meta-swupdate,1/2] Cleanup regex for automatic sha256 and version in sw-description

Message ID 20211222140401.780-1-sam.van.den.berge@gmail.com
State Changes Requested
Headers show
Series [meta-swupdate,1/2] Cleanup regex for automatic sha256 and version in sw-description | expand

Commit Message

Sam Van Den Berge Dec. 22, 2021, 2:03 p.m. UTC
Current regex has defined capture groups 'before_placeholder' and
'quote' which are not used so there is no need to capture these.

Also due to the square brackets in the part [sha256|version], the regex
will match any of those characters. What should be used here are normal
brackets () so that sha256 or version are literally matched.

>>> import re
>>> regex = re.compile(r"^\s*(sha256|version).+[=:].*[\'\"]@(?P<filename>.*)[\'\"]")
>>>
>>> # Works with sha256
>>> regex.match('sha256 = "@file.ext"').groups()
('sha256', 'file.ext')
>>>
>>> # Works with version
>>> regex.match('version = "@file.ext"').groups()
('version', 'file.ext')
>>>
>>> # Does not work with anything else
>>> regex.match('other = "@file.ext"').groups()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'groups'
>>>
>>> # Also works with white spaces up front and a colon as separator
>>> regex.match('     sha256 : "@file.ext"').groups()
('sha256', 'file.ext')

Signed-off-by: Sam Van Den Berge <sam.van.den.berge@gmail.com>
---
 classes/swupdate-common.bbclass | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

Comments

Stefano Babic Jan. 17, 2022, 3:09 p.m. UTC | #1
Hi Sam,

On 22.12.21 09:03, Sam Van Den Berge wrote:
> Current regex has defined capture groups 'before_placeholder' and
> 'quote' which are not used so there is no need to capture these.
> 
> Also due to the square brackets in the part [sha256|version], the regex
> will match any of those characters. What should be used here are normal
> brackets () so that sha256 or version are literally matched.
> 

Ok - sol let's recognize just the exact word.

>>>> import re
>>>> regex = re.compile(r"^\s*(sha256|version).+[=:].*[\'\"]@(?P<filename>.*)[\'\"]")
>>>>
>>>> # Works with sha256
>>>> regex.match('sha256 = "@file.ext"').groups()
> ('sha256', 'file.ext')
>>>>
>>>> # Works with version
>>>> regex.match('version = "@file.ext"').groups()
> ('version', 'file.ext')
>>>>
>>>> # Does not work with anything else
>>>> regex.match('other = "@file.ext"').groups()
> Traceback (most recent call last):
>    File "<stdin>", line 1, in <module>
> AttributeError: 'NoneType' object has no attribute 'groups'
>>>>

This stuff including Traceback is part of the commit message and it will 
be applied, leading to confusion. It does not add information and it 
seems it was for debugging purpose, so drop it.

>>>> # Also works with white spaces up front and a colon as separator
>>>> regex.match('     sha256 : "@file.ext"').groups()
> ('sha256', 'file.ext')
> 
> Signed-off-by: Sam Van Den Berge <sam.van.den.berge@gmail.com>
> ---
>   classes/swupdate-common.bbclass | 4 +---
>   1 file changed, 1 insertion(+), 3 deletions(-)
> 
> diff --git a/classes/swupdate-common.bbclass b/classes/swupdate-common.bbclass
> index 7d06c96..e6e5a5f 100644
> --- a/classes/swupdate-common.bbclass
> +++ b/classes/swupdate-common.bbclass
> @@ -79,9 +79,7 @@ def swupdate_write_sha256(s):
>       write_lines = []
>       with open(os.path.join(s, "sw-description"), 'r') as f:
>          for line in f:
> -          shastr = r"sha256.+=.+@(.+\")"
> -          #m = re.match(r"^(?P<before_placeholder>.+)sha256.+=.+(?P<filename>\w+)", line)
> -          m = re.match(r"^(?P<before_placeholder>.+)[sha256|version].+[=:].*(?P<quote>[\'\"])@(?P<filename>.*)(?P=quote)", line)
> +          m = re.match(r"^\s*(sha256|version).+[=:].*[\'\"]@(?P<filename>.*)[\'\"]", line)
>             if m:
>                 filename = m.group('filename')
>                 hash = swupdate_get_sha256(s, filename)
> 

Best regards,
Stefano Babic
diff mbox series

Patch

diff --git a/classes/swupdate-common.bbclass b/classes/swupdate-common.bbclass
index 7d06c96..e6e5a5f 100644
--- a/classes/swupdate-common.bbclass
+++ b/classes/swupdate-common.bbclass
@@ -79,9 +79,7 @@  def swupdate_write_sha256(s):
     write_lines = []
     with open(os.path.join(s, "sw-description"), 'r') as f:
        for line in f:
-          shastr = r"sha256.+=.+@(.+\")"
-          #m = re.match(r"^(?P<before_placeholder>.+)sha256.+=.+(?P<filename>\w+)", line)
-          m = re.match(r"^(?P<before_placeholder>.+)[sha256|version].+[=:].*(?P<quote>[\'\"])@(?P<filename>.*)(?P=quote)", line)
+          m = re.match(r"^\s*(sha256|version).+[=:].*[\'\"]@(?P<filename>.*)[\'\"]", line)
           if m:
               filename = m.group('filename')
               hash = swupdate_get_sha256(s, filename)