os_probe.py: Rework to be Object-oriented

Minimize repetitive calls to platform.uname()
This commit is contained in:
Mykola Grymalyuk
2023-02-09 12:44:26 -07:00
parent e076260a1a
commit f9c7273106
2 changed files with 72 additions and 25 deletions
+5 -4
View File
@@ -44,10 +44,11 @@ class OpenCoreLegacyPatcher:
def generate_base_data(self): def generate_base_data(self):
self.constants.detected_os = os_probe.detect_kernel_major() os_data = os_probe.OSProbe()
self.constants.detected_os_minor = os_probe.detect_kernel_minor() self.constants.detected_os = os_data.detect_kernel_major()
self.constants.detected_os_build = os_probe.detect_os_build() self.constants.detected_os_minor = os_data.detect_kernel_minor()
self.constants.detected_os_version = os_probe.detect_os_version() self.constants.detected_os_build = os_data.detect_os_build()
self.constants.detected_os_version = os_data.detect_os_version()
self.constants.computer = device_probe.Computer.probe() self.constants.computer = device_probe.Computer.probe()
self.constants.recovery_status = utilities.check_recovery() self.constants.recovery_status = utilities.check_recovery()
self.computer = self.constants.computer self.computer = self.constants.computer
+67 -21
View File
@@ -5,31 +5,77 @@ import subprocess
import plistlib import plistlib
def detect_kernel_major(): class OSProbe:
# Return Major Kernel Version """
# Example Output: 21 (integer) Library for querying OS information specific to macOS
return int(platform.uname().release.partition(".")[0]) """
def __init__(self):
self.uname_data = platform.uname()
def detect_kernel_minor(): def detect_kernel_major(self):
# Return Minor Kernel Version """
# Example Output: 1 (integer) Detect the booted major kernel version
return int(platform.uname().release.partition(".")[2].partition(".")[0])
Returns:
int: Major kernel version (ex. 21, from 21.1.0)
"""
return int(self.uname_data.release.partition(".")[0])
def detect_os_version(): def detect_kernel_minor(self):
# Return OS version """
# Example Output: 12.0 (string) Detect the booted minor kernel version
return subprocess.run("sw_vers -productVersion".split(), stdout=subprocess.PIPE).stdout.decode().strip()
Returns:
int: Minor kernel version (ex. 1, from 21.1.0)
"""
return int(self.uname_data.release.partition(".")[2].partition(".")[0])
def detect_os_build(): def detect_os_version(self):
# Return OS build """
# Example Output: 21A5522h (string) Detect the booted OS version
# With macOS 13.2, Apple implemented the Rapid Security Response system which Returns:
# will change the reported build to the RSR version and not the original host str: OS version (ex. 12.0)
# To get the proper versions: """
# - Host: /System/Library/CoreServices/SystemVersion.plist
# - RSR: /System/Volumes/Preboot/Cryptexes/OS/System/Library/CoreServices/SystemVersion.plist result = subprocess.run(["sw_vers", "-productVersion"], stdout=subprocess.PIPE)
return plistlib.load(open("/System/Library/CoreServices/SystemVersion.plist", "rb"))["ProductBuildVersion"] if result.returncode != 0:
raise RuntimeError("Failed to detect OS version")
return result.stdout.decode().strip()
def detect_os_build(self, rsr: bool = False):
"""
Detect the booted OS build
Implementation note:
With macOS 13.2, Apple implemented the Rapid Security Response system which
will change the reported build to the RSR version and not the original host
To get the proper versions:
- Host: /System/Library/CoreServices/SystemVersion.plist
- RSR: /System/Volumes/Preboot/Cryptexes/OS/System/Library/CoreServices/SystemVersion.plist
Parameters:
rsr (bool): Whether to use the RSR version of the build
Returns:
str: OS build (ex. 21A5522h)
"""
file_path = "/System/Library/CoreServices/SystemVersion.plist"
if rsr is True:
file_path = f"/System/Volumes/Preboot/Cryptexes/OS{file_path}"
try:
return plistlib.load(open(file_path, "rb"))["ProductBuildVersion"]
except Exception as e:
raise RuntimeError(f"Failed to detect OS build: {e}")