X-Git-Url: https://scripts.mit.edu/gitweb/wizard.git/blobdiff_plain/523af9325474d8be6ec72958e38cd460beaf5615..319f82896fb658d9bbaf8d01d6c2c7410eb967c7:/wizard/resolve.py diff --git a/wizard/resolve.py b/wizard/resolve.py index fd9d12d..4460cc6 100644 --- a/wizard/resolve.py +++ b/wizard/resolve.py @@ -12,10 +12,10 @@ for resolving conflicts in configuration files. The conflict resolution DSL is described here: Resolutions are specified as input-output pairs. An input -is a string with the conflict resolution markers ("<" * 7, -"=" * 7 and ">" * 7), with the HEAD content above the equals +is a string with the conflict resolution markers ``("<" * 7, +"=" * 7 and ">" * 7)``, with the HEAD content above the equals divider, and the upstream content below the equals divider. -Lines can also be marked as "***N***" where N is a natural +Lines can also be marked as ``***N***`` where N is a natural number greater than 0 (i.e. 1 or more), which means that an arbitrary number of lines may be matched and available for output. @@ -97,6 +97,10 @@ def spec_to_regex(spec): return ("<<<<<<<[^\n]*\\\n" + ours_regex + "\|\|\|\|\|\|\|\\\n" + common_regex + "=======\\\n" + theirs_regex + ">>>>>>>[^\n]*(\\\n|$)", ours_mappings) def result_to_repl(result, mappings): + """ + Converts a list of replacement strings and or references + into a replacement string appropriate for a regular expression. + """ def ritem_to_string(r): if type(r) is int: return "\\%d" % mappings[r] @@ -105,6 +109,10 @@ def result_to_repl(result, mappings): return "".join(map(ritem_to_string, result)) def resolve(contents, spec, result): + """ + Given a conflicted file, whose contents are ``contents``, attempt + to resolve all conflicts that match ``spec`` with ``result``. + """ rstring, mappings = spec_to_regex(spec) regex = re.compile(rstring, re.DOTALL) repl = result_to_repl(result, mappings) @@ -131,23 +139,9 @@ def resolve(contents, spec, result): return ret def is_conflict(contents): + """ + Given ``contents``, return ``True`` if there are any conflicts in it. + """ # Really really simple heuristic return "<<<<<<<" in contents -def fix_newlines(file, log=True): - """ - Normalizes newlines in a file into UNIX file endings. If - ``log`` is ``True`` an info log mesage is printed if - any normalization occurs. Return value is ``True`` if - normalization occurred. - """ - old_contents = open(file, "r").read() - contents = old_contents - while "\r\n" in contents: - contents = contents.replace("\r\n", "\n") - contents = contents.replace("\r", "\n") - if contents != old_contents: - logging.info("Converted %s to UNIX file endings" % file) - open(file, "w").write(contents) - return True - return False