From d0c3df0e2c2a0c9ccc81827f01b4cc95e68dffb0 Mon Sep 17 00:00:00 2001 From: "Bryn M. Reeves" Date: Mon, 10 Jun 2013 16:41:19 +0100 Subject: Add Archive.cleanup() Add a cleanup() method to the Archive classes and ensure that it is called during normal and abnormal termination (unless the pdb debugger has been invoked during --debug mode). Signed-off-by: Bryn M. Reeves --- sos/archive.py | 10 ++++++++-- sos/sosreport.py | 4 +++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/sos/archive.py b/sos/archive.py index 376d7069..8e391c3f 100644 --- a/sos/archive.py +++ b/sos/archive.py @@ -59,6 +59,10 @@ class Archive(object): def add_dir(self, path): raise NotImplementedError + def cleanup(self): + """Clean up any temporary resources used by an Archive class.""" + pass + def finalize(self, method): """Finalize an archive object via method. This may involve creating An archive that is subsequently compressed or simply closing an @@ -141,10 +145,13 @@ class FileCacheArchive(Archive): path = self.dest_path(path) return open(path, "r") + def cleanup(self): + shutil.rmtree(self._archive_root) + def finalize(self, method): self.log.debug("finalizing archive %s" % self._archive_root) - #print "finalizing archive %s" % self._archive_root self._build_archive() + self.cleanup() self.log.debug("built archive at %s (size=%d)" % (self._archive_path, os.stat(self._archive_path).st_size)) return self._compress() @@ -205,7 +212,6 @@ class TarFileArchive(FileCacheArchive): tar.add(os.path.split(self._name)[1], filter=self.copy_permissions_filter) tar.close() os.chdir(old_pwd) - shutil.rmtree(self._archive_root) def _compress(self): methods = ['xz', 'bzip2', 'gzip'] diff --git a/sos/sosreport.py b/sos/sosreport.py index b6b4cbf7..0f162cf4 100644 --- a/sos/sosreport.py +++ b/sos/sosreport.py @@ -1155,7 +1155,9 @@ class SoSReport(object): self.version() return self.final_work() - except SystemExit: + except (SystemExit, KeyboardInterrupt): + self.archive.cleanup() + self.tempfile_util.clean() return False def main(args): -- cgit