Skip to content

Commit

Permalink
sort imports
Browse files Browse the repository at this point in the history
Signed-off-by: Aryan Rajoria <[email protected]>
  • Loading branch information
aryan-rajoria committed Oct 29, 2024
1 parent cd784d1 commit bdf2c5b
Show file tree
Hide file tree
Showing 18 changed files with 187 additions and 134 deletions.
37 changes: 14 additions & 23 deletions symbols_db/cli.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,27 @@
import argparse
import concurrent
import concurrent.futures
from time import sleep
import sqlite3
import traceback
from time import sleep

from symbols_db import BLINTDB_LOCATION, VCPKG_LOCATION, logger
from symbols_db.handlers.blint_handler import (
blint_on_crates_from_purl,
get_blint_internal_functions_exe,
)
blint_on_crates_from_purl, get_blint_internal_functions_exe)
from symbols_db.handlers.cyclonedx_handler import get_purl_from_bom
from symbols_db.handlers.language_handlers.wrapdb_handler import get_wrapdb_projects
from symbols_db.handlers.language_handlers.cargo_handler import build_crates_from_purl
from symbols_db.handlers.language_handlers.vcpkg_handler import (
get_vcpkg_projects,
vcpkg_build,
find_vcpkg_executables,
)
from symbols_db.handlers.language_handlers.cargo_handler import \
build_crates_from_purl
from symbols_db.handlers.language_handlers.meson_handler import (
meson_build,
find_meson_executables,
strip_executables,
)
from symbols_db.handlers.sqlite_handler import (
add_projects,
add_binary,
add_binary_export,
)

find_meson_executables, meson_build, strip_executables)
from symbols_db.handlers.language_handlers.vcpkg_handler import (
find_vcpkg_executables, get_vcpkg_projects, vcpkg_build)
from symbols_db.handlers.language_handlers.wrapdb_handler import \
get_wrapdb_projects
# TODO: clean up reset database command
from symbols_db.handlers.sqlite_handler import clear_sqlite_database, create_database
from symbols_db import BLINTDB_LOCATION, logger, VCPKG_LOCATION
from symbols_db.handlers.sqlite_handler import (add_binary, add_binary_export,
add_projects,
clear_sqlite_database,
create_database)

clear_sqlite_database()
create_database()
Expand Down
15 changes: 6 additions & 9 deletions symbols_db/handlers/blint_handler.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import json
import os
from pathlib import Path
import subprocess
from pathlib import Path

from symbols_db import BOM_LOCATION, CWD
from symbols_db import (BOM_LOCATION, CWD, DEBUG_MODE, DELIMETER_BOM,
WRAPDB_LOCATION, logger)
from symbols_db.handlers.sqlite_handler import store_sbom_in_sqlite
from symbols_db.utils.json import get_properties_internal
from symbols_db.utils.rust import (
from_purl_to_rust_srcname,
get_all_index_names,
get_path_names_from_index_names,
)
from symbols_db import DEBUG_MODE, DELIMETER_BOM, WRAPDB_LOCATION
from symbols_db import logger
from symbols_db.utils.rust import (from_purl_to_rust_srcname,
get_all_index_names,
get_path_names_from_index_names)


def run_blint_on_file(file_path):
Expand Down
1 change: 1 addition & 0 deletions symbols_db/handlers/cyclonedx_handler.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json

from symbols_db import logger


Expand Down
2 changes: 1 addition & 1 deletion symbols_db/handlers/git_handler.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import subprocess
from symbols_db import DEBUG_MODE

from symbols_db import DEBUG_MODE

# def git_clone_wrapdb():
# command = (
Expand Down
12 changes: 11 additions & 1 deletion symbols_db/handlers/language_handlers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
import subprocess

from symbols_db import WRAPDB_LOCATION


class BaseHandler:
pass
strip = True

