Extract and return Linux's OS-name and OS-Version If extraction fails, we return ('linux', 'unknown')
(os_release_path: Path)
| 25 | |
| 26 | |
| 27 | def 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 |