From c7ec3c338c327c60c32d7f6680a6860a843aeb45 Mon Sep 17 00:00:00 2001 From: Tim Diels Date: Tue, 31 May 2011 10:47:36 +0200 Subject: [PATCH] Made DownloadSource and UnpackArchive stateless using a 'StepCommand' --- zeroinstall/injector/fetch.py | 12 +++++----- zeroinstall/injector/model.py | 53 +++++++++++++++++++++++++------------------ 2 files changed, 37 insertions(+), 28 deletions(-) diff --git a/zeroinstall/injector/fetch.py b/zeroinstall/injector/fetch.py index 11c4cbd..94c616b 100644 --- a/zeroinstall/injector/fetch.py +++ b/zeroinstall/injector/fetch.py @@ -104,7 +104,7 @@ class Fetcher(object): # Maybe we're taking this metaphor too far? # Start preparing all steps - blockers = [step.prepare(self, force, impl_hint) for step in recipe.steps] + step_commands = [step.prepare(self, force, impl_hint) for step in recipe.steps] # Create an empty directory for the new implementation store = stores.stores[0] @@ -112,13 +112,13 @@ class Fetcher(object): try: # Run steps - valid_blockers = [b for b in blockers if b is not None] - for step, blocker in zip(recipe.steps, blockers): - if blocker: - while not blocker.happened: + valid_blockers = [s.blocker for s in step_commands if s.blocker is not None] + for step_command in step_commands: + if step_command.blocker: + while not step_command.blocker.happened: yield valid_blockers tasks.check(valid_blockers) - step.run(tmpdir) + step_command.run(tmpdir) # Check that the result is correct and store it in the cache store.check_manifest_and_rename(required_digest, tmpdir) diff --git a/zeroinstall/injector/model.py b/zeroinstall/injector/model.py index 4bb9315..ec30ba3 100644 --- a/zeroinstall/injector/model.py +++ b/zeroinstall/injector/model.py @@ -404,7 +404,7 @@ class RetrievalMethod(object): class DownloadSource(RetrievalMethod): """A DownloadSource provides a way to fetch an implementation.""" - __slots__ = ['implementation', 'url', 'size', 'extract', 'start_offset', 'type', '_stream'] + __slots__ = ['implementation', 'url', 'size', 'extract', 'start_offset', 'type'] def __init__(self, implementation, url, size, extract, start_offset = 0, type = None): self.implementation = implementation @@ -413,20 +413,23 @@ class DownloadSource(RetrievalMethod): self.extract = extract self.start_offset = start_offset self.type = type # MIME type - see unpack.py - self._stream = None def prepare(self, fetcher, force, impl_hint): - blocker, self._stream = fetcher.download_archive(self, force = force, impl_hint = impl_hint) - assert self._stream, 'lol' - return blocker - - def run(self, tmpdir): - assert self._stream, 'You must prepare the download step first' - self._stream.seek(0) - unpack.unpack_archive_over(self.url, self._stream, tmpdir, - extract = self.extract, - type = self.type, - start_offset = self.start_offset or 0) + + class StepCommand(object): + __slots__ = ['blocker', '_stream'] + + def __init__(s): + s.blocker, s._stream = fetcher.download_archive(self, force = force, impl_hint = impl_hint) + + def run(s, tmpdir): + s._stream.seek(0) + unpack.unpack_archive_over(self.url, s._stream, tmpdir, + extract = self.extract, + type = self.type, + start_offset = self.start_offset or 0) + return StepCommand() + class UnpackArchive(object): """An UnpackArchive step provides unpacks/extracts an archive. @@ -440,17 +443,23 @@ class UnpackArchive(object): self.type = type def prepare(self, fetcher, force, impl_hint): - return None - def run(self, tmpdir): - path = os.path.join(tmpdir, self.path) - stream = open(path, 'rb') - stream.seek(0) + class StepCommand(object): + __slots__ = ['blocker'] + + def __init__(s): + s.blocker = None + + def run(s, tmpdir): + path = os.path.join(tmpdir, self.path) + stream = open(path, 'rb') + stream.seek(0) - unpack.unpack_archive_over(path, stream, tmpdir, - extract = self.extract, - type = self.type, - start_offset = 0) + unpack.unpack_archive_over(path, stream, tmpdir, + extract = self.extract, + type = self.type, + start_offset = 0) + return StepCommand() class Recipe(RetrievalMethod): """Get an implementation by following a series of steps. -- 2.11.4.GIT