def strip_executables(self, file_path, loc=WRAPDB_LOCATION):
if self.strip:
strip_command = f"strip --strip-all {file_path}".split(" ")
subprocess.run(strip_command, cwd=loc)
10 changes: 4 additions & 6 deletions symbols_db/handlers/language_handlers/cargo_handler.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
from symbols_db.utils.rust import (
from_purl_to_rust_srcname,
get_all_index_names,
get_path_names_from_index_names,
)
import os

from symbols_db import logger
import os
from symbols_db.utils.rust import (from_purl_to_rust_srcname,
get_all_index_names,
get_path_names_from_index_names)


def build_cargo_package(build_dir):
Expand Down
47 changes: 39 additions & 8 deletions symbols_db/handlers/language_handlers/meson_handler.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,64 @@
import os
import subprocess

from symbols_db import logger, WRAPDB_LOCATION, CWD
from pathlib import Path
from symbols_db import logger

from symbols_db import CWD, WRAPDB_LOCATION, logger
from symbols_db.handlers.language_handlers import BaseHandler
from symbols_db.projects_compiler.meson import (ensure_meson_installed,
git_checkout_wrapdb_commit,
git_clone_wrapdb)
from symbols_db.utils.utils import subprocess_run_debug


# TODO: debug
DEBUG_MODE = True


class MesonHandler(BaseHandler):

def __init__(self):
pass
if meson_present := ensure_meson_installed():
git_clone_wrapdb()
git_checkout_wrapdb_commit()
else:
ModuleNotFoundError("Meson was not found")

def build(self, project_name):
pass
setup_command = (
f"meson setup build/{project_name} -Dwraps={project_name}".split(" ")
)
meson_setup = subprocess.run(setup_command, cwd=WRAPDB_LOCATION)
subprocess_run_debug(meson_setup, project_name)
compile_command = f"meson compile -C build/{project_name}".split(" ")
meson_compile = subprocess.run(compile_command, cwd=WRAPDB_LOCATION)
subprocess_run_debug(meson_compile, project_name)

def find_executables(self, project_name):
pass
full_project_dir = WRAPDB_LOCATION / "build" / project_name / "subprojects"
executable_list = []
for root, dir, files in os.walk(full_project_dir):
for file in files:
# what is the value of variable `root`
file_path = Path(root) / file
if os.access(file_path, os.X_OK):
full_path = CWD / file_path
file_output = subprocess.run(
["file", full_path], capture_output=True
)
if b"ELF" in file_output.stdout:
executable_list.append(full_path)
return executable_list

def delete_project_files(self, project_name):
pass

def get_project_list(self):
pass
subproject_filenames = os.listdir(WRAPDB_LOCATION / "subprojects")
projects_list = []
for file in subproject_filenames:
project_path = Path(file)
if project_path.suffix == ".wrap":
projects_list.append(project_path.stem)
return projects_list


def meson_build(project_name):
Expand Down
76 changes: 17 additions & 59 deletions symbols_db/handlers/language_handlers/vcpkg_handler.py
Original file line number Diff line number Diff line change
@@ -1,53 +1,39 @@
import os
import subprocess

from symbols_db import DEBUG_MODE, VCPKG_HASH, VCPKG_LOCATION, VCPKG_URL, logger
from symbols_db.handlers.git_handler import git_checkout_commit, git_clone
from symbols_db import VCPKG_LOCATION
from symbols_db.handlers.language_handlers import BaseHandler
from symbols_db.projects_compiler.vcpkg import (exec_explorer,
git_checkout_vcpkg_commit,
git_clone_vcpkg,
run_vcpkg_install_command)
from symbols_db.utils.utils import subprocess_run_debug


class VcpkgHandler(BaseHandler):
strip = False

def __init__(self):
pass
git_clone_vcpkg()
git_checkout_vcpkg_commit()
run_vcpkg_install_command()

def build(self, project_name):
pass
inst_cmd = f"./vcpkg install {project_name}".split(" ")
inst_run = subprocess.run(inst_cmd, cwd=VCPKG_LOCATION, capture_output=True)
subprocess_run_debug(inst_run, project_name)

