@@ -358,34 +358,36 @@ def get_checksum_for(self, checksums, filename=None, index=None):
358358 :param filename: name of the file to obtain checksum for
359359 :param index: index of file in list
360360 """
361- checksum = None
362-
363- # sometimes, filename are specified as a dict
361+ chksum_input = filename
362+ # if filename is provided as dict, take 'filename' key
364363 if isinstance (filename , dict ):
365- filename = filename ['filename' ]
364+ chksum_input = filename ['filename' ]
365+ # early return if no filename given
366+ if chksum_input is None :
367+ self .log .debug ("Cannot get checksum without a file name" )
368+ return None
366369
370+ checksum = None
367371 # if checksums are provided as a dict, lookup by source filename as key
368372 if isinstance (checksums , dict ):
369- if filename is not None and filename in checksums :
370- checksum = checksums [filename ]
371- else :
372- checksum = None
373- elif isinstance (checksums , (list , tuple )):
374- if index is not None and index < len ( checksums ) and ( index >= 0 or abs ( index ) <= len ( checksums )) :
373+ try :
374+ checksum = checksums [chksum_input ]
375+ except KeyError :
376+ self . log . debug ( "Checksum not found for file: %s" , chksum_input )
377+ elif isinstance (checksums , (list , tuple )) and index is not None :
378+ try :
375379 checksum = checksums [index ]
376- else :
377- checksum = None
378- elif checksums is None :
379- checksum = None
380- else :
380+ except IndexError :
381+ self .log .debug ("Checksum not found for index list: %s" , index )
382+ elif checksums is not None :
381383 raise EasyBuildError ("Invalid type for checksums (%s), should be dict, list, tuple or None." ,
382384 type (checksums ))
383385
384386 if checksum is None or build_option ("checksum_priority" ) == CHECKSUM_PRIORITY_JSON :
385387 json_checksums = self .get_checksums_from_json ()
386- return json_checksums .get (filename , None )
387- else :
388- return checksum
388+ return json_checksums .get (chksum_input , None )
389+
390+ return checksum
389391
390392 def get_checksums_from_json (self , always_read = False ):
391393 """
0 commit comments