add function to extract ISO archives/images
This commit is contained in:
1
udib/__init__.py
Normal file
1
udib/__init__.py
Normal file
@@ -0,0 +1 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
62
udib/udib.py
Executable file
62
udib/udib.py
Executable 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}'."
|
||||||
|
)
|
||||||
Reference in New Issue
Block a user