-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
Python: add hook to check against dependencies in a requirements.txt file #71908
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
+81
−1
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
pkgs/development/interpreters/python/check-requirements/check-requirements.py
This file contains hidden or 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,55 @@ | ||
| # Check whether dependencies listed in a requiremens.txt are found | ||
| # in the current environment. | ||
| # | ||
| # Based on https://github.com/pypa/pip/issues/2733#issuecomment-257340871 | ||
| import argparse | ||
| import pip | ||
| from pip.req import parse_requirements | ||
| import pkg_resources | ||
| from pkg_resources import DistributionNotFound, VersionConflict | ||
|
|
||
|
|
||
| def get_dependencies(requirement_file_name): | ||
| dependencies = [] | ||
| session = pip.download.PipSession() | ||
| for req in parse_requirements(requirement_file_name, session=session): | ||
| if req.req is not None: | ||
| dependencies.append(str(req.req)) | ||
| else: | ||
| # The req probably refers to a url. Depending on the | ||
| # format req.link.url may be able to be parsed to find the | ||
| # required package. | ||
| pass | ||
| return dependencies | ||
|
|
||
|
|
||
| def check_dependencies(dependencies): | ||
| """Checks to see if the python dependencies are fullfilled. | ||
| If check passes return 0. Otherwise print error and return 1 | ||
| """ | ||
| try: | ||
| pkg_resources.working_set.require(dependencies) | ||
| except VersionConflict as exc: | ||
| try: | ||
| print("{} was found on your system, " | ||
| "but {} is required.\n".format(e.dist, e.req)) | ||
| return(0) | ||
| except AttributeError: | ||
| print(e) | ||
| return 1 | ||
| except DistributionNotFound as e: | ||
| print(e) | ||
| return 1 | ||
| return 0 | ||
|
|
||
|
|
||
| def parse_arguments(): | ||
| parser = argparse.ArgumentParser() | ||
| parser.add_argument("file") | ||
| return parser.parse_args() | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| args = parse_arguments() | ||
| dependencies = get_dependencies(args.file) | ||
| exit(check_dependencies(dependencies)) |
This file contains hidden or 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
15 changes: 15 additions & 0 deletions
15
pkgs/development/interpreters/python/hooks/requirements-check-hook.sh
This file contains hidden or 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,15 @@ | ||
| # Setup hook for requirements. | ||
| echo "Sourcing requirements-check-hook" | ||
|
|
||
| requirementsCheckPhase() { | ||
| if [ -z "$requirementsCheckFile" ]; then | ||
| echo "Error: no $requirementsCheckFile was given." | ||
| exit 1 | ||
|
|
||
| @pythonCheckInterpreter@ @requirementsCheckScript@ $requirementsCheckFile | ||
| } | ||
|
|
||
| if [ -z "$dontUseRequirementsCheck" ]; then | ||
| echo "Using requirementsCheckPhase" | ||
| preDistPhases+=" requirementsCheckPhase" | ||
| fi | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hardcoding the interpreter I don't like very much because it makes it harder when testing a Python env. In that case, one would have to set
PYTHONPATHorNIX_PYTHONPATHto include that env.The only alternative I see is using that env's Python to execute this, but we can't automate that, which means users would have to copy in the
check-requirements.pyscript instead.