Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup tmpdir for zip downloads #1907

Merged
merged 1 commit into from Feb 13, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 14 additions & 6 deletions custom_components/hacs/helpers/classes/repository.py
Expand Up @@ -4,6 +4,7 @@
import os
import tempfile
import zipfile
import shutil

from aiogithubapi import AIOGitHubAPIException
from queueman import QueueManager
Expand Down Expand Up @@ -317,17 +318,24 @@ async def async_download_zip_file(self, content, validate):
validate.errors.append(f"[{content.name}] was not downloaded")
return

result = await async_save_file(
f"{tempfile.gettempdir()}/{self.data.filename}", filecontent
)
with zipfile.ZipFile(
f"{tempfile.gettempdir()}/{self.data.filename}", "r"
) as zip_file:
temp_dir = await self.hacs.hass.async_add_executor_job(tempfile.mkdtemp)
temp_file = f"{temp_dir}/{self.data.filename}"

result = await async_save_file(temp_file, filecontent)
with zipfile.ZipFile(temp_file, "r") as zip_file:
zip_file.extractall(self.content.path.local)

def cleanup_temp_dir():
"""Cleanup temp_dir."""
if os.path.exists(temp_dir):
self.logger.debug("Cleaning up %s", temp_dir)
shutil.rmtree(temp_dir)

if result:
self.logger.info("%s Download of %s completed", self, content.name)
await self.hacs.hass.async_add_executor_job(cleanup_temp_dir)
return

validate.errors.append(f"[{content.name}] was not downloaded")
except (Exception, BaseException):
validate.errors.append("Download was not completed")
Expand Down