-
-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathutils.py
More file actions
115 lines (95 loc) · 3.68 KB
/
Copy pathutils.py
File metadata and controls
115 lines (95 loc) · 3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# coding=utf-8
#
# The Qubes OS Project, http://www.qubes-os.org
#
# Copyright (C) 2022 Piotr Bartman <prbartman@invisiblethingslab.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
# USA.
import re
import ast
from typing import Optional, Dict, Any
def get_os_data(logger: Optional = None) -> Dict[str, Any]:
"""
Return dictionary with info about the operating system
Dictionary contains:
id: "linux" or a lower-case string identifying the operating system,
name: "Linux" or a string identifying the operating system,
codename (optional): an operating system release code name,
release (optional): version string,
os_family: "Unknown", "RedHat", "Debian", "ArchLinux", "Guix".
"""
data = {}
os_release = _load_os_release(
"/etc/os-release", "/usr/lib/os-release", logger=logger
)
data["id"] = os_release.get("ID", "linux").strip()
data["name"] = os_release.get("NAME", "Linux").strip()
if "VERSION_ID" in os_release:
release = os_release["VERSION_ID"]
data["release"] = release
if "VERSION_CODENAME" in os_release:
data["codename"] = os_release["VERSION_CODENAME"]
family = [
os_release.get("ID", "linux"),
*os_release.get("ID_LIKE", "").split(),
]
data["os_family"] = "Unknown"
if "debian" in family:
data["os_family"] = "Debian"
if "rhel" in family or "fedora" in family:
data["os_family"] = "RedHat"
if "qubes" in family:
# We do not want to use 'RedHat' for dom0 since usually plugins do not apply to dom0
data["os_family"] = "Qubes"
if "arch" in family:
data["os_family"] = "ArchLinux"
if "guix" in family:
data["os_family"] = "Guix"
return data
def _load_os_release(*os_release_files, logger: Optional):
"""
Load os-release as dictionary.
More: http://www.freedesktop.org/software/systemd/man/os-release.html
"""
result = {}
for filename in os_release_files:
try:
with open(filename) as file:
for line_number, line in enumerate(file):
line = line.rstrip()
if not line or line.startswith("#"):
continue
match = re.match(r"([A-Z][A-Z_0-9]+)=(.*)", line)
if match:
key, val = match.groups()
if val and val[0] in "\"'":
val = ast.literal_eval(val)
result[key] = val
else:
if logger:
logger.error(
"%s:%i: error in parsing: %s",
filename,
line_number,
line,
)
break
except OSError as exc:
if logger:
logger.info("Could not read file %s: %s", filename, str(exc))
if not result:
raise IOError("Failed to read os-release file")
return result