Reland "tauto: Remove crash files before, not after, tests"

Cherry-picking for lab test compatibility

This is a reland of 371071a1e3d8be45bcf30059cd2e6cad33ef4086

Original change's description:
> tauto: Remove crash files before, not after, tests
>
> We're now collecting crashes after tests run (during release builds).
> Removing all crashes before this collection makes it much harder to
> collect them ;-).
>
> BUG=chromium:1107005
> TEST=CQ
>
> Change-Id: I28ac60e9af521067dae5f0d91c126e42fd099714
> Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/autotest/+/2633501
> Tested-by: Miriam Zimmerman <mutexlox@chromium.org>
> Reviewed-by: Derek Beckett <dbeckett@chromium.org>
> Reviewed-by: Allen Li <ayatane@chromium.org>
> Commit-Queue: Miriam Zimmerman <mutexlox@chromium.org>

Bug: chromium:1107005
Change-Id: I9a8c4250f63103ed3d692186567afc854f01e93c
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/autotest/+/2844817
Tested-by: Derek Beckett <dbeckett@chromium.org>
Auto-Submit: Derek Beckett <dbeckett@chromium.org>
Reviewed-by: Miriam Zimmerman <mutexlox@chromium.org>
Commit-Queue: Miriam Zimmerman <mutexlox@chromium.org>
diff --git a/client/bin/site_sysinfo.py b/client/bin/site_sysinfo.py
index 94454f0..5519bd6 100755
--- a/client/bin/site_sysinfo.py
+++ b/client/bin/site_sysinfo.py
@@ -292,6 +292,16 @@
             utils.system("rm -rf %s/*" % (self.dir))
 
 
+class purged_on_init_logdir(logdir):
+    """Represents a log directory that is purged *when initialized*."""
+
+    def __init__(self, directory, excludes=logdir.DEFAULT_EXCLUDES):
+        super(purged_on_init_logdir, self).__init__(directory, excludes)
+
+        if os.path.exists(self.dir):
+            utils.system("rm -rf %s/*" % (self.dir))
+
+
 class site_sysinfo(base_sysinfo.base_sysinfo):
     """Represents site system info."""
     def __init__(self, job_resultsdir):
@@ -322,17 +332,20 @@
         self.test_loggables.add(
             purgeable_logdir(
                 os.path.join(constants.CRYPTOHOME_MOUNT_PT, "log")))
-        # We only want to gather and purge crash reports after the client test
-        # runs in case a client test is checking that a crash found at boot
-        # (such as a kernel crash) is handled.
+
+        # We do *not* want to purge crashes after iteration to allow post-test
+        # infrastructure to collect them as well. Instead, purge them before.
+        # TODO(mutexlox, ayatane): test_runner should handle the purging.
         self.after_iteration_loggables.add(
-            purgeable_logdir(
-                os.path.join(constants.CRYPTOHOME_MOUNT_PT, "crash"),
-                excludes=logdir.DEFAULT_EXCLUDES + (crash_exclude_string,)))
+                purged_on_init_logdir(os.path.join(
+                        constants.CRYPTOHOME_MOUNT_PT, "crash"),
+                                      excludes=logdir.DEFAULT_EXCLUDES +
+                                      (crash_exclude_string, )))
         self.after_iteration_loggables.add(
-            purgeable_logdir(
-                constants.CRASH_DIR,
-                excludes=logdir.DEFAULT_EXCLUDES + (crash_exclude_string,)))
+                purged_on_init_logdir(constants.CRASH_DIR,
+                                      excludes=logdir.DEFAULT_EXCLUDES +
+                                      (crash_exclude_string, )))
+
         self.test_loggables.add(
             logfile(os.path.join(constants.USER_DATA_DIR,
                                  ".Google/Google Talk Plugin/gtbplugin.log")))