-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Update ctexplain to run with bazel and newer python versions #17109
Open
torgil
wants to merge
1
commit into
bazelbuild:master
Choose a base branch
from
torgil:run-ctexplain-with-bazel-and-newer-python-versions
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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
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 |
---|---|---|
|
@@ -15,8 +15,10 @@ | |
|
||
There's no Python Bazel API so we invoke Bazel as a subprocess. | ||
""" | ||
import functools | ||
import json | ||
import os | ||
import re | ||
import subprocess | ||
from typing import Callable | ||
from typing import List | ||
|
@@ -28,8 +30,12 @@ | |
from tools.ctexplain.types import HostConfiguration | ||
from tools.ctexplain.types import NullConfiguration | ||
|
||
CQUERY_RESULT_LINE_REGEX = re.compile(r"^(.*?) \((\S+)\) \[([^[]*)\]$") | ||
DEFAULT_BAZEL_BINARY = "bazel" | ||
|
||
def run_bazel_in_client(args: List[str]) -> Tuple[int, List[str], List[str]]: | ||
|
||
def run_bazel_in_client(args: List[str], | ||
bazel: str = DEFAULT_BAZEL_BINARY) -> Tuple[int, List[str], List[str]]: | ||
"""Calls bazel within the current workspace. | ||
|
||
For production use. Tests use an alternative invoker that goes through test | ||
|
@@ -42,7 +48,7 @@ def run_bazel_in_client(args: List[str]) -> Tuple[int, List[str], List[str]]: | |
Tuple of (return code, stdout, stderr) | ||
""" | ||
result = subprocess.run( | ||
["blaze"] + args, | ||
[bazel] + args, | ||
cwd=os.getcwd(), | ||
stdout=subprocess.PIPE, | ||
stderr=subprocess.PIPE, | ||
|
@@ -57,8 +63,12 @@ class BazelApi(): | |
def __init__(self, | ||
run_bazel: Callable[[List[str]], | ||
Tuple[int, List[str], | ||
List[str]]] = run_bazel_in_client): | ||
List[str]]] = None, | ||
bazel: str = DEFAULT_BAZEL_BINARY): | ||
self.bazel = bazel | ||
self.run_bazel = run_bazel | ||
if run_bazel is None: | ||
self.run_bazel = functools.partial(run_bazel_in_client, bazel=self.bazel) | ||
|
||
def cquery(self, | ||
args: List[str]) -> Tuple[bool, str, Tuple[ConfiguredTarget, ...]]: | ||
|
@@ -144,20 +154,16 @@ def _parse_cquery_result_line(line: str) -> ConfiguredTarget: | |
Returns: | ||
Corresponding ConfiguredTarget if the line matches else None. | ||
""" | ||
tokens = line.split(maxsplit=2) | ||
label = tokens[0] | ||
if tokens[1][0] != "(" or tokens[1][-1] != ")": | ||
raise ValueError(f"{tokens[1]} in {line} not surrounded by parentheses") | ||
config_hash = tokens[1][1:-1] | ||
if config_hash == "null": | ||
fragments = () | ||
result = CQUERY_RESULT_LINE_REGEX.search(line) | ||
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. Implementing the TODO from line 139 would be a great followup, especially if it's not hard (not sure it is). I know you're just expanding on the parsing logic already implemented, but this (existing) logic is way more brittle than it should be. |
||
if result is None: | ||
raise ValueError(f"{repr(line)} does not match {repr(CQUERY_RESULT_LINE_REGEX.pattern)}") | ||
label, config_hash, fragments_str = result.groups() | ||
if fragments_str: | ||
# The fragments list looks like 'Fragment1, Fragment2, ...'. Split on | ||
# ', ' to convert it to a structured tuple. | ||
fragments = tuple(fragments_str.split(", ")) | ||
else: | ||
if tokens[2][0] != "[" or tokens[2][-1] != "]": | ||
raise ValueError(f"{tokens[2]} in {line} not surrounded by [] brackets") | ||
# The fragments list looks like '[Fragment1, Fragment2, ...]'. Split the | ||
# whole line on ' [' to get just this list, then remove the final ']', then | ||
# split again on ', ' to convert it to a structured tuple. | ||
fragments = tuple(line.split(" [")[1][0:-1].split(", ")) | ||
fragments = () | ||
return ConfiguredTarget( | ||
label=label, | ||
config=None, # Not yet available: we'll need `bazel config` to get this. | ||
|
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
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.
I'm not sure what official style recommendations are, but can this be colocated closer to its sole consumer for less context jumping?