-
Notifications
You must be signed in to change notification settings - Fork 23
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
Add script that checks dependency conflicts #218
Closed
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
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,130 @@ | ||
import argparse | ||
import pathlib | ||
import subprocess | ||
import sys | ||
import tempfile | ||
import platform | ||
import venv | ||
import contextlib | ||
|
||
import yaml | ||
|
||
from komodo.check_up_to_date_pypi import get_pypi_packages | ||
from komodo.package_version import strip_version | ||
|
||
|
||
@contextlib.contextmanager | ||
def temporary_environment(): | ||
class EnvBuilder(venv.EnvBuilder): | ||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
self.context = None | ||
|
||
def post_setup(self, context): | ||
self.context = context | ||
|
||
with tempfile.TemporaryDirectory() as tmp_dir: | ||
builder = EnvBuilder(with_pip=True) | ||
builder.create(str(tmp_dir)) | ||
context = builder.context | ||
yield context.env_exe | ||
|
||
|
||
def main(): | ||
parser = argparse.ArgumentParser( | ||
description="Checks if pypi packages are compatible" | ||
) | ||
parser.add_argument( | ||
"release_file", | ||
type=lambda arg: arg | ||
if pathlib.Path(arg).is_file() | ||
else parser.error("{} is not a file".format(arg)), | ||
help="Release file you would like to check dependencies on.", | ||
) | ||
parser.add_argument( | ||
"repository_file", | ||
type=lambda arg: arg | ||
if pathlib.Path(arg).is_file() | ||
else parser.error("{} is not a file".format(arg)), | ||
help="Repository file where the source of the packages is found", | ||
) | ||
parser.add_argument( | ||
"--output-file", | ||
type=str, | ||
default=False, | ||
help="Output file where requirements are written", | ||
) | ||
parser.add_argument( | ||
"--try-pip", | ||
action="store_true", | ||
help="If given, will try to install packages and check validity. Note that " | ||
"creates a virtual environment and is quite slow", | ||
) | ||
|
||
args = parser.parse_args() | ||
with open(args.release_file) as fin: | ||
releases = yaml.safe_load(fin) | ||
with open(args.repository_file) as fin: | ||
repository = yaml.safe_load(fin) | ||
|
||
pypi_packages = get_pypi_packages(releases, repository) | ||
|
||
requirements = [] | ||
for package in pypi_packages: | ||
if package == "opm" and platform.system() == "Darwin": | ||
print("Found opm, which has no osx source (as of 08.2021)") | ||
continue | ||
version = strip_version(releases[package]) | ||
requirements.append(f"{package}=={version}") | ||
|
||
if args.output_file: | ||
with open(args.output_file, "w") as fout: | ||
fout.write("\n".join(requirements) + "\n") | ||
|
||
if args.try_pip: | ||
print("Checking dependencies, this is usually slow") | ||
with temporary_environment() as current_python: | ||
upgrade_pip = subprocess.run( | ||
[ | ||
current_python, | ||
"-m", | ||
"pip", | ||
"install", | ||
"pip", | ||
"-U", | ||
], | ||
stdout=subprocess.PIPE, | ||
stderr=subprocess.PIPE, | ||
) | ||
jondequinor marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if upgrade_pip.returncode == 1: | ||
sys.exit( | ||
upgrade_pip.stderr.decode("utf-8") | ||
+ "\n Upgrading pip failed" | ||
) | ||
install = subprocess.run( | ||
[ | ||
current_python, | ||
"-m", | ||
"pip", | ||
"install", | ||
*requirements, | ||
"--no-deps", | ||
"--force-reinstall", # Probably not needed | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not needed |
||
], | ||
stdout=subprocess.PIPE, | ||
stderr=subprocess.PIPE, | ||
) | ||
if install.returncode == 1: | ||
sys.exit( | ||
install.stderr.decode("utf-8") | ||
+ "\n Installation of packages failed" | ||
) | ||
result = subprocess.run( | ||
[current_python, "-m", "pip", "check"], | ||
stdout=subprocess.PIPE, | ||
stderr=subprocess.PIPE, | ||
) | ||
if result.returncode == 1: | ||
sys.exit(result.stdout.decode("utf-8")) | ||
else: | ||
print("Everything is awesome!") |
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
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.
Do I need to pass
--try-pip
to actually have anything checked? What happens if this is not provided?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.
Either way, I think invoking this program like so
should
The parser is called "Checks if pypi packages are compatible" and the command is called "komodo-check-conflicts", so introducing "output", "requirements", and "try-pip" as default false is a bit unexpected.
Maybe remove
--try-pip
, rename--output-file
to--generate-requirements
. The latter should alsokomodo-check-conflicts --generate-requirements > requirements.txt
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.
Yes you do, will remove that option so it always checks.