@@ -53,22 +53,21 @@ class Recipe(with_metaclass(RecipeMeta)):
53
53
'''A string giving the version of the software the recipe describes,
54
54
e.g. ``2.0.3`` or ``master``.'''
55
55
56
- md5sum = None
57
- '''The md5sum of the source from the :attr:`url`. Non-essential, but
58
- you should try to include this, it is used to check that the download
59
- finished correctly.
56
+ sha256sum = None
57
+ '''The sha256sum of the source from the :attr:`url`. As of 2020, pip
58
+ recommendes use of this hash. You should try to include this. It
59
+ is used to check that the download finished correctly.
60
60
'''
61
61
62
- sha512sum = None
63
- '''The sha512sum of the source from the :attr:`url`. Non-essential, but
64
- you should try to include this, it is used to check that the download
65
- finished correctly.
62
+ md5sum = None
63
+ '''The md5sum of the source from the :attr:`url`. Non-essential. It
64
+ is used to check that the download finished correctly.
66
65
'''
67
66
68
- blake2bsum = None
69
- '''The blake2bsum of the source from the :attr:`url`. Non-essential, but
70
- you should try to include this, it is used to check that the download
71
- finished correctly.
67
+ blake2b_256sum = None
68
+ '''The blake2b_256sum of the source from the :attr:`url`. Non-essential,
69
+ but you should try to include this, it is used to check that the
70
+ download finished correctly.
72
71
'''
73
72
74
73
depends = []
@@ -355,8 +354,8 @@ def download(self):
355
354
356
355
url = self .versioned_url
357
356
expected_digests = {}
358
- for alg in set (hashlib .algorithms_guaranteed ) | set (('md5 ' , 'sha512 ' , 'blake2b ' )):
359
- expected_digest = getattr (self , alg + 'sum' ) if hasattr ( self , alg + 'sum' ) else None
357
+ for alg in set (hashlib .algorithms_guaranteed ) | set (('sha256 ' , 'md5 ' , 'blake2b_256 ' )):
358
+ expected_digest = getattr (self , alg + 'sum' , None )
360
359
ma = match (u'^(.+)#' + alg + u'=([0-9a-f]{32,})$' , url )
361
360
if ma : # fragmented URL?
362
361
if expected_digest :
@@ -379,16 +378,7 @@ def download(self):
379
378
if not exists (marker_filename ):
380
379
shprint (sh .rm , filename )
381
380
else :
382
- for alg , expected_digest in expected_digests .items ():
383
- current_digest = algsum (alg , filename )
384
- if current_digest != expected_digest :
385
- debug ('* Generated {}sum: {}' .format (alg ,
386
- current_digest ))
387
- debug ('* Expected {}sum: {}' .format (alg ,
388
- expected_digest ))
389
- raise ValueError (
390
- ('Generated {0}sum does not match expected {0}sum '
391
- 'for {1} recipe' ).format (alg , self .name ))
381
+ self .verify_algsum (expected_digests , filename )
392
382
do_download = False
393
383
394
384
# If we got this far, we will download
@@ -400,16 +390,7 @@ def download(self):
400
390
shprint (sh .touch , marker_filename )
401
391
402
392
if exists (filename ) and isfile (filename ):
403
- for alg , expected_digest in expected_digests .items ():
404
- current_digest = algsum (alg , filename )
405
- if current_digest != expected_digest :
406
- debug ('* Generated {}sum: {}' .format (alg ,
407
- current_digest ))
408
- debug ('* Expected {}sum: {}' .format (alg ,
409
- expected_digest ))
410
- raise ValueError (
411
- ('Generated {0}sum does not match expected {0}sum '
412
- 'for {1} recipe' ).format (alg , self .name ))
393
+ self .verify_algsum (expected_digests , filename )
413
394
else :
414
395
info ('{} download already cached, skipping' .format (self .name ))
415
396
@@ -1195,10 +1176,28 @@ def reduce_object_file_names(self, dirn):
1195
1176
shprint (sh .mv , filen , join (file_dirname , parts [0 ] + '.so' ))
1196
1177
1197
1178
1198
- def algsum (alg , filen ):
1199
- '''Calculate the digest of a file.
1200
- '''
1201
- with open (filen , 'rb' ) as fileh :
1202
- digest = getattr (hashlib , alg )(fileh .read ())
1203
-
1204
- return digest .hexdigest ()
1179
+ def verify_algsum (self , algs , filen ):
1180
+ '''Verify digest of a file.
1181
+ '''
1182
+
1183
+ for alg , expected_digest in algs .items ():
1184
+
1185
+ with open (filen , 'rb' ) as fileh :
1186
+ func = getattr (hashlib , alg , None )
1187
+ if func is not None :
1188
+ digest = func (fileh .read ())
1189
+ elif '_' in alg : # for custom digest_sizes, such as blake2b_256
1190
+ offset = alg .rfind ('_' )
1191
+ func = getattr (hashlib , alg [:offset ])
1192
+ digest_size = int (alg [offset + 1 :])
1193
+ digest = func (fileh .read (), digest_size = digest_size )
1194
+ current_digest = digest .hexdigest ()
1195
+
1196
+ if current_digest != expected_digest :
1197
+ debug ('* Generated {}sum: {}' .format (alg ,
1198
+ current_digest ))
1199
+ debug ('* Expected {}sum: {}' .format (alg ,
1200
+ expected_digest ))
1201
+ raise ValueError (
1202
+ ('Generated {0}sum does not match expected {0}sum '
1203
+ 'for {1} recipe' ).format (alg , self .name ))
0 commit comments