From 13121ef431e8b49a7f5650afbbdfd1b4c9ddd223 Mon Sep 17 00:00:00 2001 From: Lucas PASCAL Date: Wed, 17 Apr 2024 17:24:42 +0200 Subject: [PATCH] [fix] Replacing deprecated 'pkg_resources' with 'importlib.resources' --- CHANGELOG.md | 6 ++++++ speculos/api/api.py | 4 ++-- speculos/api/helpers.py | 7 +++---- speculos/main.py | 14 +++++++------- speculos/mcu/automation.py | 7 +++---- tests/python/api/__init__.py | 0 tests/python/api/test_api.py | 5 ++--- tests/python/apps/test_btc.py | 7 +++---- tests/python/mcu/__init__.py | 0 tests/python/mcu/test_automation.py | 5 ++--- 10 files changed, 28 insertions(+), 27 deletions(-) create mode 100644 tests/python/api/__init__.py create mode 100644 tests/python/mcu/__init__.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 00b04b4e..3a80eecb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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.8.7] - 2024-??-?? + +### Fixed + +- Replacing deprecated `pkg_resources` usages with `importlib.resources` equivalents + ## [0.8.6] - 2024-04-11 ### Fixed diff --git a/speculos/api/api.py b/speculos/api/api.py index 3cb78216..ff4c441d 100644 --- a/speculos/api/api.py +++ b/speculos/api/api.py @@ -1,6 +1,6 @@ import socket import threading -import pkg_resources +import importlib.resources from typing import Any, Dict from flask import Flask from flask_restful import Api @@ -59,7 +59,7 @@ def __init__(self, seph: SeProxyHal, automation_server: BroadcastInterface): self._port = api_port - static_folder = pkg_resources.resource_filename(__name__, "/static") + static_folder = str(importlib.resources.files(__package__) / "static") self._app = Flask(__name__, static_url_path="", static_folder=static_folder) self._app.env = "development" diff --git a/speculos/api/helpers.py b/speculos/api/helpers.py index d873162a..0d6bb33f 100644 --- a/speculos/api/helpers.py +++ b/speculos/api/helpers.py @@ -1,10 +1,9 @@ +import importlib.resources import json -import os.path -import pkg_resources def load_json_schema(filename): - path = os.path.join("resources", filename) - with pkg_resources.resource_stream(__name__, path) as fp: + path = importlib.resources.files(__package__) / "resources" / filename + with path.open("rb") as fp: schema = json.load(fp) return schema diff --git a/speculos/main.py b/speculos/main.py index cd743f69..4fd735f9 100644 --- a/speculos/main.py +++ b/speculos/main.py @@ -7,6 +7,7 @@ import argparse import binascii import ctypes +import importlib.resources import logging import os import re @@ -14,7 +15,6 @@ import socket import sys import threading -import pkg_resources from elftools.elf.elffile import ELFFile from ledgered.binary import LedgerBinaryApp from mnemonic import mnemonic @@ -38,7 +38,7 @@ logger = logging.getLogger("speculos") -launcher_path = pkg_resources.resource_filename(__name__, "/resources/launcher") +launcher_path = str(importlib.resources.files(__package__) / "resources" / "launcher") def set_pdeath(sig): @@ -139,10 +139,10 @@ def run_qemu(s1: socket.socket, s2: socket.socket, args: argparse.Namespace, use # load cxlib only if available for the specified api level or sdk if args.apiLevel: - cxlib_filepath = f"/cxlib/{args.model}-api-level-cx-{args.apiLevel}.elf" + cxlib_filepath = f"cxlib/{args.model}-api-level-cx-{args.apiLevel}.elf" else: - cxlib_filepath = f"/cxlib/{args.model}-cx-{args.sdk}.elf" - cxlib = pkg_resources.resource_filename(__name__, cxlib_filepath) + cxlib_filepath = f"cxlib/{args.model}-cx-{args.sdk}.elf" + cxlib = str(importlib.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}' @@ -152,8 +152,8 @@ 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 = pkg_resources.resource_filename(__name__, fonts_filepath) + fonts_filepath = f"fonts/{args.model}-fonts-{args.apiLevel}.bin" + fonts = str(importlib.resources.files(__package__) / fonts_filepath) if os.path.exists(fonts): argv += ['-f', fonts] else: diff --git a/speculos/mcu/automation.py b/speculos/mcu/automation.py index 9a2e6c54..a3065a8a 100644 --- a/speculos/mcu/automation.py +++ b/speculos/mcu/automation.py @@ -1,8 +1,7 @@ +import importlib.resources import json import jsonschema import logging -import os -import pkg_resources import re @@ -20,8 +19,8 @@ def __init__(self, document): self.validate() def validate(self): - path = os.path.join("resources", "automation.schema") - with pkg_resources.resource_stream(__name__, path) as fp: + path = importlib.resources.files(__package__) / "resources" / "automation.schema" + with path.open("rb") as fp: schema = json.load(fp) jsonschema.validate(instance=self.json, schema=schema) diff --git a/tests/python/api/__init__.py b/tests/python/api/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/python/api/test_api.py b/tests/python/api/test_api.py index cf20bdb4..ecb9b9b3 100644 --- a/tests/python/api/test_api.py +++ b/tests/python/api/test_api.py @@ -1,6 +1,6 @@ +import importlib.resources import json import os -import pkg_resources import pytest import re import requests @@ -16,8 +16,7 @@ class TestApi: @staticmethod def get_automation_data(name): - path = os.path.join("resources", name) - path = pkg_resources.resource_filename(__name__, path) + path = importlib.resources.files(__package__) / "resources" / name with open(path, "rb") as fp: data = fp.read() return data diff --git a/tests/python/apps/test_btc.py b/tests/python/apps/test_btc.py index 4dad60fd..094b1537 100755 --- a/tests/python/apps/test_btc.py +++ b/tests/python/apps/test_btc.py @@ -5,9 +5,9 @@ ''' import json +import importlib.resources import io import os -import pkg_resources import pytest from enum import IntEnum @@ -28,8 +28,7 @@ def client(client_btc): def read_automation_rules(name): - path = os.path.join("resources", name) - path = pkg_resources.resource_filename(__name__, path) + path = importlib.resources.files(__package__) / "resources" / name with open(path, "rb") as fp: rules = json.load(fp) return rules @@ -59,7 +58,7 @@ def test_btc_get_public_key_with_user_approval(client, app): event = client.get_next_event() screenshot = client.get_screenshot() - path = pkg_resources.resource_filename(__name__, f"resources/btc_getpubkey_{app.model}.png") + path = importlib.resources.files(__package__) / "resources" / f"btc_getpubkey_{app.model}.png" assert speculos.client.screenshot_equal(path, io.BytesIO(screenshot)) if app.model == "blue": diff --git a/tests/python/mcu/__init__.py b/tests/python/mcu/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/python/mcu/test_automation.py b/tests/python/mcu/test_automation.py index a44758d6..1cd0da45 100644 --- a/tests/python/mcu/test_automation.py +++ b/tests/python/mcu/test_automation.py @@ -1,7 +1,7 @@ +import importlib.resources import json import jsonschema import os -import pkg_resources import pytest from speculos.mcu import automation @@ -10,8 +10,7 @@ class TestAutomation: @staticmethod def get_json_path(name): - path = os.path.join("resources", name) - path = pkg_resources.resource_filename(__name__, path) + path = importlib.resources.files(__package__) / "resources" / name return f"file:{path}" def test_valid_json(self):