| OLD | NEW |
| 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 431 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 input_phs = collections.defaultdict(lambda: collections.defaultdict(list)) | 451 input_phs = collections.defaultdict(lambda: collections.defaultdict(list)) |
| 452 output_phs = collections.defaultdict(lambda: collections.defaultdict(list)) | 452 output_phs = collections.defaultdict( |
| 453 lambda: collections.defaultdict(collections.OrderedDict)) |
| 453 new_cmd = [] | 454 new_cmd = [] |
| 454 for item in step.get('cmd', []): | 455 for item in step.get('cmd', []): |
| 455 if isinstance(item, util.Placeholder): | 456 if isinstance(item, util.Placeholder): |
| 456 module_name, placeholder_name = item.name_pieces | 457 module_name, placeholder_name = item.namespaces |
| 457 if isinstance(item, util.InputPlaceholder): | 458 if isinstance(item, util.InputPlaceholder): |
| 458 tdata = step_test.pop_input_placeholder(item.name_pieces) | 459 tdata = step_test.pop_input_placeholder(module_name, placeholder_name) |
| 459 input_phs[module_name][placeholder_name].append((item, tdata)) | 460 input_phs[module_name][placeholder_name].append((item, tdata)) |
| 460 else: | 461 else: |
| 461 assert isinstance(item, util.OutputPlaceholder), ( | 462 assert isinstance(item, util.OutputPlaceholder), ( |
| 462 'Not an OutputPlaceholder: %r' % item) | 463 'Not an OutputPlaceholder: %r' % item) |
| 463 tdata = step_test.pop_output_placeholder(item.name_pieces) | 464 # This assert also ensures at most one placeholder has the default name. |
| 464 output_phs[module_name][placeholder_name].append((item, tdata)) | 465 assert item.name not in output_phs[module_name][placeholder_name], ( |
| 466 'Step "%s" has multiple output placeholders of %s.%s. Please ' |
| 467 'specify explicit and different names for them.' % ( |
| 468 step['name'], module_name, placeholder_name)) |
| 469 tdata = step_test.pop_output_placeholder( |
| 470 module_name, placeholder_name, item.name) |
| 471 output_phs[module_name][placeholder_name][item.name] = (item, tdata) |
| 465 new_cmd.extend(item.render(tdata)) | 472 new_cmd.extend(item.render(tdata)) |
| 466 else: | 473 else: |
| 467 new_cmd.append(item) | 474 new_cmd.append(item) |
| 468 rendered_step['cmd'] = new_cmd | 475 rendered_step['cmd'] = new_cmd |
| 469 | 476 |
| 470 # Process 'stdout', 'stderr' and 'stdin' placeholders, if given. | 477 # Process 'stdout', 'stderr' and 'stdin' placeholders, if given. |
| 471 stdio_placeholders = {} | 478 stdio_placeholders = {} |
| 472 for key in ('stdout', 'stderr', 'stdin'): | 479 for key in ('stdout', 'stderr', 'stdin'): |
| 473 placeholder = step.get(key) | 480 placeholder = step.get(key) |
| 474 tdata = None | 481 tdata = None |
| (...skipping 30 matching lines...) Expand all Loading... |
| 505 for _, items in pholders.iteritems(): | 512 for _, items in pholders.iteritems(): |
| 506 for ph, td in items: | 513 for ph, td in items: |
| 507 ph.result(step_result.presentation, td) | 514 ph.result(step_result.presentation, td) |
| 508 | 515 |
| 509 # Output placeholders inside step |cmd|. | 516 # Output placeholders inside step |cmd|. |
| 510 for module_name, pholders in placeholders.outputs_cmd.iteritems(): | 517 for module_name, pholders in placeholders.outputs_cmd.iteritems(): |
| 511 assert not hasattr(step_result, module_name) | 518 assert not hasattr(step_result, module_name) |
| 512 o = BlankObject() | 519 o = BlankObject() |
| 513 setattr(step_result, module_name, o) | 520 setattr(step_result, module_name, o) |
| 514 | 521 |
| 515 for placeholder_name, items in pholders.iteritems(): | 522 for placeholder_name, instances in pholders.iteritems(): |
| 516 lst = [ph.result(step_result.presentation, td) for ph, td in items] | 523 named_results = {} |
| 517 setattr(o, placeholder_name+"_all", lst) | 524 for _, (ph, td) in instances.iteritems(): |
| 518 setattr(o, placeholder_name, lst[0]) | 525 named_results[ph.name] = ph.result(step_result.presentation, td) |
| 526 setattr(o, placeholder_name + "s", named_results) |
| 527 |
| 528 default_result = None |
| 529 if len(named_results) == 1: # Only 1 output placeholder. |
| 530 default_result = named_results.values()[0] |
| 531 else: |
| 532 # 2+ output placeholder, check for the one without an explicit name. |
| 533 for ph, _ in instances.values(): |
| 534 if ph.name is None: |
| 535 default_result = named_results[ph.name] |
| 536 break |
| 537 setattr(o, placeholder_name, default_result) |
| 519 | 538 |
| 520 # Placeholders that are used with IO redirection. | 539 # Placeholders that are used with IO redirection. |
| 521 for key in ('stdout', 'stderr', 'stdin'): | 540 for key in ('stdout', 'stderr', 'stdin'): |
| 522 assert not hasattr(step_result, key) | 541 assert not hasattr(step_result, key) |
| 523 ph, td = getattr(placeholders, key) | 542 ph, td = getattr(placeholders, key) |
| 524 result = ph.result(step_result.presentation, td) if ph else None | 543 result = ph.result(step_result.presentation, td) if ph else None |
| 525 if key != 'stdin': | 544 if key != 'stdin': |
| 526 setattr(step_result, key, result) | 545 setattr(step_result, key, result) |
| 527 | 546 |
| 528 return step_result | 547 return step_result |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 577 supplied command, and only uses the |env| kwarg for modifying the environment | 596 supplied command, and only uses the |env| kwarg for modifying the environment |
| 578 of the child process. | 597 of the child process. |
| 579 """ | 598 """ |
| 580 saved_path = os.environ['PATH'] | 599 saved_path = os.environ['PATH'] |
| 581 try: | 600 try: |
| 582 if path is not None: | 601 if path is not None: |
| 583 os.environ['PATH'] = path | 602 os.environ['PATH'] = path |
| 584 yield | 603 yield |
| 585 finally: | 604 finally: |
| 586 os.environ['PATH'] = saved_path | 605 os.environ['PATH'] = saved_path |
| OLD | NEW |