@@ -80,13 +80,7 @@ def download(archive_url, output_directory, force=False, compress=False):
8080 with open (output_directory , "wb" ) as archive :
8181 archive .write (response .content )
8282 else :
83- # Support for file-like objects for tarfile.is_tarfile was added
84- # in Python 3.9, so as pyhf is currently Python 3.8+ then can't
85- # do tarfile.is_tarfile(BytesIO(response.content)).
86- # Instead, just use a 'try except' block to determine if the
87- # archive is a valid tarfile.
88- # TODO: Simplify after pyhf is Python 3.9+ only
89- try :
83+ if tarfile .is_tarfile (BytesIO (response .content )):
9084 # Use transparent compression to allow for .tar or .tar.gz
9185 with tarfile .open (
9286 mode = "r:*" , fileobj = BytesIO (response .content )
@@ -97,13 +91,7 @@ def download(archive_url, output_directory, force=False, compress=False):
9791 archive .extractall (output_directory , filter = "data" )
9892 else :
9993 archive .extractall (output_directory )
100- except tarfile .ReadError :
101- if not zipfile .is_zipfile (BytesIO (response .content )):
102- raise exceptions .InvalidArchive (
103- f"The archive downloaded from { archive_url } is not a tarfile"
104- + " or a zipfile and so can not be opened as one."
105- )
106-
94+ elif zipfile .is_zipfile (BytesIO (response .content )):
10795 output_directory = Path (output_directory )
10896 if output_directory .exists ():
10997 rmtree (output_directory )
@@ -129,6 +117,11 @@ def download(archive_url, output_directory, force=False, compress=False):
129117 # from creation time
130118 rmtree (output_directory )
131119 _tmp_path .replace (output_directory )
120+ else :
121+ raise exceptions .InvalidArchive (
122+ f"The archive downloaded from { archive_url } is not a tarfile"
123+ + " or a zipfile and so can not be opened as one."
124+ )
132125
133126except ModuleNotFoundError :
134127 log .error (
0 commit comments