Skip to content

Commit

Permalink
refactor: remove main from modules
Browse files Browse the repository at this point in the history
  • Loading branch information
artem-burashnikov committed Nov 5, 2023
1 parent 308da02 commit 7e95227
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 29 deletions.
32 changes: 18 additions & 14 deletions depinspect/load/extract.py
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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")
Expand All @@ -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()
2 changes: 1 addition & 1 deletion depinspect/load/fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 1 addition & 8 deletions depinspect/load/sqlite_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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()
16 changes: 10 additions & 6 deletions depinspect/load/ubuntu/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 7e95227

Please sign in to comment.