Skip to content

Commit

Permalink
new method for console rules
Browse files Browse the repository at this point in the history
  • Loading branch information
wisechengyi committed Oct 21, 2018
1 parent ed7d4d0 commit 1b77e56
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 4 deletions.
57 changes: 54 additions & 3 deletions src/python/pants/engine/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ def execute(self, execution_request):
except TaskError as e:
return ExecutionResult.failure(e)

def products_request(self, products, subjects, v2_ui):
def products_request_console_rule(self, products, subjects, v2_ui):
"""Executes a request for multiple products for some subjects, and returns the products.
:param list products: A list of product type for the request.
Expand All @@ -534,6 +534,57 @@ def products_request(self, products, subjects, v2_ui):
if unknown_state_types:
State.raise_unrecognized(unknown_state_types)

# Throw handling.
# TODO: See https://github.com/pantsbuild/pants/issues/3912
throw_root_states = tuple(state for root, state in result.root_products if type(state) is Throw)
if throw_root_states:
unique_exceptions = tuple({t.exc for t in throw_root_states})

if len(unique_exceptions) == 1 and isinstance(unique_exceptions[0], GracefulTerminationException):
raise unique_exceptions[0]

exception_noun = pluralize(len(unique_exceptions), 'Exception')

if self._scheduler.include_trace_on_error:
cumulative_trace = '\n'.join(self.trace(request))
raise ExecutionError(
'{} encountered:\n{}'.format(exception_noun, cumulative_trace),
unique_exceptions,
)
else:
raise ExecutionError(
'{} encountered:\n {}'.format(
exception_noun,
'\n '.join('{}: {}'.format(type(t).__name__, str(t)) for t in unique_exceptions)),
unique_exceptions
)

# Everything is a Return: we rely on the fact that roots are ordered to preserve subject
# order in output lists.
product_results = defaultdict(list)
for (_, product), state in result.root_products:
product_results[product].append(state.value)
return product_results

def products_request(self, products, subjects):
"""Executes a request for multiple products for some subjects, and returns the products.
:param list products: A list of product type for the request.
:param list subjects: A list of subjects for the request.
:returns: A dict from product type to lists of products each with length matching len(subjects).
"""
request = self.execution_request(products, subjects)
result = self.execute(request)
if result.error:
raise result.error

# State validation.
unknown_state_types = tuple(
type(state) for _, state in result.root_products if type(state) not in (Throw, Return)
)
if unknown_state_types:
State.raise_unrecognized(unknown_state_types)

# Throw handling.
# TODO: See https://github.com/pantsbuild/pants/issues/3912
throw_root_states = tuple(state for root, state in result.root_products if type(state) is Throw)
Expand Down Expand Up @@ -568,15 +619,15 @@ def products_request(self, products, subjects, v2_ui):
product_results[product].append(state.value)
return product_results

def product_request(self, product, subjects, v2_ui=False):
def product_request(self, product, subjects):
"""Executes a request for a single product for some subjects, and returns the products.
:param class product: A product type for the request.
:param list subjects: A list of subjects for the request.
:param bool v2_ui: whether to render the v2 engine UI
:returns: A list of the requested products, with length match len(subjects).
"""
return self.products_request([product], subjects, v2_ui)[product]
return self.products_request([product], subjects)[product]

def capture_snapshots(self, path_globs_and_roots):
"""Synchronously captures Snapshots for each matching PathGlobs rooted at a its root directory.
Expand Down
2 changes: 1 addition & 1 deletion src/python/pants/init/engine_initializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ def run_console_rules(self, goals, target_roots, v2_ui):
for goal in goals:
goal_product = self.goal_map[goal]
logger.debug('requesting {} to satisfy execution of `{}` goal'.format(goal_product, goal))
self.scheduler_session.product_request(goal_product, subjects, v2_ui)
self.scheduler_session.products_request_console_rule([goal_product], subjects, v2_ui)

def create_build_graph(self, target_roots, build_root=None):
"""Construct and return a `BuildGraph` given a set of input specs.
Expand Down

0 comments on commit 1b77e56

Please sign in to comment.