2022-01-13 17:39:35 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
from pathlib import Path
|
|
|
|
import os
|
|
|
|
import shutil
|
2022-01-20 20:35:19 +01:00
|
|
|
from time import time
|
2022-01-13 17:39:35 +01:00
|
|
|
|
|
|
|
|
2022-01-29 08:58:54 +01:00
|
|
|
__version__ = "0.0.6"
|
2022-01-13 17:39:35 +01:00
|
|
|
__author__ = "rigodron, algoflash, GGLinnk"
|
|
|
|
__license__ = "MIT"
|
|
|
|
__status__ = "developpement"
|
2022-01-16 17:59:30 +01:00
|
|
|
|
|
|
|
|
2022-01-13 17:44:29 +01:00
|
|
|
##################################################
|
|
|
|
# Set roms_path with your ROMs (at the root of the ROM folder)
|
2022-01-18 17:23:08 +01:00
|
|
|
# Extract all files from file system using dolphin emu - put it in f"{dolphin_unpack_path}/root/"
|
|
|
|
# Extract boot.dol and apploader.img using dolphin emu - put it in f"{dolphin_unpack_path}/sys/"
|
2022-01-13 17:44:29 +01:00
|
|
|
##################################################
|
2022-01-16 17:59:30 +01:00
|
|
|
roms_path = Path("../ROM")
|
|
|
|
dolphin_unpack_path = Path("dolphin_unpack")
|
2022-01-18 17:23:08 +01:00
|
|
|
|
|
|
|
# Created tmp paths
|
2022-01-16 17:59:30 +01:00
|
|
|
unpack_path = Path("unpack")
|
|
|
|
unpack2_path = Path("unpack2")
|
2022-01-18 07:49:52 +01:00
|
|
|
repack_path = Path("repack")
|
2022-01-13 17:39:35 +01:00
|
|
|
# if there is a way to create symlinks on GCM filesystem, it could create strange situations
|
|
|
|
|
|
|
|
|
2022-01-29 08:58:54 +01:00
|
|
|
def test_storage():
|
|
|
|
total, used, free = shutil.disk_usage("/")
|
|
|
|
if free - 10**12 < 3 * sum(path.stat().st_size for path in roms_path.glob('*') if path.is_file()):
|
|
|
|
raise Exception("Error - Not enought free space on the disk to run tests.")
|
|
|
|
|
|
|
|
|
2022-01-13 17:39:35 +01:00
|
|
|
def print_paths_differences(folder1_paths:list, folder2_paths:list):
|
|
|
|
backup_paths = folder2_paths.copy()
|
|
|
|
for path in folder1_paths:
|
|
|
|
if path in folder2_paths:
|
|
|
|
folder2_paths.remove(path)
|
|
|
|
for path in backup_paths:
|
|
|
|
if path in folder1_paths:
|
|
|
|
folder1_paths.remove(path)
|
2022-01-29 08:58:54 +01:00
|
|
|
print("folder1 diff:")
|
2022-01-13 17:39:35 +01:00
|
|
|
for path in folder1_paths:
|
|
|
|
print(path)
|
2022-01-29 08:58:54 +01:00
|
|
|
print("folder2 diff:")
|
2022-01-13 17:39:35 +01:00
|
|
|
for path in folder2_paths:
|
|
|
|
print(path)
|
|
|
|
|
|
|
|
|
2022-01-20 20:35:19 +01:00
|
|
|
# compare two files
|
|
|
|
def compare_files(file1_path:Path, file2_path:Path):
|
|
|
|
CLUSTER_LEN = 131072
|
|
|
|
with file1_path.open("rb") as file1, file2_path.open("rb") as file2:
|
|
|
|
# Init
|
|
|
|
bytes1 = file1.read(CLUSTER_LEN)
|
|
|
|
bytes2 = file2.read(CLUSTER_LEN)
|
|
|
|
while bytes1 or bytes2: # continue if bytes1 and bytes2 have a len > 0
|
|
|
|
if bytes1 != bytes2:
|
|
|
|
return False
|
|
|
|
bytes1 = file1.read(CLUSTER_LEN)
|
|
|
|
bytes2 = file2.read(CLUSTER_LEN)
|
|
|
|
return True
|
2022-01-13 17:39:35 +01:00
|
|
|
|
|
|
|
|
|
|
|
# compare two GCM
|
|
|
|
# -> raise an exception if there is a difference
|
2022-01-20 20:35:19 +01:00
|
|
|
def compare_GCM(folder1: Path, folder2: Path):
|
|
|
|
folder1_root_paths = list((folder1 / "root").glob("**/*"))
|
|
|
|
folder1_file_count = len(folder1_root_paths)
|
2022-01-18 17:23:08 +01:00
|
|
|
print(f"compare \"{folder1}\" - \"{folder2}\" ({folder1_file_count} files)")
|
|
|
|
if folder1_file_count == 0:
|
2022-01-29 08:58:54 +01:00
|
|
|
raise Exception(f"Error - Empty folder: {folder1}")
|
2022-01-18 17:23:08 +01:00
|
|
|
|
2022-01-13 17:39:35 +01:00
|
|
|
len1 = len(folder1.parts)
|
|
|
|
len2 = len(folder2.parts)
|
|
|
|
# 1. Compare names of filesystems
|
2022-01-20 20:35:19 +01:00
|
|
|
folder1_paths = [Path(*path.parts[len1:]) for path in folder1_root_paths]
|
|
|
|
folder2_paths = [Path(*path.parts[len2:]) for path in (folder2 / "root").glob('**/*')]
|
2022-01-13 17:39:35 +01:00
|
|
|
if folder1_paths != folder2_paths:
|
|
|
|
print_paths_differences(folder1_paths, folder2_paths)
|
2022-01-29 08:58:54 +01:00
|
|
|
raise Exception(f"Error - Folders \"{folder1}\" and \"{folder2}\" are different (not the same folders or files names).")
|
2022-01-13 17:39:35 +01:00
|
|
|
# 2. Compare sys files content
|
2022-01-29 08:58:54 +01:00
|
|
|
if not compare_files(folder1 / "sys" / "apploader.img", folder2 / "sys" / "apploader.img"):
|
|
|
|
raise Exception(f"Error - \"{folder1 / 'sys/apploader.bin'}\" and \"{folder2 / 'sys/apploader.bin'}\" are different.")
|
|
|
|
if not compare_files(folder1 / "sys" / "boot.dol", folder2 / "sys" / "boot.dol"):
|
|
|
|
raise Exception(f"Error - \"{folder1 / 'sys/boot.bin'}\" and \"{folder2 / 'sys/boot.bin'}\" are different.")
|
2022-01-13 17:39:35 +01:00
|
|
|
|
2022-01-20 20:35:19 +01:00
|
|
|
for path1 in folder1_root_paths:
|
2022-01-13 17:39:35 +01:00
|
|
|
if path1.is_file():
|
|
|
|
path2 = folder2/Path(*path1.parts[len1:])
|
2022-01-20 20:35:19 +01:00
|
|
|
if not compare_files(path1, path2):
|
2022-01-29 08:58:54 +01:00
|
|
|
raise Exception(f"Error - \"{path1}\" and \"{path2}\" are different.")
|
2022-01-13 17:39:35 +01:00
|
|
|
|
|
|
|
|
2022-01-20 20:35:19 +01:00
|
|
|
##################################################
|
|
|
|
# gcmtool.py commands wrappers
|
|
|
|
##################################################
|
|
|
|
def gcmtool_unpack(iso_path:Path, folder_path:Path):
|
|
|
|
if os.system(f"python gcmtool.py -u \"{iso_path}\" \"{folder_path}\"") != 0:
|
2022-01-29 08:58:54 +01:00
|
|
|
raise Exception("Error while unpacking GCM.")
|
2022-01-20 20:35:19 +01:00
|
|
|
def gcmtool_pack(folder_path:Path, iso_path:Path):
|
|
|
|
if os.system(f"python gcmtool.py -p \"{folder_path}\" \"{iso_path}\"") != 0:
|
2022-01-29 08:58:54 +01:00
|
|
|
raise Exception("Error while packing GCM.")
|
2022-01-20 20:35:19 +01:00
|
|
|
def gcmtool_rebuild_fst(folder_path:Path):
|
|
|
|
if os.system(f"python gcmtool.py -r \"{folder_path}\"") != 0:
|
|
|
|
raise Exception("Error while rebuilding FST.")
|
|
|
|
|
|
|
|
|
2022-01-29 08:58:54 +01:00
|
|
|
TEST_COUNT = 3
|
|
|
|
|
2022-01-20 20:35:19 +01:00
|
|
|
start = time()
|
2022-01-18 07:49:52 +01:00
|
|
|
print("###############################################################################")
|
|
|
|
print("# Checking tests folder")
|
|
|
|
print("###############################################################################")
|
|
|
|
# Check if tests folders exist
|
|
|
|
if unpack_path.is_dir() or unpack2_path.is_dir() or repack_path.is_dir():
|
2022-01-18 17:23:08 +01:00
|
|
|
raise Exception(f"Error - Please remove:\n-{unpack_path}\n-{unpack2_path}\n-{repack_path}")
|
2022-01-18 07:49:52 +01:00
|
|
|
|
2022-01-29 08:58:54 +01:00
|
|
|
test_storage()
|
|
|
|
|
2022-01-18 07:49:52 +01:00
|
|
|
print("###############################################################################")
|
2022-01-29 08:58:54 +01:00
|
|
|
print(f"# TEST 1/{TEST_COUNT}")
|
2022-01-18 17:23:08 +01:00
|
|
|
print("# Comparing roms_path->unpack->[unpack_path] ROMs with [dolphin_unpack_path]")
|
2022-01-18 07:49:52 +01:00
|
|
|
print("###############################################################################")
|
2022-01-16 17:59:30 +01:00
|
|
|
# unpack ROM in unpack_path
|
2022-01-13 17:39:35 +01:00
|
|
|
unpack_path.mkdir(parents=True)
|
2022-01-13 17:44:29 +01:00
|
|
|
for iso_path in roms_path.glob("*"):
|
2022-01-13 17:39:35 +01:00
|
|
|
if iso_path.is_file():
|
2022-01-20 20:35:19 +01:00
|
|
|
gcmtool_unpack(iso_path, unpack_path / iso_path.name)
|
2022-01-13 17:39:35 +01:00
|
|
|
|
2022-01-16 17:59:30 +01:00
|
|
|
# compare unpack_path dolphin_unpack_path
|
2022-01-13 17:39:35 +01:00
|
|
|
for folder_path in unpack_path.glob("*"):
|
2022-01-20 20:35:19 +01:00
|
|
|
compare_GCM(folder_path, dolphin_unpack_path / folder_path.name)
|
2022-01-13 17:39:35 +01:00
|
|
|
|
2022-01-18 07:49:52 +01:00
|
|
|
print("###############################################################################")
|
2022-01-29 08:58:54 +01:00
|
|
|
print(f"# TEST 2/{TEST_COUNT}")
|
2022-01-18 17:23:08 +01:00
|
|
|
print("# Comparing [unpack_path]->pack->unpack->[unpack2_path]")
|
2022-01-18 07:49:52 +01:00
|
|
|
print("###############################################################################")
|
2022-01-16 17:59:30 +01:00
|
|
|
# repack unpack_path repack_path
|
|
|
|
repack_path.mkdir()
|
2022-01-20 20:35:19 +01:00
|
|
|
unpack_paths = list(unpack_path.glob("*"))
|
|
|
|
for folder_path in unpack_paths:
|
|
|
|
gcmtool_pack(folder_path, repack_path / folder_path.name)
|
2022-01-13 17:39:35 +01:00
|
|
|
|
2022-01-16 17:59:30 +01:00
|
|
|
# unpack repack_path unpack2_path
|
2022-01-13 17:39:35 +01:00
|
|
|
unpack2_path.mkdir()
|
2022-01-16 17:59:30 +01:00
|
|
|
for iso_path in repack_path.glob("*"):
|
2022-01-20 20:35:19 +01:00
|
|
|
gcmtool_unpack(iso_path, unpack2_path / iso_path.name)
|
2022-01-13 17:39:35 +01:00
|
|
|
|
2022-01-16 17:59:30 +01:00
|
|
|
# remove repack_path
|
|
|
|
shutil.rmtree(repack_path)
|
2022-01-13 17:39:35 +01:00
|
|
|
|
2022-01-16 17:59:30 +01:00
|
|
|
# compare unpack_path unpack2_path
|
2022-01-20 20:35:19 +01:00
|
|
|
for folder_path in unpack_paths:
|
|
|
|
compare_GCM(folder_path, unpack2_path / folder_path.name)
|
2022-01-13 17:39:35 +01:00
|
|
|
|
2022-01-16 17:59:30 +01:00
|
|
|
# remove unpack2_path
|
2022-01-13 17:39:35 +01:00
|
|
|
shutil.rmtree(unpack2_path)
|
|
|
|
|
2022-01-18 07:49:52 +01:00
|
|
|
print("###############################################################################")
|
2022-01-29 08:58:54 +01:00
|
|
|
print(f"# TEST 3/{TEST_COUNT}")
|
2022-01-18 17:23:08 +01:00
|
|
|
print("# Comparing [unpack_path]->rebuild_fst->repack->unpack->[unpack2_path]")
|
2022-01-18 07:49:52 +01:00
|
|
|
print("###############################################################################")
|
2022-01-16 17:59:30 +01:00
|
|
|
# rebuild unpack_path FSTs
|
2022-01-20 20:35:19 +01:00
|
|
|
unpack_paths = list(unpack_path.glob("*"))
|
|
|
|
for folder_path in unpack_paths:
|
|
|
|
gcmtool_rebuild_fst(folder_path)
|
2022-01-13 17:39:35 +01:00
|
|
|
|
2022-01-16 17:59:30 +01:00
|
|
|
# repack unpack_path repack_path
|
|
|
|
repack_path.mkdir()
|
2022-01-20 20:35:19 +01:00
|
|
|
for folder_path in unpack_paths:
|
|
|
|
gcmtool_pack(folder_path, repack_path / folder_path.name)
|
2022-01-13 17:39:35 +01:00
|
|
|
|
2022-01-16 17:59:30 +01:00
|
|
|
# unpack repack_path unpack2_path
|
2022-01-13 17:39:35 +01:00
|
|
|
unpack2_path.mkdir()
|
2022-01-16 17:59:30 +01:00
|
|
|
for iso_path in repack_path.glob("*"):
|
2022-01-20 20:35:19 +01:00
|
|
|
gcmtool_unpack(iso_path, unpack2_path / iso_path.name)
|
2022-01-13 17:39:35 +01:00
|
|
|
|
2022-01-16 17:59:30 +01:00
|
|
|
# remove repack_path
|
|
|
|
shutil.rmtree(repack_path)
|
2022-01-13 17:39:35 +01:00
|
|
|
|
2022-01-16 17:59:30 +01:00
|
|
|
# compare unpack_path unpack2_path
|
2022-01-13 17:39:35 +01:00
|
|
|
for folder_path in unpack_path.glob("*"):
|
2022-01-20 20:35:19 +01:00
|
|
|
compare_GCM(folder_path, unpack2_path / folder_path.name)
|
2022-01-13 17:39:35 +01:00
|
|
|
|
2022-01-18 17:23:08 +01:00
|
|
|
# Remove tests folders
|
2022-01-18 07:49:52 +01:00
|
|
|
print("###############################################################################")
|
|
|
|
print(f"# Cleaning test folders.")
|
|
|
|
print("###############################################################################")
|
2022-01-13 17:39:35 +01:00
|
|
|
shutil.rmtree(unpack_path)
|
|
|
|
shutil.rmtree(unpack2_path)
|
|
|
|
|
2022-01-20 20:35:19 +01:00
|
|
|
end = time()
|
2022-01-18 07:49:52 +01:00
|
|
|
print("###############################################################################")
|
2022-01-29 08:58:54 +01:00
|
|
|
print(f"# All tests are OK - elapsed time: {end - start}")
|
2022-01-18 07:49:52 +01:00
|
|
|
print("###############################################################################")
|