示例#1
0
def merge(base, local, remote, args=None):
    """Merge changes introduced by notebooks local and remote from a shared ancestor base.

    Return new (partially) merged notebook and unapplied diffs from the local and remote side.
    """
    if args and args.log_level == "DEBUG":
        # log pretty-print config object:
        config = PrettyPrintConfig()
        for (name, nb) in [("base", base), ("local", local),
                           ("remote", remote)]:
            nbdime.log.debug("In merge, input %s notebook:", name)
            config.out = StringIO()
            pretty_print_notebook(nb, config)
            nbdime.log.debug(config.out.getvalue())

    decisions = decide_merge(base, local, remote, args)

    merged = apply_decisions(base, decisions)

    if args and args.log_level == "DEBUG":
        nbdime.log.debug("In merge, merged notebook:")
        config.out = StringIO()
        pretty_print_notebook(merged, config)
        nbdime.log.debug(config.out.getvalue())
        nbdime.log.debug("End merge")

    return merged, decisions
示例#2
0
def main_show(args):

    if len(args.notebook) == 1 and args.notebook[0] == "-":
        files = [sys.stdin]
    else:
        for fn in args.notebook:
            if not os.path.exists(fn):
                print("Missing file {}".format(fn))
                return 1
        files = args.notebook
        if not files:
            print("Missing filenames.")
            return 1

    for fn in files:
        nb = nbformat.read(fn, as_version=4)

        # This printer is to keep the unit tests passing,
        # some tests capture output with capsys which doesn't
        # pick up on sys.stdout.write()
        class Printer:
            def write(self, text):
                print(text, end="")

        # This configures which parts to include/ignore
        config = PrettyPrintConfig(out=Printer(), include=args)

        if len(args.notebook) > 1:
            # 'more' prints filenames with colons, should be good enough for us as well
            print(":" * 14)
            print(fn)
            print(":" * 14)
        pretty_print_notebook(nb, config)

    return 0
    def diff_nbnode_with_cache(self,
                               pk: int,
                               nb: nbf.NotebookNode,
                               uri: str = "",
                               as_str=False,
                               **kwargs):
        """Return a diff of a notebook to a cached one.

        Note: this will not diff markdown content, since it is not stored in the cache.
        """
        import nbdime
        from nbdime.prettyprint import pretty_print_diff, PrettyPrintConfig

        cached_nb = self.get_cache_bundle(pk).nb
        nb, _ = self.create_hashed_notebook(nb)

        diff = nbdime.diff_notebooks(cached_nb, nb)
        if not as_str:
            return diff
        stream = io.StringIO()
        stream.writelines(
            ["nbdiff\n", f"--- cached pk={pk}\n", f"+++ other: {uri}\n"])
        pretty_print_diff(cached_nb, diff, "nb",
                          PrettyPrintConfig(out=stream, **kwargs))
        return stream.getvalue()
示例#4
0
def handle_agreed_deletion(base_fn, output_fn, print_decisions=False):
    """Handle merge when file has been deleted both locally and remotely"""
    assert base_fn != EXPLICIT_MISSING_FILE, (
        'sanity check failed: cannot have agreed decision on base %r' %
        base_fn)
    b = read_notebook(base_fn, on_null='minimal')
    if print_decisions:
        # Print merge decision (delete all)
        from nbdime.diffing.notebooks import diff_notebooks
        from nbdime.merging.decisions import MergeDecisionBuilder
        # Build diff for deleting all content:
        diff = diff_notebooks(b, {})
        # Make agreed decision from diff:
        bld = MergeDecisionBuilder()
        bld.agreement([], local_diff=diff, remote_diff=diff)
        decisions = bld.validated(b)
        # Print decition
        config = PrettyPrintConfig(out=io.StringIO())
        pretty_print_merge_decisions(b, decisions, config=config)
        nbdime.log.warning("Decisions:\n%s", out.getvalue())

    elif output_fn:
        # Delete file if existing, if not do nothing
        if os.path.exists(output_fn):
            os.remove(output_fn)
            nbdime.log.info("Output file deleted: %s", output_fn)
