Skip to content

Commit 51dcaab

Browse files
authored
fix: temp file not cleaned up after extract (#2)
* fix: temp file not cleaned up after extract * fix: windows file handle On Windows, file handles must be closed before other processes can access or delete the file. Added file.close() immediately after creating the temp file so: 1. urlretrieve can write to it 2. tarfile.open can read it 3. os.unlink can delete it
1 parent 8d02ded commit 51dcaab

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

install.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -87,20 +87,25 @@ def install(url: str, install_dir: str):
8787
"""
8888
logging.info(f'Downloading {url}')
8989
file = tempfile.NamedTemporaryFile(delete=False)
90-
request.urlretrieve(url, file.name)
91-
logging.info(f'Successfully downloaded {file.name}')
92-
93-
os.makedirs(install_dir, exist_ok=True)
94-
with tarfile.open(file.name, 'r:gz') as tar:
95-
for member in tar.getmembers():
96-
# Strip off the first path component (i.e., `--strip-components=1`).
97-
parts = member.name.split('/')
98-
if len(parts) > 1:
99-
member.name = '/'.join(parts[1:])
100-
# Eventually we will want to pass `filter='tar'` here, but Windows runners have a
101-
# pre-3.9.17 version of Python.
102-
tar.extract(member, path=install_dir)
103-
logging.info(f'Extracted to {install_dir}')
90+
file.close() # Close the handle so Windows can access the file
91+
try:
92+
request.urlretrieve(url, file.name)
93+
logging.info(f'Successfully downloaded {file.name}')
94+
95+
os.makedirs(install_dir, exist_ok=True)
96+
with tarfile.open(file.name, 'r:gz') as tar:
97+
for member in tar.getmembers():
98+
# Strip off the first path component (i.e., `--strip-components=1`).
99+
parts = member.name.split('/')
100+
if len(parts) > 1:
101+
member.name = '/'.join(parts[1:])
102+
# Eventually we will want to pass `filter='tar'` here, but Windows runners have a
103+
# pre-3.9.17 version of Python.
104+
tar.extract(member, path=install_dir)
105+
logging.info(f'Extracted to {install_dir}')
106+
finally:
107+
os.unlink(file.name)
108+
logging.debug(f'Cleaned up temporary file {file.name}')
104109

105110
sep = os.path.sep
106111
ext = '.exe' if sep == '\\' else ''

0 commit comments

Comments
 (0)