Skip to content

Commit

Permalink
Merge pull request #479 from LedgerHQ/fix/deprecated_dpkg_resources
Browse files Browse the repository at this point in the history
Replacing deprecated 'pkg_resources' with 'importlib.resources'
  • Loading branch information
lpascal-ledger authored Apr 17, 2024
2 parents a5db170 + 13121ef commit 5139c2b
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 27 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.8.7] - 2024-??-??

### Fixed

- Replacing deprecated `pkg_resources` usages with `importlib.resources` equivalents

## [0.8.6] - 2024-04-11

### Fixed
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,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
Expand Down Expand Up @@ -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"

Expand Down
7 changes: 3 additions & 4 deletions speculos/api/helpers.py
Original file line number Diff line number Diff line change
@@ -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
14 changes: 7 additions & 7 deletions speculos/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
import argparse
import binascii
import ctypes
import importlib.resources
import logging
import os
import re
import signal
import socket
import sys
import threading
import pkg_resources
from elftools.elf.elffile import ELFFile
from ledgered.binary import LedgerBinaryApp
from mnemonic import mnemonic
Expand All @@ -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):
Expand Down Expand Up @@ -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}'
Expand All @@ -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:
Expand Down
7 changes: 3 additions & 4 deletions speculos/mcu/automation.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import importlib.resources
import json
import jsonschema
import logging
import os
import pkg_resources
import re


Expand All @@ -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)

Expand Down
Empty file added tests/python/api/__init__.py
Empty file.
5 changes: 2 additions & 3 deletions tests/python/api/test_api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import importlib.resources
import json
import os
import pkg_resources
import pytest
import re
import requests
Expand All @@ -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
Expand Down
7 changes: 3 additions & 4 deletions tests/python/apps/test_btc.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
'''

import json
import importlib.resources
import io
import os
import pkg_resources
import pytest

from enum import IntEnum
Expand All @@ -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
Expand Down Expand Up @@ -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":
Expand Down
Empty file added tests/python/mcu/__init__.py
Empty file.
5 changes: 2 additions & 3 deletions tests/python/mcu/test_automation.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import importlib.resources
import json
import jsonschema
import os
import pkg_resources
import pytest

from speculos.mcu import automation
Expand All @@ -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):
Expand Down

0 comments on commit 5139c2b

Please sign in to comment.