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)
def _handle_agreed_deletion(base_fn, output_fn, args=None): """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_package_json(base_fn) if args and args.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 = prettyprint_config_from_args(args, out=io.StringIO()) pretty_print_merge_decisions(b, decisions, config=config) logger.warning("Decisions:\n%s", config.out.getvalue()) elif output_fn: # Delete file if existing, if not do nothing if os.path.exists(output_fn): os.remove(output_fn) logger.info("Output file deleted: %s", output_fn)
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)
def main_merge(args): bfn = args.base lfn = args.local rfn = args.remote mfn = args.out from nbdime.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: logger.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) # Agreed on deletion = no conflics = return 0 return 0 formatting = detect(bfn) or detect(lfn) or detect(rfn) b = read_package_json(bfn) l = read_package_json(lfn) r = read_package_json(rfn) merged, decisions = merge(b, l, r, args) conflicted = [d for d in decisions if d.conflict] returncode = 1 if conflicted else 0 if conflicted: logger.warning("Conflicts occured during merge operation.") else: logger.debug( "Merge completed successfully with no unresolvable conflicts.") if args.decisions: # Print merge decisions (including unconflicted) config = prettyprint_config_from_args(args, out=io.StringIO()) pretty_print_merge_decisions(b, decisions, config=config) logger.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", newline=formatting.newline) as f: json.dump(merged, f, indent=formatting.indent) f.write(formatting.newline) logger.info("Merge result written to %s", mfn) else: # Write merged notebook to terminal json.dump(merged, sys.stdout) return returncode
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) out = io.StringIO() pretty_print_merge_decisions(b, decisions, out=out) nbdime.log.warning("Decisions:\n%s", 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
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
def main_merge(args): bfn = args.base lfn = args.local rfn = args.remote mfn = args.output for fn in (bfn, lfn, rfn): if not os.path.exists(fn): nbdime.log.error("Cannot find file '{}'".format(fn)) return 1 b = nbformat.read(bfn, as_version=4) l = nbformat.read(lfn, as_version=4) r = nbformat.read(rfn, as_version=4) 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) out = io.StringIO() pretty_print_merge_decisions(b, decisions, out=out) nbdime.log.warning("Conflicts:\n%s", out.getvalue()) elif mfn: # Write partial or fully completed merge to given foo.ipynb filename with io.open(mfn, "w", encoding="utf8") as mf: 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
def main_merge(args): bfn = args.base lfn = args.local rfn = args.remote mfn = args.output for fn in (bfn, lfn, rfn): if not os.path.exists(fn): nbdime.log.error("Cannot find file '{}'".format(fn)) return 1 b = nbformat.read(bfn, as_version=4) l = nbformat.read(lfn, as_version=4) r = nbformat.read(rfn, as_version=4) 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) out = io.StringIO() pretty_print_merge_decisions(b, decisions, out=out) nbdime.log.warning("Conflicts:\n%s", out.getvalue()) elif mfn: # Write partial or fully completed merge to given foo.ipynb filename with io.open(mfn, "w", encoding="utf8") as mf: 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
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