[libvirt PATCH 1/6] scripts: group-qemu-caps: use read

Ján Tomko posted 6 patches 3 months ago
[libvirt PATCH 1/6] scripts: group-qemu-caps: use read
Posted by Ján Tomko 3 months ago
Signed-off-by: Ján Tomko <jtomko@redhat.com>
---
 scripts/group-qemu-caps.py | 34 ++++++++++++++++++----------------
 1 file changed, 18 insertions(+), 16 deletions(-)

diff --git a/scripts/group-qemu-caps.py b/scripts/group-qemu-caps.py
index 8a899a76c2..ec10f24384 100755
--- a/scripts/group-qemu-caps.py
+++ b/scripts/group-qemu-caps.py
@@ -30,24 +30,26 @@ import sys
 def load_caps_flags(filename, start_regex, end_regex):
     capsflags = []
     game_on = False
+    lines = []
 
     with open(filename, "r") as fh:
-        for line in fh:
-            line = line.rstrip("\n")
-            if game_on:
-                if re.search(r'''.*/\* [0-9]+ \*/.*''', line):
-                    continue
-                if re.search(r'''^\s*$''', line):
-                    continue
-                match = re.search(r'''[ ]+([A-Z0-9_]+)''', line)
-
-                if match:
-                    capsflags.append(match[1])
-
-            if re.search(start_regex, line):
-                game_on = True
-            elif game_on and re.search(end_regex, line):
-                game_on = False
+        lines = fh.read().splitlines()
+
+    for line in lines:
+        if game_on:
+            if re.search(r'''.*/\* [0-9]+ \*/.*''', line):
+                continue
+            if re.search(r'''^\s*$''', line):
+                continue
+            match = re.search(r'''[ ]+([A-Z0-9_]+)''', line)
+
+            if match:
+                capsflags.append(match[1])
+
+        if re.search(start_regex, line):
+            game_on = True
+        elif game_on and re.search(end_regex, line):
+            game_on = False
 
     return capsflags
 
-- 
2.45.2
Re: [libvirt PATCH 1/6] scripts: group-qemu-caps: use read
Posted by Martin Kletzander 4 weeks ago
On Wed, Jul 31, 2024 at 03:20:47PM +0200, Ján Tomko wrote:
>Signed-off-by: Ján Tomko <jtomko@redhat.com>
>---
> scripts/group-qemu-caps.py | 34 ++++++++++++++++++----------------
> 1 file changed, 18 insertions(+), 16 deletions(-)
>
>diff --git a/scripts/group-qemu-caps.py b/scripts/group-qemu-caps.py
>index 8a899a76c2..ec10f24384 100755
>--- a/scripts/group-qemu-caps.py
>+++ b/scripts/group-qemu-caps.py
>@@ -30,24 +30,26 @@ import sys
> def load_caps_flags(filename, start_regex, end_regex):
>     capsflags = []
>     game_on = False
>+    lines = []
>
>     with open(filename, "r") as fh:
>-        for line in fh:
>-            line = line.rstrip("\n")
>-            if game_on:
>-                if re.search(r'''.*/\* [0-9]+ \*/.*''', line):
>-                    continue
>-                if re.search(r'''^\s*$''', line):
>-                    continue
>-                match = re.search(r'''[ ]+([A-Z0-9_]+)''', line)
>-
>-                if match:
>-                    capsflags.append(match[1])
>-
>-            if re.search(start_regex, line):
>-                game_on = True
>-            elif game_on and re.search(end_regex, line):
>-                game_on = False
>+        lines = fh.read().splitlines()

What's the point?  This way you have to read the whole file into the
memory.  If you keep it as is and just break on `game_on = False` you
skip reading the rest of the file at least.  You can even do:

   for line in open(filename, 'r'):

>+
>+    for line in lines:
>+        if game_on:
>+            if re.search(r'''.*/\* [0-9]+ \*/.*''', line):
>+                continue
>+            if re.search(r'''^\s*$''', line):
>+                continue
>+            match = re.search(r'''[ ]+([A-Z0-9_]+)''', line)
>+
>+            if match:
>+                capsflags.append(match[1])
>+
>+        if re.search(start_regex, line):
>+            game_on = True
>+        elif game_on and re.search(end_regex, line):
>+            game_on = False
>
>     return capsflags
>
>-- 
>2.45.2
>