Skip to content

Commit

Permalink
Merge pull request #486 from LedgerHQ/fix/importlib
Browse files Browse the repository at this point in the history
[fix] 'importlib' does not exist on Python<=3.8
  • Loading branch information
lpascal-ledger authored May 14, 2024
2 parents a4a983b + 2a0d97a commit 56ed996
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 25 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.9.1] 2024-05-14

### Fix

- `importlib.resources` does not exists on Python 3.8

## [0.9.0] - 2024-05-06

### Added
Expand Down
4 changes: 2 additions & 2 deletions speculos/api/apdu.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
from flask import stream_with_context, Response, request
from typing import Generator, Optional

from speculos.resources_importer import get_resource_schema_as_json
from ..mcu.seproxyhal import SeProxyHal
from .helpers import load_json_schema
from .restful import SephResource


Expand Down Expand Up @@ -48,7 +48,7 @@ def seph_apdu_callback(self, data: bytes) -> None:


class APDU(SephResource):
schema = load_json_schema("apdu.schema")
schema = get_resource_schema_as_json("api", "apdu.schema")

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
Expand Down
4 changes: 2 additions & 2 deletions speculos/api/api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import socket
import threading
import importlib.resources
from typing import Any, Dict
from flask import Flask
from flask_restful import Api
Expand All @@ -9,6 +8,7 @@
from speculos.mcu.readerror import ReadError
from speculos.mcu.seproxyhal import SeProxyHal
from speculos.observer import BroadcastInterface
from speculos.resources_importer import resources
from .apdu import APDU
from .automation import Automation
from .button import Button
Expand Down Expand Up @@ -59,7 +59,7 @@ def __init__(self,
seph: SeProxyHal,
automation_server: BroadcastInterface):
self._port = api_port
static_folder = str(importlib.resources.files(__package__) / "static")
static_folder = str(resources.files(__package__) / "static")
self._app = Flask(__name__, static_url_path="", static_folder=static_folder)
self._app.env = "development"

Expand Down
4 changes: 2 additions & 2 deletions speculos/api/button.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import jsonschema
from flask import request

from .helpers import load_json_schema
from speculos.resources_importer import get_resource_schema_as_json
from .restful import SephResource


class Button(SephResource):
schema = load_json_schema("button.schema")
schema = get_resource_schema_as_json("api", "button.schema")

def post(self):
args = request.get_json(force=True)
Expand Down
4 changes: 2 additions & 2 deletions speculos/api/finger.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import jsonschema
from flask import request

from .helpers import load_json_schema
from speculos.resources_importer import get_resource_schema_as_json
from .restful import SephResource


class Finger(SephResource):
schema = load_json_schema("finger.schema")
schema = get_resource_schema_as_json("api", "finger.schema")

def post(self):
args = request.get_json(force=True)
Expand Down
9 changes: 0 additions & 9 deletions speculos/api/helpers.py

This file was deleted.

4 changes: 2 additions & 2 deletions speculos/api/ticker.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import jsonschema
from flask import request

from .helpers import load_json_schema
from speculos.resources_importer import get_resource_schema_as_json
from .restful import SephResource


class Ticker(SephResource):
schema = load_json_schema("ticker.schema")
schema = get_resource_schema_as_json("api", "ticker.schema")

def post(self):
args = request.get_json(force=True)
Expand Down
8 changes: 4 additions & 4 deletions speculos/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import argparse
import binascii
import ctypes
import importlib.resources
import logging
import os
import re
Expand All @@ -31,14 +30,15 @@
from .mcu.struct import DisplayArgs, ServerArgs
from .mcu.vnc import VNC
from .observer import BroadcastInterface
from .resources_importer import resources


DEFAULT_SEED = ('glory promote mansion idle axis finger extra february uncover one trip resource lawn turtle enact '
'monster seven myth punch hobby comfort wild raise skin')

logger = logging.getLogger("speculos")

launcher_path = str(importlib.resources.files(__package__) / "resources" / "launcher")
launcher_path = str(resources.files(__package__) / "resources" / "launcher")


def set_pdeath(sig):
Expand Down Expand Up @@ -142,7 +142,7 @@ def run_qemu(s1: socket.socket, s2: socket.socket, args: argparse.Namespace, use
cxlib_filepath = f"cxlib/{args.model}-api-level-cx-{args.apiLevel}.elf"
else:
cxlib_filepath = f"cxlib/{args.model}-cx-{args.sdk}.elf"
cxlib = str(importlib.resources.files(__package__) / cxlib_filepath)
cxlib = str(resources.files(__package__) / cxlib_filepath)
if os.path.exists(cxlib):
sh_offset, sh_size, sh_load, cx_ram_size, cx_ram_load = get_cx_infos(cxlib)
cxlib_args = f'{cxlib}:{sh_offset:#x}:{sh_size:#x}:{sh_load:#x}:{cx_ram_size:#x}:{cx_ram_load:#x}'
Expand All @@ -153,7 +153,7 @@ def run_qemu(s1: socket.socket, s2: socket.socket, args: argparse.Namespace, use
# for NBGL apps, fonts binary file is mandatory
if not use_bagl:
fonts_filepath = f"fonts/{args.model}-fonts-{args.apiLevel}.bin"
fonts = str(importlib.resources.files(__package__) / fonts_filepath)
fonts = str(resources.files(__package__) / fonts_filepath)
if os.path.exists(fonts):
argv += ['-f', fonts]
else:
Expand Down
5 changes: 3 additions & 2 deletions speculos/mcu/automation.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import importlib.resources
import json
import jsonschema
import logging
import re

from speculos.resources_importer import get_resources_path


class Automation:
def __init__(self, document):
Expand All @@ -19,7 +20,7 @@ def __init__(self, document):
self.validate()

def validate(self):
path = importlib.resources.files(__package__) / "resources" / "automation.schema"
path = get_resources_path("mcu", "automation.schema")
with path.open("rb") as fp:
schema = json.load(fp)
jsonschema.validate(instance=self.json, schema=schema)
Expand Down
22 changes: 22 additions & 0 deletions speculos/resources_importer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import json
from pathlib import Path
from platform import python_version_tuple
from typing import Dict

major, minor, _ = python_version_tuple()
assert major == "3"

if int(minor) <= 8:
import importlib_resources as resources
else:
import importlib.resources as resources


def get_resources_path(module: str, filename: str) -> Path:
return resources.files(__package__) / module / "resources" / filename


def get_resource_schema_as_json(module: str, filename: str) -> Dict:
with get_resources_path(module, filename).open("rb") as fp:
schema = json.load(fp)
return schema
14 changes: 14 additions & 0 deletions tests/python/test_resources_importer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import speculos.resources_importer as r


def test_api_resource_exists():
assert r.get_resources_path("api", "").exists()


def test_mcu_resource_exists():
assert r.get_resources_path("mcu", "").exists()


def test_load_json():
schema = r.get_resource_schema_as_json("api", "finger.schema")
assert isinstance(schema, dict)

0 comments on commit 56ed996

Please sign in to comment.