BIOSUtilities v24.10.06

24.10.06

Changed BIOSUtility.parse_format() to return a boolean
Changed 7-Zip and EFI decompressors to return booleans
Apple EFI Package Extractor support for InstallAssistant
Apple EFI Image Identifier support for Apple ROM Version
Added Apple EFI Image Identifier class instance attributes
Improved flow of non-PATH external executable dependencies
Fixed crash when attempting to clear read-only attribute
Fixed incompatibility with Python versions prior to 3.12
Performance improvements when initializing BIOSUtilities
Improved argument naming and definitions of "main" script
Improved the README with new "main" and Apple EFI changes
This commit is contained in:
Plato Mavropoulos 2024-10-07 01:24:12 +03:00
parent 4175af9eb1
commit eda154b0f2
26 changed files with 346 additions and 223 deletions

View file

@ -41,7 +41,7 @@ def is_szip_supported(in_path: str, padding: int = 0, args: list | None = None,
def szip_decompress(in_path: str, out_path: str, in_name: str | None, padding: int = 0, args: list | None = None,
check: bool = False, silent: bool = False) -> int:
check: bool = False, silent: bool = False) -> bool:
""" Archive decompression via 7-Zip """
if not in_name:
@ -64,12 +64,12 @@ def szip_decompress(in_path: str, out_path: str, in_name: str | None, padding: i
if not silent:
printer(message=f'Error: 7-Zip could not extract {in_name} file {in_path}: {error}!', padding=padding)
return 1
return False
if not silent:
printer(message=f'Successful {in_name} decompression via 7-Zip!', padding=padding)
return 0
return True
def efi_compress_sizes(data: bytes | bytearray) -> tuple[int, int]:
@ -98,7 +98,7 @@ def is_efi_compressed(data: bytes | bytearray, strict: bool = True) -> bool:
def efi_decompress(in_path: str, out_path: str, padding: int = 0, silent: bool = False,
comp_type: str = '--uefi') -> int:
comp_type: str = '--uefi') -> bool:
""" EFI/Tiano Decompression via TianoCompress """
try:
@ -114,9 +114,9 @@ def efi_decompress(in_path: str, out_path: str, padding: int = 0, silent: bool =
if not silent:
printer(message=f'Error: TianoCompress could not extract file {in_path}: {error}!', padding=padding)
return 1
return False
if not silent:
printer(message='Successful EFI decompression via TianoCompress!', padding=padding)
return 0
return True

View file

@ -20,7 +20,7 @@ from biosutilities.common.paths import project_root
from biosutilities.common.texts import to_string
def get_external_path(cmd: str | list | tuple, raise_on_error: bool = True) -> str | None:
def get_external_path(cmd: str | list | tuple) -> str:
""" Get external dependency path (PATH environment variable or "external" directory) """
external_root: str = os.path.join(project_root(), 'external')
@ -33,18 +33,15 @@ def get_external_path(cmd: str | list | tuple, raise_on_error: bool = True) -> s
if command_path and os.path.isfile(path=command_path):
return command_path
if raise_on_error:
raise OSError(f'{to_string(in_object=cmd, sep_char=", ")} could not be found!')
return None
raise OSError(f'{to_string(in_object=cmd, sep_char=", ")} could not be found!')
def big_script_tool() -> Type | None:
""" Get Intel BIOS Guard Script Tool class """
bgst: str | None = get_external_path(cmd='big_script_tool', raise_on_error=False)
try:
bgst: str = get_external_path(cmd='big_script_tool')
if bgst is not None:
bgst_spec: ModuleSpec | None = spec_from_file_location(
name='big_script_tool', location=re.sub(r'\.PY$', '.py', bgst))
@ -57,35 +54,37 @@ def big_script_tool() -> Type | None:
bgst_spec.loader.exec_module(module=bgst_module)
return getattr(bgst_module, 'BigScript')
except OSError:
pass
return None
def comextract_path() -> str | None:
def comextract_path() -> str:
""" Get ToshibaComExtractor path """
return get_external_path(cmd='comextract')
def szip_path() -> str | None:
def szip_path() -> str:
""" Get 7-Zip path """
return get_external_path(cmd=['7zzs', '7zz', '7z'])
def tiano_path() -> str | None:
def tiano_path() -> str:
""" Get TianoCompress path """
return get_external_path(cmd='TianoCompress')
def uefifind_path() -> str | None:
def uefifind_path() -> str:
""" Get UEFIFind path """
return get_external_path(cmd='UEFIFind')
def uefiextract_path() -> str | None:
def uefiextract_path() -> str:
""" Get UEFIExtract path """
return get_external_path(cmd='UEFIExtract')

View file

@ -132,7 +132,7 @@ def delete_dirs(in_path: str) -> None:
""" Delete folder(s), if present """
if Path(in_path).is_dir():
shutil.rmtree(path=in_path, onexc=clear_readonly_callback)
shutil.rmtree(path=in_path, onerror=clear_readonly_callback) # pylint: disable=deprecated-argument
def delete_file(in_path: str) -> None:
@ -164,7 +164,7 @@ def clear_readonly_callback(in_func: Callable, in_path: str, _) -> None:
clear_readonly(in_path=in_path)
in_func(in_path=in_path)
in_func(path=in_path)
def path_files(in_path: str, follow_links: bool = False) -> list[str]:

View file

@ -19,9 +19,8 @@ PAT_AMI_UCP: Final[re.Pattern[bytes]] = re.compile(
flags=re.DOTALL
)
PAT_APPLE_EFI: Final[re.Pattern[bytes]] = re.compile(
pattern=br'\$IBIOSI\$.{16}\x2E\x00.{6}\x2E\x00.{8}\x2E\x00.{6}\x2E\x00.{20}\x00{2}',
flags=re.DOTALL
PAT_APPLE_ROM_VER: Final[re.Pattern[bytes]] = re.compile(
pattern=br'Apple ROM Version\x0A\x20{2}'
)
PAT_APPLE_IM4P: Final[re.Pattern[bytes]] = re.compile(
@ -32,14 +31,18 @@ PAT_APPLE_PBZX: Final[re.Pattern[bytes]] = re.compile(
pattern=br'pbzx'
)
PAT_APPLE_PKG_XAR: Final[re.Pattern[bytes]] = re.compile(
pattern=br'xar!'
PAT_APPLE_PKG_DMG: Final[re.Pattern[bytes]] = re.compile(
pattern=br'EFI PART'
)
PAT_APPLE_PKG_TAR: Final[re.Pattern[bytes]] = re.compile(
pattern=br'<key>IFPkgDescriptionDescription</key>'
)
PAT_APPLE_PKG_XAR: Final[re.Pattern[bytes]] = re.compile(
pattern=br'xar!'
)
PAT_AWARD_LZH: Final[re.Pattern[bytes]] = re.compile(
pattern=br'-lh[04567]-'
)
@ -71,16 +74,21 @@ PAT_INSYDE_SFX: Final[re.Pattern[bytes]] = re.compile(
pattern=br'\x0D\x0A;!@InstallEnd@!\x0D\x0A(7z\xBC\xAF\x27|\x6E\xF4\x79\x5F\x4E)'
)
PAT_INTEL_ENG: Final[re.Pattern[bytes]] = re.compile(
PAT_INTEL_ENGINE: Final[re.Pattern[bytes]] = re.compile(
pattern=br'\x04\x00{3}[\xA1\xE1]\x00{3}.{8}\x86\x80.{9}\x00\$((MN2)|(MAN))',
flags=re.DOTALL
)
PAT_INTEL_IFD: Final[re.Pattern[bytes]] = re.compile(
PAT_INTEL_FD: Final[re.Pattern[bytes]] = re.compile(
pattern=br'\x5A\xA5\xF0\x0F.{172}\xFF{16}',
flags=re.DOTALL
)
PAT_INTEL_IBIOSI: Final[re.Pattern[bytes]] = re.compile(
pattern=br'\$IBIOSI\$.{16}\x2E\x00.{6}\x2E\x00.{8}\x2E\x00.{6}\x2E\x00.{20}\x00{2}',
flags=re.DOTALL
)
PAT_MICROSOFT_CAB: Final[re.Pattern[bytes]] = re.compile(
pattern=br'MSCF\x00{4}'
)

View file

@ -30,17 +30,19 @@ def python_version() -> tuple:
def printer(message: str | list | tuple | None = None, padding: int = 0, new_line: bool = True,
pause: bool = False, sep_char: str = ' ') -> None:
""" Show message(s), controlling padding, newline, pausing & separator """
pause: bool = False, sep_char: str = ' ', strip: bool = False) -> None:
""" Show message(s), controlling padding, newline, stripping, pausing & separating """
message_string: str = to_string(in_object='' if message is None else message, sep_char=sep_char)
message_output: str = '\n' if new_line else ''
for line_index, line_text in enumerate(iterable=message_string.split('\n')):
line_newline: str = '' if line_index == 0 else '\n'
for message_line_index, message_line_text in enumerate(iterable=message_string.split('\n')):
line_new: str = '' if message_line_index == 0 else '\n'
message_output += f'{line_newline}{" " * padding}{line_text}'
line_text: str = message_line_text.strip() if strip else message_line_text
message_output += f'{line_new}{" " * padding}{line_text}'
if pause:
input(message_output)

View file

@ -30,10 +30,6 @@ class BIOSUtility:
MIN_PYTHON_VER: Final[tuple[int, int]] = (3, 10)
def __init__(self, arguments: list[str] | None = None) -> None:
self._check_sys_py()
self._check_sys_os()
self.title: str = f'{self.TITLE.strip()} v{__version__}'
argparser: ArgumentParser = ArgumentParser(allow_abbrev=False)
@ -53,9 +49,13 @@ class BIOSUtility:
self._output_path: str = ''
def run_utility(self, padding: int = 0) -> int:
def run_utility(self, padding: int = 0) -> bool:
""" Run utility after checking for supported format """
self._check_sys_py()
self._check_sys_os()
self.show_version(padding=padding)
self._setup_input_files(padding=padding)
@ -85,8 +85,7 @@ class BIOSUtility:
break
if self.parse_format(input_object=input_file, extract_path=extract_path,
padding=padding + 8) in [0, None]:
if self.parse_format(input_object=input_file, extract_path=extract_path, padding=padding + 8):
exit_code -= 1
if is_empty_dir(in_path=extract_path):
@ -94,14 +93,14 @@ class BIOSUtility:
printer(message='Done!\n' if not self.arguments.auto_exit else None, pause=not self.arguments.auto_exit)
return exit_code
return exit_code == 0
def show_version(self, is_boxed: bool = True, padding: int = 0) -> None:
""" Show title and version of utility """
printer(message=to_boxed(in_text=self.title) if is_boxed else self.title, new_line=False, padding=padding)
def parse_format(self, input_object: str | bytes | bytearray, extract_path: str, padding: int = 0) -> int | None:
def parse_format(self, input_object: str | bytes | bytearray, extract_path: str, padding: int = 0) -> bool:
""" Process input object as a specific supported format """
raise NotImplementedError(f'Method "parse_format" not implemented at {__name__}')
@ -112,6 +111,8 @@ class BIOSUtility:
raise NotImplementedError(f'Method "check_format" not implemented at {__name__}')
def _setup_input_files(self, padding: int = 0) -> None:
self._input_files = []
input_paths: list[str] = self.arguments.paths
if not input_paths:
@ -126,6 +127,8 @@ class BIOSUtility:
self._input_files.append(input_path_real)
def _setup_output_dir(self, padding: int = 0) -> None:
self._output_path = ''
output_path: str = self.arguments.output_dir
if not output_path: