Exemplo n.º 1
0
    def testDiff(self):
        assert Diff.diff(
            [],
            ["one", "two", "three"]
        ) == [("+", ["one", "two","three"])]

        assert Diff.diff(
            ["one", "two", "three"],
            ["one", "two", "three", "four", "five"]
        ) == [("=", 11), ("+", ["four", "five"])]

        assert Diff.diff(
            ["one", "two", "three", "six"],
            ["one", "two", "three", "four", "five", "six"]
        ) == [("=", 11), ("+", ["four", "five"]), ("=", 3)]

        assert Diff.diff(
            ["one", "two", "three", "hmm", "six"],
            ["one", "two", "three", "four", "five", "six"]
        ) == [("=", 11), ("-", 3), ("+", ["four", "five"]), ("=", 3)]

        assert Diff.diff(
            ["one", "two", "three"],
            []
        ) == [("-", 11)]
Exemplo n.º 2
0
 def getDiffs(self, inner_path, limit=30 * 1024, update_files=True):
     if inner_path not in self.contents:
         return {}
     diffs = {}
     content_inner_path_dir = helper.getDirname(inner_path)
     for file_relative_path in self.contents[inner_path].get("files", {}):
         file_inner_path = content_inner_path_dir + file_relative_path
         if self.site.storage.isFile(file_inner_path + "-new"):  # New version present
             diffs[file_relative_path] = Diff.diff(
                 list(self.site.storage.open(file_inner_path)),
                 list(self.site.storage.open(file_inner_path + "-new")),
                 limit=limit
             )
             if update_files:
                 self.site.storage.delete(file_inner_path)
                 self.site.storage.rename(file_inner_path + "-new", file_inner_path)
         if self.site.storage.isFile(file_inner_path + "-old"):  # Old version present
             diffs[file_relative_path] = Diff.diff(
                 list(self.site.storage.open(file_inner_path + "-old")),
                 list(self.site.storage.open(file_inner_path)),
                 limit=limit
             )
             if update_files:
                 self.site.storage.delete(file_inner_path + "-old")
     return diffs
Exemplo n.º 3
0
 def getDiffs(self, inner_path, limit=30 * 1024, update_files=True):
     if inner_path not in self.contents:
         return None
     diffs = {}
     content_inner_path_dir = helper.getDirname(inner_path)
     for file_relative_path in self.contents[inner_path].get("files", {}):
         file_inner_path = content_inner_path_dir + file_relative_path
         if self.site.storage.isFile(file_inner_path + "-new"):  # New version present
             diffs[file_relative_path] = Diff.diff(
                 list(self.site.storage.open(file_inner_path)),
                 list(self.site.storage.open(file_inner_path + "-new")),
                 limit=limit
             )
             if update_files:
                 self.site.storage.delete(file_inner_path)
                 self.site.storage.rename(file_inner_path + "-new", file_inner_path)
         if self.site.storage.isFile(file_inner_path + "-old"):  # Old version present
             diffs[file_relative_path] = Diff.diff(
                 list(self.site.storage.open(file_inner_path + "-old")),
                 list(self.site.storage.open(file_inner_path)),
                 limit=limit
             )
             if update_files:
                 self.site.storage.delete(file_inner_path + "-old")
     return diffs
Exemplo n.º 4
0
    def testDiff(self):
        assert Diff.diff(
            [],
            ["one", "two", "three"]
        ) == [("+", ["one", "two","three"])]

        assert Diff.diff(
            ["one", "two", "three"],
            ["one", "two", "three", "four", "five"]
        ) == [("=", 11), ("+", ["four", "five"])]

        assert Diff.diff(
            ["one", "two", "three", "six"],
            ["one", "two", "three", "four", "five", "six"]
        ) == [("=", 11), ("+", ["four", "five"]), ("=", 3)]

        assert Diff.diff(
            ["one", "two", "three", "hmm", "six"],
            ["one", "two", "three", "four", "five", "six"]
        ) == [("=", 11), ("-", 3), ("+", ["four", "five"]), ("=", 3)]

        assert Diff.diff(
            ["one", "two", "three"],
            []
        ) == [("-", 11)]
