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 time |
| 6 import os |
| 7 import collections |
| 8 |
| 9 from .types import Handler |
| 10 from .serialize import WriteNewData, DiffData, NonExistant, GetCurrentData |
| 11 |
| 12 |
| 13 DirSeen = collections.namedtuple('DirSeen', 'dir') |
| 14 WriteAction = collections.namedtuple('WriteAction', 'test why') |
| 15 NoAction = collections.namedtuple('NoAction', 'test') |
| 16 |
| 17 |
| 18 class TrainHandler(Handler): |
| 19 """Write test expectations to disk.""" |
| 20 @classmethod |
| 21 def add_options(cls, parser): |
| 22 parser.add_argument( |
| 23 '--force', action='store_true', help=( |
| 24 'Immediately write expectations to disk instead of determining if ' |
| 25 'they contain a diff from the current expectations.' |
| 26 )) |
| 27 |
| 28 @classmethod |
| 29 def gen_stage_loop(cls, _opts, tests, put_next_stage, put_result_stage): |
| 30 dirs_seen = set() |
| 31 for test in tests: |
| 32 if test.expect_dir not in dirs_seen: |
| 33 try: |
| 34 os.makedirs(test.expect_dir) |
| 35 except OSError: |
| 36 pass |
| 37 put_result_stage(DirSeen(test.expect_dir)) |
| 38 dirs_seen.add(test.expect_dir) |
| 39 put_next_stage(test) |
| 40 |
| 41 @classmethod |
| 42 def run_stage_loop(cls, opts, tests_results, put_next_stage): |
| 43 for test, result in tests_results: |
| 44 if opts.force: |
| 45 WriteNewData(test, result.data) |
| 46 put_next_stage(WriteAction(test, 'forced')) |
| 47 return |
| 48 |
| 49 current, same_schema = GetCurrentData(test) |
| 50 diff = DiffData(current, result.data) |
| 51 if diff is not None or not same_schema: |
| 52 WriteNewData(test, result.data) |
| 53 if current is NonExistant: |
| 54 why = 'missing' |
| 55 elif diff: |
| 56 why = 'diff' |
| 57 else: |
| 58 why = 'schema changed' |
| 59 put_next_stage(WriteAction(test, why)) |
| 60 else: |
| 61 put_next_stage(NoAction(test)) |
| 62 |
| 63 class ResultStageHandler(Handler.ResultStageHandler): |
| 64 def __init__(self, opts): |
| 65 super(TrainHandler.ResultStageHandler, self).__init__(opts) |
| 66 self.dirs_seen = set() |
| 67 self.files_expected = collections.defaultdict(set) |
| 68 self.start = time.time() |
| 69 self.num_tests = 0 |
| 70 |
| 71 def _record_expected(self, test): |
| 72 head, tail = os.path.split(test.expect_path()) |
| 73 self.files_expected[head].add(tail) |
| 74 |
| 75 # Handlers |
| 76 def DirSeen(self, dirseen): |
| 77 self.dirs_seen.add(dirseen.dir) |
| 78 |
| 79 def NoAction(self, result): |
| 80 self._record_expected(result.test) |
| 81 if self.opts.verbose: |
| 82 print '%s did not change' % result.test.name |
| 83 |
| 84 def WriteAction(self, result): |
| 85 self._record_expected(result.test) |
| 86 if not self.opts.quiet: |
| 87 test = result.test |
| 88 name = test.expect_path() if self.opts.verbose else test.name |
| 89 print 'Wrote %s: %s' % (name, result.why) |
| 90 |
| 91 def finalize(self, aborted): |
| 92 if not aborted and not self.opts.test_glob: |
| 93 for d in self.dirs_seen: |
| 94 expected = self.files_expected[d] |
| 95 for f in os.listdir(d): |
| 96 if f == 'OWNERS': |
| 97 continue |
| 98 if f not in expected: |
| 99 path = os.path.join(d, f) |
| 100 os.unlink(path) |
| 101 if self.opts.verbose: |
| 102 print 'Removed unexpected file', path |
| 103 if not self.opts.quiet: |
| 104 num_tests = sum(len(x) for x in self.files_expected.itervalues()) |
| 105 print 'Trained %d tests in %0.3fs' % ( |
| 106 num_tests, time.time() - self.start) |
| 107 |
| 108 |
| 109 |
OLD | NEW |