Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(6076)

Unified Diff: chrome/test/mini_installer/cleaner_visitor.py

Issue 2747023002: Cleanup machine based on the state in configuration file for mini installer test.
Patch Set: refactor to use visitor pattern Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | chrome/test/mini_installer/entry.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/test/mini_installer/cleaner_visitor.py
diff --git a/chrome/test/mini_installer/cleaner_visitor.py b/chrome/test/mini_installer/cleaner_visitor.py
new file mode 100644
index 0000000000000000000000000000000000000000..b81909803ca61cf177d071297fc6320f893261fd
--- /dev/null
+++ b/chrome/test/mini_installer/cleaner_visitor.py
@@ -0,0 +1,50 @@
+# 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
+
+class CleanerVisitor(object):
+
+ def VisitFile(self, entry):
+ """ Delete the file if file is existed but shouldn't exist. Only support
+ Directory.
+ """
+ if entry.is_file_existed and not entry.should_file_exist and os.path.isdir(
+ entry.file_path):
+ shutil.rmtree(entry.file_path, ignore_errors=True)
+
+ def VisitRegistryKey(self, entry):
+ """ Reset the registry key to match the expectation.
+ """
+ if entry.key_handle is not None and entry.should_key_exist == 'forbidden':
+ try:
+ self._DeleteRegKey(entry.root_key_value, entry.sub_key)
+ except WindowsError:
+ # Suppress any delete error
+ pass
+
+
+ def VisitRegistryValue(self, entry):
+ pass
+
+ def VisitProcess(self, entry):
+ pass
+
+ 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)
« no previous file with comments | « no previous file | chrome/test/mini_installer/entry.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698