From 54edef07eaed4e086ff974aaff8c6f2b260f7f92 Mon Sep 17 00:00:00 2001 From: Catherine Date: Wed, 4 Oct 2023 16:47:49 +0000 Subject: [PATCH] support.plugin: abominable workaround for Python 3.8-3.9. --- .github/workflows/main.yml | 5 +---- software/.gitignore | 2 +- software/glasgow/support/plugin.py | 27 +++++++++++++++++++-------- software/pyproject.toml | 1 - 4 files changed, 21 insertions(+), 14 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 64115f5b9..bde4519f8 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -47,12 +47,9 @@ jobs: run: pdm install -G :all - name: Run tests working-directory: ./software - env: - YOSYS: yowasp-yosys - NEXTPNR_ICE40: yowasp-nextpnr-ice40 - ICEPACK: yowasp-icepack run: | pdm run glasgow --help + pdm run glasgow build --rev C3 uart pdm run test build-firmware: diff --git a/software/.gitignore b/software/.gitignore index e3b30be78..68dfd9d2b 100644 --- a/software/.gitignore +++ b/software/.gitignore @@ -7,5 +7,5 @@ __pycache__/ # pdm /.pdm-plugins /.pdm-python -/.venv +/.venv* /pdm.lock diff --git a/software/glasgow/support/plugin.py b/software/glasgow/support/plugin.py index d4747f619..4da0806b0 100644 --- a/software/glasgow/support/plugin.py +++ b/software/glasgow/support/plugin.py @@ -1,16 +1,26 @@ +import importlib.metadata import packaging.requirements import pathlib import sysconfig import textwrap -try: - import importlib_metadata # py3.9- -except ImportError: - import importlib.metadata as importlib_metadata __all__ = ["PluginRequirementsUnmet", "PluginMetadata"] +# There are subtle differences between Python versions for both importlib.metadata (the built-in +# package) and importlib_metadata (the PyPI installable shim), so implement this function the way +# we need ourselves based on the Python 3.8 API. Once we drop Python 3.9 support this abomination +# can be removed. +def _entry_points(*, group, name=None): + for distribution in importlib.metadata.distributions(): + distribution.name = distribution.metadata["Name"] + for entry_point in distribution.entry_points: + if entry_point.group == group and (name is None or entry_point.name == name): + entry_point.dist = distribution + yield entry_point + + def _requirements_for_optional_dependencies(distribution, depencencies): requirements = map(packaging.requirements.Requirement, distribution.requires) selected_requirements = set() @@ -27,8 +37,8 @@ def _unmet_requirements_in(requirements): unmet_requirements = set() for requirement in requirements: try: - version = importlib_metadata.version(requirement.name) - except importlib_metadata.PackageNotFoundError: + version = importlib.metadata.version(requirement.name) + except importlib.metadata.PackageNotFoundError: unmet_requirements.add(requirement) continue if not requirement.specifier.contains(version): @@ -67,11 +77,12 @@ class PluginMetadata: @classmethod def get(cls, handle): - return cls(importlib_metadata.entry_points(group=cls.GROUP_NAME, name=handle)[0]) + entry_point, *_ = _entry_points(group=cls.GROUP_NAME, name=handle) + return cls(entry_point) @classmethod def all(cls): - return {ep.name: cls(ep) for ep in importlib_metadata.entry_points(group=cls.GROUP_NAME)} + return {ep.name: cls(ep) for ep in _entry_points(group=cls.GROUP_NAME)} def __init__(self, entry_point): if entry_point.dist.name != "glasgow": diff --git a/software/pyproject.toml b/software/pyproject.toml index 4b40abad2..30da71e6a 100644 --- a/software/pyproject.toml +++ b/software/pyproject.toml @@ -18,7 +18,6 @@ classifiers = [ requires-python = "~=3.8" dependencies = [ "packaging>=23", - "importlib_metadata; python_version<'3.10'", "appdirs~=1.4", "amaranth[builtin-yosys] @ git+https://github.com/amaranth-lang/amaranth.git", "fx2>=0.11",