From 8d35c8f3f9d0b335ded7f9a7f86d2784f53899b4 Mon Sep 17 00:00:00 2001 From: Lucas PASCAL Date: Mon, 20 Nov 2023 17:00:23 +0100 Subject: [PATCH] [clean] Shared 'utils.py' --- scripts/build_and_test/__init__.py | 2 +- scripts/build_and_test/build_app.py | 2 +- scripts/build_and_test/scan_app.py | 2 +- scripts/build_and_test/test_app.py | 2 +- scripts/create_app_list/__init__.py | 2 +- scripts/create_app_list/makefile_dump.py | 4 +- scripts/create_app_list/utils.py | 53 ------------------------ scripts/entrypoint.py | 4 +- scripts/{build_and_test => }/utils.py | 20 ++++----- 9 files changed, 18 insertions(+), 73 deletions(-) delete mode 100644 scripts/create_app_list/utils.py rename scripts/{build_and_test => }/utils.py (85%) diff --git a/scripts/build_and_test/__init__.py b/scripts/build_and_test/__init__.py index eb36c52..dfc19c9 100644 --- a/scripts/build_and_test/__init__.py +++ b/scripts/build_and_test/__init__.py @@ -7,7 +7,7 @@ from build_and_test.test_app import test_all_devices from build_and_test.scan_app import scan_all_devices from build_and_test.device import Devices -from build_and_test.utils import git_setup, merge_json +from utils import git_setup, merge_json SDK_NAME = "sdk" SDK_URL = "https://github.com/LedgerHQ/ledger-secure-sdk.git" diff --git a/scripts/build_and_test/build_app.py b/scripts/build_and_test/build_app.py index 6dc9f6e..1f4b9ac 100644 --- a/scripts/build_and_test/build_app.py +++ b/scripts/build_and_test/build_app.py @@ -2,7 +2,7 @@ from pathlib import Path from build_and_test.device import Devices, Device -from build_and_test.utils import run_cmd +from utils import run_cmd def build_variant(target: str, diff --git a/scripts/build_and_test/scan_app.py b/scripts/build_and_test/scan_app.py index 23615a9..f129284 100644 --- a/scripts/build_and_test/scan_app.py +++ b/scripts/build_and_test/scan_app.py @@ -1,7 +1,7 @@ from pathlib import Path from build_and_test.device import Devices, Device -from build_and_test.utils import run_cmd +from utils import run_cmd def scan_variant(target: str, diff --git a/scripts/build_and_test/test_app.py b/scripts/build_and_test/test_app.py index 34a9d78..001914d 100644 --- a/scripts/build_and_test/test_app.py +++ b/scripts/build_and_test/test_app.py @@ -3,7 +3,7 @@ from build_and_test.device import Devices, Device from build_and_test.build_app import build_variant -from build_and_test.utils import run_cmd +from utils import run_cmd def test(model: str, app_test_path: Path, app_build_path: Path, test_params: str) -> Tuple[str, str]: diff --git a/scripts/create_app_list/__init__.py b/scripts/create_app_list/__init__.py index 51e610f..484c312 100644 --- a/scripts/create_app_list/__init__.py +++ b/scripts/create_app_list/__init__.py @@ -4,7 +4,7 @@ from create_app_list.parse_github import parse_github from create_app_list.gen_variant import gen_variant -from create_app_list.utils import git_setup, merge_json +from utils import git_setup, merge_json def main(args: Namespace) -> None: diff --git a/scripts/create_app_list/makefile_dump.py b/scripts/create_app_list/makefile_dump.py index b6fa6e2..a1e0dbd 100755 --- a/scripts/create_app_list/makefile_dump.py +++ b/scripts/create_app_list/makefile_dump.py @@ -7,14 +7,14 @@ from pathlib import Path from typing import Tuple, List -from create_app_list.utils import run_cmd +from utils import run_cmd def get_app_listvariants(app_build_path: Path, sdk: str = "$NANOS_SDK", allow_failure: bool = False) -> Tuple[str, List[str]]: # Using listvariants Makefile target - listvariants = run_cmd(f"make BOLOS_SDK={sdk} listvariants", cwd=app_build_path, no_throw=allow_failure) + _, listvariants = run_cmd(f"make BOLOS_SDK={sdk} listvariants", cwd=app_build_path, no_throw=allow_failure) if "VARIANTS" not in listvariants: raise ValueError(f"Invalid variants retrieved: {listvariants}") diff --git a/scripts/create_app_list/utils.py b/scripts/create_app_list/utils.py deleted file mode 100644 index 548cc81..0000000 --- a/scripts/create_app_list/utils.py +++ /dev/null @@ -1,53 +0,0 @@ -import subprocess -from pathlib import Path - - -def run_cmd(cmd: str, - cwd: Path, - print_output: bool = True, - no_throw: bool = False) -> str: - print(f"[run_cmd] Running: {cmd} from {cwd}") - - ret = subprocess.run(cmd, - shell=True, - stdout=subprocess.PIPE, - stderr=subprocess.STDOUT, - universal_newlines=True, - cwd=cwd) - if no_throw is False and ret.returncode: - print(f"[run_cmd] Error {ret.returncode} raised while running cmd: {cmd}") - print("[run_cmd] Output was:") - print(ret.stdout) - raise ValueError() - - if ret.returncode: - print(f"[run_cmd] Output:\n{ret.stdout}") - - return ret.stdout.strip() - - -def git_setup(repo_name: str, repo_ref: str, repo_url: str, workdir: Path): - # Force clone in https over SSH - GIT_CONFIG = ' -c url."https://github.com/".insteadOf="git@github.com:" -c url."https://".insteadOf="git://"' - - if not Path.exists(workdir/Path(repo_name)): - run_cmd(f"git {GIT_CONFIG} clone {repo_url} --recurse-submodules {repo_name}", cwd=workdir) - else: - run_cmd("git fetch", cwd=workdir/Path(repo_name)) - - run_cmd(f"git checkout {repo_ref}", cwd=workdir/Path(repo_name)) - run_cmd("git submodule update --recursive", cwd=workdir/Path(repo_name)) - - -def merge_json(json1: dict, json2: dict, key: str): - merged_data = [] - - for obj1 in json1: - merged_obj = obj1.copy() - for obj2 in json2: - if obj1[key] == obj2[key]: - merged_obj.update(obj2) - break - merged_data.append(merged_obj) - - return merged_data diff --git a/scripts/entrypoint.py b/scripts/entrypoint.py index dc78881..10039fc 100644 --- a/scripts/entrypoint.py +++ b/scripts/entrypoint.py @@ -2,7 +2,7 @@ from argparse import ArgumentParser, Namespace from pathlib import Path -sys.path.insert(1, Path(__file__).resolve().parent) +sys.path.insert(1, str(Path(__file__).resolve().parent)) def parse_args() -> Namespace: @@ -33,7 +33,7 @@ def parse_args() -> Namespace: subparser.add_argument("--input_file", required=False, type=Path, default=Path("input_files/test_input.json")) subparser.add_argument("--output_file", required=False, type=Path, default=Path("output_files/output.json")) subparser.add_argument("--logs_file", required=False, type=Path, - default=Path("output_files/error_logs.txt")) + default=Path("output_files/error_logs.txt")) subparser.add_argument("--workdir", required=False, type=str, default="workdir") subparser.add_argument("--use_sha1_from_live", required=False, action='store_true') diff --git a/scripts/build_and_test/utils.py b/scripts/utils.py similarity index 85% rename from scripts/build_and_test/utils.py rename to scripts/utils.py index 705a906..6bb9ac8 100644 --- a/scripts/build_and_test/utils.py +++ b/scripts/utils.py @@ -8,7 +8,7 @@ def run_cmd(cmd: str, cwd: Path, print_output: bool = True, no_throw: bool = False) -> Tuple[int, str]: - error_log = "" + stdout = "" print(f"[run_cmd] Running: {cmd} from {cwd}") ret = subprocess.run(cmd, @@ -16,25 +16,23 @@ def run_cmd(cmd: str, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True, - cwd=cwd) - if no_throw is False and ret.returncode: - print(f"[run_cmd] Error {ret.returncode} raised while running cmd: {cmd}") - print("[run_cmd] Output was:") - print(ret.stdout) - raise ValueError() + cwd=cwd, + check=not no_throw) if ret.returncode: print(f"[run_cmd] Output:\n{ret.stdout}") - - error_log = f''' + stdout = f''' ############################################################################### [run_cmd] Running: {cmd} from {cwd}" ############################################################################### ''' + ret.stdout - return ret.returncode, error_log + else: + stdout = ret.stdout.strip() + + return ret.returncode, stdout -def git_setup(repo_name: str, repo_ref: str, repo_url: str, workdir: Path): +def git_setup(repo_name: str, repo_ref: str, repo_url: str, workdir: Path) -> None: # Force clone in https over SSH GIT_CONFIG = ' -c url."https://github.com/".insteadOf="git@github.com:" -c url."https://".insteadOf="git://"'