Bug 1455143 - Make hashlib required; r?Build draft
authorGregory Szorc <gps@mozilla.com>
Wed, 18 Apr 2018 15:31:11 -0700
changeset 785297 29667c2511ba963993cebfe2476456a41caf8311
parent 785296 339c8767742110d2a2ab2761ef1726e864da4fcf
child 785298 9f0b34908530f0afe00bd10cd490471ee4f41481
push id107184
push userbmo:gps@mozilla.com
push dateThu, 19 Apr 2018 21:22:59 +0000
reviewersBuild
bugs1455143
milestone61.0a1
Bug 1455143 - Make hashlib required; r?Build A missing or unimportable hashlib is only possible on very old or very busted Pythons. Remove code for conditionally handling a failure to import it. MozReview-Commit-ID: EXVlmttcwUa
build/checksums.py
--- a/build/checksums.py
+++ b/build/checksums.py
@@ -1,52 +1,44 @@
 #!/usr/bin/python
 # This Source Code Form is subject to the terms of the Mozilla Public
 # License, v. 2.0. If a copy of the MPL was not distributed with this
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
 from __future__ import with_statement
 
 from optparse import OptionParser
+import hashlib
 import logging
 import os
-try:
-    import hashlib
-except ImportError:
-    hashlib = None
 
 
 def digest_file(filename, digest, chunk_size=1024):
     '''Produce a checksum for the file specified by 'filename'.  'filename'
     is a string path to a file that is opened and read in this function.  The
     checksum algorithm is specified by 'digest' and is a valid OpenSSL
     algorithm.  If the digest used is not valid or Python's hashlib doesn't
     work, the None object will be returned instead.  The size of blocks
     that this function will read from the file object it opens based on
     'filename' can be specified by 'chunk_size', which defaults to 1K'''
     assert not os.path.isdir(filename), 'this function only works with files'
     logger = logging.getLogger('checksums.py')
-    if hashlib is not None:
-        logger.debug('Creating new %s object' % digest)
-        h = hashlib.new(digest)
-        with open(filename, 'rb') as f:
-            while True:
-                data = f.read(chunk_size)
-                if not data:
-                    logger.debug('Finished reading in file')
-                    break
-                h.update(data)
-        hash = h.hexdigest()
-        logger.debug('Hash for %s is %s' % (filename, hash))
-        return hash
-    else:
-        # In this case we could subprocess.Popen and .communicate with
-        # sha1sum or md5sum
-        logger.warn('The python module for hashlib is missing!')
-        return None
+
+    logger.debug('Creating new %s object' % digest)
+    h = hashlib.new(digest)
+    with open(filename, 'rb') as f:
+        while True:
+            data = f.read(chunk_size)
+            if not data:
+                logger.debug('Finished reading in file')
+                break
+            h.update(data)
+    hash = h.hexdigest()
+    logger.debug('Hash for %s is %s' % (filename, hash))
+    return hash
 
 
 def process_files(files, output_filename, digests, strip):
     '''This function takes a list of file names, 'files'.  It will then
     compute the checksum for each of the files by opening the files.
     Once each file is read and its checksum is computed, this function
     will write the information to the file specified by 'output_filename'.
     The path written in the output file will have anything specified by 'strip'