fix: typo in ISO extraction and bug preventing cleanup

This commit is contained in:
ulinja
2022-04-22 21:04:53 +02:00
parent 64a3bf422b
commit 352e92cd96

View File

@@ -54,7 +54,7 @@ def extract_iso(path_to_output_dir, path_to_input_file):
subprocess.run( subprocess.run(
[ [
"xorriso", "xorriso",
"-osirrox", "on" "-osirrox", "on",
"-indev", path_to_input_file.resolve(), "-indev", path_to_input_file.resolve(),
"-extract", "/", "-extract", "/",
path_to_output_dir.resolve() path_to_output_dir.resolve()
@@ -114,22 +114,22 @@ def append_file_contents_to_initrd_archive(path_to_initrd_archive,
# make archive and its parent directory temporarily writable # make archive and its parent directory temporarily writable
path_to_initrd_archive.chmod(0o644) path_to_initrd_archive.chmod(0o644)
path_to_initrd_archive.parent.chmod(0o644) path_to_initrd_archive.parent.chmod(0o755)
path_to_initrd_extracted = path_to_initrd_archive.with_suffix("") path_to_initrd_extracted = path_to_initrd_archive.with_suffix("")
# extract archive in-place # extract archive in-place
with gzip.open(path_to_initrd_archive, "rb") as file_gz: with gzip.open(path_to_initrd_archive, "rb") as file_gz:
with open(path_to_initrd_extracted) as file_raw: with open(path_to_initrd_extracted, "wb") as file_raw:
shutil.copyfileobj(file_gz, file_raw) shutil.copyfileobj(file_gz, file_raw)
path_to_initrd_archive.unlink() path_to_initrd_archive.unlink()
try: try:
# append contents of input_file to extracted archive using cpio # append contents of input_file to extracted archive using cpio
subprocess.run( subprocess.run(
["echo", path_to_input_file, ["echo", str(path_to_input_file.resolve()),
"|", "cpio", "-H", "newc", "-o", "-A", "|", "cpio", "-H", "newc", "-o", "-A",
"-F", path_to_initrd_extracted], "-F", str(path_to_initrd_extracted.resolve())],
shell=True, shell=True,
check=True) check=True)
@@ -146,7 +146,7 @@ def append_file_contents_to_initrd_archive(path_to_initrd_archive,
# revert write permissions from repacked archive and its parent dir # revert write permissions from repacked archive and its parent dir
path_to_initrd_archive.chmod(0o444) path_to_initrd_archive.chmod(0o444)
path_to_initrd_archive.parent.chmod(0o444) path_to_initrd_archive.parent.chmod(0o555)
def regenerate_iso_md5sums_file(path_to_extracted_iso_root): def regenerate_iso_md5sums_file(path_to_extracted_iso_root):
@@ -185,26 +185,34 @@ def regenerate_iso_md5sums_file(path_to_extracted_iso_root):
# make md5sum file and its parent dir temporarily writable # make md5sum file and its parent dir temporarily writable
path_to_md5sum_file.chmod(0o644) path_to_md5sum_file.chmod(0o644)
path_to_md5sum_file.parent.chmod(0o644) path_to_md5sum_file.parent.chmod(0o755)
try: # remove original md5sum.txt
# find all files within ISO's root and regenerate md5sum.txt file path_to_md5sum_file.unlink()
subprocess.run(
["find", path_to_extracted_iso_root,
"-follow", "-type", "f", "!", "-name", "md5sum.txt", "-print0"
"|", "xargs", "-0", "md5sum",
">", path_to_md5sum_file],
shell=True,
check=True)
except subprocess.CalledProcessError: # create a new md5sum file:
raise RuntimeError(f"Failed while regenerating " # structure: '<md5_hash> path/to/file/relative/to/iso_root'
f"'md5sum.txt' within " # with one line per file, for each file anywhere under the ISO root folder.
f"'{path_to_extracted_iso_root}'.") # Note the two spaces between hash and filepath!
# find all files
subpaths = _find_all_files_under(path_to_extracted_iso_root)
with open(path_to_md5sum_file, "w") as md5sum_file:
for subpath in subpaths:
md5hash = hashlib.md5()
with open(subpath, "rb") as file:
# calculate md5 hash
md5hash.update(file.read())
md5sum_file.write(
md5hash.hexdigest()
+ " "
+ str(subpath.relative_to(path_to_extracted_iso_root))
+ "\n")
# revert write permissions from md5sum.txt and its parent dir # revert write permissions from md5sum.txt and its parent dir
path_to_md5sum_file.chmod(0o444) path_to_md5sum_file.chmod(0o444)
path_to_md5sum_file.parent.chmod(0o444) path_to_md5sum_file.parent.chmod(0o555)
def extract_mbr_from_iso(path_to_output_file, path_to_source_iso): def extract_mbr_from_iso(path_to_output_file, path_to_source_iso):
@@ -341,16 +349,16 @@ def repack_iso(path_to_output_iso,
subprocess.run( subprocess.run(
["xorriso", "-as", "mkisofs", ["xorriso", "-as", "mkisofs",
"-r", "-V", created_iso_filesystem_name, "-r", "-V", created_iso_filesystem_name,
"-o", path_to_output_iso, "-o", path_to_output_iso.resolve(),
"-J", "-J", "-joliet-long", "-cache-inodes", "-J", "-J", "-joliet-long", "-cache-inodes",
"-isohybrid-mbr", path_to_mbr_data_file, "-isohybrid-mbr", path_to_mbr_data_file.resolve(),
"-b", "isolinux/isolinux.bin", "-b", "isolinux/isolinux.bin",
"-c", "isolinux/boot.cat", "-c", "isolinux/boot.cat",
"-boot-load-size", "4", "-boot-info-table", "-no-emul-boot", "-boot-load-size", "4", "-boot-info-table", "-no-emul-boot",
"-eltorito-alt-boot", "-eltorito-alt-boot",
"-e", "boot/grub/efi.img", "-no-emul-boot", "-e", "boot/grub/efi.img", "-no-emul-boot",
"-isohybrid-gpt-basdat", "-isohybrid-apm-hfsplus", "-isohybrid-gpt-basdat", "-isohybrid-apm-hfsplus",
path_to_input_files_root_dir], path_to_input_files_root_dir.resolve()],
check=True) check=True)
except subprocess.CalledProcessError: except subprocess.CalledProcessError: