diff options
author | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2021-05-20 20:00:18 +0200 |
---|---|---|
committer | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2021-05-21 16:33:04 +0200 |
commit | 3ca606d103cb9c4011cfcb1d377b6749fb73aaec (patch) | |
tree | 267b4d54a33ffd91749e1f2802a96e88abb69770 /man/check-os-release.py | |
parent | e839ebe5511028f6032ae53ebb43601c1f4624be (diff) | |
download | systemd-3ca606d103cb9c4011cfcb1d377b6749fb73aaec.tar.gz |
man: add example os-release mangling in python
This is also not entirely obvious. I think the code I came
up with is pretty elegant ;] The final part of of the code that makes
use of the parsed data is kept very similar to the shell code on purpose,
even though it could be written a bit more idiomatically.
Diffstat (limited to 'man/check-os-release.py')
-rw-r--r-- | man/check-os-release.py | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/man/check-os-release.py b/man/check-os-release.py new file mode 100644 index 0000000000..f6d23c8c46 --- /dev/null +++ b/man/check-os-release.py @@ -0,0 +1,28 @@ +#!/usr/bin/python + +import ast +import re + +def read_os_release(): + try: + f = open('/etc/os-release') + except FileNotFoundError: + f = open('/usr/lib/os-release') + + for line_number, line in enumerate(f): + 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) + +os_release = dict(read_os_release()) + +pretty_name = os_release.get('PRETTY_NAME', 'Linux') +print(f'Running on {pretty_name}') + +if (os_release.get('ID', 'linux') == 'debian' or + os_release.get('ID_LIKE', None) == 'debian'): + print('Looks like Debian!') |