]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Closing the etc/os-release file, which raises a warning when conducting unit tests... 885/head
authorgsegatti <gabrielsegatti2@gmail.com>
Mon, 17 Jan 2022 17:03:54 +0000 (09:03 -0800)
committergsegatti <gabrielsegatti2@gmail.com>
Mon, 17 Jan 2022 17:24:43 +0000 (09:24 -0800)
mkosi/__init__.py

index b75224c70aa21ba14e41fc84a59ea97f1e58b79f..4a3a62ab679ca980f55ec7f671319e1884fb2e2c 100644 (file)
@@ -178,18 +178,19 @@ def read_os_release() -> Iterator[Tuple[str, str]]:
         filename = "/usr/lib/os-release"
         f = open(filename)
 
-    for line_number, line in enumerate(f, start=1):
-        line = line.rstrip()
-        if not line or line.startswith("#"):
-            continue
-        m = re.match(r"([A-Z][A-Z_0-9]+)=(.*)", line)
-        if m:
-            name, val = m.groups()
-            if val and val[0] in "\"'":
-                val = ast.literal_eval(val)
-            yield name, val
-        else:
-            print(f"{filename}:{line_number}: bad line {line!r}", file=sys.stderr)
+    with f:
+        for line_number, line in enumerate(f, start=1):
+            line = line.rstrip()
+            if not line or line.startswith("#"):
+                continue
+            m = re.match(r"([A-Z][A-Z_0-9]+)=(.*)", line)
+            if m:
+                name, val = m.groups()
+                if val and val[0] in "\"'":
+                    val = ast.literal_eval(val)
+                yield name, val
+            else:
+                print(f"{filename}:{line_number}: bad line {line!r}", file=sys.stderr)
 
 
 def print_running_cmd(cmdline: Iterable[str]) -> None: