@@ -59,6 +59,18 @@ class Recipe(with_metaclass(RecipeMeta)):
59
59
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.
66
+ '''
67
+
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.
72
+ '''
73
+
62
74
depends = []
63
75
'''A list containing the names of any recipes that this recipe depends on.
64
76
'''
@@ -342,16 +354,19 @@ def download(self):
342
354
return
343
355
344
356
url = self .versioned_url
345
- ma = match (u'^(.+)#md5=([0-9a-f]{32})$' , url )
346
- if ma : # fragmented URL?
347
- if self .md5sum :
348
- raise ValueError (
349
- ('Received md5sum from both the {} recipe '
350
- 'and its url' ).format (self .name ))
351
- url = ma .group (1 )
352
- expected_md5 = ma .group (2 )
353
- else :
354
- expected_md5 = self .md5sum
357
+ 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
360
+ ma = match (u'^(.+)#' + alg + u'=([0-9a-f]{32,})$' , url )
361
+ if ma : # fragmented URL?
362
+ if expected_digest :
363
+ raise ValueError (
364
+ ('Received {}sum from both the {} recipe '
365
+ 'and its url' ).format (alg , self .name ))
366
+ url = ma .group (1 )
367
+ expected_digest = ma .group (2 )
368
+ if expected_digest :
369
+ expected_digests [alg ] = expected_digest
355
370
356
371
shprint (sh .mkdir , '-p' , join (self .ctx .packages_path , self .name ))
357
372
@@ -363,16 +378,17 @@ def download(self):
363
378
if exists (filename ) and isfile (filename ):
364
379
if not exists (marker_filename ):
365
380
shprint (sh .rm , filename )
366
- elif expected_md5 :
367
- current_md5 = md5sum (filename )
368
- if current_md5 != expected_md5 :
369
- debug ('* Generated md5sum: {}' .format (current_md5 ))
370
- debug ('* Expected md5sum: {}' .format (expected_md5 ))
371
- raise ValueError (
372
- ('Generated md5sum does not match expected md5sum '
373
- 'for {} recipe' ).format (self .name ))
374
- do_download = False
375
381
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 ))
376
392
do_download = False
377
393
378
394
# If we got this far, we will download
@@ -383,15 +399,17 @@ def download(self):
383
399
self .download_file (self .versioned_url , filename )
384
400
shprint (sh .touch , marker_filename )
385
401
386
- if exists (filename ) and isfile (filename ) and expected_md5 :
387
- current_md5 = md5sum (filename )
388
- if expected_md5 is not None :
389
- if current_md5 != expected_md5 :
390
- debug ('* Generated md5sum: {}' .format (current_md5 ))
391
- debug ('* Expected md5sum: {}' .format (expected_md5 ))
402
+ 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 ))
392
410
raise ValueError (
393
- ('Generated md5sum does not match expected md5sum '
394
- 'for {} recipe' ).format (self .name ))
411
+ ('Generated {0}sum does not match expected {0}sum '
412
+ 'for {1 } recipe' ).format (alg , self .name ))
395
413
else :
396
414
info ('{} download already cached, skipping' .format (self .name ))
397
415
@@ -419,7 +437,7 @@ def unpack(self, arch):
419
437
420
438
filename = shprint (
421
439
sh .basename , self .versioned_url ).stdout [:- 1 ].decode ('utf-8' )
422
- ma = match (u'^(.+)#md5 =([0-9a-f]{32})$' , filename )
440
+ ma = match (u'^(.+)#[a-z0-9_]{3,} =([0-9a-f]{32, })$' , filename )
423
441
if ma : # fragmented URL?
424
442
filename = ma .group (1 )
425
443
@@ -1177,10 +1195,10 @@ def reduce_object_file_names(self, dirn):
1177
1195
shprint (sh .mv , filen , join (file_dirname , parts [0 ] + '.so' ))
1178
1196
1179
1197
1180
- def md5sum ( filen ):
1181
- '''Calculate the md5sum of a file.
1198
+ def algsum ( alg , filen ):
1199
+ '''Calculate the digest of a file.
1182
1200
'''
1183
1201
with open (filen , 'rb' ) as fileh :
1184
- md5 = hashlib . md5 (fileh .read ())
1202
+ digest = getattr ( hashlib , alg ) (fileh .read ())
1185
1203
1186
- return md5 .hexdigest ()
1204
+ return digest .hexdigest ()
0 commit comments