-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added dependency check at beginning of each run script
- Loading branch information
Sarina Meyer
committed
Feb 15, 2024
1 parent
90006b7
commit 846abdc
Showing
5 changed files
with
41 additions
and
3 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
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
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from importlib.metadata import version, PackageNotFoundError | ||
from packaging.requirements import Requirement | ||
from packaging.version import parse | ||
|
||
def check_dependencies(requirements_file): | ||
missing_dependencies = [] | ||
nonmatching_versions = [] | ||
|
||
with open(requirements_file) as f: | ||
for line in f: | ||
line = line.strip() | ||
if len(line) == 0: | ||
continue | ||
requirement = Requirement(line) | ||
|
||
try: | ||
installed_version = version(requirement.name) | ||
except PackageNotFoundError: | ||
missing_dependencies.append(line) | ||
|
||
if not parse(installed_version) in requirement.specifier: | ||
nonmatching_versions.append((requirement, installed_version)) | ||
|
||
error_msg = '' | ||
if missing_dependencies: | ||
error_msg += f'Missing dependencies: {" ".join(missing_dependencies)}.\n' | ||
if nonmatching_versions: | ||
error_msg += f'The following packages are installed with a version that does not match the requirement:\n' | ||
for req, installed_version in nonmatching_versions: | ||
error_msg += f'Package: {req.name}, installed: {installed_version}, required: {str(req.specifier)}\n' | ||
|
||
if len(error_msg) > 0: | ||
raise ModuleNotFoundError(f'{error_msg}--Make sure to install {requirements_file} to run this code!--') | ||
|