Skip to content

Commit

Permalink
Feat: Remove temp folders (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
aryan-rajoria authored Nov 9, 2024
1 parent d660cd8 commit f09b39c
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 30 deletions.
22 changes: 12 additions & 10 deletions .oras/orasclient.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import oras.client
import os
import argparse
import os

import oras.client

client = oras.client.OrasClient()

Expand All @@ -10,7 +11,8 @@
client.login(password=token, username=username)

parser = argparse.ArgumentParser(
prog="orasclient_blintdb", description="Helps pushing blint.db into a container and uploading to ghcr.io"
prog="orasclient_blintdb",
description="Helps pushing blint.db into a container and uploading to ghcr.io",
)
parser.add_argument(
"-p",
Expand All @@ -23,16 +25,16 @@
if pkg := args.get("pkg", None):
# not using fstring here to make sure value is correct
# otherwise wrong file may be uploaded
if pkg=="vcpkg":
if pkg == "vcpkg":
client.push(
target="ghcr.io/appthreat/blintdb-vcpkg:v0.1",
config_path="./.oras/config.json",
annotation_file="./.oras/annotations.json",
files=[
"./blint.db:application/vnd.appthreat.blintdb.layer.v1+tar",
],
)
if pkg=="meson":
)
if pkg == "meson":
client.push(
target="ghcr.io/appthreat/blintdb-meson:v0.1",
config_path="./.oras/config.json",
Expand All @@ -41,21 +43,21 @@
"./blint.db:application/vnd.appthreat.blintdb.layer.v1+tar",
],
)
if pkg=="vcpkg-tst":
if pkg == "vcpkg-tst":
client.push(
target="ghcr.io/appthreat/blintdb-vcpkg-tst:v0.1",
config_path="./.oras/config.json",
annotation_file="./.oras/annotations.json",
files=[
"./blint.db:application/vnd.appthreat.blintdb.layer.v1+tar",
],
)
if pkg=="meson-tst":
)
if pkg == "meson-tst":
client.push(
target="ghcr.io/appthreat/blintdb-meson-tst:v0.1",
config_path="./.oras/config.json",
annotation_file="./.oras/annotations.json",
files=[
"./blint.db:application/vnd.appthreat.blintdb.layer.v1+tar",
],
)
)
36 changes: 27 additions & 9 deletions blint_db/cli.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import os
import argparse
import os
import shutil
import sqlite3
from concurrent import futures
from pathlib import Path
from typing import List

from blint_db import BLINTDB_LOCATION, COMMON_CONNECTION
from blint_db.handlers.language_handlers.vcpkg_handler import \
get_vcpkg_projects, remove_vcpkg_project
from blint_db.handlers.language_handlers.vcpkg_handler import (
get_vcpkg_projects, remove_vcpkg_project)
from blint_db.handlers.language_handlers.wrapdb_handler import \
get_wrapdb_projects
from blint_db.handlers.sqlite_handler import (clear_sqlite_database,
create_database)
from blint_db.projects_compiler.meson import mt_meson_blint_db_build
from blint_db.projects_compiler.vcpkg import mt_vcpkg_blint_db_build

from typing import List


def arguments_parser():
parser = argparse.ArgumentParser(
Expand Down Expand Up @@ -78,20 +79,20 @@ def arguments_parser():
"--select-project",
nargs="+",
dest="sel_project",
help="List of project you would like to compile helpful for debugging"
help="List of project you would like to compile helpful for debugging",
)

return parser.parse_args()


def reset_and_backup():
if COMMON_CONNECTION:
if os.path.exists(BLINTDB_LOCATION) and os.path.isfile(BLINTDB_LOCATION):
os.remove(BLINTDB_LOCATION)
COMMON_CONNECTION.execute(f"vacuum main into '{BLINTDB_LOCATION}'")



def meson_add_blint_bom_process(test_mode=False, sel_project: List=None):
def meson_add_blint_bom_process(test_mode=False, sel_project: List = None):
projects_list = get_wrapdb_projects()
if test_mode:
projects_list = projects_list[:10]
Expand All @@ -108,7 +109,23 @@ def meson_add_blint_bom_process(test_mode=False, sel_project: List=None):
print(f"Ran complete for {project_name} and we found {len(executables)}")


def vcpkg_add_blint_bom_process(test_mode=False, sel_project: List=None):
def remove_temp_ar():
"""
Removes `ar-temp-########` files created by blint extract-ar function,
after we have completed our tasks.
"""

try:
for dirname in Path("/tmp").glob("ar-temp-*"):
try:
shutil.rmtree(dirname)
except OSError as e:
print(f"Error deleting file {dirname}: {e}")
except Exception as e:
print(f"Error during cleanup: {e}")


def vcpkg_add_blint_bom_process(test_mode=False, sel_project: List = None):
projects_list = get_vcpkg_projects()
if test_mode:
projects_list = projects_list[:10]
Expand All @@ -122,6 +139,7 @@ def vcpkg_add_blint_bom_process(test_mode=False, sel_project: List=None):
count += 1
if count == 100:
reset_and_backup()
remove_temp_ar()
count = 0


Expand Down
2 changes: 1 addition & 1 deletion blint_db/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@
COMMON_CONNECTION = None
# COMMON_CONNECTION = sqlite3.connect(":memory:")

ARCH_OS="arm64-linux"
ARCH_OS = "arm64-linux"
10 changes: 5 additions & 5 deletions blint_db/handlers/language_handlers/vcpkg_handler.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import os
import subprocess

from blint_db import (DEBUG_MODE, VCPKG_HASH, VCPKG_LOCATION, VCPKG_URL, ARCH_OS,
logger)
from blint_db import (ARCH_OS, DEBUG_MODE, VCPKG_HASH, VCPKG_LOCATION,
VCPKG_URL, logger)
from blint_db.handlers.git_handler import git_checkout_commit, git_clone
from blint_db.handlers.language_handlers import BaseHandler
from blint_db.utils.utils import subprocess_run_debug
Expand Down Expand Up @@ -60,13 +60,15 @@ def run_vcpkg_install_command():
print(int_run.stdout)
logger.debug(f"'vcpkg integrate install: {int_run.stdout.decode('ascii')}")


def remove_vcpkg_project(project_name):
rem_cmd = ["./vcpkg", "remove", "--recurse", project_name]
rem_run = subprocess.run(
rem_cmd, cwd=VCPKG_LOCATION, capture_output=True, check=False
)
subprocess_run_debug(rem_run, project_name)


def get_vcpkg_projects():
git_clone_vcpkg()
git_checkout_vcpkg_commit()
Expand Down Expand Up @@ -145,7 +147,5 @@ def exec_explorer(directory):
print(
"Error: 'file' command not found. Make sure it's installed and in your PATH."
)
return (
[]
)
return []
return executables
1 change: 0 additions & 1 deletion blint_db/handlers/sqlite_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
SQLITE_TIMEOUT)



def use_existing_connection(connection=None): # Decorator now accepts connection
"""
Decorator to use an existing connection when BLINTDB_LOCATION is ':memory:'.
Expand Down
3 changes: 1 addition & 2 deletions blint_db/projects_compiler/vcpkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
import traceback
from sqlite3 import OperationalError

from blint_db import (DEBUG_MODE, VCPKG_HASH, VCPKG_LOCATION, VCPKG_URL,
logger)
from blint_db import DEBUG_MODE, VCPKG_HASH, VCPKG_LOCATION, VCPKG_URL, logger
from blint_db.handlers.blint_handler import get_blint_internal_functions_exe
from blint_db.handlers.git_handler import git_checkout_commit, git_clone
from blint_db.handlers.language_handlers.vcpkg_handler import (
Expand Down
4 changes: 2 additions & 2 deletions blint_db/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import os
from pathlib import Path

from blint_db import VCPKG_LOCATION, DEBUG_MODE, WRAPDB_LOCATION
from blint_db import DEBUG_MODE, VCPKG_LOCATION, WRAPDB_LOCATION

HOME_DIRECTORY = Path.home()


def _create_python_dirs():
wl = WRAPDB_LOCATION
vl = VCPKG_LOCATION

os.makedirs(wl, exist_ok=True)
os.makedirs(vl, exist_ok=True)

Expand Down

0 comments on commit f09b39c

Please sign in to comment.