From 82ca34e6e6e2f779f7a6ee8da67cb8f81e0fc2a5 Mon Sep 17 00:00:00 2001 From: Zbigniew Jędrzejewski-Szmek Date: Sat, 22 May 2021 20:23:02 +0200 Subject: man/check-os-release.py: ignore comment and empty lines Plus fix off-by-one in error printing. --- man/check-os-release.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'man/check-os-release.py') diff --git a/man/check-os-release.py b/man/check-os-release.py index 069ca74b20..2440202349 100644 --- a/man/check-os-release.py +++ b/man/check-os-release.py @@ -2,21 +2,28 @@ import ast import re +import sys def read_os_release(): try: - f = open('/etc/os-release') + filename = '/etc/os-release' + f = open(filename) except FileNotFoundError: - f = open('/usr/lib/os-release') + filename = '/usr/lib/os-release' + f = open(filename) for line_number, line in enumerate(f): - if m := re.match(r'([A-Z][A-Z_0-9]+)=(.*?)\s*$', line): + line = line.rstrip() + if not line or line.startswith('#'): + continue + if m := re.match(r'([A-Z][A-Z_0-9]+)=(.*)', line): name, val = m.groups() if val and val[0] in '"\'': val = ast.literal_eval(val) yield name, val else: - print(f'Warning: bad line {line_number}: {line}', file=sys.stderr) + print(f'{filename}:{line_number + 1}: bad line {line!r}', + file=sys.stderr) os_release = dict(read_os_release()) -- cgit v1.2.3