| # Copyright 2020 The Fuchsia Authors. All rights reserved. |
| # Use of this source code is governed by a BSD-style license that can be |
| # found in the LICENSE file. |
| |
| from contextlib import contextmanager |
| from recipe_engine import recipe_api |
| |
| |
| class CacheApi(recipe_api.RecipeApi): |
| @contextmanager |
| def guard(self, cache): |
| """Context wrapping Swarming named cache writes.""" |
| cache_path = self.m.path["cache"].join(cache) |
| cache_guard_file = cache_path.join(".GUARD_FILE") |
| if self.m.path.exists(cache_guard_file): |
| self.m.file.rmcontents( |
| name="%s cache corrupted; purging" % cache, source=cache_path |
| ) |
| self.m.file.write_raw(name="write guard file", dest=cache_guard_file, data="") |
| try: |
| yield |
| # This block is covered by git.cache_failure but recipe_engine says its not. |
| except: # pragma: no cover |
| self.m.file.rmcontents( |
| name="%s cache corrupted; purging" % cache, source=cache_path |
| ) |
| raise |
| finally: |
| self.m.file.remove(name="remove guard file", source=cache_guard_file) |