add function to extract ISO archives/images

This commit is contained in:
ulinja
2022-04-20 22:44:42 +02:00
parent 1635d4c127
commit ab9008db73
2 changed files with 63 additions and 0 deletions

1
udib/__init__.py Normal file
View File

@@ -0,0 +1 @@
#!/usr/bin/env python3

62
udib/udib.py Executable file
View File

@@ -0,0 +1,62 @@
#!/usr/bin/env python3
from pathlib import Path
import subprocess
def extract_iso(path_to_output_dir, path_to_input_file):
"""Extracts the input ISO-file into the specified directory using 'bsdtar'.
Parameters
----------
path_to_output_dir : str or pathlike object
Path to the directory into which the contents of the archive will be
extracted.
path_to_input_file : str or pathlike object
Path to the file/archive which should be extracted.
Raises
------
RuntimeError
Raised if 'bsdtar' is not installed or encountered an error during the
extraction.
FileNotFoundError
Raised if the input file does not exist or is not a file.
NotADirectoryError
Raised if the output directory does not exist or is not a directory.
Example
-------
extract_iso("/tmp/isocontents", "/home/myuser/downloads/debian-11.iso")
"""
path_to_output_dir = Path(path_to_output_dir)
path_to_input_file = Path(path_to_input_file)
# check if paths are valid
if not path_to_output_dir.is_dir():
raise NotADirectoryError(f"No such directory: '{path_to_output_dir}'.")
if not path_to_input_file.is_file():
raise FileNotFoundError(f"No such file: '{path_to_input_file}'.")
# check if 'bsdtar' is installed
try:
subprocess.run(["command", "-v", "bsdtar"], check=True)
except subprocess.CalledProcessError:
raise RuntimeError(f"Program not found in $PATH: 'bsdtar'.")
# extract file to destination
try:
subprocess.run(
[
"bsdtar",
"-C",
str(path_to_output_dir),
"-xf",
str(path_to_input_file)
],
check=True
)
except subprocess.CalledProcessError:
raise RuntimeError(
f"An error occurred while extracting '{path_to_input_file}'."
)