MCPcopy
hub / github.com/GitGuardian/ggshield / parse_os_release

Function parse_os_release

ggshield/utils/os.py:27–52  ·  view source on GitHub ↗

Extract and return Linux's OS-name and OS-Version If extraction fails, we return ('linux', 'unknown')

(os_release_path: Path)

Source from the content-addressed store, hash-verified

25
26
27def parse_os_release(os_release_path: Path) -> Tuple[str, str]:
28 """
29 Extract and return Linux's OS-name and OS-Version
30 If extraction fails, we return ('linux', 'unknown')
31 """
32 error_tuple = "linux", "unknown"
33
34 try:
35 with open(os_release_path) as f:
36 lines = f.readlines()
37
38 # Build a dictionary from the os-release file contents
39 data_dict = {}
40 for line in lines:
41 if "=" in line:
42 key, value = line.split("=")
43 key, value = key.strip(), value.strip()
44 data_dict[key] = value.strip('"')
45
46 if "ID" not in data_dict:
47 return error_tuple
48
49 return data_dict["ID"], data_dict.get("VERSION_ID", "unknown")
50 except Exception as exc:
51 logger.warning(f"Failed to read Linux OS name and version: {exc}")
52 return error_tuple
53
54
55@contextmanager

Callers 2

test_parse_os_releaseFunction · 0.90
get_os_infoFunction · 0.85

Calls 1

getMethod · 0.80

Tested by 1

test_parse_os_releaseFunction · 0.72