Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(593)

Side by Side Diff: recipe_engine/step_runner.py

Issue 1773273003: Make output placeholders like json.output index-able by name. (Closed) Base URL: https://chromium.googlesource.com/external/github.com/luci/recipes-py@master
Patch Set: Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 # Copyright (c) 2013-2015 The Chromium Authors. All rights reserved. 1 # Copyright (c) 2013-2015 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be 2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file. 3 # found in the LICENSE file.
4 4
5 import cStringIO 5 import cStringIO
6 import collections 6 import collections
7 import contextlib 7 import contextlib
8 import datetime 8 import datetime
9 import json 9 import json
10 import os 10 import os
(...skipping 430 matching lines...) Expand 10 before | Expand all | Expand 10 after
441 step: The step to render. 441 step: The step to render.
442 step_test: The test data json dictionary for this step, if any. 442 step_test: The test data json dictionary for this step, if any.
443 Passed through unaltered to each placeholder. 443 Passed through unaltered to each placeholder.
444 444
445 Returns the rendered step and a Placeholders object representing any 445 Returns the rendered step and a Placeholders object representing any
446 placeholder instances that were found while rendering. 446 placeholder instances that were found while rendering.
447 """ 447 """
448 rendered_step = dict(step) 448 rendered_step = dict(step)
449 449
450 # Process 'cmd', rendering placeholders there. 450 # Process 'cmd', rendering placeholders there.
451 placeholders = collections.defaultdict(lambda: collections.defaultdict(list)) 451 placeholders = collections.defaultdict(
452 lambda: collections.defaultdict(collections.OrderedDict))
452 new_cmd = [] 453 new_cmd = []
453 for item in step.get('cmd', []): 454 for item in step.get('cmd', []):
454 if isinstance(item, util.Placeholder): 455 if isinstance(item, util.Placeholder):
455 module_name, placeholder_name = item.name_pieces 456 module_name, placeholder_name = item.name_pieces
456 tdata = step_test.pop_placeholder(item.name_pieces) 457 tdata = step_test.pop_placeholder(module_name, placeholder_name, item.id)
457 new_cmd.extend(item.render(tdata)) 458 new_cmd.extend(item.render(tdata))
458 placeholders[module_name][placeholder_name].append((item, tdata)) 459 # TODO(http://crbug.com/593198): enable this assert when all recipes and
460 # recipe modules are updated.
461 #assert item.id not in placeholders[module_name][placeholder_name], (
462 # 'You have multiple place holders of %s.%s, please specify different '
463 # 'ids for them.' % (module_name, placeholder_name))
464 placeholders[module_name][placeholder_name][item.id] = (item, tdata)
459 else: 465 else:
460 new_cmd.append(item) 466 new_cmd.append(item)
461 rendered_step['cmd'] = new_cmd 467 rendered_step['cmd'] = new_cmd
462 468
463 # Process 'stdout', 'stderr' and 'stdin' placeholders, if given. 469 # Process 'stdout', 'stderr' and 'stdin' placeholders, if given.
464 stdio_placeholders = {} 470 stdio_placeholders = {}
465 for key in ('stdout', 'stderr', 'stdin'): 471 for key in ('stdout', 'stderr', 'stdin'):
466 placeholder = step.get(key) 472 placeholder = step.get(key)
467 tdata = None 473 tdata = None
468 if placeholder: 474 if placeholder:
(...skipping 19 matching lines...) Expand all
488 class BlankObject(object): 494 class BlankObject(object):
489 pass 495 pass
490 496
491 # Placeholders inside step |cmd|. 497 # Placeholders inside step |cmd|.
492 for module_name, pholders in placeholders.cmd.iteritems(): 498 for module_name, pholders in placeholders.cmd.iteritems():
493 assert not hasattr(step_result, module_name) 499 assert not hasattr(step_result, module_name)
494 o = BlankObject() 500 o = BlankObject()
495 setattr(step_result, module_name, o) 501 setattr(step_result, module_name, o)
496 502
497 for placeholder_name, items in pholders.iteritems(): 503 for placeholder_name, items in pholders.iteritems():
498 lst = [ph.result(step_result.presentation, td) for ph, td in items] 504 named_results = {}
499 setattr(o, placeholder_name+"_all", lst) 505 lst = []
506 for _, (ph, td) in items.iteritems():
507 result = ph.result(step_result.presentation, td)
508 lst.append(result)
509 named_results[ph.id] = result
510 setattr(o, placeholder_name + "s", named_results)
511 setattr(o, placeholder_name + "_all", lst)
iannucci 2016/03/10 03:17:42 so, actually, because of the way the recipe pinnin
stgao 2016/03/10 20:34:23 Sounds good. Let's go with this approach then.
500 setattr(o, placeholder_name, lst[0]) 512 setattr(o, placeholder_name, lst[0])
501 513
502 # Placeholders that are used with IO redirection. 514 # Placeholders that are used with IO redirection.
503 for key in ('stdout', 'stderr', 'stdin'): 515 for key in ('stdout', 'stderr', 'stdin'):
504 assert not hasattr(step_result, key) 516 assert not hasattr(step_result, key)
505 ph, td = getattr(placeholders, key) 517 ph, td = getattr(placeholders, key)
506 result = ph.result(step_result.presentation, td) if ph else None 518 result = ph.result(step_result.presentation, td) if ph else None
507 setattr(step_result, key, result) 519 setattr(step_result, key, result)
508 520
509 return step_result 521 return step_result
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
558 supplied command, and only uses the |env| kwarg for modifying the environment 570 supplied command, and only uses the |env| kwarg for modifying the environment
559 of the child process. 571 of the child process.
560 """ 572 """
561 saved_path = os.environ['PATH'] 573 saved_path = os.environ['PATH']
562 try: 574 try:
563 if path is not None: 575 if path is not None:
564 os.environ['PATH'] = path 576 os.environ['PATH'] = path
565 yield 577 yield
566 finally: 578 finally:
567 os.environ['PATH'] = saved_path 579 os.environ['PATH'] = saved_path
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698