Remove temp model archive after extraction

Without this change every time a model is downloaded
it leaks a file in the temp directory.
This commit is contained in:
Marshall Scorcio
2020-04-25 22:06:17 -07:00
parent 0366a4c83d
commit ba9999ac22

View File

@@ -16,6 +16,7 @@
import hashlib
import tarfile
import os
from tempfile import NamedTemporaryFile
@@ -96,6 +97,7 @@ class GithubModelProvider(ModelProvider):
with requests.get(url, stream=True) as response:
response.raise_for_status()
archive = NamedTemporaryFile(delete=False)
try:
with archive as stream:
# Note: check for chunk size parameters ?
for chunk in response.iter_content(chunk_size=8192):
@@ -107,4 +109,6 @@ class GithubModelProvider(ModelProvider):
get_logger().info('Extracting downloaded %s archive', name)
with tarfile.open(name=archive.name) as tar:
tar.extractall(path=path)
finally:
os.unlink(archive.name)
get_logger().info('%s model file(s) extracted', name)