"""Provide some common utility functions."""from__future__importabsolute_importfrom__future__importdivisionfrom__future__importprint_functionimportosimporttarfileimportwgetfrompaddle.v2.dataset.commonimportmd5filedefdownload(url,md5sum,target_dir):"""Download file from url to target_dir, and check md5sum."""ifnotos.path.exists(target_dir):os.makedirs(target_dir)filepath=os.path.join(target_dir,url.split("/")[-1])ifnot(os.path.exists(filepath)andmd5file(filepath)==md5sum):print("Downloading %s ..."%url)wget.download(url,target_dir)print("\nMD5 Chesksum %s ..."%filepath)ifnotmd5file(filepath)==md5sum:raiseRuntimeError("MD5 checksum failed.")else:print("File exists, skip downloading. (%s)"%filepath)returnfilepathdefunpack(filepath,target_dir,rm_tar=False):"""Unpack the file to the target_dir."""print("Unpacking %s ..."%filepath)tar=tarfile.open(filepath)tar.extractall(target_dir)tar.close()ifrm_tar==True:os.remove(filepath)