aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastian Pipping <sebastian@pipping.org>2010-02-17 00:26:51 +0100
committerSebastian Pipping <sebastian@pipping.org>2010-02-17 00:26:51 +0100
commit3b5e998d2ad91d992cd9764bf8b3a3e00916ebf9 (patch)
tree1fcc711f7648cf9d3bd0ab3643e13da19dd8104c
parentFix syncing of tar overlays (bug #304547) (diff)
downloadlayman-3b5e998d2ad91d992cd9764bf8b3a3e00916ebf9.tar.gz
layman-3b5e998d2ad91d992cd9764bf8b3a3e00916ebf9.tar.bz2
layman-3b5e998d2ad91d992cd9764bf8b3a3e00916ebf9.zip
Make fail of tar sync not leave temp files
-rw-r--r--layman/overlays/tar.py28
-rw-r--r--layman/tests/external.py2
2 files changed, 20 insertions, 10 deletions
diff --git a/layman/overlays/tar.py b/layman/overlays/tar.py
index e5779bf..65fbc39 100644
--- a/layman/overlays/tar.py
+++ b/layman/overlays/tar.py
@@ -28,6 +28,7 @@ import os, os.path, sys, urllib2, shutil, tempfile
import xml.etree.ElementTree as ET # Python 2.5
from layman.utils import path, ensure_unicode
+from layman.debug import OUT
from layman.overlays.source import OverlaySource
#===============================================================================
@@ -132,9 +133,25 @@ class TarOverlay(OverlaySource):
return result
def _add_unchecked(self, base, quiet):
+ def try_to_wipe(path):
+ if not os.path.exists(path):
+ return
+
+ try:
+ OUT.info('Deleting directory "%s"' % path, 2)
+ shutil.rmtree(path)
+ except Exception, error:
+ raise Exception('Failed to remove unnecessary tar structure "'
+ + path + '"\nError was:' + str(error))
+
final_path = path([base, self.parent.name])
temp_path = tempfile.mkdtemp(dir=base)
- result = self._extract(base=base, tar_url=self.src, dest_dir=temp_path)
+ try:
+ result = self._extract(base=base, tar_url=self.src, dest_dir=temp_path)
+ except Exception, error:
+ try_to_wipe(temp_path)
+ raise error
+
if result == 0:
if self.subpath:
source = temp_path + '/' + self.subpath
@@ -156,14 +173,7 @@ class TarOverlay(OverlaySource):
raise Exception('Given subpath "' + source + '" does not exist '
' in the tar package!')
- if os.path.exists(temp_path):
- try:
- OUT.info('Deleting directory "%s"' % temp_path, 2)
- shutil.rmtree(temp_path)
- except Exception, error:
- raise Exception('Failed to remove unnecessary tar structure "'
- + temp_path + '"\nError was:' + str(error))
-
+ try_to_wipe(temp_path)
return result
def add(self, base, quiet = False):
diff --git a/layman/tests/external.py b/layman/tests/external.py
index e0b8b1b..3c23373 100644
--- a/layman/tests/external.py
+++ b/layman/tests/external.py
@@ -127,7 +127,7 @@ class TarAddRemoveSync(unittest.TestCase):
# Cleanup
os.unlink(temp_collection_path)
- shutil.rmtree(temp_dir_path)
+ os.rmdir(temp_dir_path)
if __name__ == '__main__':