diff --git a/tuf/interposition/configuration.py b/tuf/interposition/configuration.py index 5a66e4af..295ccac6 100644 --- a/tuf/interposition/configuration.py +++ b/tuf/interposition/configuration.py @@ -1,5 +1,4 @@ import os.path -import tempfile import types import urlparse @@ -43,7 +42,6 @@ def __init__(self, hostname, port, repository_directory, repository_mirrors, self.repository_mirrors = repository_mirrors self.target_paths = target_paths self.ssl_certificates = ssl_certificates - self.tempdir = tempfile.mkdtemp() def __repr__(self): diff --git a/tuf/interposition/updater.py b/tuf/interposition/updater.py index 1a54ac47..15a789f7 100644 --- a/tuf/interposition/updater.py +++ b/tuf/interposition/updater.py @@ -2,6 +2,7 @@ import os.path import re import shutil +import tempfile import urllib import urlparse @@ -37,7 +38,12 @@ class Updater(object): def __init__(self, configuration): + CREATED_TEMPDIR_MESSAGE = "Created temporary directory at {tempdir}" + self.configuration = configuration + # A temporary directory used for this updater over runtime. + self.tempdir = tempfile.mkdtemp() + Logger.debug(CREATED_TEMPDIR_MESSAGE.format(tempdir=self.tempdir)) # must switch context before instantiating updater # because updater depends on some module (tuf.conf) variables @@ -46,11 +52,19 @@ def __init__(self, configuration): self.configuration.repository_mirrors) + def cleanup(self): + """Clean up after certain side effects, such as temporary directories.""" + + DELETED_TEMPDIR_MESSAGE = "Deleted temporary directory at {tempdir}" + shutil.rmtree(self.tempdir) + Logger.debug(DELETED_TEMPDIR_MESSAGE.format(tempdir=self.tempdir)) + + def download_target(self, target_filepath): """Downloads target with TUF as a side effect.""" # download file into a temporary directory shared over runtime - destination_directory = self.configuration.tempdir + destination_directory = self.tempdir filename = os.path.join(destination_directory, target_filepath) self.switch_context() # switch TUF context @@ -314,8 +328,12 @@ def remove(self, configuration): assert configuration.hostname in self.__updaters assert repository_mirror_hostnames.issubset(self.__repository_mirror_hostnames) + # Get the updater. + updater = self.__updaters.get(configuration.hostname) + # If all is well, remove the stored Updater as well as its associated # repository mirror hostnames. + updater.cleanup() del self.__updaters[configuration.hostname] self.__repository_mirror_hostnames.difference_update(repository_mirror_hostnames) diff --git a/tuf/interposition/utility.py b/tuf/interposition/utility.py index c68ad777..8e1c4cd9 100644 --- a/tuf/interposition/utility.py +++ b/tuf/interposition/utility.py @@ -23,6 +23,11 @@ class Logger(object): __logger = logging.getLogger("tuf.interposition") + @staticmethod + def debug(message): + Logger.__logger.debug(message) + + @staticmethod def exception(message): Logger.__logger.exception(message)