From 8b883426c4987c6f4f736080acba96d78ea3c26b Mon Sep 17 00:00:00 2001
From: Kazuhiko Shiozaki <kazuhiko@nexedi.com>
Date: Fri, 7 Apr 2017 16:14:01 +0200
Subject: [PATCH] downloadunpacked: remove downloaded files after unpacking.

---
 slapos/recipe/download.py         | 12 ++++++++----
 slapos/recipe/downloadunpacked.py |  2 ++
 2 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/slapos/recipe/download.py b/slapos/recipe/download.py
index ee16997..42e8767 100644
--- a/slapos/recipe/download.py
+++ b/slapos/recipe/download.py
@@ -60,10 +60,14 @@ class Recipe(object):
     if parts is not None and not os.path.isdir(parts):
       os.mkdir(parts)
       result.append(parts)
-    path, _ = self._downloader(self._url, md5sum=self._md5sum)
-    if os.path.exists(destination):
-      os.unlink(destination)
-    shutil.copy(path, destination)
+    path, is_temp = self._downloader(self._url, md5sum=self._md5sum)
+    try:
+      if os.path.exists(destination):
+        os.unlink(destination)
+      shutil.copy(path, destination)
+    finally:
+      if is_temp:
+        os.unlink(path)
     mode = self._mode
     if mode is not None:
       os.chmod(destination, mode)
diff --git a/slapos/recipe/downloadunpacked.py b/slapos/recipe/downloadunpacked.py
index 85bc50a..595fef5 100644
--- a/slapos/recipe/downloadunpacked.py
+++ b/slapos/recipe/downloadunpacked.py
@@ -118,6 +118,8 @@ class Recipe:
         setuptools.archive_util.unpack_archive(path, extract_dir)
     finally:
       unpatch_archive_util()
+      if is_temp:
+        os.unlink(path)
 
     # Delete destination directory if exist
     if os.path.exists(self.destination):
-- 
2.30.9