[Reproducible-commits] [diffoscope] 13/17: Allow .get_reverse() on Futures

Jérémy Bobbio lunar at moszumanska.debian.org
Tue Dec 8 18:15:21 UTC 2015


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

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

commit 26bc41034918e048123e3062f3766a9647a0a5b4
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, 34 insertions(+), 5 deletions(-)

diff --git a/diffoscope/comparators/utils.py b/diffoscope/comparators/utils.py
index c6f43a7..a07c12c 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
 
 
@@ -209,7 +209,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 f47d57b..1c3563f 100644
--- a/diffoscope/difference.py
+++ b/diffoscope/difference.py
@@ -261,6 +261,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):
@@ -359,7 +390,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
@@ -404,8 +435,6 @@ class Difference(object):
 
     @property
     def unified_diff(self):
-        #if isinstance(self._unified_diff, Future):
-        #    self._unified_diff = self._unified_diff.result()
         return self._unified_diff
 
     @unified_diff.setter

-- 
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