| 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 os |
| 6 import shutil |
| 7 |
| 8 import visitor |
| 9 |
| 10 class FileVisitor(visitor.Visitor): |
| 11 """ Visitor for file entry in property |
| 12 """ |
| 13 def _VisitEntry(self, entry_name, entry_value): |
| 14 """ Check whether a file is existed or not. |
| 15 |
| 16 Args: |
| 17 'entry_name' - A string represents the path to the file being |
| 18 verified. |
| 19 'entry_value' - A dictionary with the following key and value: |
| 20 'exists': a boolean indicating whether the file should |
| 21 exist. |
| 22 """ |
| 23 file_path = self._variable_expander.Expand(entry_name) |
| 24 is_file_exists = os.path.exists(file_path) |
| 25 should_file_exist = entry_value['exists'] |
| 26 self._HandleFile(file_path, is_file_exists, should_file_exist) |
| 27 |
| 28 def _HandleFile(self, file_path, is_file_exists, should_file_exist): |
| 29 """ Abstract function to check a file." |
| 30 |
| 31 Args: |
| 32 file_path: A string represents the path of the file. |
| 33 is_file_exists: A boolean represents whether the file existed. |
| 34 should_file_exist: A boolean represents whether the file should exist. |
| 35 """ |
| 36 raise NotImplementedError() |
| 37 |
| 38 |
| 39 class FileVerifier(FileVisitor): |
| 40 """Verifies that the current files match the expectation dictionaries.""" |
| 41 def _HandleFile(self, file_path, is_file_exists, should_file_exist): |
| 42 """ Overridden FileVisitor._HandleFile. |
| 43 |
| 44 Throw AssertionError if |is_file_exists| doesn't match |
| 45 |should_file_exist|. |
| 46 """ |
| 47 error_message = "" |
| 48 if is_file_exists: |
| 49 error_message = 'File %s exists' % file_path |
| 50 else: |
| 51 error_message = 'File %s is missing' % file_path |
| 52 assert should_file_exist == is_file_exists, error_message |
| 53 |
| 54 class FileCleaner(FileVisitor): |
| 55 """ Reset the current file system state to match the expectation. |
| 56 """ |
| 57 def _HandleFile(self, file_path, is_file_exists, should_file_exist): |
| 58 """ Overridden FileVisitor._HandleFile. |
| 59 |
| 60 Delete the file if file is existed but shouldn't exist. Only support |
| 61 Directory. |
| 62 """ |
| 63 if is_file_exists and not should_file_exist and os.path.isdir(file_path): |
| 64 shutil.rmtree(file_path, ignore_errors=True) |
| OLD | NEW |