diff options
author | George Burgess IV <gbiv@google.com> | 2022-05-17 00:46:39 -0700 |
---|---|---|
committer | Chromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com> | 2022-05-17 18:43:05 +0000 |
commit | 73c422a852cacbee86cb565e8c1812c2b97add3b (patch) | |
tree | f5016fe0ad211b4a6084770fbc046f9baa0d3918 | |
parent | 2cd85bd3afa88b6368d1b2c3052dc079b755a63b (diff) | |
download | toolchain-utils-73c422a852cacbee86cb565e8c1812c2b97add3b.tar.gz |
auto_delete_nightly_test_data: gracefully handle dirs not existing
To avoid cases like the attached bug, simply log when a dir doesn't
exist. Doesn't seem bad to consider this a success.
BUG=b:232843376
TEST=None
Change-Id: I291d1b1ca4007d2402a6707c83ce483470099c84
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/toolchain-utils/+/3651935
Auto-Submit: George Burgess <gbiv@chromium.org>
Reviewed-by: Jordan Abrahams-Whitehead <ajordanr@google.com>
Commit-Queue: Jordan Abrahams-Whitehead <ajordanr@google.com>
Tested-by: George Burgess <gbiv@chromium.org>
-rwxr-xr-x | auto_delete_nightly_test_data.py | 17 |
1 files changed, 13 insertions, 4 deletions
diff --git a/auto_delete_nightly_test_data.py b/auto_delete_nightly_test_data.py index ca721b24..b625783f 100755 --- a/auto_delete_nightly_test_data.py +++ b/auto_delete_nightly_test_data.py @@ -7,8 +7,6 @@ """A crontab script to delete night test data.""" -from __future__ import print_function - __author__ = 'shenhan@google.com (Han Shen)' import argparse @@ -27,6 +25,7 @@ from cros_utils import command_executer from cros_utils import constants from cros_utils import misc + DIR_BY_WEEKDAY = ('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun') NIGHTLY_TESTS_WORKSPACE = os.path.join(constants.CROSTC_WORKSPACE, 'nightly-tests') @@ -109,14 +108,24 @@ def ProcessArguments(argv): def RemoveAllSubdirsMatchingPredicate( base_dir: Path, days_to_preserve: int, dry_run: bool, - is_name_removal_worthy: Callable[[str], bool]) -> bool: + is_name_removal_worthy: Callable[[str], bool]) -> int: """Removes all subdirs of base_dir that match the given predicate.""" secs_to_preserve = 60 * 60 * 24 * days_to_preserve now = time.time() remove_older_than_time = now - secs_to_preserve + try: + dir_entries = list(base_dir.iterdir()) + except FileNotFoundError as e: + # We get this if the directory itself doesn't exist. Since we're cleaning + # tempdirs, that's as good as a success. Further, the prior approach here + # was using the `find` binary, which exits successfully if nothing is + # found. + print(f"Error enumerating {base_dir}'s contents; skipping removal: {e}") + return 0 + had_errors = False - for file in base_dir.iterdir(): + for file in dir_entries: if not is_name_removal_worthy(file.name): continue |