-
-
Notifications
You must be signed in to change notification settings - Fork 598
refactor/docs: improve compile_pip_requirements error message and docs #2792
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
538e4fe
refactor: Unify invoking pip-compile
brandonchinn178 370901f
fix: Hide stacktrace on resolution failure
brandonchinn178 75b0ced
feat: Show hint for --verbose on pip-compile error
brandonchinn178 e4f5ba0
docs: Add compile_pip_requirements documentation to "Using dependenci…
brandonchinn178 3caa444
docs: Add warning about hermetic builds with pyproject.toml
brandonchinn178 3cd9457
Merge branch 'main' into compile-pip
rickeylev 37fe3fe
Merge branch 'main' into compile-pip
rickeylev 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 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
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 |
---|---|---|
|
@@ -15,14 +15,17 @@ | |
"Set defaults for the pip-compile command to run it under Bazel" | ||
|
||
import atexit | ||
import functools | ||
import os | ||
import shutil | ||
import sys | ||
from pathlib import Path | ||
from typing import Optional, Tuple | ||
from typing import List, Optional, Tuple | ||
|
||
import click | ||
import piptools.writer as piptools_writer | ||
from pip._internal.exceptions import DistributionNotFound | ||
from pip._vendor.resolvelib.resolvers import ResolutionImpossible | ||
rickeylev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
from piptools.scripts.compile import cli | ||
|
||
from python.runfiles import runfiles | ||
|
@@ -82,15 +85,15 @@ def _locate(bazel_runfiles, file): | |
@click.command(context_settings={"ignore_unknown_options": True}) | ||
@click.option("--src", "srcs", multiple=True, required=True) | ||
@click.argument("requirements_txt") | ||
@click.argument("update_target_label") | ||
@click.argument("target_label_prefix") | ||
@click.option("--requirements-linux") | ||
@click.option("--requirements-darwin") | ||
@click.option("--requirements-windows") | ||
@click.argument("extra_args", nargs=-1, type=click.UNPROCESSED) | ||
def main( | ||
srcs: Tuple[str, ...], | ||
requirements_txt: str, | ||
update_target_label: str, | ||
target_label_prefix: str, | ||
requirements_linux: Optional[str], | ||
requirements_darwin: Optional[str], | ||
requirements_windows: Optional[str], | ||
|
@@ -152,9 +155,10 @@ def main( | |
# or shutil.copyfile, as they will fail with OSError: [Errno 18] Invalid cross-device link. | ||
shutil.copy(resolved_requirements_file, requirements_out) | ||
|
||
update_command = os.getenv("CUSTOM_COMPILE_COMMAND") or "bazel run %s" % ( | ||
update_target_label, | ||
update_command = ( | ||
os.getenv("CUSTOM_COMPILE_COMMAND") or f"bazel run {target_label_prefix}.update" | ||
) | ||
test_command = f"bazel test {target_label_prefix}_test" | ||
|
||
os.environ["CUSTOM_COMPILE_COMMAND"] = update_command | ||
os.environ["PIP_CONFIG_FILE"] = os.getenv("PIP_CONFIG_FILE") or os.devnull | ||
|
@@ -168,6 +172,12 @@ def main( | |
) | ||
argv.extend(extra_args) | ||
|
||
_run_pip_compile = functools.partial( | ||
run_pip_compile, | ||
argv, | ||
srcs_relative=srcs_relative, | ||
) | ||
|
||
if UPDATE: | ||
print("Updating " + requirements_file_relative) | ||
|
||
|
@@ -187,49 +197,66 @@ def main( | |
atexit.register( | ||
lambda: shutil.copy(absolute_output_file, requirements_file_tree) | ||
) | ||
cli(argv, standalone_mode=False) | ||
_run_pip_compile(verbose_command=f"{update_command} -- --verbose") | ||
requirements_file_relative_path = Path(requirements_file_relative) | ||
content = requirements_file_relative_path.read_text() | ||
content = content.replace(absolute_path_prefix, "") | ||
requirements_file_relative_path.write_text(content) | ||
else: | ||
# cli will exit(0) on success | ||
try: | ||
print("Checking " + requirements_file) | ||
cli(argv) | ||
print("cli() should exit", file=sys.stderr) | ||
print("Checking " + requirements_file) | ||
sys.stdout.flush() | ||
_run_pip_compile(verbose_command=f"{test_command} --test_arg=--verbose") | ||
golden = open(_locate(bazel_runfiles, requirements_file)).readlines() | ||
out = open(requirements_out).readlines() | ||
out = [line.replace(absolute_path_prefix, "") for line in out] | ||
if golden != out: | ||
import difflib | ||
|
||
print("".join(difflib.unified_diff(golden, out)), file=sys.stderr) | ||
print( | ||
f"Lock file out of date. Run '{update_command}' to update.", | ||
file=sys.stderr, | ||
) | ||
sys.exit(1) | ||
|
||
|
||
def run_pip_compile( | ||
args: List[str], | ||
*, | ||
srcs_relative: List[str], | ||
verbose_command: str, | ||
) -> None: | ||
try: | ||
cli(args, standalone_mode=False) | ||
except DistributionNotFound as e: | ||
if isinstance(e.__cause__, ResolutionImpossible): | ||
# pip logs an informative error to stderr already | ||
# just render the error and exit | ||
print(e) | ||
sys.exit(1) | ||
else: | ||
raise | ||
Comment on lines
+237
to
+238
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. Super nit: the except ...:
if ...:
sys.exit(1)
raise 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. Sure; I like being explicit 🙂 |
||
except SystemExit as e: | ||
if e.code == 0: | ||
return # shouldn't happen, but just in case | ||
elif e.code == 2: | ||
print( | ||
"pip-compile exited with code 2. This means that pip-compile found " | ||
"incompatible requirements or could not find a version that matches " | ||
f"the install requirement in one of {srcs_relative}.\n" | ||
"Try re-running with verbose:\n" | ||
f" {verbose_command}", | ||
file=sys.stderr, | ||
) | ||
sys.exit(1) | ||
else: | ||
print( | ||
f"pip-compile unexpectedly exited with code {e.code}.\n" | ||
"Try re-running with verbose:\n" | ||
f" {verbose_command}", | ||
file=sys.stderr, | ||
) | ||
sys.exit(1) | ||
except SystemExit as e: | ||
if e.code == 2: | ||
print( | ||
"pip-compile exited with code 2. This means that pip-compile found " | ||
"incompatible requirements or could not find a version that matches " | ||
f"the install requirement in one of {srcs_relative}.", | ||
file=sys.stderr, | ||
) | ||
sys.exit(1) | ||
elif e.code == 0: | ||
golden = open(_locate(bazel_runfiles, requirements_file)).readlines() | ||
out = open(requirements_out).readlines() | ||
out = [line.replace(absolute_path_prefix, "") for line in out] | ||
if golden != out: | ||
import difflib | ||
|
||
print("".join(difflib.unified_diff(golden, out)), file=sys.stderr) | ||
print( | ||
"Lock file out of date. Run '" | ||
+ update_command | ||
+ "' to update.", | ||
file=sys.stderr, | ||
) | ||
sys.exit(1) | ||
sys.exit(0) | ||
else: | ||
print( | ||
f"pip-compile unexpectedly exited with code {e.code}.", | ||
file=sys.stderr, | ||
) | ||
sys.exit(1) | ||
|
||
|
||
if __name__ == "__main__": | ||
|
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.
Uh oh!
There was an error while loading. Please reload this page.