forked from MODFLOW-USGS/modflow-devtools
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(has_pkg): introduce strict flag (MODFLOW-USGS#106)
* add strict flag to has_pkg() toggling whether to try to import or only check metadata * always invalidate/refresh the installed package cache if strict is on * add pytest-virtualenv to test dependencies, test with/without strict * use --dist loadfile with xdist in CI for compatibility * use strict=True for requires_pkg marker
- Loading branch information
Showing
6 changed files
with
75 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
from pathlib import Path | ||
|
||
pytest_plugins = ["modflow_devtools.fixtures"] | ||
project_root_path = Path(__file__).parent |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -398,21 +398,47 @@ def has_exe(exe): | |
return _has_exe_cache[exe] | ||
|
||
|
||
def has_pkg(pkg): | ||
def has_pkg(pkg: str, strict: bool = False) -> bool: | ||
""" | ||
Determines if the given Python package is installed. | ||
Parameters | ||
---------- | ||
pkg : str | ||
Name of the package to check. | ||
strict : bool | ||
If False, only check if package metadata is available. | ||
If True, try to import the package (all dependencies must be present). | ||
Returns | ||
------- | ||
bool | ||
True if the package is installed, otherwise False. | ||
Notes | ||
----- | ||
Originally written by Mike Toews ([email protected]) for FloPy. | ||
""" | ||
if pkg not in _has_pkg_cache: | ||
found = True | ||
|
||
def try_import(): | ||
try: # import name, e.g. "import shapefile" | ||
importlib.import_module(pkg) | ||
return True | ||
except ModuleNotFoundError: | ||
return False | ||
|
||
def try_metadata() -> bool: | ||
try: # package name, e.g. pyshp | ||
metadata.distribution(pkg) | ||
return True | ||
except metadata.PackageNotFoundError: | ||
try: # import name, e.g. "import shapefile" | ||
importlib.import_module(pkg) | ||
except ModuleNotFoundError: | ||
found = False | ||
_has_pkg_cache[pkg] = found | ||
return False | ||
|
||
found = False | ||
if not strict: | ||
found = pkg in _has_pkg_cache or try_metadata() | ||
if not found: | ||
found = try_import() | ||
_has_pkg_cache[pkg] = found | ||
|
||
return _has_pkg_cache[pkg] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters