Bug 1355731 - For artifacts cache, use the file name from the url if it looks like a hash digest. r=gps draft
authorMike Hommey <mh+mozilla@glandium.org>
Wed, 12 Apr 2017 16:26:20 +0900
changeset 563031 ffd1bf7a3ef682c9a3df7e50f6cf3cfd32dbe471
parent 563030 64a5d7598d60a0914348c286e8d0a29030f3fb8b
child 563032 a8a33f3d81d26bfdee6fc88f15a93c32687e57ae
push id54193
push userbmo:mh+mozilla@glandium.org
push dateFri, 14 Apr 2017 21:55:43 +0000
reviewersgps
bugs1355731
milestone55.0a1
Bug 1355731 - For artifacts cache, use the file name from the url if it looks like a hash digest. r=gps
python/mozbuild/mozbuild/artifacts.py
--- a/python/mozbuild/mozbuild/artifacts.py
+++ b/python/mozbuild/mozbuild/artifacts.py
@@ -37,16 +37,17 @@ A future need, perhaps.
 This module requires certain modules be importable from the ambient Python
 environment.  |mach artifact| ensures these modules are available, but other
 consumers will need to arrange this themselves.
 '''
 
 
 from __future__ import absolute_import, print_function, unicode_literals
 
+import binascii
 import collections
 import functools
 import glob
 import hashlib
 import logging
 import operator
 import os
 import pickle
@@ -759,23 +760,30 @@ class ArtifactCache(object):
             self._cache_dir, persist_limit=self._persist_limit)
         self._last_dl_update = -1
 
     def log(self, *args, **kwargs):
         if self._log:
             self._log(*args, **kwargs)
 
     def fetch(self, url, force=False):
-        # We download to a temporary name like HASH[:16]-basename to
-        # differentiate among URLs with the same basenames.  We used to then
-        # extract the build ID from the downloaded artifact and use it to make a
-        # human readable unique name, but extracting build IDs is time consuming
-        # (especially on Mac OS X, where we must mount a large DMG file).
-        hash = hashlib.sha256(url).hexdigest()[:16]
-        fname = hash + '-' + os.path.basename(url)
+        fname = os.path.basename(url)
+        try:
+            # Use the file name from the url if it looks like a hash digest.
+            if len(fname) not in (32, 40, 56, 64, 96, 128):
+                raise TypeError()
+            binascii.unhexlify(fname)
+        except TypeError:
+            # We download to a temporary name like HASH[:16]-basename to
+            # differentiate among URLs with the same basenames.  We used to then
+            # extract the build ID from the downloaded artifact and use it to make a
+            # human readable unique name, but extracting build IDs is time consuming
+            # (especially on Mac OS X, where we must mount a large DMG file).
+            hash = hashlib.sha256(url).hexdigest()[:16]
+            fname = hash + '-' + os.path.basename(url)
 
         path = os.path.abspath(mozpath.join(self._cache_dir, fname))
         if self._skip_cache and os.path.exists(path):
             self.log(logging.DEBUG, 'artifact',
                 {'path': path},
                 'Skipping cache: removing cached downloaded artifact {path}')
             os.remove(path)