Now cleaning old firefox archives.

We have a few gigs of old archives out on the bots, and nothing
to clean them up. There's no reason to keep more than one old
archive around, so this makes the download script prune them.

I considered making this a separate script, but arguably it's the
job of the download script to deal with its byproducts (and I
could share some code) so I made the existing script do it.

BUG=360516
R=kjellander@chromium.org

Review URL: https://codereview.chromium.org/891303002

git-svn-id: http://src.chromium.org/svn/trunk/deps/third_party/webrtc/webrtc.DEPS@293897 4ff67af0-8c30-449e-8e8b-ad334ec8d88c
diff --git a/DEPS b/DEPS
index 0ac437c..f9059b0 100644
--- a/DEPS
+++ b/DEPS
@@ -93,7 +93,8 @@
     "pattern": ".",
     "action" : ["python",
                 "webrtc.DEPS/download_firefox_nightly.py",
-                "-t",
+                "--clean-old-firefox-archives",
+                "--target-dir",
                 "firefox-nightly"],
   },
 ]
diff --git a/download_firefox_nightly.py b/download_firefox_nightly.py
index bb3d96b..1286eb8 100755
--- a/download_firefox_nightly.py
+++ b/download_firefox_nightly.py
@@ -31,12 +31,35 @@
     os.utime(a_file, None)
 
 
-def _FindFallbackFirefoxBuild(target_dir):
+def _GetFirefoxArchivesSortedOnModifiedDate(target_dir):
   firefox_archives = glob.glob(os.path.join(target_dir, '*tar.bz2'))
   if not firefox_archives:
     return None
 
   firefox_archives.sort(key=os.path.getmtime, reverse=True)
+  return firefox_archives
+
+
+def _CleanOldFirefoxArchives(target_dir): 
+  firefox_archives = _GetFirefoxArchivesSortedOnModifiedDate(target_dir)
+  if not firefox_archives or len(firefox_archives) < 2:
+    return
+
+  # Keep the newest archive around as a fallback build and delete the rest.
+  rest = firefox_archives[1:]
+  print 'About to delete old Firefox archives %s.' % rest
+  for old_archive in rest:
+    try:
+      os.remove(old_archive)
+    except OSError:
+      pass
+
+
+def _FindFallbackFirefoxBuild(target_dir):
+  firefox_archives = _GetFirefoxArchivesSortedOnModifiedDate(target_dir)
+  if not firefox_archives:
+    return None
+
   newest_build = firefox_archives[0]
   build_age_seconds = time.time() - os.path.getmtime(newest_build)
   build_age_days = datetime.timedelta(seconds=build_age_seconds).days
@@ -116,6 +139,9 @@
   parser.add_option('-f', '--force', action='store_true',
                     help=('Force download even if the current nightly is '
                           'already downloaded.'))
+  parser.add_option('-c', '--clean-old-archives', action='store_true',
+                    help=('Clean old firefox archives; one will always be '
+                          'kept as a fallback.'))
   options, _args = parser.parse_args()
   if not options.target_dir:
     parser.error('You must specify the target directory.')
@@ -128,5 +154,8 @@
   if firefox_archive:
     return _ExtractArchive(firefox_archive, target_dir)
 
+  if options.clean_old_archives:
+    _CleanOldFirefoxArchives(target_dir)
+
 if __name__ == '__main__':
   sys.exit(main())