From: gsegatti Date: Mon, 17 Jan 2022 17:03:54 +0000 (-0800) Subject: Closing the etc/os-release file, which raises a warning when conducting unit tests... X-Git-Tag: v13~95^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F885%2Fhead;p=thirdparty%2Fmkosi.git Closing the etc/os-release file, which raises a warning when conducting unit tests, even in read only mode. --- diff --git a/mkosi/__init__.py b/mkosi/__init__.py index b75224c70..4a3a62ab6 100644 --- a/mkosi/__init__.py +++ b/mkosi/__init__.py @@ -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: