def unified_diff(filename, content2=None):
# type: (str, Optional[bytes]) -> Tuple[int, Iterable[str]]
"""This function prints a unified diff of the contents of
filename and the standard input, when used from the command line
as follows:
echo 123 > d.txt ; echo 456 | ./whatstyle.py --stdindiff d.txt
We get this result:
---
+++
@@ -1 +1 @@
-123
+456
"""
use_stdin = content2 is None
if content2 is None:
# Read binary input stream
stdin = rawstream(sys.stdin)
econtent2 = bytestr(stdin.read())
else:
econtent2 = content2
exit_code, diff = compute_unified_diff(filename, econtent2, lineterm='')
if use_stdin:
write('\n'.join(diff))
return exit_code, diff
python类unified_diff()的实例源码
def compute_unified_diff(filename, content2, **kwargs):
# type: (str, bytes, **Any) -> Tuple[int, Iterable[str]]
diff = () # type: Iterable[str]
exit_code = ERROR
kw = kwargs.copy()
if 'n' not in kwargs:
# zero context lines
kw['n'] = 0
try:
content1 = get_cached_file(filename)
if PY3:
c1 = unistr(content1)
c2 = unistr(content2)
else:
c1 = content1
c2 = content2
diff = difflib.unified_diff(c1.splitlines(True), c2.splitlines(True), **kw)
exit_code = OK
finally:
return exit_code, diff
# ---------------------------------------------------------------------
# Spare the user from specifying a formatter by finding a suitable one.
def openDiffInTab(viewHandle, edit, oldTextName, newTextName, oldText, newText):
diffs = difflib.unified_diff(oldText.splitlines(), newText.splitlines(), oldTextName, newTextName)
diffText = u"\n".join(line for line in diffs)
if diffText == "":
sublime.status_message("No changes between revisions.")
else:
scratch = viewHandle.window().new_file()
scratch.set_scratch(True)
scratch.set_name("{old} -> {new}".format(old = oldTextName, new = newTextName))
scratch.set_syntax_file("Packages/Diff/Diff.tmLanguage")
if (int(sublime.version()) >= 3000):
scratch.run_command("append", {"characters": diffText})
else:
scratch.insert(edit, 0, diffText)
def test_05_package_to_api2(self):
context = {"model": model,
"session": model.Session}
pkg = model.Session.query(model.Package).filter_by(name='annakarenina').first()
as_dict = pkg.as_dict(ref_package_by='id', ref_group_by='id')
dictize = package_to_api2(pkg, context)
as_dict_string = pformat(as_dict)
dictize_string = pformat(dictize)
print as_dict_string
print dictize_string
assert package_to_api2(pkg, context) == dictize, "\n".join(unified_diff(as_dict_string.split("\n"), dictize_string.split("\n")))
def test_06_package_to_api2_with_relationship(self):
context = {"model": model,
"session": model.Session}
pkg = model.Session.query(model.Package).filter_by(name='homer').one()
as_dict = pkg.as_dict(ref_package_by='id', ref_group_by='id')
as_dict['license_title'] = None
as_dict['num_tags'] = 0
as_dict['num_resources'] = 0
dictize = package_to_api2(pkg, context)
as_dict["relationships"].sort(key=lambda x:x.items())
dictize["relationships"].sort(key=lambda x:x.items())
# the is_dict method doesn't care about organizations
del dictize['organization']
as_dict_string = pformat(as_dict)
dictize_string = pformat(dictize)
print as_dict_string
print dictize_string
assert as_dict == dictize, "\n".join(unified_diff(as_dict_string.split("\n"), dictize_string.split("\n")))
def get_diff_po(po1_fn, po2_fn):
po1_lines = []
po2_lines = []
for entry in sorted(polib.pofile(po1_fn), key=lambda obj: obj.msgid):
po1_lines.append((
u'msgid {}\n\n'
u'msgstr {}\n\n'
).format(entry.msgid, entry.msgstr))
for entry in sorted(polib.pofile(po2_fn), key=lambda obj: obj.msgid):
po2_lines.append((
u'msgid {}\n\n'
u'msgstr {}\n\n'
).format(entry.msgid, entry.msgstr))
added = removed = 0
for diff_line in difflib.unified_diff(po1_lines, po2_lines):
if diff_line.startswith('+++ ') or diff_line.startswith('--- ') or diff_line.startswith('@@ '):
continue
if diff_line.startswith('+'):
added += 1
elif diff_line.startswith('-'):
removed += 1
return added + removed
def compare_configs(cfg1,cfg2):
d = difflib.unified_diff(cfg1, cfg2)
diffstr = ""
for line in d:
if line.find('Current configuration') == -1:
if line.find('Last configuration change') == -1:
if line.find('length 0') == -1:
if line.find('login authentication tacplus') == -1:
if (line.find("+++")==-1) and (line.find("---")==-1):
if (line.find("-!")==-1) and (line.find('+!')==-1):
if line.startswith('+'):
diffstr = diffstr + "\n" + line
elif line.startswith('-'):
diffstr = diffstr + "\n" + line
return diffstr
def _Diff(lhs, rhs):
"""Given two pathnames, compare two files. Raise if they differ."""
# Some people rely on being able to specify TEST_DIFF in the environment to
# have tests use their own diff wrapper for use when updating golden data.
external_diff = os.environ.get('TEST_DIFF')
if external_diff:
return _DiffViaExternalProgram(lhs, rhs, external_diff)
try:
with open(lhs, 'r') as lhs_f:
with open(rhs, 'r') as rhs_f:
diff_text = ''.join(
difflib.unified_diff(lhs_f.readlines(), rhs_f.readlines()))
if not diff_text:
return True
raise OutputDifferedError('\nComparing %s and %s\nTest output differed '
'from golden file:\n%s' % (lhs, rhs, diff_text))
except EnvironmentError as error:
# Unable to read the files.
raise DiffFailureError('\nComparing %s and %s\nFailure diffing test output '
'with golden file: %s\n' % (lhs, rhs, error))
def _Diff(lhs, rhs):
"""Given two pathnames, compare two files. Raise if they differ."""
# Some people rely on being able to specify TEST_DIFF in the environment to
# have tests use their own diff wrapper for use when updating golden data.
external_diff = os.environ.get('TEST_DIFF')
if external_diff:
return _DiffViaExternalProgram(lhs, rhs, external_diff)
try:
with open(lhs, 'r') as lhs_f:
with open(rhs, 'r') as rhs_f:
diff_text = ''.join(
difflib.unified_diff(lhs_f.readlines(), rhs_f.readlines()))
if not diff_text:
return True
raise OutputDifferedError('\nComparing %s and %s\nTest output differed '
'from golden file:\n%s' % (lhs, rhs, diff_text))
except EnvironmentError as error:
# Unable to read the files.
raise DiffFailureError('\nComparing %s and %s\nFailure diffing test output '
'with golden file: %s\n' % (lhs, rhs, error))
def get_diff_text(old, new, filename):
"""Return text of unified diff between old and new."""
newline = '\n'
diff = difflib.unified_diff(
old, new,
'original/' + filename,
'fixed/' + filename,
lineterm=newline)
text = ''
for line in diff:
text += line
# Work around missing newline (http://bugs.python.org/issue2142).
if text and not line.endswith(newline):
text += newline + r'\ No newline at end of file' + newline
return text
def text_diff(old_as_text, new_as_text): # {{{1
""" returns a unicode string containing a diff text showing
differences of the utf8 parameters """
old_as_text = smart_unicode( old_as_text ).splitlines()
new_as_text = smart_unicode( new_as_text ).splitlines()
text_diff = unified_diff(
old_as_text, new_as_text, n = 0, lineterm = "" )
# we now delete from the text diff all control lines
# TODO: when the description field of an event contains such lines,
# they will be deleted: avoid it.
text_diff = [line for line in text_diff if not
re.match(r"^---\s*$", line) and not
re.match(r"^\+\+\+\s*$", line) and not
re.match(r"^@@.*@@$", line)]
text_diff = u'\n'.join( text_diff )
return text_diff
def diff():
"""Show diff of files changed (between index and working copy)."""
changed, _, _ = get_status()
entries_by_path = {e.path: e for e in read_index()}
for i, path in enumerate(changed):
sha1 = entries_by_path[path].sha1.hex()
obj_type, data = read_object(sha1)
assert obj_type == 'blob'
index_lines = data.decode().splitlines()
working_lines = read_file(path).decode().splitlines()
diff_lines = difflib.unified_diff(
index_lines, working_lines,
'{} (index)'.format(path),
'{} (working copy)'.format(path),
lineterm='')
for line in diff_lines:
print(line)
if i < len(changed) - 1:
print('-' * 70)
def diff_with_destination(self, lines):
'''Check if destination exists, if it does, diff files.'''
if isfile(self.dest) and not self.opts['force_overwrite']:
tempfile = self.dest + ' NEW'
with open(self.dest) as f:
content = f.readlines()
# Perform diff
diff_lines = list()
for line in difflib.unified_diff(content, lines, fromfile=self.dest, tofile=tempfile, lineterm='\n'):
if line.startswith('-'):
color_line = self.color.colorize(line, 'red')
elif line.startswith('+'):
color_line = self.color.colorize(line, 'green')
else:
color_line = self.color.colorize(line, 'white')
diff_lines.append(line)
print(color_line)
return self.continue_prompt()
else:
return True
def diff(self):
"""
Yield diffs between each template's render and current file.
"""
for template, dest, result in self.render():
try:
with codecs.open(dest, 'r', 'utf-8') as f:
yield unified_diff(
f.readlines(),
result.splitlines(True),
fromfile=dest,
tofile='%s (rendered)' % dest)
except IOError:
yield [
"=== No destination file \"%s\" for comparison.\n"
% dest]
def get_diff_text(old, new, filename):
"""Return text of unified diff between old and new."""
newline = '\n'
diff = difflib.unified_diff(
old, new,
'original/' + filename,
'fixed/' + filename,
lineterm=newline)
text = ''
for line in diff:
text += line
# Work around missing newline (http://bugs.python.org/issue2142).
if text and not line.endswith(newline):
text += newline + r'\ No newline at end of file' + newline
return text
def file_diff(filename1, filename2, filtered_reader):
remove_absdir(filename1)
remove_absdir(filename2)
#
INPUT=open(filename1, 'r')
lines1 = list(filtered_reader(INPUT))
INPUT.close()
#
INPUT=open(filename2, 'r')
lines2 = list(filtered_reader(INPUT))
INPUT.close()
#
diff = list(difflib.unified_diff(lines2, lines1,
fromfile=filename2, tofile=filename1))
if diff:
make_diff_readable(diff)
raise Exception("ERROR: \n\n%s\n\n%s\n\n" % (lines1, lines2))
diff = '\n'.join(diff)
return diff
def run(self):
try:
manifest = yaml.safe_load(self.wd.read('manifest'))
except:
log.error("No snapshot found")
return (False, "no snapshot found")
fails = []
for filepath in manifest:
f = File(filepath)
s = File(manifest[filepath], parent=self.wd)
diff = list(difflib.unified_diff(f.content.splitlines(), s.content.splitlines()))
if len(diff):
fails.append(filepath)
log.info("Check for {} failed with diff:")
log.info("\n".join(diff))
if len(fails):
return (False, "the following files have changed: {}".format(', '.join(fails)))
else:
return (True, "no files have changed")
def compare_code_solutions(first_solution, second_solution):
current_code = first_solution.code.splitlines()
next_code = second_solution.code.splitlines()
diff_percentage, unified_diff = calculate_difference_percentage(current_code, next_code)
result = ""
if diff_percentage < MIN_ALLOWED_DIFFERENCE_PERCENTAGE:
result = f"""
Matching contents in solutions
{first_solution} from {first_solution.student.email} and
{second_solution} from {second_solution.student.email}
on task {first_solution.task.name}
--------------------------------------------
Differences: {diff_percentage}%\n
"""
for line in unified_diff:
result += line + '\n'
return result
def compare_file_solutions(first_solution, second_solution):
current_code = first_solution.file.read().decode('utf-8').splitlines()
next_code = second_solution.file.read().decode('utf-8').splitlines()
# Reset file pointers
first_solution.file.seek(0)
second_solution.file.seek(0)
diff_percentage, unified_diff = calculate_difference_percentage(current_code, next_code)
result = ""
if diff_percentage < MIN_ALLOWED_DIFFERENCE_PERCENTAGE:
result = f"""
Matching contents in files
{first_solution.file.name} from {first_solution.student.email} and
{second_solution.file.name} from {second_solution.student.email}
on Task: {first_solution.task.name}
Differences: {diff_percentage}%\n
"""
for line in unified_diff:
result += line + '\n'
return result
def compare(self, old: str, old_date: str, new: str, new_date: str,
ctx: common.Context, meta: dict) \
-> ty.Tuple[bool, ty.Optional[str], ty.Optional[dict]]:
# pylint: disable=invalid-sequence-index
old = old.replace(common.RECORD_SEPARATOR, '\n\n')
new = new.replace(common.RECORD_SEPARATOR, '\n\n')
old_lines = old.split('\n')
res = list(difflib.unified_diff(
old_lines, new.split('\n'),
fromfiledate=old_date, tofiledate=new_date,
lineterm='\n'))
changed_lines = sum(1 for line in res[2:]
if line and line[0] != ' ' and line[0] != '@')
if not _check_changes(ctx, changed_lines, len(old_lines),
self.conf.get("changes_threshold"),
self.conf.get("min_changed")):
return False, None, None
return True, "\n".join(res), self.opts
def _assertSchemaEqual(expected, actual, testcase):
"""Utility method to dump diffs if the schema aren't equal.
Args:
expected: object, the expected results.
actual: object, the actual results.
testcase: unittest.TestCase, the test case this assertion is used within.
"""
if expected != actual:
expected_text = json.dumps(expected, indent=2, sort_keys=True)
actual_text = json.dumps(actual, indent=2, sort_keys=True)
diff = difflib.unified_diff(expected_text.splitlines(True),
actual_text.splitlines(True),
fromfile='expected.schema',
tofile='actual.schema')
diff_text = ''.join(list(diff))
testcase.fail('Schema differs from expected:\n%s' % diff_text)
def print_diff(self, row1, row2):
print "Discrepancy for {HITId}".format(**row1)
print yaml.dump(self.inputs(row1), default_flow_style=False)
formatted = [self.format_for_diff(r).split('\n') for r in [row1, row2]]
diff = unified_diff(*formatted, n=20)
for s in diff:
if s.strip() in ('---', '+++'):
continue
if s.startswith('-'):
print colored("{:<40}".format(s[1:]), 'red')
elif s.startswith('+'):
print colored("{:<40}".format(s[1:]), 'blue')
elif s.startswith(' '):
print s[1:]
#print "{:<40} {:<40}".format(s[1:], s[1:])
def _change_set_diff(change_set):
diff = []
for change in change_set.changes:
new = change.new_contents
old = change.old_contents
if old is None:
if change.resource.exists():
old = change.resource.read()
else:
old = ''
result = unified_diff(
old.splitlines(True), new.splitlines(True),
'a/' + change.resource.path, 'b/' + change.resource.path
)
diff.extend(list(result))
diff.append('\n')
return diff
def get_correct_indentation_diff(code, filename):
"""
Generate a diff to make code correctly indented.
:param code: a string containing a file's worth of Python code
:param filename: the filename being considered (used in diff generation only)
:returns: a unified diff to make code correctly indented, or
None if code is already correctedly indented
"""
code_buffer = StringIO(code)
output_buffer = StringIO()
reindenter = reindent.Reindenter(code_buffer)
reindenter.run()
reindenter.write(output_buffer)
reindent_output = output_buffer.getvalue()
output_buffer.close()
if code != reindent_output:
diff_generator = difflib.unified_diff(code.splitlines(True), reindent_output.splitlines(True),
fromfile=filename, tofile=filename + " (reindented)")
# work around http://bugs.python.org/issue2142
diff_tuple = map(clean_diff_line_for_python_bug_2142, diff_generator)
diff = "".join(diff_tuple)
return diff
else:
return None
def compare_file(reference, this_test):
ref_lines = open(reference,'r').readlines()
ref_lines = [ x.rstrip() for x in ref_lines]
this_test = [ x.rstrip() for x in this_test]
for line in difflib.unified_diff(ref_lines, this_test,
fromfile=reference, tofile="current"):
sys.stdout.write(line.rstrip()+'\n')
if len(ref_lines) != len(this_test):
mbuild.msgb("DIFFERENT NUMBER OF LINES", "ref %d test %d" % (len(ref_lines),len(this_test)))
for ref in ref_lines:
mbuild.msgb("EXPECTED",'%s' % (ref.strip()))
return False
for (ref,test) in zip(ref_lines,this_test):
if ref.strip() != test.strip():
if ref.find("XED version") != -1: # skip the version lines
continue
mbuild.msgb("DIFFERENT", "\n\tref [%s]\n\ttest [%s]" % (ref, test))
return False
return True
def diff_texts(a, b, filename):
"""Return a unified diff of two strings."""
a = a.splitlines()
b = b.splitlines()
return difflib.unified_diff(a, b, filename, filename,
"(original)", "(refactored)",
lineterm="")
def generate_diff(current, coming):
"""Generates diff of changes"""
return '\n'.join(
difflib.unified_diff(
current.splitlines(), coming.splitlines(),
'Current content', 'Coming changes'
)
)
def testRoundTrip(self):
import difflib
srcDir = GLYPHSETDIR
dstDir = self.dstDir
src = GlyphSet(srcDir, ufoFormatVersion=2)
dst = GlyphSet(dstDir, ufoFormatVersion=2)
for glyphName in src.keys():
g = src[glyphName]
g.drawPoints(None) # load attrs
dst.writeGlyph(glyphName, g, g.drawPoints)
# compare raw file data:
for glyphName in sorted(src.keys()):
fileName = src.contents[glyphName]
with open(os.path.join(srcDir, fileName), "r") as f:
org = f.read()
with open(os.path.join(dstDir, fileName), "r") as f:
new = f.read()
added = []
removed = []
for line in difflib.unified_diff(
org.split("\n"), new.split("\n")):
if line.startswith("+ "):
added.append(line[1:])
elif line.startswith("- "):
removed.append(line[1:])
self.assertEqual(
added, removed,
"%s.glif file differs after round tripping" % glyphName)
def diff(self, expected, actual):
import difflib
expected = str(self.Glyph(expected)).splitlines(True)
actual = str(self.Glyph(actual)).splitlines(True)
diff = difflib.unified_diff(
expected, actual, fromfile='expected', tofile='actual')
return "".join(diff)
def print_commit(commit, with_diff=False):
t = time.strftime("%a, %d %b %Y %H:%M", time.gmtime(commit.authored_date))
print(commit.hexsha, commit.author.name, t, commit.message)
print("stats:", commit.stats.total)
print()
if with_diff and len(commit.parents):
diffs = commit.diff(commit.parents[0])
for d in diffs:
print(d)
b_lines = str(d.b_blob.data_stream.read()).split()
a_lines = str(d.a_blob.data_stream.read()).split()
differ = difflib.Differ()
delta = differ.compare(b_lines, a_lines)
for i in delta:
print(i)
line_number = 0
for line in delta:
# split off the code
code = line[:2]
# if the line is in both files or just a, increment the
# line number.
if code in (" ", "+ "):
line_number += 1
# if this line is only in a, print the line number and
# the text on the line
if code == "+ ":
print("%d: %s" % (line_number, line[2:].strip()))
# print(b_lines)
# print(a_lines)
# dcont = list(difflib.unified_diff(
# b_lines, a_lines, d.b_path, d.a_path))
# for l in dcont:
# print(l)
print("------------------------")
print("+++++++++++++++++++++++++++++++++++++++++++++++++++++++")
print()