OLD | NEW |
(Empty) | |
| 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 |
| 5 import collections |
| 6 import os |
| 7 import sys |
| 8 import time |
| 9 |
| 10 from .type_definitions import Handler |
| 11 from .serialize import WriteNewData, DiffData, NonExistant, GetCurrentData |
| 12 |
| 13 |
| 14 DirSeen = collections.namedtuple('DirSeen', 'dir') |
| 15 ForcedWriteAction = collections.namedtuple('ForcedWriteAction', 'test') |
| 16 DiffWriteAction = collections.namedtuple('DiffWriteAction', 'test') |
| 17 SchemaDiffWriteAction = collections.namedtuple('SchemaDiffWriteAction', 'test') |
| 18 MissingWriteAction = collections.namedtuple('MissingWriteAction', 'test') |
| 19 NoAction = collections.namedtuple('NoAction', 'test') |
| 20 |
| 21 |
| 22 class TrainHandler(Handler): |
| 23 """Write test expectations to disk.""" |
| 24 @classmethod |
| 25 def add_options(cls, parser): |
| 26 parser.add_argument( |
| 27 '--force', action='store_true', help=( |
| 28 'Immediately write expectations to disk instead of determining if ' |
| 29 'they contain a diff from the current expectations.' |
| 30 )) |
| 31 |
| 32 @classmethod |
| 33 def gen_stage_loop(cls, _opts, tests, put_next_stage, put_result_stage): |
| 34 dirs_seen = set() |
| 35 for test in tests: |
| 36 if test.expect_dir not in dirs_seen: |
| 37 try: |
| 38 os.makedirs(test.expect_dir) |
| 39 except OSError: |
| 40 pass |
| 41 put_result_stage(DirSeen(test.expect_dir)) |
| 42 dirs_seen.add(test.expect_dir) |
| 43 put_next_stage(test) |
| 44 |
| 45 @classmethod |
| 46 def run_stage_loop(cls, opts, tests_results, put_next_stage): |
| 47 for test, result in tests_results: |
| 48 if opts.force: |
| 49 WriteNewData(test, result.data) |
| 50 put_next_stage(ForcedWriteAction(test)) |
| 51 return |
| 52 |
| 53 current, same_schema = GetCurrentData(test) |
| 54 diff = DiffData(current, result.data) |
| 55 if diff is not None or not same_schema: |
| 56 WriteNewData(test, result.data) |
| 57 if current is NonExistant: |
| 58 put_next_stage(MissingWriteAction(test)) |
| 59 elif diff: |
| 60 put_next_stage(DiffWriteAction(test)) |
| 61 else: |
| 62 put_next_stage(SchemaDiffWriteAction(test)) |
| 63 else: |
| 64 put_next_stage(NoAction(test)) |
| 65 |
| 66 class ResultStageHandler(Handler.ResultStageHandler): |
| 67 def __init__(self, opts): |
| 68 super(TrainHandler.ResultStageHandler, self).__init__(opts) |
| 69 self.dirs_seen = set() |
| 70 self.files_expected = collections.defaultdict(set) |
| 71 self.start = time.time() |
| 72 self.num_tests = 0 |
| 73 self.verbose_actions = [] |
| 74 self.normal_actions = [] |
| 75 |
| 76 def _record_expected(self, test, indicator): |
| 77 if not self.opts.quiet: |
| 78 sys.stdout.write(indicator) |
| 79 sys.stdout.flush() |
| 80 head, tail = os.path.split(test.expect_path()) |
| 81 self.files_expected[head].add(tail) |
| 82 |
| 83 def _record_write(self, test, indicator, why): |
| 84 self._record_expected(test, indicator) |
| 85 name = test.expect_path() if self.opts.verbose else test.name |
| 86 self.normal_actions.append('Wrote %s: %s' % (name, why)) |
| 87 |
| 88 def handle_DirSeen(self, dirseen): |
| 89 self.dirs_seen.add(dirseen.dir) |
| 90 |
| 91 def handle_NoAction(self, result): |
| 92 self._record_expected(result.test, '.') |
| 93 self.verbose_actions.append('%s did not change' % result.test.name) |
| 94 |
| 95 def handle_ForcedWriteAction(self, result): |
| 96 self._record_write(result.test, 'F', 'forced') |
| 97 |
| 98 def handle_DiffWriteAction(self, result): |
| 99 self._record_write(result.test, 'D', 'diff') |
| 100 |
| 101 def handle_SchemaDiffWriteAction(self, result): |
| 102 self._record_write(result.test, 'S', 'schema changed') |
| 103 |
| 104 def handle_MissingWriteAction(self, result): |
| 105 self._record_write(result.test, 'M', 'missing') |
| 106 |
| 107 def finalize(self, aborted): |
| 108 if not aborted and not self.opts.test_glob: |
| 109 for d in self.dirs_seen: |
| 110 expected = self.files_expected[d] |
| 111 for f in os.listdir(d): |
| 112 if f == 'OWNERS': |
| 113 continue |
| 114 if f not in expected: |
| 115 path = os.path.join(d, f) |
| 116 os.unlink(path) |
| 117 if self.opts.verbose: |
| 118 print 'Removed unexpected file', path |
| 119 |
| 120 if not self.opts.quiet: |
| 121 print |
| 122 |
| 123 if self.normal_actions: |
| 124 print '\n'.join(self.normal_actions) |
| 125 if self.opts.verbose: |
| 126 if self.verbose_actions: |
| 127 print '\n'.join(self.verbose_actions) |
| 128 |
| 129 num_tests = sum(len(x) for x in self.files_expected.itervalues()) |
| 130 print '-' * 70 |
| 131 print 'Trained %d tests in %0.3fs' % ( |
| 132 num_tests, time.time() - self.start) |
| 133 |
| 134 |
| 135 |
OLD | NEW |