示例#5
0
def main_merge(args):
    bfn = args.base
    lfn = args.local
    rfn = args.remote
    mfn = args.out

    from .args import process_diff_flags
    process_diff_flags(args)

    for fn in (bfn, lfn, rfn):
        if not os.path.exists(fn) and fn != EXPLICIT_MISSING_FILE:
            nbdime.log.error("Cannot find file '%s'", fn)
            return 1

    if lfn == rfn == EXPLICIT_MISSING_FILE:
        # Deleted both locally and remotely
        # Special case not well solved by routines below
        handle_agreed_deletion(bfn, mfn, args.decisions)
        # Agreed on deletion = no conflics = return 0
        return 0

    b = read_notebook(bfn, on_null='minimal')
    l = read_notebook(lfn, on_null='minimal')
    r = read_notebook(rfn, on_null='minimal')

    merged, decisions = merge_notebooks(b, l, r, args)
    conflicted = [d for d in decisions if d.conflict]

    returncode = 1 if conflicted else 0

    if conflicted:
        nbdime.log.warning("Conflicts occured during merge operation.")
    else:
        nbdime.log.debug(
            "Merge completed successfully with no unresolvable conflicts.")

    if args.decisions:
        # Print merge decisions (including unconflicted)
        config = PrettyPrintConfig(out=io.StringIO())
        pretty_print_merge_decisions(b, decisions, config=config)
        nbdime.log.warning("Decisions:\n%s", config.out.getvalue())
    elif mfn:
        # Write partial or fully completed merge to given foo.ipynb filename
        with io.open(mfn, "w", encoding="utf8"):
            nbformat.write(merged, mfn)
        nbdime.log.info("Merge result written to %s", mfn)
    else:
        # Write merged notebook to terminal
        nbformat.write(merged, sys.stdout)
    return returncode
示例#6
0
def decide_merge(base, local, remote, args=None):
    # Build merge strategies for each document path from arguments
    strategies = merge_strategies(args)

    # Compute diffs
    local_diffs = diff_dicts(base, local)
    remote_diffs = diff_dicts(base, remote)

    # Debug outputs
    if args and args.log_level == "DEBUG":
        # log pretty-print config object:
        config = PrettyPrintConfig()

        nbdime.log.debug("In merge, base-local diff:")
        config.out = StringIO()
        pretty_print_notebook_diff("<base>", "<local>", base, local_diffs,
                                   config)
        nbdime.log.debug(config.out.getvalue())

        nbdime.log.debug("In merge, base-remote diff:")
        config.out = StringIO()
        pretty_print_notebook_diff("<base>", "<remote>", base, remote_diffs,
                                   config)
        nbdime.log.debug(config.out.getvalue())

    # Execute a generic merge operation
    decisions = decide_merge_with_diff(base, local, remote, local_diffs,
                                       remote_diffs, strategies)

    # Debug outputs
    if args and args.log_level == "DEBUG":
        nbdime.log.debug("In merge, decisions:")
        config.out = StringIO()
        pretty_print_merge_decisions(base, decisions, config)
        nbdime.log.debug(config.out.getvalue())

    return decisions
示例#7
0
def _handle_diff(base, remote, output, args):
    """Handles diffs of files, either as filenames or file-like objects"""
    # Check that if args are filenames they either exist, or are
    # explicitly marked as missing (added/removed):
    for fn in (base, remote):
        if (isinstance(fn, string_types) and not os.path.exists(fn)
                and fn != EXPLICIT_MISSING_FILE):
            print("Missing file {}".format(fn))
            return 1
    # Both files cannot be missing
    assert not (base == EXPLICIT_MISSING_FILE and remote
                == EXPLICIT_MISSING_FILE), ('cannot diff %r against %r' %
                                            (base, remote))

    # Perform actual work:
    a = read_notebook(base, on_null='empty')
    b = read_notebook(remote, on_null='empty')

    d = diff_notebooks(a, b)

    # Output as JSON to file, or print to stdout:
    if output:
        with open(output, "w") as df:
            # Compact version:
            #json.dump(d, df)
            # Verbose version:
            json.dump(d, df, indent=2, separators=(",", ": "))
    else:
        # This printer is to keep the unit tests passing,
        # some tests capture output with capsys which doesn't
        # pick up on sys.stdout.write()
        class Printer:
            def write(self, text):
                print(text, end="")

        # This sets up what to ignore:
        config = PrettyPrintConfig(out=Printer(),
                                   include=args,
                                   color_words=args.color_words)
        # Separate out filenames:
        base_name = base if isinstance(base, string_types) else base.name
        remote_name = remote if isinstance(remote,
                                           string_types) else remote.name
        pretty_print_notebook_diff(base_name, remote_name, a, d, config)

    return 0
示例#8
0
def main_patch(args):
    base_filename = args.base
    patch_filename = args.patch
    output_filename = args.output

    for fn in (base_filename, patch_filename):
        if not os.path.exists(fn) and fn != EXPLICIT_MISSING_FILE:
            print("Missing file {}".format(fn))
            return 1

    before = read_notebook(base_filename, on_null='empty')
    with io.open(patch_filename, encoding="utf8") as patch_file:
        diff = json.load(patch_file)
    diff = to_diffentry_dicts(diff)

    after = patch_notebook(before, diff)

    if output_filename:
        nbformat.write(after, output_filename)
    else:
        try:
            nbformat.validate(after, version=4)
        except nbformat.ValidationError:
            print("Patch result is not a valid notebook, printing as JSON:")
            json.dump(after, sys.stdout)
        else:
            # This printer is to keep the unit tests passing,
            # some tests capture output with capsys which doesn't
            # pick up on sys.stdout.write()
            class Printer:
                def write(self, text):
                    print(text, end="")

            config = PrettyPrintConfig(out=Printer())

            pretty_print_notebook(after, config=config)

    return 0