Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a40dd4ae86 | ||
|
|
ac8575ca5c | ||
|
|
ead691983b |
@@ -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
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user