Gracefully handle missing dependencies.

Some people's workflows involve not using `pip`. This is not
a recommended way to use nMigen, but is prevalent enough for good
enough reason that we try to keep them working anyway.
This commit is contained in:
whitequark 2020-07-01 07:00:02 +00:00
parent f5670a9b71
commit 126f0be731
2 changed files with 18 additions and 9 deletions

View file

@ -1,8 +1,14 @@
try:
from importlib import metadata as importlib_metadata # py3.8+ stdlib
try:
from importlib import metadata as importlib_metadata # py3.8+ stdlib
except ImportError:
import importlib_metadata # py3.7- shim
__version__ = importlib_metadata.version(__package__)
except ImportError:
import importlib_metadata # py3.7- shim
__version__ = importlib_metadata.version(__package__)
# No importlib_metadata. This shouldn't normally happen, but some people prefer not installing
# packages via pip at all, instead using PYTHONPATH directly or copying the package files into
# `lib/pythonX.Y/site-packages`. Although not a recommended way, we still try to support it.
__version__ = "unknown"
from .hdl import *

View file

@ -12,13 +12,16 @@ except ImportError:
except ImportError:
importlib_metadata = None # not installed
try:
from importlib import resources as importlib_resources
try:
importlib_resources.files # py3.9+ stdlib
except AttributeError:
import importlib_resources # py3.8- shim
from importlib import resources as importlib_resources
try:
importlib_resources.files # py3.9+ stdlib
except AttributeError:
import importlib_resources # py3.8- shim
except ImportError:
import importlib_resources # py3.6- shim
except ImportError:
import importlib_resources # py3.6- shim
importlib_resources = None
from ._toolchain import has_tool, require_tool
@ -115,7 +118,7 @@ class _BuiltinYosys(YosysBinary):
@classmethod
def available(cls):
if importlib_metadata is None:
if importlib_metadata is None or importlib_resources is None:
return False
try:
importlib_metadata.version(cls.YOSYS_PACKAGE)