Skip to content

Commit

Permalink
[fix] mypy review
Browse files Browse the repository at this point in the history
  • Loading branch information
lpascal-ledger committed Nov 21, 2023
1 parent 8d35c8f commit 9765ea8
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 58 deletions.
10 changes: 4 additions & 6 deletions scripts/build_and_test/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,14 @@ def main(args: Namespace) -> None:

git_setup(SDK_NAME, args.sdk_ref, SDK_URL, abs_workdir)

output = {}
test_output = {}
build_output = []
logs = ""

for app_json in input_json:
repo_name = app_json.get("name")
repo_name = app_json["name"]
if not args.skip_setup:
repo_ref = app_json.get("ref")
repo_url = app_json.get("url")
repo_ref = app_json["ref"]
repo_url = app_json["url"]
print(f"Setup {repo_name}")
git_setup(repo_name, repo_ref, repo_url, abs_workdir)

Expand All @@ -95,7 +93,7 @@ def main(args: Namespace) -> None:
build_output.append(scan_app)
logs += log

output = merge_json(build_output, test_output, "name")
output = merge_json(build_output, {}, "name")

with open(args.output_file, 'w') as json_file:
json.dump(output, json_file, indent=1)
Expand Down
35 changes: 22 additions & 13 deletions scripts/build_and_test/build_app.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
import os
from pathlib import Path
from typing import Dict, Optional, Tuple, Union

from build_and_test.device import Devices, Device
from utils import run_cmd


def build_variant(target: str,
sdk_path: str,
variant_param: str,
sdk_path: Path,
variant_param: Optional[str],
variant_value: str,
app_build_path: Path,
extra_flags: str = ""):
extra_flags: str = "") -> Tuple[int, str]:

if not os.path.exists(app_build_path):
print("\t=> KO")
return True, f"Error: {app_build_path} does not exists\n"

