aboutsummaryrefslogtreecommitdiff
path: root/auto_delete_nightly_test_data.py
diff options
context:
space:
mode:
authorGeorge Burgess IV <gbiv@google.com>2022-04-25 16:07:20 -0700
committerChromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com>2022-04-26 00:41:43 +0000
commit42ef7387e95de2e97cd109194dbe1b24f88d4ae5 (patch)
treedde8c227aa537713f912f42f5ee6d57c8abe45d5 /auto_delete_nightly_test_data.py
parent64f87754560bc7e99fb19a74c4f693e586977687 (diff)
downloadtoolchain-utils-42ef7387e95de2e97cd109194dbe1b24f88d4ae5.tar.gz
auto_delete_nightly_test_data: refactor tmp file cleaning
This function fails pretty often, and its output is very unhelpful (similar quality to "something somewhere failed"). This turns the function into Python code, and makes it error out more gracefully. BUG=b:230201673 TEST=Ran on chrotomation without issue. Change-Id: I57397c7a19a86aa62d5320ae35ca72615115ba35 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/toolchain-utils/+/3607074 Reviewed-by: Jordan Abrahams-Whitehead <ajordanr@google.com> Commit-Queue: George Burgess <gbiv@chromium.org> Auto-Submit: George Burgess <gbiv@chromium.org> Tested-by: George Burgess <gbiv@chromium.org>
Diffstat (limited to 'auto_delete_nightly_test_data.py')
-rwxr-xr-xauto_delete_nightly_test_data.py79
1 files changed, 59 insertions, 20 deletions
diff --git a/auto_delete_nightly_test_data.py b/auto_delete_nightly_test_data.py
index cc17f7d4..a79b6cf9 100755
--- a/auto_delete_nightly_test_data.py
+++ b/auto_delete_nightly_test_data.py
@@ -17,8 +17,11 @@ import os
import re
import shlex
import shutil
+import stat
import sys
import time
+import traceback
+from pathlib import Path
from cros_utils import command_executer, constants, misc
@@ -102,31 +105,67 @@ def ProcessArguments(argv):
return options
+def IsChromeOsTmpDeletionCandidate(file_name: str):
+ """Returns whether the given basename can be deleted from a chroot's /tmp."""
+ name_prefixes = (
+ 'test_that_',
+ 'cros-update',
+ 'CrAU_temp_data',
+ )
+ if any(file_name.startswith(x) for x in name_prefixes):
+ return True
+ # Remove files that look like `tmpABCDEFGHI`.
+ return len(file_name) == 9 and file_name.startswith('tmp')
+
+
def CleanChromeOsTmpFiles(chroot_tmp, days_to_preserve, dry_run):
- rv = 0
- ce = command_executer.GetCommandExecuter()
# Clean chroot/tmp/test_that_* and chroot/tmp/tmpxxxxxx, that were last
# accessed more than specified time.
- minutes = 1440 * days_to_preserve
- cmd = (r'find {0} -maxdepth 1 -type d '
- r'\( -name "test_that_*" -amin +{1} -o '
- r' -name "cros-update*" -amin +{1} -o '
- r' -name "CrAU_temp_data*" -amin +{1} -o '
- r' -regex "{0}/tmp......" -amin +{1} \) '
- r'-exec bash -c "echo rm -fr {{}}" \; '
- r'-exec bash -c "rm -fr {{}}" \;').format(chroot_tmp, minutes)
- if dry_run:
- print('Going to execute:\n%s' % cmd)
- else:
- rv = ce.RunCommand(cmd, print_to_console=False)
- if rv == 0:
- print('Successfully cleaned chromeos tree tmp directory '
- '"{0}".'.format(chroot_tmp))
+ secs_to_preserve = 60 * 60 * 24 * days_to_preserve
+ now = time.time()
+ remove_older_than_time = now - secs_to_preserve
+
+ had_errors = False
+ for file in Path(chroot_tmp).iterdir():
+ if not IsChromeOsTmpDeletionCandidate(file.name):
+ continue
+
+ try:
+ # Take the stat here and use that later, so we only need to check for a
+ # nonexistent file once.
+ st = file.stat()
+ except FileNotFoundError:
+ # This was deleted while were checking; ignore it.
+ continue
+
+ if not stat.S_ISDIR(st.st_mode):
+ continue
+
+ if st.st_atime >= remove_older_than_time:
+ continue
+
+ if dry_run:
+ print(f'Would remove {file}')
+ continue
+
+ this_iteration_had_errors = False
+
+ def OnError(_func, path_name, excinfo):
+ nonlocal this_iteration_had_errors
+ this_iteration_had_errors = True
+ print(f'Failed removing path at {path_name}; traceback:')
+ traceback.print_exception(*excinfo)
+
+ shutil.rmtree(file, onerror=OnError)
+
+ # Some errors can be other processes racing with us to delete things. Don't
+ # count those as an error which we complain loudly about.
+ if this_iteration_had_errors and file.exists():
+ had_errors = True
else:
- print('Some directories were not removed under chromeos tree '
- 'tmp directory -"{0}".'.format(chroot_tmp))
+ print(f'Discarding removal errors for {file}; dir was still removed.')
- return rv
+ return 1 if had_errors else 0
def CleanChromeOsImageFiles(chroot_tmp, subdir_suffix, days_to_preserve,