Exemplo n.º 5
0
def calculate():
    f = request.args.get('f')
    start_point = request.args.get('start_point')
    eps = request.args.get('eps')
    option = request.args.get('option')
    if start_point is not None:
        start_point = [float(x) for x in start_point.split()]
    if eps is not None:
        eps = float(eps)
    find_min = True
    if option == 'max':
        find_min = False
    descent = GradientDescent(f, start_point, eps, find_min)
    length = Diff.length(descent.get_start_point())
    diff_by_direction = descent.calculate_diff_by_direction(length)
    mess = 'not monotonically'
    if Diff.is_increasing(diff_by_direction):
        mess = 'monotonically increasing'
    if Diff.is_decreasing(diff_by_direction):
        mess = 'monotonically decreasing'
    res = descent.calculate()
    report = descent.get_report()
    return render_template('index.html',
                           descent=descent,
                           point=' '.join(map(str, descent.get_start_point())),
                           length=length,
                           diff_by_direction=diff_by_direction,
                           mess=mess,
                           report=report,
                           res='; '.join(map(str, res)))
Exemplo n.º 6
0
 def testPatch(self):
     old_f = StringIO.StringIO("one\ntwo\nthree\nhmm\nsix")
     new_f = StringIO.StringIO("one\ntwo\nthree\nfour\nfive\nsix")
     actions = Diff.diff(
         list(old_f),
         list(new_f)
     )
     old_f.seek(0)
     assert Diff.patch(old_f, actions).getvalue() == new_f.getvalue()
Exemplo n.º 7
0
 def testPatch(self):
     old_f = io.BytesIO(b"one\ntwo\nthree\nhmm\nsix")
     new_f = io.BytesIO(b"one\ntwo\nthree\nfour\nfive\nsix")
     actions = Diff.diff(
         list(old_f),
         list(new_f)
     )
     old_f.seek(0)
     assert Diff.patch(old_f, actions).getvalue() == new_f.getvalue()
Exemplo n.º 8
0
    def testDiffLimit(self):
        old_f = io.BytesIO(b"one\ntwo\nthree\nhmm\nsix")
        new_f = io.BytesIO(b"one\ntwo\nthree\nfour\nfive\nsix")
        actions = Diff.diff(list(old_f), list(new_f), limit=1024)
        assert actions

        old_f = io.BytesIO(b"one\ntwo\nthree\nhmm\nsix")
        new_f = io.BytesIO(b"one\ntwo\nthree\nfour\nfive\nsix" * 1024)
        actions = Diff.diff(list(old_f), list(new_f), limit=1024)
        assert actions is False
Exemplo n.º 9
0
    def testDiffLimit(self):
        old_f = StringIO.StringIO("one\ntwo\nthree\nhmm\nsix")
        new_f = StringIO.StringIO("one\ntwo\nthree\nfour\nfive\nsix")
        actions = Diff.diff(list(old_f), list(new_f), limit=1024)
        assert actions

        old_f = StringIO.StringIO("one\ntwo\nthree\nhmm\nsix")
        new_f = StringIO.StringIO("one\ntwo\nthree\nfour\nfive\nsix"*1024)
        actions = Diff.diff(list(old_f), list(new_f), limit=1024)
        assert actions is False
Exemplo n.º 10
0
    def downloadContent(self, inner_path, download_files=True, peer=None, check_modifications=False, diffs={}):
        s = time.time()
        if config.verbose:
            self.log.debug("Downloading %s..." % inner_path)
        found = self.needFile(inner_path, update=self.bad_files.get(inner_path))
        content_inner_dir = helper.getDirname(inner_path)
        if not found:
            self.log.debug("Download %s failed, check_modifications: %s" % (inner_path, check_modifications))
            if check_modifications:  # Download failed, but check modifications if its succed later
                self.onFileDone.once(lambda file_name: self.checkModifications(0), "check_modifications")
            return False  # Could not download content.json

        if config.verbose:
            self.log.debug("Got %s" % inner_path)
        changed, deleted = self.content_manager.loadContent(inner_path, load_includes=False)

        if peer:  # Update last received update from peer to prevent re-sending the same update to it
            peer.last_content_json_update = self.content_manager.contents[inner_path]["modified"]

        # Start download files
        file_threads = []
        if download_files:
            for file_relative_path in self.content_manager.contents[inner_path].get("files", {}).keys():
                file_inner_path = content_inner_dir + file_relative_path

                # Try to diff first
                diff_success = False
                diff_actions = diffs.get(file_relative_path)
                if diff_actions and self.bad_files.get(file_inner_path):
                    try:
                        new_file = Diff.patch(self.storage.open(file_inner_path, "rb"), diff_actions)
                        new_file.seek(0)
                        diff_success = self.content_manager.verifyFile(file_inner_path, new_file)
                        if diff_success:
                            self.log.debug("Patched successfully: %s" % file_inner_path)
                            new_file.seek(0)
                            self.storage.write(file_inner_path, new_file)
                            self.onFileDone(file_inner_path)
                    except Exception, err:
                        self.log.debug("Failed to patch %s: %s" % (file_inner_path, err))
                        diff_success = False

                if not diff_success:
                    # Start download and dont wait for finish, return the event
                    res = self.needFile(file_inner_path, blocking=False, update=self.bad_files.get(file_inner_path), peer=peer)
                    if res is not True and res is not False:  # Need downloading and file is allowed
                        file_threads.append(res)  # Append evt

            # Optionals files
            if inner_path == "content.json":
                gevent.spawn(self.updateHashfield)

            if self.settings.get("autodownloadoptional"):
                for file_relative_path in self.content_manager.contents[inner_path].get("files_optional", {}).keys():
                    file_inner_path = content_inner_dir + file_relative_path
                    # Start download and dont wait for finish, return the event
                    res = self.needFile(file_inner_path, blocking=False, update=self.bad_files.get(file_inner_path), peer=peer)
                    if res is not True and res is not False:  # Need downloading and file is allowed
                        file_threads.append(res)  # Append evt
