Skip to content

Commit

Permalink
validation.py: Add optional support for checking unused PatcherSuppor…
Browse files Browse the repository at this point in the history
…tPkg files
  • Loading branch information
khronokernel committed Feb 11, 2024
1 parent f29c629 commit 82084c8
Showing 1 changed file with 77 additions and 1 deletion.
78 changes: 77 additions & 1 deletion resources/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ class PatcherValidation:
Primarily for Continuous Integration
"""

def __init__(self, global_constants: constants.Constants) -> None:
def __init__(self, global_constants: constants.Constants, verify_unused_files: bool = False) -> None:
self.constants: constants.Constants = global_constants
self.verify_unused_files = verify_unused_files
self.active_patchset_files = []

self.constants.validate = True

Expand Down Expand Up @@ -119,6 +121,8 @@ def _validate_root_patch_files(self, major_kernel: int, minor_kernel: int) -> No
if not Path(source_file).exists():
logging.info(f"File not found: {source_file}")
raise Exception(f"Failed to find {source_file}")
if self.verify_unused_files is True:
self.active_patchset_files.append(source_file)

logging.info(f"Validating against Darwin {major_kernel}.{minor_kernel}")
if not sys_patch_helpers.SysPatchHelpers(self.constants).generate_patchset_plist(patchset, f"OpenCore-Legacy-Patcher-{major_kernel}.{minor_kernel}.plist", None):
Expand All @@ -141,6 +145,30 @@ def _validate_sys_patch(self) -> None:
raise Exception("Failed to download Universal-Binaries.dmg")

logging.info("Validating Root Patch File integrity")

if Path(self.constants.payload_path / Path("Universal-Binaries_overlay")).exists():
subprocess.run(
[
"/bin/rm", "-f", Path(self.constants.payload_path / Path("Universal-Binaries_overlay"))
],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT
)
if Path(self.constants.payload_path / Path("Universal-Binaries")).exists():
output = subprocess.run(
[
"/usr/bin/hdiutil", "detach", Path(self.constants.payload_path / Path("Universal-Binaries")),
"-force"
],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT
)

if output.returncode != 0:
logging.info("Failed to unmount Universal-Binaries.dmg")
logging.info(f"Output: {output.stdout.decode()}")
logging.info(f"Return Code: {output.returncode}")

raise Exception("Failed to unmount Universal-Binaries.dmg")

output = subprocess.run(
[
"/usr/bin/hdiutil", "attach", "-noverify", f"{self.constants.payload_local_binaries_root_path_dmg}",
Expand All @@ -165,10 +193,14 @@ def _validate_sys_patch(self) -> None:
for supported_os in [os_data.os_data.big_sur, os_data.os_data.monterey, os_data.os_data.ventura, os_data.os_data.sonoma]:
for i in range(0, 10):
self._validate_root_patch_files(supported_os, i)

logging.info("Validating SNB Board ID patcher")
self.constants.computer.reported_board_id = "Mac-7BA5B2DFE22DDD8C"
sys_patch_helpers.SysPatchHelpers(self.constants).snb_board_id_patch(self.constants.payload_local_binaries_root_path)

if self.verify_unused_files is True:
self._find_unused_files()

# unmount the dmg
output = subprocess.run(
[
Expand All @@ -193,6 +225,50 @@ def _validate_sys_patch(self) -> None:
)


def _find_unused_files(self) -> None:
"""
Find PatcherSupportPkg files that are unused by the patcher
Note this function is extremely slow, so only manually run when needed
"""
if self.active_patchset_files == []:
return

unused_files = []

for file in Path(self.constants.payload_local_binaries_root_path).rglob("*"):
if file.is_dir():
continue

relative_path = Path(file).relative_to(self.constants.payload_local_binaries_root_path)

if relative_path.name == ".DS_Store":
continue

if str(relative_path) in [".fseventsd/fseventsd-uuid", ".signed"]:
continue

is_used = False
for used_file in self.active_patchset_files:
used_relative_path = Path(used_file).relative_to(self.constants.payload_local_binaries_root_path)
if str(relative_path) in str(used_relative_path):
is_used = True
break
if str(used_relative_path) in str(relative_path):
is_used = True
break

if is_used:
continue

unused_files.append(relative_path)

if len(unused_files) > 0:
logging.info("Unused files found:")
for file in unused_files:
logging.info(f" {file}")


def _validate_configs(self) -> None:
"""
Validates build modules
Expand Down

0 comments on commit 82084c8

Please sign in to comment.