Chromium Code Reviews| 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 _winreg | |
| 6 import os | |
| 7 import shutil | |
| 8 | |
| 9 import file_verifier | |
| 10 import registry_verifier | |
| 11 import verifier_runner | |
| 12 | |
| 13 class FileCleaner(file_verifier.FileVerifier): | |
| 14 """ Delete the directory if it exists but marked as shouldn't exist | |
| 15 in the config file. | |
| 16 """ | |
| 17 def _Assert(self, is_true, message): | |
| 18 """ Override verifyer.Verifier._Assert | |
| 19 """ | |
| 20 if not is_true and self._file_exists and os.path.isdir(self._file_path): | |
| 21 shutil.rmtree(self._file_path, ignore_errors=True) | |
| 22 | |
| 23 class RegistryCleaner(registry_verifier.RegistryVerifier): | |
| 24 """ Delete the registry key recursively if it exists and marked as forbidden | |
| 25 in the config file. | |
| 26 """ | |
| 27 def _DeleteRegKey(self, root, key_path): | |
| 28 """ Delete a registry key recursively. | |
| 29 | |
| 30 Args: | |
| 31 root: An integer represents HKEY_ constant. | |
| 32 key_path: A string represents the path of key that needs to be | |
| 33 deleted. | |
| 34 """ | |
| 35 with _winreg.OpenKey(root, key_path, 0, _winreg.KEY_SET_VALUE | | |
| 36 _winreg.KEY_READ | | |
| 37 _winreg.KEY_WOW64_32KEY) as key: | |
| 38 num_of_sub_keys, _, _ = _winreg.QueryInfoKey(key) | |
| 39 for i in range(0, num_of_sub_keys): | |
| 40 self._DeleteRegKey(root, key_path + "\\" + _winreg.EnumKey(key, i)) | |
| 41 _winreg.DeleteKey(root, key_path) | |
| 42 | |
| 43 | |
| 44 def _Assert(self, is_true, message): | |
| 45 """ Override verifyer.Verifier._Assert | |
| 46 """ | |
| 47 if not is_true and self._is_key_forbidden: | |
| 48 try: | |
| 49 self._DeleteRegKey(self._root_key_value, self._sub_key) | |
| 50 except WindowsError: | |
| 51 # Suppress any delete error | |
| 52 pass | |
| 53 | |
| 54 class CleanupRunner: | |
| 55 """ Run cleaners based on the config state. | |
| 56 """ | |
| 57 _instance = None | |
| 58 | |
| 59 def __init__(self): | |
| 60 self._cleaners = { | |
| 61 verifier_runner.FILES: FileCleaner(), | |
| 62 verifier_runner.REGISTRY_ENTRIES: RegistryCleaner(), | |
| 63 } | |
| 64 | |
| 65 @classmethod | |
| 66 def Create(cls): | |
| 67 """Singleton constructor. | |
| 68 """ | |
| 69 if not cls._instance: | |
| 70 cls._instance = CleanupRunner() | |
| 71 return cls._instance | |
| 72 | |
| 73 def CleanAll(self, state, variable_expander): | |
| 74 """ Put the machine to a clean state based on the config file. | |
| 75 | |
| 76 Only support: | |
| 77 1) Delete directory, always recursively. Delete file is NOT supported. | |
| 78 2) Delete registry key, always recursively. | |
| 79 Args: | |
| 80 state: A property dictionary reprsents the clean state in the | |
| 81 config file. | |
| 82 variable_expander: A VariableExpander object. | |
| 83 """ | |
| 84 for verifier_name, verifier_input in state.iteritems(): | |
| 85 if verifier_name not in self._cleaners: | |
| 86 continue | |
| 87 self._cleaners[verifier_name].VerifyInput(verifier_input, | |
|
grt (UTC plus 2)
2017/03/14 09:19:44
rather than using a Verifier to do the cleaning, i
zmin
2017/03/14 23:13:00
Ok, I'll refactor the code.
| |
| 88 variable_expander) | |
| OLD | NEW |