Exemplo n.º 11
0
    def downloadContent(self, inner_path, download_files=True, peer=None, check_modifications=False, diffs={}):
        s = time.time()
        if config.verbose:
            self.log.debug("Downloading %s..." % inner_path)
        found = self.needFile(inner_path, update=self.bad_files.get(inner_path))
        content_inner_dir = helper.getDirname(inner_path)
        if not found:
            self.log.debug("Download %s failed, check_modifications: %s" % (inner_path, check_modifications))
            if check_modifications:  # Download failed, but check modifications if its succed later
                self.onFileDone.once(lambda file_name: self.checkModifications(0), "check_modifications")
            return False  # Could not download content.json

        if config.verbose:
            self.log.debug("Got %s" % inner_path)
        changed, deleted = self.content_manager.loadContent(inner_path, load_includes=False)

        if peer:  # Update last received update from peer to prevent re-sending the same update to it
            peer.last_content_json_update = self.content_manager.contents[inner_path]["modified"]

        # Start download files
        file_threads = []
        if download_files:
            for file_relative_path in self.content_manager.contents[inner_path].get("files", {}).keys():
                file_inner_path = content_inner_dir + file_relative_path

                # Try to diff first
                diff_success = False
                diff_actions = diffs.get(file_relative_path)
                if diff_actions and self.bad_files.get(file_inner_path):
                    try:
                        new_file = Diff.patch(self.storage.open(file_inner_path, "rb"), diff_actions)
                        new_file.seek(0)
                        diff_success = self.content_manager.verifyFile(file_inner_path, new_file)
                        if diff_success:
                            self.log.debug("Patched successfully: %s" % file_inner_path)
                            new_file.seek(0)
                            self.storage.write(file_inner_path, new_file)
                            self.onFileDone(file_inner_path)
                    except Exception, err:
                        self.log.debug("Failed to patch %s: %s" % (file_inner_path, err))
                        diff_success = False

                if not diff_success:
                    # Start download and dont wait for finish, return the event
                    res = self.needFile(file_inner_path, blocking=False, update=self.bad_files.get(file_inner_path), peer=peer)
                    if res is not True and res is not False:  # Need downloading and file is allowed
                        file_threads.append(res)  # Append evt

            # Optionals files
            if inner_path == "content.json":
                gevent.spawn(self.updateHashfield)

            if self.settings.get("autodownloadoptional"):
                for file_relative_path in self.content_manager.contents[inner_path].get("files_optional", {}).keys():
                    file_inner_path = content_inner_dir + file_relative_path
                    # Start download and dont wait for finish, return the event
                    res = self.needFile(file_inner_path, blocking=False, update=self.bad_files.get(file_inner_path), peer=peer)
                    if res is not True and res is not False:  # Need downloading and file is allowed
                        file_threads.append(res)  # Append evt
Exemplo n.º 12
0
 def testUtf8(self):
     assert Diff.diff(
         ["one", "\xe5\xad\xa6\xe4\xb9\xa0\xe4\xb8\x8b", "two", "three"], [
             "one", "\xe5\xad\xa6\xe4\xb9\xa0\xe4\xb8\x8b", "two", "three",
             "four", "five"
         ]) == [("=", 20), ("+", ["four", "five"])]
Exemplo n.º 13
0
 def calculate_diff_by_direction(self, length):
     return Diff.diff_by_direction(self.__calculated_grad, self.__start_point, length)