def unmap_url_cache(cachedir, url, expected_hash, method='md5'):
 cachedir = os.path.realpath(cachedir)
 if (not os.path.isdir(cachedir)):
  try:
   os.makedirs(cachedir)
  except Exception:
   raise ValueError(('Could not create cache directory %s' % cachedir))
 file_from_url = os.path.basename(url)
 file_local_path = os.path.join(cachedir, file_from_url)
 file_hash = None
 failure_counter = 0
 while (not (file_hash == expected_hash)):
  if os.path.isfile(file_local_path):
   file_hash = hash_file(file_local_path, method)
   if (file_hash == expected_hash):
    src = file_from_url
   else:
    logging.error(('Seems that file %s is corrupted, trying to download it again' % file_from_url))
    src = url
    failure_counter += 1
  else:
   src = url
  if (failure_counter > 1):
   raise EnvironmentError(('Consistently failed to download the package %s. Aborting further download attempts. This might mean either the network connection has problems or the expected hash string that was determined for this file is wrong' % file_from_url))
  file_path = utils.unmap_url(cachedir, src, cachedir)
 return file_path

def unmap_url_cache ( cachedir , url , expected_hash , method = 'md5' ) : cachedir = os . path . realpath ( cachedir ) if ( not os . path . isdir ( cachedir ) ) : try : os . makedirs ( cachedir ) except Exception : raise ValueError ( ( 'Could not create cache directory %s' % cachedir ) ) file_from_url = os . path . basename ( url ) file_local_path = os . path . join ( cachedir , file_from_url ) file_hash = None failure_counter = 0 while ( not ( file_hash == expected_hash ) ) : if os . path . isfile ( file_local_path ) : file_hash = hash_file ( file_local_path , method ) if ( file_hash == expected_hash ) : src = file_from_url else : logging . error ( ( 'Seems that file %s is corrupted, trying to download it again' % file_from_url ) ) src = url failure_counter += 1 else : src = url if ( failure_counter > 1 ) : raise EnvironmentError ( ( 'Consistently failed to download the package %s. Aborting further download attempts. This might mean either the network connection has problems or the expected hash string that was determined for this file is wrong' % file_from_url ) ) file_path = utils . unmap_url ( cachedir , src , cachedir ) return file_path
downloads a file from a url to a cache directory .