toolchain-utils: Delete temporary chroot image after verification step.

When a local image is outside the chroot, it is temporarily copied into the
chroot so that it can be mounted by chroot scripts. Changed the logic to
delay the cleanup of the temporary copy until after the verification step.
The image needs to be mounted once again during verification.

Didn't catch this bug earlier, as the images were cached inside the chroot by
previous failed runs.

BUG=b:79376118
TEST=Tested with crosperf, two local images and a clean chroot without any
cached images.

Change-Id: I700f9aa0d9827aa349e75ef7b6b22618baf26fa9
Reviewed-on: https://chromium-review.googlesource.com/1062330
Commit-Ready: Gabriel Marin <gmx@chromium.org>
Tested-by: Gabriel Marin <gmx@chromium.org>
Reviewed-by: Gabriel Marin <gmx@chromium.org>
Reviewed-by: Yunlian Jiang <yunlian@chromium.org>
diff --git a/image_chromeos.py b/image_chromeos.py
index 3d9c180..aa8824b 100755
--- a/image_chromeos.py
+++ b/image_chromeos.py
@@ -280,11 +280,6 @@
       if log_level == 'average':
         cmd_executer.SetLogLevel(log_level)
 
-      if found == False:
-        temp_dir = os.path.dirname(located_image)
-        l.LogOutput('Deleting temp image dir: %s' % temp_dir)
-        shutil.rmtree(temp_dir)
-
       logger.GetLogger().LogFatalIf(ret, 'Image command failed')
 
       # Unfortunately cros_image_to_target.py sometimes returns early when the
@@ -316,6 +311,11 @@
                                       'Image verification failed!')
         TryRemountPartitionAsRW(options.chromeos_root, options.remote,
                                 log_level)
+
+      if found == False:
+        temp_dir = os.path.dirname(located_image)
+        l.LogOutput('Deleting temp image dir: %s' % temp_dir)
+        shutil.rmtree(temp_dir)
     else:
       l.LogOutput('Checksums match. Skipping reimage')
     return ret