Compare commits

...
Author SHA1 Message Date
thomwolf a40dd4ae86 more robust 2020-02-14 23:33:53 +01:00
thomwolf ac8575ca5c clean up 2020-02-14 23:01:55 +01:00
thomwolf ead691983b add extract dir option to file cache 2020-02-14 22:13:21 +01:00
+19 -19
View File
@@ -214,6 +214,7 @@ def cached_path(
user_agent=None, user_agent=None,
extract_compressed_file=False, extract_compressed_file=False,
force_extract=False, force_extract=False,
extract_dir=None,
) -> Optional[str]: ) -> Optional[str]:
""" """
Given something that might be a URL (or might be a local path), Given something that might be a URL (or might be a local path),
@@ -229,6 +230,7 @@ def cached_path(
file in a folder along the archive. file in a folder along the archive.
force_extract: if True when extract_compressed_file is True and the archive was already extracted, force_extract: if True when extract_compressed_file is True and the archive was already extracted,
re-extract the archive and overide the folder where it was extracted. re-extract the archive and overide the folder where it was extracted.
extract_dir: specify a directory to extract the archive to (overwrite the default created directory).
Return: Return:
None in case of non-recoverable file (non-existent or inaccessible url + no cache on disk). None in case of non-recoverable file (non-existent or inaccessible url + no cache on disk).
@@ -261,7 +263,7 @@ def cached_path(
# Something unknown # Something unknown
raise ValueError("unable to parse {} as a URL or as a local path".format(url_or_filename)) raise ValueError("unable to parse {} as a URL or as a local path".format(url_or_filename))
if extract_compressed_file: if extract_compressed_file and os.path.isfile(output_path):
if not is_zipfile(output_path) and not tarfile.is_tarfile(output_path): if not is_zipfile(output_path) and not tarfile.is_tarfile(output_path):
return output_path return output_path
@@ -271,24 +273,22 @@ def cached_path(
output_extract_dir_name = output_file.replace(".", "-") + "-extracted" output_extract_dir_name = output_file.replace(".", "-") + "-extracted"
output_path_extracted = os.path.join(output_dir, output_extract_dir_name) output_path_extracted = os.path.join(output_dir, output_extract_dir_name)
if os.path.isdir(output_path_extracted) and os.listdir(output_path_extracted) and not force_extract: if not os.path.isdir(output_path_extracted) or force_extract:
return output_path_extracted # Extract file with a lock to prevent parallel extractions
lock_path = output_path + ".lock"
# Prevent parallel extractions with FileLock(lock_path):
lock_path = output_path + ".lock" shutil.rmtree(output_path_extracted, ignore_errors=True)
with FileLock(lock_path): os.makedirs(output_path_extracted)
shutil.rmtree(output_path_extracted, ignore_errors=True) if is_zipfile(output_path):
os.makedirs(output_path_extracted) with ZipFile(output_path, "r") as zip_file:
if is_zipfile(output_path): zip_file.extractall(output_path_extracted)
with ZipFile(output_path, "r") as zip_file: zip_file.close()
zip_file.extractall(output_path_extracted) elif tarfile.is_tarfile(output_path):
zip_file.close() tar_file = tarfile.open(output_path)
elif tarfile.is_tarfile(output_path): tar_file.extractall(output_path_extracted)
tar_file = tarfile.open(output_path) tar_file.close()
tar_file.extractall(output_path_extracted) else:
tar_file.close() raise EnvironmentError("Archive format of {} could not be identified".format(output_path))
else:
raise EnvironmentError("Archive format of {} could not be identified".format(output_path))
return output_path_extracted return output_path_extracted