[Reproducible-commits] [diffoscope] 02/03: Allow .get_reverse() on Futures

Joachim Breitner nomeata at moszumanska.debian.org
Thu Dec 3 12:23:12 UTC 2015


This is an automated email from the git hooks/post-receive script.

nomeata pushed a commit to branch pu/parallel2
in repository diffoscope.

commit f98fe5cc3c32128781ebaeb14b743994f0b3c9b8
Author: Joachim Breitner <mail at joachim-breitner.de>
Date:   Thu Dec 3 13:12:50 2015 +0100

    Allow .get_reverse() on Futures
    
    by introducing the FutureDifference object, which defers the
    .get_reverse call until result() is going to be called..
---
 diffoscope/comparators/utils.py |  4 ++--
 diffoscope/difference.py        | 35 +++++++++++++++++++++++++++++++++--
 2 files changed, 35 insertions(+), 4 deletions(-)

diff --git a/diffoscope/comparators/utils.py b/diffoscope/comparators/utils.py
index 9c06f7a..995eebe 100644
--- a/diffoscope/comparators/utils.py
+++ b/diffoscope/comparators/utils.py
@@ -31,7 +31,7 @@ from threading import Thread
 import diffoscope.comparators
 from diffoscope.comparators.binary import File, NonExistingFile
 from diffoscope.config import Config
-from diffoscope.difference import Difference
+from diffoscope.difference import FutureDifference, Difference
 from diffoscope import logger, tool_required, get_temporary_directory
 
 
@@ -194,7 +194,7 @@ class Container(object, metaclass=ABCMeta):
                 yield NonExistingFile('/dev/null', other_file), other_file, NO_NOTIFICATION
 
     def compare(self, other, source=None):
-        return [Config.general.executor.submit(diffoscope.comparators.compare_files_with_notification, *args) for args in self.comparisons(other)]
+        return [FutureDifference(Config.general.executor.submit(diffoscope.comparators.compare_files_with_notification, *args)) for args in self.comparisons(other)]
 
 
 class ArchiveMember(File):
diff --git a/diffoscope/difference.py b/diffoscope/difference.py
index 6728718..4f80307 100644
--- a/diffoscope/difference.py
+++ b/diffoscope/difference.py
@@ -271,6 +271,37 @@ def diff(feeder1, feeder2):
         with fd_from_feeder(feeder2, end_nl_q2) as fd2:
             return run_diff(fd1, fd2, end_nl_q1, end_nl_q2)
 
+class FutureDifference(object):
+    """
+    We usually produce Futures in place of Differences, for parallelization.
+    These are never looked at until all of them are produced. Then
+    finish_threads is called, which replaces them by their .result().
+
+    There is, however, one operation that we do on Differences before:
+    get_reverse(). So this object allows a way to defer that to reverse time.
+    """
+
+    def __init__(self, future):
+        self._is_reversed = False
+        assert (isinstance(future, Future) or isinstance(future, FutureDifference))
+        self._future = future
+        self._difference = None
+
+    def result(self,*args):
+        if self._difference is None:
+            self._difference = self._future.result(*args)
+
+            if self._is_reversed:
+                self._difference = self._difference.get_reverse()
+
+        return self._difference
+
+    def get_reverse(self):
+        reversed_future = FutureDifference(self)
+        reversed_future._is_reverse = True
+        return reversed_future
+
+
 
 class Difference(object):
     def __init__(self, path1, path2, source=None, notification=None, comment=None):
@@ -386,7 +417,7 @@ class Difference(object):
     def finish_threads(self):
         finished_details = []
         for detail in self._details:
-            if isinstance(detail, Future):
+            if isinstance(detail, FutureDifference):
                 detail = detail.result()
             if not detail:
                 continue
@@ -416,7 +447,7 @@ class Difference(object):
 
     @property
     def unified_diff(self):
-        #if isinstance(self._unified_diff, Future):
+        #if isinstance(self._unified_diff, FutureDifference):
         #    self._unified_diff = self._unified_diff.result()
         return self._unified_diff
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/reproducible/diffoscope.git



More information about the Reproducible-commits mailing list