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 import file_visitor | |
8 import process_visitor | |
9 import registry_visitor | |
10 | |
11 class VisitorRunner(object): | |
12 """Run all visitor based on a state's property dictionary. | |
13 | |
14 A property dictionary is a dictionary where each key is a verifier's name | |
15 and the associated value is the input to that verifier. For details about | |
16 the input format for each verifier, take a look at http://goo.gl/1P85WL | |
17 | |
18 All visitor's name should match the name of property. For example, visitor | |
19 for property "RegistryEntries" will be "registry_entries_visitor". | |
20 """ | |
21 def __init__(self): | |
22 self._property_name_match = {} | |
23 | |
24 def Run(self, state): | |
25 """ Walk through all properties of the state. | |
26 | |
27 Args: | |
28 state: A property dictionary belongs to a state. | |
29 """ | |
30 for property_name, property_value in state.iteritems(): | |
31 visitor_name = self._Convert(property_name) + '_visitor' | |
grt (UTC plus 2)
2017/03/15 09:00:08
i don't understand this part -- why is there a per
| |
32 if not hasattr(self, visitor_name): | |
33 self._HandleInvalidProperty(property_name) | |
34 else: | |
35 getattr(self, visitor_name).Traverse(property_value) | |
36 | |
37 | |
38 | |
39 def _HandleInvalidProperty(self, property_name): | |
40 """ Abstract class. | |
41 | |
42 Handle the situation that the property in config file does not supported | |
43 by the runner. | |
44 | |
45 Args: | |
46 property_name: A string represents the unsupported property's name. | |
47 """ | |
48 pass | |
49 | |
50 def _Convert(self, input_str): | |
51 """Convert CamelCase property name in config file into snake_case for | |
52 reflection | |
53 """ | |
54 if input_str not in self._property_name_match: | |
55 output_str = re.sub('([A-Z][a-z]*)', r'_\1', input_str).lower()[1:] | |
56 self._property_name_match[input_str] = output_str | |
57 return self._property_name_match[input_str] | |
58 | |
59 | |
60 class VerifierRunner(VisitorRunner): | |
61 """Run all verifing visitor to verify that the current machine states match | |
62 the property dictionary. | |
63 """ | |
64 | |
65 def __init__(self, variable_expander): | |
66 """Constructor.""" | |
67 # TODO(sukolsak): Implement other verifiers | |
68 super(VerifierRunner, self).__init__() | |
69 self.files_visitor = file_visitor.FileVerifier(variable_expander) | |
70 self.registry_entries_visitor = registry_visitor.RegistryVerifier( | |
71 variable_expander) | |
72 self.processes_visitor = process_visitor.ProcessVerifier(variable_expander) | |
73 | |
74 def _HandleInvalidProperty(self, property_name): | |
75 """ Overridden VisitorRunner._HandleInvalidProperty, unsupported | |
76 property raises KeyError. | |
77 """ | |
78 raise KeyError('Unknown verifier %s' % property_name) | |
79 | |
80 | |
81 class CleanupRunner(VisitorRunner): | |
82 """ Run all cleaning visitor based on the config state to reset the machine to | |
83 a clean state. | |
84 | |
85 Only support: | |
86 1) Delete directory, always recursively. Delete file is NOT supported. | |
87 2) Delete registry key, always recursively. | |
88 """ | |
89 | |
90 _instance = None | |
91 | |
92 def __init__(self, variable_expander): | |
93 """Constructor.""" | |
94 # TODO(sukolsak): Implement other verifiers | |
95 super(CleanupRunner, self).__init__() | |
96 self.files_visitor = file_visitor.FileCleaner(variable_expander) | |
97 self.registry_entries_visitor = registry_visitor.RegistryCleaner( | |
98 variable_expander) | |
99 | |
100 | |
101 @classmethod | |
102 def Create(cls, variable_expander): | |
103 """Singleton constructor. | |
104 """ | |
105 if not cls._instance: | |
106 cls._instance = CleanupRunner(variable_expander) | |
107 return cls._instance | |
108 | |
109 def _HandleInvalidProperty(self, property_name): | |
110 """ Overridden VisitorRunner._HandleInvalidProperty, ignore unsupported | |
111 state's property. | |
112 """ | |
113 return | |
OLD | NEW |