From 7e9522739d752080bf5ded66fb42285b247fd545 Mon Sep 17 00:00:00 2001 From: Artem Burashnikov Date: Sun, 5 Nov 2023 21:44:47 +0300 Subject: [PATCH] refactor: remove main from modules --- depinspect/load/extract.py | 32 +++++++++++++++++------------- depinspect/load/fetch.py | 2 +- depinspect/load/sqlite_db.py | 9 +-------- depinspect/load/ubuntu/metadata.py | 16 +++++++++------ 4 files changed, 30 insertions(+), 29 deletions(-) diff --git a/depinspect/load/extract.py b/depinspect/load/extract.py index 9258410..05327b9 100644 --- a/depinspect/load/extract.py +++ b/depinspect/load/extract.py @@ -1,9 +1,8 @@ import lzma -import sys from pathlib import Path from shutil import rmtree -from depinspect.load import fetch, files +from depinspect.load.files import list_files_in_directory, remove_file def extract_xz_archive(archive_path: Path, output_path: Path) -> None: @@ -28,25 +27,36 @@ def extract_xz_archive(archive_path: Path, output_path: Path) -> None: output_file.write(extracted_data) -def main() -> Path: +def process_archives(archives_dir: Path) -> Path: """ Process archives by extracting contents and removing original archive files. + This function takes a directory containing archives, extracts the contents of each + archive, removes the original archive files, and returns the path to the directory + containing the processed archives. + + Parameters: + - archives_dir (Path): The path to the directory containing archives. + + Raises: + - Exception: If there is an issue with extracting or cleaning up the archives. + Returns: Path: The path to the directory containing processed archives. """ - archives_dir = fetch.main() - archives = files.list_files_in_directory(archives_dir) + archives_files = list_files_in_directory(archives_dir) try: - for archive_path in archives: + for archive_path in archives_files: # Construct output file path # archive_path.parts[-1] returns for example amd64_pacakges.xz file_name = archive_path.parts[-1].split(".")[0] file_extension = ".txt" output_path = archives_dir / f"{file_name}{file_extension}" - extract_xz_archive(archive_path, output_path) - files.remove_file(archive_path) + + if archive_path.is_file(): + remove_file(archive_path) + except Exception as e: print(f"Failed to extract {archive_path}\n{e}") print("Removing downloaded files and temprorary ditectory") @@ -60,10 +70,4 @@ def main() -> Path: f"There was an error trying to clean up temproraty directory {archives_dir}\nSome files may be left and will have to be removed manually." ) - sys.exit(1) - return archives_dir - - -if __name__ == "__main__": - main() diff --git a/depinspect/load/fetch.py b/depinspect/load/fetch.py index b709a46..48f159a 100644 --- a/depinspect/load/fetch.py +++ b/depinspect/load/fetch.py @@ -47,7 +47,7 @@ def pull_target_from_URL(target_url: str, local_target_path: Path) -> None: request.urlretrieve(target_url, local_target_path) -def fetch_and_save_metadata() -> Path: +def fetch_and_save_metadata_to_tmp() -> Path: """ Download metadata from configured sources and save them to temporary files. diff --git a/depinspect/load/sqlite_db.py b/depinspect/load/sqlite_db.py index 4978e8f..4f977c7 100644 --- a/depinspect/load/sqlite_db.py +++ b/depinspect/load/sqlite_db.py @@ -5,7 +5,7 @@ from depinspect.load import files -def new() -> None: +def new_db() -> None: """ Creates a new SQLite database named 'dependencies.db' and initializes pre-defined tables. @@ -32,16 +32,9 @@ def new() -> None: connection.close() -def main() -> None: - pass - - """ SELECT Dependencies.package_id, Dependencies.dependency_name FROM Dependencies JOIN Packages ON Dependencies.package_id = Packages.id WHERE Packages.distribution = ? AND Packages.package_name = ? AND Packages.architecture = ?; """ - -if __name__ == "__main__": - main() diff --git a/depinspect/load/ubuntu/metadata.py b/depinspect/load/ubuntu/metadata.py index e4b31a3..d7b6f16 100644 --- a/depinspect/load/ubuntu/metadata.py +++ b/depinspect/load/ubuntu/metadata.py @@ -3,7 +3,10 @@ from sqlite3 import Connection, connect from typing import List -from depinspect.load import extract, files, sqlite_db +from depinspect.load.extract import process_archives +from depinspect.load.fetch import fetch_and_save_metadata_to_tmp +from depinspect.load.files import list_files_in_directory, remove_file +from depinspect.load.sqlite_db import new_db ARCH_ALL_ANY = ["i386", "amd64", "riscv64"] @@ -102,24 +105,25 @@ def process_metadata(file_path: Path, db_connection: Connection) -> None: def main() -> None: - tmp_dir = extract.main() + tmp_dir = fetch_and_save_metadata_to_tmp() + process_archives(tmp_dir) if Path("dependencies.db").is_file(): - files.remove_file(Path("dependencies.db")) + remove_file(Path("dependencies.db")) - sqlite_db.new() + new_db() db = connect("dependencies.db") try: - for file_path in files.list_files_in_directory(tmp_dir): + for file_path in list_files_in_directory(tmp_dir): process_metadata(file_path, db) except Exception as e: db.close() print(f"{e}") if Path("dependencies.db").is_file(): print("Removing database") - files.remove_file(Path("dependencies.db")) + remove_file(Path("dependencies.db")) finally: print("Closing database connection") db.close()