OLD | NEW |
(Empty) | |
| 1 # Copyright 2017 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 re |
| 6 |
| 7 from file import File |
| 8 from registry import Registry |
| 9 from process import Process |
| 10 |
| 11 class StateWalker(object): |
| 12 _TYPE_TO_ENTRY = { |
| 13 'Files': File, |
| 14 'Processes': Process, |
| 15 'RegistryEntries': Registry, |
| 16 } |
| 17 |
| 18 def __init__(self, visitor): |
| 19 self._visitor = visitor |
| 20 |
| 21 def Walk(self, variable_expander, property_dict): |
| 22 """Traverses |property_dict|, fetch data from OS and invoke |_visitor| in |
| 23 the end.""" |
| 24 for property_name, property_value in property_dict.iteritems(): |
| 25 if property_name not in StateWalker._TYPE_TO_ENTRY: |
| 26 raise KeyError('Unknown verifier %s' % property_name) |
| 27 for entry_name, entry_value, in property_value.iteritems(): |
| 28 # Skip over expectations with conditions that aren't satisfied. |
| 29 if 'condition' in property_value and not self._EvaluateCondition( |
| 30 variable_expander.Expand(entry_value['condition'])): |
| 31 continue |
| 32 entry = StateWalker._TYPE_TO_ENTRY[property_name](variable_expander, |
| 33 self._visitor) |
| 34 entry.Check(entry_name, entry_value) |
| 35 |
| 36 def _EvaluateCondition(self, condition): |
| 37 """Evaluates |condition| using eval(). |
| 38 |
| 39 Args: |
| 40 condition: A condition string. |
| 41 |
| 42 Returns: |
| 43 The result of the evaluated condition. |
| 44 """ |
| 45 return eval(condition, {'__builtins__': {'False': False, 'True': True}}) |
OLD | NEW |