def find_executables(self, project_name):
pass
project_path = f"{project_name}_x64-linux"
target_directory = VCPKG_LOCATION / "packages" / project_path
return exec_explorer(target_directory)

def delete_project_files(self, project_name):
pass

def get_project_list(self):
pass


def git_clone_vcpkg():
git_clone(VCPKG_URL, VCPKG_LOCATION)


def git_checkout_vcpkg_commit():
git_checkout_commit(VCPKG_LOCATION, VCPKG_HASH)


def run_vcpkg_install_command():
# Linux command
install_command = ["./bootstrap-vcpkg.sh"]
install_run = subprocess.run(
install_command, cwd=VCPKG_LOCATION, capture_output=True
)
if DEBUG_MODE:
print(install_run.stdout)
logger.debug(f'"bootstrap-vcpkg.sh: ":{install_run.stdout.decode('ascii')}')

int_command = "./vcpkg integrate install".split(" ")
int_run = subprocess.run(int_command, cwd=VCPKG_LOCATION, capture_output=True)
if DEBUG_MODE:
print(int_run.stdout)
logger.debug(f'"vcpkg integrate install: ":{int_run.stdout.decode('ascii')}')
ports_path = VCPKG_LOCATION / "ports"
return os.listdir(ports_path)


def get_vcpkg_projects():
Expand All @@ -65,34 +51,6 @@ def vcpkg_build(project_name):
subprocess_run_debug(inst_run, project_name)


def exec_explorer(directory):
"""
Walks through a directory and identifies executable files using the `file` command.
Args:
directory: The directory to search.
Returns:
A list of executable file paths.
"""
executables = []
for root, _, files in os.walk(directory):
for file in files:
file_path = os.path.join(root, file)
try:
result = subprocess.run(["file", file_path], capture_output=True)
if b"ELF" in result.stdout:
executables.append(file_path)
if b"archive" in result.stdout:
executables.append(file_path)
except FileNotFoundError:
print(
"Error: 'file' command not found. Make sure it's installed and in your PATH."
)
return []
return executables


def archive_explorer(directory):
"""
Walks through a directory and identifies executable files using the `file` command.
Expand Down
12 changes: 3 additions & 9 deletions symbols_db/handlers/language_handlers/wrapdb_handler.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
import os
from pathlib import Path
from symbols_db import WRAPDB_HASH, WRAPDB_LOCATION, WRAPDB_URL
from symbols_db.handlers.git_handler import git_checkout_commit, git_clone


def git_clone_wrapdb():
git_clone(WRAPDB_URL, WRAPDB_LOCATION)


def git_checkout_wrapdb_commit():
git_checkout_commit(WRAPDB_LOCATION, WRAPDB_HASH)
from symbols_db import WRAPDB_LOCATION
from symbols_db.projects_compiler.meson import (git_checkout_wrapdb_commit,
git_clone_wrapdb)


def get_wrapdb_projects():
Expand Down
7 changes: 3 additions & 4 deletions symbols_db/handlers/sqlite_handler.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import datetime
import os
import sqlite3
from symbols_db import DEBUG_MODE, BLINTDB_LOCATION
from pathlib import PurePath
from symbols_db import logger
from contextlib import closing
from symbols_db import SQLITE_TIMEOUT
from pathlib import PurePath

from symbols_db import BLINTDB_LOCATION, DEBUG_MODE, SQLITE_TIMEOUT, logger


def get_cursor():
Expand Down
16 changes: 16 additions & 0 deletions symbols_db/projects_compiler/meson.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import shutil

from symbols_db import WRAPDB_HASH, WRAPDB_LOCATION, WRAPDB_URL
from symbols_db.handlers.git_handler import git_checkout_commit, git_clone


def git_clone_wrapdb():
git_clone(WRAPDB_URL, WRAPDB_LOCATION)


def git_checkout_wrapdb_commit():
git_checkout_commit(WRAPDB_LOCATION, WRAPDB_HASH)


def ensure_meson_installed():
return shutil.which("meson") is not None
Loading

0 comments on commit bdf2c5b

Please sign in to comment.