Update tmp cleanup on chrotomation3

Remove "*-llvm-next-nightly" images from chroot /tmp.
Updated CleanChromeOsImageFiles which now deletes directories with
rmtree. This should fix the problem with cron job auto delete.

BUG=None
TEST=Tested with --dry_run on chrotomation3.

Change-Id: If8458540e009c50dec829f14c47581fdd7be305e
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/toolchain-utils/+/2202728
Tested-by: Denis Nikitin <denik@chromium.org>
Reviewed-by: Manoj Gupta <manojgupta@chromium.org>
diff --git a/auto_delete_nightly_test_data.py b/auto_delete_nightly_test_data.py
index 4ec5b0f..e40b95c 100755
--- a/auto_delete_nightly_test_data.py
+++ b/auto_delete_nightly_test_data.py
@@ -15,7 +15,9 @@
 import datetime
 import os
 import re
+import shutil
 import sys
+import time
 
 from cros_utils import command_executer
 from cros_utils import constants
@@ -134,30 +136,33 @@
 
 def CleanChromeOsImageFiles(chroot_tmp, subdir_suffix, days_to_preserve,
                             dry_run):
-  rv = 0
-  rv2 = 0
-  ce = command_executer.GetCommandExecuter()
-  minutes = 1440 * days_to_preserve
   # Clean files that were last accessed more than the specified time.
-  rv2 = 0
-  cmd = (r'find {0}/*{1}/* -maxdepth 1 -type d '
-         r'-amin +{2} '
-         r'-exec bash -c "echo rm -fr {{}}" \; '
-         r'-exec bash -c "rm -fr {{}}" \;').format(chroot_tmp, subdir_suffix,
-                                                   minutes)
-  if dry_run:
-    print('Going to execute:\n%s' % cmd)
-  else:
-    rv2 = ce.RunCommand(cmd, print_to_console=False)
-    if rv2 == 0:
-      print('Successfully cleaned chromeos image autotest directories from '
-            '"{0}/*{1}".'.format(chroot_tmp, subdir_suffix))
-    else:
-      print('Some image autotest directories were not removed from '
-            '"{0}/*{1}".'.format(chroot_tmp, subdir_suffix))
+  seconds_delta = days_to_preserve * 24 * 3600
+  now = time.time()
+  errors = 0
 
-  rv += rv2
-  return rv
+  for tmp_dir in os.listdir(chroot_tmp):
+    # Directory under /tmp
+    tmp_dir = os.path.join(chroot_tmp, tmp_dir)
+    if tmp_dir.endswith(subdir_suffix):
+      # Tmp directory which ends with subdir_suffix.
+      for subdir in os.listdir(tmp_dir):
+        # Subdirectories targeted for deletion.
+        subdir_path = os.path.join(tmp_dir, subdir)
+        if now - os.path.getatime(subdir_path) > seconds_delta:
+          if dry_run:
+            print('Will run:\nshutil.rmtree({})'.format(subdir_path))
+          else:
+            try:
+              shutil.rmtree(subdir_path)
+              print('Successfully cleaned chromeos image autotest directories '
+                    'from "{}".'.format(subdir_path))
+            except OSError:
+              print('Some image autotest directories were not removed from '
+                    '"{}".'.format(subdir_path))
+              errors += 1
+
+  return errors
 
 
 def CleanChromeOsTmpAndImages(days_to_preserve=1, dry_run=False):
@@ -175,6 +180,9 @@
   # Clean image files in *-pfq directories
   rv += CleanChromeOsImageFiles(chromeos_chroot_tmp, '-pfq', days_to_preserve,
                                 dry_run)
+  # Clean image files in *-llvm-next-nightly directories
+  rv += CleanChromeOsImageFiles(chromeos_chroot_tmp, '-llvm-next-nightly',
+                                days_to_preserve, dry_run)
 
   return rv