Chromium Code Reviews| Index: chrome/test/mini_installer/cleanup.py |
| diff --git a/chrome/test/mini_installer/cleanup.py b/chrome/test/mini_installer/cleanup.py |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..30ce172ebbe72e4ee3445b093262d82e76d8ae16 |
| --- /dev/null |
| +++ b/chrome/test/mini_installer/cleanup.py |
| @@ -0,0 +1,88 @@ |
| +# Copyright 2017 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| + |
| +import _winreg |
| +import os |
| +import shutil |
| + |
| +import file_verifier |
| +import registry_verifier |
| +import verifier_runner |
| + |
| +class FileCleaner(file_verifier.FileVerifier): |
| + """ Delete the directory if it exists but marked as shouldn't exist |
| + in the config file. |
| + """ |
| + def _Assert(self, is_true, message): |
| + """ Override verifyer.Verifier._Assert |
| + """ |
| + if not is_true and self._file_exists and os.path.isdir(self._file_path): |
| + shutil.rmtree(self._file_path, ignore_errors=True) |
| + |
| +class RegistryCleaner(registry_verifier.RegistryVerifier): |
| + """ Delete the registry key recursively if it exists and marked as forbidden |
| + in the config file. |
| + """ |
| + def _DeleteRegKey(self, root, key_path): |
| + """ Delete a registry key recursively. |
| + |
| + Args: |
| + root: An integer represents HKEY_ constant. |
| + key_path: A string represents the path of key that needs to be |
| + deleted. |
| + """ |
| + with _winreg.OpenKey(root, key_path, 0, _winreg.KEY_SET_VALUE | |
| + _winreg.KEY_READ | |
| + _winreg.KEY_WOW64_32KEY) as key: |
| + num_of_sub_keys, _, _ = _winreg.QueryInfoKey(key) |
| + for i in range(0, num_of_sub_keys): |
| + self._DeleteRegKey(root, key_path + "\\" + _winreg.EnumKey(key, i)) |
| + _winreg.DeleteKey(root, key_path) |
| + |
| + |
| + def _Assert(self, is_true, message): |
| + """ Override verifyer.Verifier._Assert |
| + """ |
| + if not is_true and self._is_key_forbidden: |
| + try: |
| + self._DeleteRegKey(self._root_key_value, self._sub_key) |
| + except WindowsError: |
| + # Suppress any delete error |
| + pass |
| + |
| +class CleanupRunner: |
| + """ Run cleaners based on the config state. |
| + """ |
| + _instance = None |
| + |
| + def __init__(self): |
| + self._cleaners = { |
| + verifier_runner.FILES: FileCleaner(), |
| + verifier_runner.REGISTRY_ENTRIES: RegistryCleaner(), |
| + } |
| + |
| + @classmethod |
| + def Create(cls): |
| + """Singleton constructor. |
| + """ |
| + if not cls._instance: |
| + cls._instance = CleanupRunner() |
| + return cls._instance |
| + |
| + def CleanAll(self, state, variable_expander): |
| + """ Put the machine to a clean state based on the config file. |
| + |
| + Only support: |
| + 1) Delete directory, always recursively. Delete file is NOT supported. |
| + 2) Delete registry key, always recursively. |
| + Args: |
| + state: A property dictionary reprsents the clean state in the |
| + config file. |
| + variable_expander: A VariableExpander object. |
| + """ |
| + for verifier_name, verifier_input in state.iteritems(): |
| + if verifier_name not in self._cleaners: |
| + continue |
| + 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.
|
| + variable_expander) |