Skip to content

Commit

Permalink
feat: add logger; add main entry point
Browse files Browse the repository at this point in the history
  • Loading branch information
artem-burashnikov committed Nov 6, 2023
1 parent 2dbcde8 commit 3c02c64
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 26 deletions.
13 changes: 7 additions & 6 deletions depinspect/load/extract.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import lzma
from pathlib import Path
from shutil import rmtree
Expand Down Expand Up @@ -57,16 +58,16 @@ def process_archives(archives_dir: Path) -> 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")
except Exception:
logging.exception(f"Failed to extract {archive_path}")
logging.info("Removing downloaded files and temprorary ditectory")
try:
rmtree(archives_dir)
print(
logging.info(
f"Temporary directory {archives_dir} and containing files were removed successfully"
)
except Exception as e:
print(
except Exception:
logging.exception(
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."
)

Expand Down
31 changes: 19 additions & 12 deletions depinspect/load/files.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
from pathlib import Path
from typing import List

Expand All @@ -19,13 +20,13 @@ def remove_file(file_path: Path) -> None:
"""
try:
Path.unlink(file_path)
print(f"File '{file_path}' deleted successfully.")
except NotADirectoryError as e:
print(f"Directory '{file_path}' not found\n{e}")
except PermissionError as e:
print(f"Permission error: Unable to delete directory '{file_path}'\n{e}")
except OSError as e:
print(f"Error removing directory '{file_path}'\n{e}")
logging.info(f"File '{file_path}' deleted successfully.")
except NotADirectoryError:
logging.exception(f"Directory '{file_path}' not found.")
except PermissionError:
logging.exception(f"Permission error: Unable to delete directory '{file_path}")
except OSError:
logging.exception(f"Error removing directory '{file_path}")


def remove_dir(directory_path: Path) -> None:
Expand All @@ -45,13 +46,15 @@ def remove_dir(directory_path: Path) -> None:
"""
try:
Path.rmdir(directory_path)
print(f"Directory '{directory_path}' deleted successfully.")
logging.info(f"Directory '{directory_path}' deleted successfully.")
except NotADirectoryError:
print(f"File '{directory_path}' not found.")
logging.exception(f"File '{directory_path}' not found.")
except PermissionError:
print(f"Permission error: Unable to delete file '{directory_path}'.")
except OSError as e:
print(f"Error removing directory '{directory_path}': {e}")
logging.exception(
f"Permission error: Unable to delete file '{directory_path}'."
)
except OSError:
logging.exception(f"Error removing directory '{directory_path}'.")


def list_files_in_directory(directory_path: Path) -> List[Path]:
Expand All @@ -71,6 +74,7 @@ def list_files_in_directory(directory_path: Path) -> List[Path]:
files = [path for path in Path.iterdir(directory_path) if path.is_file()]
return files
else:
logging.error("list_files_in_directory: The specified path is not a directory")
raise NotADirectoryError


Expand All @@ -91,4 +95,7 @@ def list_subdirs_in_directory(directory_path: Path) -> List[Path]:
sub_dirs = [path for path in Path.iterdir(directory_path) if path.is_dir()]
return sub_dirs
else:
logging.error(
"list_subdirs_in_directory: The specified path is not a directory"
)
raise NotADirectoryError
15 changes: 7 additions & 8 deletions depinspect/load/ubuntu/metadata.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
from pathlib import Path
from shutil import rmtree
from sqlite3 import connect
Expand All @@ -8,8 +9,6 @@
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"]


def parse_string_to_list(
string: str, prefix_to_exclude: str, delimiter: str, result: List[str]
Expand Down Expand Up @@ -44,7 +43,7 @@ def process_metadata_into_db(file_path: Path, db_path: Path) -> None:

with db_connection:
with open(file_path, "r") as file:
print(f"Processing packages metadata from {file_path}.\nPlease wait")
logging.info(f"Processing packages metadata from {file_path}.\nPlease wait")
package_name: str = ""
version: str = ""
architecture: List[str] = []
Expand Down Expand Up @@ -101,7 +100,7 @@ def process_metadata_into_db(file_path: Path, db_path: Path) -> None:
architecture.clear()
depends.clear()

print(f"File {file_path} has been processed succesfully.")
logging.info(f"File {file_path} has been processed succesfully.")

db_connection.close()

Expand All @@ -118,13 +117,13 @@ def main() -> None:
try:
for file_path in list_files_in_directory(tmp_dir):
process_metadata_into_db(file_path, db)
except Exception as e:
print(f"There was an exception trying to process ubuntu metadata: {e}")
except Exception:
logging.exception("There was an exception trying to process ubuntu metadata.")
if db.is_file():
print("Removing database")
logging.info("Removing database")
remove_file(db)
finally:
print("Cleaning up temporary files and directory")
logging.info("Cleaning up temporary files and directory")
rmtree(tmp_dir)


Expand Down
37 changes: 37 additions & 0 deletions depinspect/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import logging
from typing import Tuple

import click

# Set up logging configuration
logging.basicConfig(
level=logging.INFO,
format="- %(levelname)s - %(asctime)s: %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)


@click.command()
@click.option(
"--package1",
nargs=2,
type=(str, str),
required=True,
help="Provide the first package name alog with an architecture separated by whitespace. Example: --package1 package1_name arch_1",
)
@click.option(
"--package2",
nargs=2,
type=(str, str),
required=True,
help="Provide the second package name alog with an architecture separated by whitespace. Example: --package2 package2_name arch_2",
)
def main(package1: Tuple[str, str], package2: Tuple[str, str]) -> None:
package1_name, architecture1 = package1[0].lower(), package1[1].lower()
package2_name, architecture2 = package2[0].lower(), package2[1].lower()
logging.info(f"package1: {package1_name}, arch1: {architecture1}")
logging.info(f"package2: {package2_name}, arch2: {architecture2}")


if __name__ == "__main__":
main()

0 comments on commit 3c02c64

Please sign in to comment.