diff mbox series

[V4,2/3] kernel-module-split: use context manager to open files

Message ID 20230622084452.2026849-2-jose.quaresma@foundries.io
State New
Headers show
Series [V4,1/3] kernel-module-split: make autoload and probeconf distribution specific | expand

Commit Message

Jose Quaresma June 22, 2023, 8:44 a.m. UTC
Signed-off-by: Jose Quaresma <jose.quaresma@foundries.io>
---
 .../kernel-module-split.bbclass               | 23 ++++++++-----------
 1 file changed, 10 insertions(+), 13 deletions(-)
diff mbox series

Patch

diff --git a/meta/classes-recipe/kernel-module-split.bbclass b/meta/classes-recipe/kernel-module-split.bbclass
index 1d5e39b44d..e19ad8e3c5 100644
--- a/meta/classes-recipe/kernel-module-split.bbclass
+++ b/meta/classes-recipe/kernel-module-split.bbclass
@@ -76,9 +76,8 @@  python split_kernel_module_packages () {
             cmd = "%sobjcopy -j .modinfo -O binary %s %s" % (d.getVar("HOST_PREFIX") or "", file, tmpfile)
         subprocess.check_call(cmd, shell=True)
         # errors='replace': Some old kernel versions contain invalid utf-8 characters in mod descriptions (like 0xf6, 'รถ')
-        f = open(tmpfile, errors='replace')
-        l = f.read().split("\000")
-        f.close()
+        with open(tmpfile, errors='replace') as f:
+            l = f.read().split("\000")
         os.close(tf[0])
         os.unlink(tmpfile)
         if compressed:
@@ -106,13 +105,12 @@  python split_kernel_module_packages () {
         if autoload and basename not in autoloadlist:
             bb.warn("module_autoload_%s is defined but '%s' isn't included in KERNEL_MODULE_AUTOLOAD, please add it there" % (basename, basename))
         if basename in autoloadlist:
-            f = open("%s%s" % (dvar, autoloadpath), 'w')
-            if autoload:
-                for m in autoload.split():
-                    f.write('%s\n' % m)
-            else:
-                f.write('%s\n' % basename)
-            f.close()
+            with open("%s%s" % (dvar, autoloadpath), 'w') as f:
+                if autoload:
+                    for m in autoload.split():
+                        f.write('%s\n' % m)
+                else:
+                    f.write('%s\n' % basename)
             autoloadpath2append = ' %s' % autoloadpath
             d.appendVar('FILES:%s' % pkg, autoloadpath2append)
             d.appendVar('CONFFILES:%s' % pkg, autoloadpath2append)
@@ -127,9 +125,8 @@  python split_kernel_module_packages () {
         modconflist = (d.getVar("KERNEL_MODULE_PROBECONF") or "").split()
         modconf = d.getVar('module_conf_%s' % basename)
         if modconf and basename in modconflist:
-            f = open("%s%s" % (dvar, modconfpath), 'w')
-            f.write("%s\n" % modconf)
-            f.close()
+            with open("%s%s" % (dvar, modconfpath), 'w') as f:
+                f.write("%s\n" % modconf)
             modconfpath2append = ' %s' % modconfpath
             d.appendVar('FILES:%s' % pkg, modconfpath2append)
             d.appendVar('CONFFILES:%s' % pkg, modconfpath2append)