[dev-util] Dev server should clean up debug.tgz after staging

The initial code for this only cleaned up on error; this cleans up the
temporary download dir after staging the symbols appropriately no
matter what.

BUG=chromium-os:31069
TEST=unit
TEST=use curl to stage debug symbols; tail dev server log to ensure download dir is removed.
STATUS=Fixed

Change-Id: I40e4c19e1d24c7847a2d155550c7ff9a6b8e5d2c
Reviewed-on: https://gerrit.chromium.org/gerrit/23055
Tested-by: Chris Masone <cmasone@chromium.org>
Reviewed-by: Scott Zawalski <scottz@chromium.org>
Commit-Ready: Chris Masone <cmasone@chromium.org>
diff --git a/downloader.py b/downloader.py
index dc22ad5..955e796 100755
--- a/downloader.py
+++ b/downloader.py
@@ -24,6 +24,8 @@
     - Install components to static dir.
   """
 
+  _LOG_TAG = 'DOWNLOAD'
+
   def __init__(self, static_dir):
     self._static_dir = static_dir
     self._build_dir = None
@@ -79,7 +81,7 @@
 
     if Downloader.BuildStaged(archive_url, self._static_dir):
       cherrypy.log('Build %s has already been processed.' % self._lock_tag,
-                   'DOWNLOAD')
+                   self._LOG_TAG)
       self._status_queue.put('Success')
       return 'Success'
 
@@ -92,13 +94,13 @@
       self._staging_dir = tempfile.mkdtemp(suffix='_'.join([target,
                                                             short_build]))
       cherrypy.log('Gathering download requirements %s' % archive_url,
-                   'DOWNLOAD')
+                   self._LOG_TAG)
       artifacts = self.GatherArtifactDownloads(
           self._staging_dir, archive_url, short_build, self._build_dir)
       devserver_util.PrepareBuildDirectory(self._build_dir)
 
       cherrypy.log('Downloading foreground artifacts from %s' % archive_url,
-                   'DOWNLOAD')
+                   self._LOG_TAG)
       background_artifacts = []
       for artifact in artifacts:
         if artifact.Synchronous():
@@ -129,14 +131,14 @@
     """Cleans up the staging dir for this downloader instanfce."""
     if self._staging_dir:
       cherrypy.log('Cleaning up staging directory %s' % self._staging_dir,
-                   'DOWNLOAD')
+                   self._LOG_TAG)
       shutil.rmtree(self._staging_dir)
 
     self._staging_dir = None
 
   def _DownloadArtifactsSerially(self, artifacts):
     """Simple function to download all the given artifacts serially."""
-    cherrypy.log('Downloading background artifacts serially.', 'DOWNLOAD')
+    cherrypy.log('Downloading background artifacts serially.', self._LOG_TAG)
     try:
       for artifact in artifacts:
         artifact.Download()
@@ -196,6 +198,7 @@
   """
 
   _DONE_FLAG = 'done'
+  _LOG_TAG = 'SYMBOL_DOWNLOAD'
 
   @staticmethod
   def GenerateLockTag(target, short_build):
@@ -216,7 +219,7 @@
     if self.SymbolsStaged(archive_url, self._static_dir):
       cherrypy.log(
           'Symbols for build %s have already been staged.' % self._lock_tag,
-          'SYMBOL_DOWNLOAD')
+          self._LOG_TAG)
       return 'Success'
 
     try:
@@ -228,12 +231,13 @@
       self._staging_dir = tempfile.mkdtemp(suffix='_'.join([target,
                                                             short_build]))
       cherrypy.log('Downloading debug symbols from %s' % archive_url,
-                   'SYMBOL_DOWNLOAD')
+                   self._LOG_TAG)
 
       [symbol_artifact] = self.GatherArtifactDownloads(
           self._staging_dir, archive_url, '', self._static_dir)
       symbol_artifact.Download()
       symbol_artifact.Stage()
+      self.MarkSymbolsStaged()
 
     except Exception:
       # Release processing "lock", which will indicate to future runs that we
@@ -241,10 +245,9 @@
       if self._build_dir:
         devserver_util.ReleaseLock(static_dir=self._static_dir,
                                    tag=self._lock_tag)
-      self._Cleanup()
       raise
-
-    self.MarkSymbolsStaged()
+    finally:
+      self._Cleanup()
     return 'Success'
 
   def GatherArtifactDownloads(self, temp_download_dir, archive_url, short_build,