def cached_download(url):
 cache_root = os.path.join(_dataset_root, '_dl_cache')
 try:
  os.makedirs(cache_root)
 except OSError:
  if (not os.path.exists(cache_root)):
   raise RuntimeError('cannot create download cache directory')
 lock_path = os.path.join(cache_root, '_dl_lock')
 urlhash = hashlib.md5(url.encode('utf-8')).hexdigest()
 cache_path = os.path.join(cache_root, urlhash)
 with filelock.FileLock(lock_path):
  if os.path.exists(cache_path):
   return cache_path
 temp_root = tempfile.mkdtemp(dir=cache_root)
 try:
  temp_path = os.path.join(temp_root, 'dl')
  print('Downloading from {}...'.format(url))
  request.urlretrieve(url, temp_path)
  with filelock.FileLock(lock_path):
   shutil.move(temp_path, cache_path)
 finally:
  shutil.rmtree(temp_root)
 return cache_path

def cached_download ( url ) : cache_root = os . path . join ( _dataset_root , '_dl_cache' ) try : os . makedirs ( cache_root ) except OSError : if ( not os . path . exists ( cache_root ) ) : raise RuntimeError ( 'cannot create download cache directory' ) lock_path = os . path . join ( cache_root , '_dl_lock' ) urlhash = hashlib . md5 ( url . encode ( 'utf-8' ) ) . hexdigest ( ) cache_path = os . path . join ( cache_root , urlhash ) with filelock . FileLock ( lock_path ) : if os . path . exists ( cache_path ) : return cache_path temp_root = tempfile . mkdtemp ( dir = cache_root ) try : temp_path = os . path . join ( temp_root , 'dl' ) print ( 'Downloading from {}...' . format ( url ) ) request . urlretrieve ( url , temp_path ) with filelock . FileLock ( lock_path ) : shutil . move ( temp_path , cache_path ) finally : shutil . rmtree ( temp_root ) return cache_path
downloads a file and caches it .