error = run_cmd(f"TARGET={target} BOLOS_SDK={sdk_path} make clean", cwd=app_build_path, no_throw=True)
if variant_param:
run_cmd(f"TARGET={target} BOLOS_SDK={sdk_path} make clean", cwd=app_build_path, no_throw=True)
if variant_param is not None:
error, log = run_cmd(f"TARGET={target} BOLOS_SDK={sdk_path} make {variant_param}={variant_value} {extra_flags}",
cwd=app_build_path, no_throw=True)
else:
Expand All @@ -30,7 +31,11 @@ def build_variant(target: str,
return error, log


def build_all_variants(target: str, sdk_path: str, variant_param: str, variant_list: list, app_build_path: Path):
def build_all_variants(target: str,
sdk_path: Path,
variant_param: Optional[str],
variant_list: list,
app_build_path: Path) -> Tuple[Dict[str, str], str]:
output = {}
error_log = ""
for variant in variant_list:
Expand All @@ -45,18 +50,22 @@ def build_all_variants(target: str, sdk_path: str, variant_param: str, variant_l
return output, error_log


def build_device(device: Device, variant_param: str, app_build_path: Path, sdk_path: Path, app_json: dict):
blacklist = app_json.get("build_blacklist", "[]")
def build_device(device: Device,
variant_param: Optional[str],
app_build_path: Path,
sdk_path: Path,
app_json: dict) -> Tuple[Union[str, Dict[str, str]], str]:
blacklist = app_json.get("build_blacklist", [])
error_log = ""

if not device.selected:
return None, error_log
return "Skipped - not selected", error_log

if device.model_name in blacklist:
return "Skipped", error_log
return "Skipped - blacklisted", error_log

variants = app_json.get(f"variants_{device.model_name}", [])
variant_output = {}
variant_output: Dict[str, str] = {}
if len(variants) > 0:
variant_output, error_log = build_all_variants(device.target_name,
sdk_path,
Expand All @@ -68,9 +77,9 @@ def build_device(device: Device, variant_param: str, app_build_path: Path, sdk_p


def build_all_devices(devices: Devices, sdk_path: Path, app_json: dict, workdir: Path):
repo_name = app_json.get("name")
repo_name = app_json["name"]
variant_param = app_json.get("variant_param")
app_build_path = workdir / Path(app_json.get("name") + "/" + app_json.get("build_path", "."))
app_build_path = workdir / Path(repo_name + "/" + app_json.get("build_path", "."))

output = {
"name": repo_name,
Expand Down
29 changes: 15 additions & 14 deletions scripts/build_and_test/scan_app.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
from pathlib import Path
from typing import Tuple

from build_and_test.device import Devices, Device
from utils import run_cmd


def scan_variant(target: str,
sdk_path: str,
sdk_path: Path,
variant_param: str,
variant_value: str,
app_build_path: Path,
extra_flags: str = ""):
error = run_cmd(f"TARGET={target} BOLOS_SDK={sdk_path} make clean", cwd=app_build_path, no_throw=True)
extra_flags: str = "") -> Tuple[int, str]:
run_cmd(f"TARGET={target} BOLOS_SDK={sdk_path} make clean", cwd=app_build_path, no_throw=True)
if variant_param:
error, log = run_cmd(f"TARGET={target} BOLOS_SDK={sdk_path} make {variant_param}={variant_value} "
"-j ENABLE_SDK_WERROR=1 scan-build",
cwd=app_build_path, no_throw=True)
status, log = run_cmd(f"TARGET={target} BOLOS_SDK={sdk_path} make {variant_param}={variant_value} "
"-j ENABLE_SDK_WERROR=1 scan-build",
cwd=app_build_path, no_throw=True)
else:
error, log = run_cmd(f"TARGET={target} BOLOS_SDK={sdk_path} make -j ENABLE_SDK_WERROR=1 scan-build",
cwd=app_build_path, no_throw=True)
status, log = run_cmd(f"TARGET={target} BOLOS_SDK={sdk_path} make -j ENABLE_SDK_WERROR=1 scan-build",
cwd=app_build_path, no_throw=True)

if error:
if status:
print("\t=> KO")

return error, log
return status, log


def scan_all_variants(target: str, sdk_path: str, variant_param: str, variant_list: list, app_build_path: Path):
def scan_all_variants(target: str, sdk_path: Path, variant_param: str, variant_list: list, app_build_path: Path):
output = {}
error_log = ""
for variant in variant_list:
Expand Down Expand Up @@ -63,9 +64,9 @@ def scan_device(device: Device, variant_param: str, app_build_path: Path, sdk_pa


def scan_all_devices(devices: Devices, sdk_path: Path, app_json: dict, workdir: Path):
repo_name = app_json.get("name")
variant_param = app_json.get("variant_param")
app_build_path = workdir / Path(app_json.get("name") + "/" + app_json.get("build_path", "."))
repo_name = app_json["name"]
variant_param = app_json["variant_param"]
app_build_path = workdir / Path(repo_name + "/" + app_json.get("build_path", "."))

output = {
"name": repo_name,
Expand Down
24 changes: 15 additions & 9 deletions scripts/build_and_test/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,27 @@ def test(model: str, app_test_path: Path, app_build_path: Path, test_params: str
return output, log


def install_dependencies(app_test_path: Path):
def install_dependencies(app_test_path: Path) -> Tuple[int, str]:
error, log = run_cmd("pip install -r requirements.txt", cwd=app_test_path, no_throw=True)
return error, log


def test_device(device: Device, variant_param: str, app_build_path: Path, app_test_path: Path,
sdk_path: Path, extra_flags: str, blacklist: str, test_params: str):
def test_device(device: Device,
variant_param: str,
app_build_path: Path,
app_test_path: Path,
sdk_path: Path,
extra_flags: str,
blacklist: str,
test_params: str) -> Tuple[str, str]:
test_output: str
log = ""

if not device.selected:
return None, log
return "Skipped - not selected", log

if device.model_name in blacklist:
return "Skipped", log
return "Skipped - blacklisted", log

error, log = install_dependencies(app_test_path)
if error:
Expand All @@ -51,10 +57,10 @@ def test_device(device: Device, variant_param: str, app_build_path: Path, app_te


def test_all_devices(devices: Devices, sdk_path: Path, app_json: dict, workdir: Path):
repo_name = app_json.get("name")
variant_param = app_json.get("variant_param")
app_build_path = workdir / Path(app_json.get("name") + "/" + app_json.get("build_path", "."))
app_test_path = workdir / Path(app_json.get("name") + "/" + app_json.get("test_dir", "."))
repo_name = app_json["name"]
variant_param = app_json["variant_param"]
app_build_path = workdir / Path(repo_name + "/" + app_json.get("build_path", "."))
app_test_path = workdir / Path(repo_name + "/" + app_json.get("test_dir", "."))
extra_flags = app_json.get("extra_flags", "")
blacklist = app_json.get("build_blacklist", "[]")

Expand Down
2 changes: 1 addition & 1 deletion scripts/create_app_list/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def main(args: Namespace) -> None:
repo_name = repo.get("name")
repo_ref = repo.get("ref")
repo_url = repo.get("url")
repo_build_path = abs_workdir/Path(repo_name)/Path(repo.get("build_path", "."))
repo_build_path = abs_workdir / Path(repo_name) / Path(repo.get("build_path", "."))

if not args.skip_setup:
print("Cloning repo")
Expand Down
4 changes: 2 additions & 2 deletions scripts/create_app_list/app_load_params_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ def check_manifest(manifest: dict, database: dict) -> int:

# Check that the params match with the one from the database
for key in APP_LOAD_PARAMS_VALUE_CHECK:
app_params_ref_value = app_params_ref.get(key)
app_load_params_value = app_load_params.get(key)
app_params_ref_value = app_params_ref[key]
app_load_params_value = app_load_params[key]
if key == "appName":
if len(app_load_params_value) != 1:
print("[ERROR] Expected a single value for 'appName' "
Expand Down
27 changes: 16 additions & 11 deletions scripts/create_app_list/parse_github.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
import requests
from typing import Dict, List, Union
from dataclasses import dataclass, asdict
from typing import Dict, List

base_url = "https://api.github.com"
org_name = "LedgerHQ"

repos_endpoint = f"{base_url}/orgs/{org_name}/repos"

params: Dict[str, Union[str, int]] = {
"type": "public",
"archived": "false",
"sort": "full_name",
"page": 1,
"per_page": 100
}

@dataclass
class Params:
type: str
archived: str
sort: str
page: int
per_page: int

def parse_github(access_token: str = "") -> List[Dict]:

params = Params("public", "false", "full_name", 1, 100)


def parse_github(access_token: str = "") -> List[Dict[str, str]]:
repos = []
headers = {
"Authorization": f"Bearer {access_token}",
"Accept": "application/vnd.github.v3+json"
}

while True:
response = requests.get(repos_endpoint, params=params, headers=headers)
response = requests.get(repos_endpoint, params=asdict(params), headers=headers)
repos_data = response.json()
if not repos_data: # No more repositories to fetch
break
Expand All @@ -45,6 +50,6 @@ def parse_github(access_token: str = "") -> List[Dict]:
owner_name = parent_data["parent"]["owner"]["login"]
repos.append({"name": repo_name, "owner": owner_name, "ref": ref, "url": repo_url})

params["page"] += 1
params.page += 1

return repos
4 changes: 2 additions & 2 deletions scripts/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import subprocess
import shutil
from pathlib import Path
from typing import Tuple
from typing import List, Tuple


def run_cmd(cmd: str,
Expand Down Expand Up @@ -56,7 +56,7 @@ def git_setup(repo_name: str, repo_ref: str, repo_url: str, workdir: Path) -> No
return


def merge_json(json1: dict, json2: dict, key: str):
def merge_json(json1: dict, json2: dict, key: str) -> List:
merged_data = []

for obj1 in json1:
Expand Down

0 comments on commit 9765ea8

Please sign in to comment.