Skip to content

Commit

Permalink
support.plugin: abominable workaround for Python 3.8-3.9.
Browse files Browse the repository at this point in the history
  • Loading branch information
whitequark committed Oct 4, 2023
1 parent 69d23bf commit 54edef0
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 14 deletions.
5 changes: 1 addition & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion software/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ __pycache__/
# pdm
/.pdm-plugins
/.pdm-python
/.venv
/.venv*
/pdm.lock
27 changes: 19 additions & 8 deletions software/glasgow/support/plugin.py
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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):
Expand Down Expand Up @@ -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":
Expand Down
1 change: 0 additions & 1 deletion software/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 54edef0

Please sign in to comment.