Python difflib.unified_diff() Examples

The following are 30 code examples of difflib.unified_diff(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module difflib , or try the search function .
Example #1
Source File: autopep8.py    From PyDev.Debugger with Eclipse Public License 1.0 7 votes vote down vote up
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 
Example #2
Source File: autopep8.py    From python-netsurv with MIT License 6 votes vote down vote up
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 
Example #3
Source File: test_dictization.py    From daf-recipes with GNU General Public License v3.0 6 votes vote down vote up
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"))) 
Example #4
Source File: autopep8.py    From python-netsurv with MIT License 6 votes vote down vote up
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 
Example #5
Source File: test_dictization.py    From daf-recipes with GNU General Public License v3.0 6 votes vote down vote up
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"))) 
Example #6
Source File: message_parser_test.py    From endpoints-python with Apache License 2.0 6 votes vote down vote up
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) 
Example #7
Source File: diff.py    From oss-ftp with MIT License 6 votes vote down vote up
def main():

    usage = "usage: %prog [options] fromfile tofile"
    parser = optparse.OptionParser(usage)
    parser.add_option("-c", action="store_true", default=False, help='Produce a context format diff (default)')
    parser.add_option("-u", action="store_true", default=False, help='Produce a unified format diff')
    parser.add_option("-m", action="store_true", default=False, help='Produce HTML side by side diff (can use -c and -l in conjunction)')
    parser.add_option("-n", action="store_true", default=False, help='Produce a ndiff format diff')
    parser.add_option("-l", "--lines", type="int", default=3, help='Set number of context lines (default 3)')
    (options, args) = parser.parse_args()

    if len(args) == 0:
        parser.print_help()
        sys.exit(1)
    if len(args) != 2:
        parser.error("need to specify both a fromfile and tofile")

    n = options.lines
    fromfile, tofile = args

    fromdate = time.ctime(os.stat(fromfile).st_mtime)
    todate = time.ctime(os.stat(tofile).st_mtime)
    fromlines = open(fromfile, 'U').readlines()
    tolines = open(tofile, 'U').readlines()

    if options.u:
        diff = difflib.unified_diff(fromlines, tolines, fromfile, tofile, fromdate, todate, n=n)
    elif options.n:
        diff = difflib.ndiff(fromlines, tolines)
    elif options.m:
        diff = difflib.HtmlDiff().make_file(fromlines,tolines,fromfile,tofile,context=options.c,numlines=n)
    else:
        diff = difflib.context_diff(fromlines, tolines, fromfile, tofile, fromdate, todate, n=n)

    sys.stdout.writelines(diff) 
Example #8
Source File: freeze_tests.py    From openSUSE-release-tools with GNU General Public License v2.0 6 votes vote down vote up
def test_bootstrap_copy(self):
        wf = OBSLocal.StagingWorkflow()

        fc = FreezeCommand(wf.api)

        fp = self._get_fixture_path('staging-meta-for-bootstrap-copy.xml')
        fixture = subprocess.check_output('/usr/bin/xmllint --format %s' % fp, shell=True).decode('utf-8')

        f = tempfile.NamedTemporaryFile(delete=False)
        f.write(fc.prj_meta_for_bootstrap_copy('openSUSE:Factory:Staging:A'))
        f.close()

        output = subprocess.check_output('/usr/bin/xmllint --format %s' % f.name, shell=True).decode('utf-8')

        for line in difflib.unified_diff(fixture.split("\n"), output.split("\n")):
            print(line)
        self.assertEqual(output, fixture) 
Example #9
Source File: test_helpers.py    From glyphsLib with Apache License 2.0 6 votes vote down vote up
def assertLinesEqual(self, expected, actual, message):
        if actual != expected:
            if len(actual) < len(expected):
                sys.stderr.write(
                    dedent(
                        """\
                    WARNING: the actual text is shorter that the expected text.
                             Some information may be LOST!
                    """
                    )
                )
            for line in difflib.unified_diff(
                expected, actual, fromfile="<expected>", tofile="<actual>"
            ):
                if not line.endswith("\n"):
                    line += "\n"
                sys.stderr.write(line)
            self.fail(message) 
Example #10
Source File: tldextract.py    From CrawlBox with The Unlicense 6 votes vote down vote up
def _cache_tlds(self, tlds):
        '''Logs a diff of the new TLDs and caches them on disk, according to
        settings passed to __init__.'''
        if LOG.isEnabledFor(logging.DEBUG):
            import difflib
            snapshot_stream = pkg_resources.resource_stream(__name__, '.tld_set_snapshot')
            with closing(snapshot_stream) as snapshot_file:
                snapshot = sorted(
                    json.loads(snapshot_file.read().decode('utf-8'))
                )
            new = sorted(tlds)
            LOG.debug('computed TLD diff:\n' + '\n'.join(difflib.unified_diff(
                snapshot,
                new,
                fromfile=".tld_set_snapshot",
                tofile=self.cache_file
            )))

        if self.cache_file:
            try:
                with open(self.cache_file, 'w') as cache_file:
                    json.dump(tlds, cache_file)
            except IOError as ioe:
                LOG.warn("unable to cache TLDs in file %s: %s", self.cache_file, ioe) 
Example #11
Source File: check_whitespace.py    From D-VAE with MIT License 6 votes vote down vote up
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 
Example #12
Source File: selective.py    From network-tutorials with Apache License 2.0 5 votes vote down vote up
def _print_diff(self, diff, indent_level):
        if isinstance(diff, dict):
            try:
                diff = '\n'.join(difflib.unified_diff(diff['before'].splitlines(),
                                                      diff['after'].splitlines(),
                                                      fromfile=diff.get('before_header',
                                                                        'new_file'),
                                                      tofile=diff['after_header']))
            except AttributeError:
                diff = dict_diff(diff['before'], diff['after'])
        if diff:
            diff = colorize(diff, 'changed')
            print(self._indent_text(diff, indent_level+4)) 
Example #13
Source File: test_pydoc.py    From oss-ftp with MIT License 5 votes vote down vote up
def print_diffs(text1, text2):
    "Prints unified diffs for two texts"
    lines1 = text1.splitlines(True)
    lines2 = text2.splitlines(True)
    diffs = difflib.unified_diff(lines1, lines2, n=0, fromfile='expected',
                                 tofile='got')
    print '\n' + ''.join(diffs) 
Example #14
Source File: test_difflib.py    From oss-ftp with MIT License 5 votes vote down vote up
def test_tab_delimiter(self):
        args = ['one', 'two', 'Original', 'Current',
            '2005-01-26 23:30:50', '2010-04-02 10:20:52']
        ud = difflib.unified_diff(*args, lineterm='')
        self.assertEqual(list(ud)[0:2], [
                           "--- Original\t2005-01-26 23:30:50",
                           "+++ Current\t2010-04-02 10:20:52"])
        cd = difflib.context_diff(*args, lineterm='')
        self.assertEqual(list(cd)[0:2], [
                           "*** Original\t2005-01-26 23:30:50",
                           "--- Current\t2010-04-02 10:20:52"]) 
Example #15
Source File: selective.py    From network-tutorials with Apache License 2.0 5 votes vote down vote up
def _print_diff(self, diff, indent_level):
        if isinstance(diff, dict):
            try:
                diff = '\n'.join(difflib.unified_diff(diff['before'].splitlines(),
                                                      diff['after'].splitlines(),
                                                      fromfile=diff.get('before_header',
                                                                        'new_file'),
                                                      tofile=diff['after_header']))
            except AttributeError:
                diff = dict_diff(diff['before'], diff['after'])
        if diff:
            diff = colorize(diff, 'changed')
            print(self._indent_text(diff, indent_level+4)) 
Example #16
Source File: selective.py    From network-tutorials with Apache License 2.0 5 votes vote down vote up
def _print_diff(self, diff, indent_level):
        if isinstance(diff, dict):
            try:
                diff = '\n'.join(difflib.unified_diff(diff['before'].splitlines(),
                                                      diff['after'].splitlines(),
                                                      fromfile=diff.get('before_header',
                                                                        'new_file'),
                                                      tofile=diff['after_header']))
            except AttributeError:
                diff = dict_diff(diff['before'], diff['after'])
        if diff:
            diff = colorize(diff, 'changed')
            print(self._indent_text(diff, indent_level+4)) 
Example #17
Source File: isort.py    From PyDev.Debugger with Eclipse Public License 1.0 5 votes vote down vote up
def _show_diff(self, file_contents):
        for line in unified_diff(
            file_contents.splitlines(1),
            self.output.splitlines(1),
            fromfile=self.file_path + ':before',
            tofile=self.file_path + ':after',
            fromfiledate=str(datetime.fromtimestamp(os.path.getmtime(self.file_path))
                             if self.file_path else datetime.now()),
            tofiledate=str(datetime.now())
        ):
            sys.stdout.write(line) 
Example #18
Source File: diff.py    From dcos-deploy with Apache License 2.0 5 votes vote down vote up
def compare_strings(left, right):
    left_str = left.replace(r'\n', '\n')
    right_str = right.replace(r'\n', '\n')
    diff = list(difflib.unified_diff(left_str.splitlines(), right_str.splitlines(), lineterm=''))
    if diff:
        return "    " + '\n    '.join([color_diff_line(line) for line in diff])
    else:
        return None 
Example #19
Source File: main.py    From PyDev.Debugger with Eclipse Public License 1.0 5 votes vote down vote up
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="") 
Example #20
Source File: test_dictization.py    From daf-recipes with GNU General Public License v3.0 5 votes vote down vote up
def test_04_package_to_api1_with_relationship(self):

        context = {"model": model,
                 "session": model.Session}

        create = CreateTestData

        create.create_family_test_data()
        pkg = model.Session.query(model.Package).filter_by(name='homer').one()

        as_dict = pkg.as_dict()
        as_dict['license_title'] = None
        as_dict['num_tags'] = 0
        as_dict['num_resources'] = 0
        dictize = package_to_api1(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"))) 
Example #21
Source File: test_package.py    From daf-recipes with GNU General Public License v3.0 5 votes vote down vote up
def diff_html(self, html1, html2):
        return '\n'.join(unified_diff(html1.split('\n'),
                                      html2.split('\n'))) 
Example #22
Source File: common.py    From godot-gdscript-toolkit with MIT License 5 votes vote down vote up
def _compare(formatted_code, expected_output_code):
    diff = "\n".join(
        difflib.unified_diff(
            expected_output_code.splitlines(), formatted_code.splitlines()
        )
    )
    assert formatted_code == expected_output_code, diff 
Example #23
Source File: common.py    From godot-gdscript-toolkit with MIT License 5 votes vote down vote up
def _tree_invariant_check(input_code, formatted_code):
    input_code_parse_tree = parser.parse(input_code)
    formatted_code_parse_tree = parser.parse(formatted_code)
    loosen_tree_transformer = LoosenTreeTransformer()
    input_code_parse_tree = loosen_tree_transformer.transform(input_code_parse_tree)
    formatted_code_parse_tree = loosen_tree_transformer.transform(
        formatted_code_parse_tree
    )
    diff = "\n".join(
        difflib.unified_diff(
            str(input_code_parse_tree.pretty()).splitlines(),
            str(formatted_code_parse_tree.pretty()).splitlines(),
        )
    )
    assert input_code_parse_tree == formatted_code_parse_tree, diff 
Example #24
Source File: check-style.py    From IEEE-802.11ah-ns-3 with GNU General Public License v2.0 5 votes vote down vote up
def indent_files(files, diff=False, debug=False, level=0, inplace=False):
    output = []
    for f in files:
        dst = indent(f, debug=debug, level=level)
        output.append([f,dst])

    # First, copy to inplace
    if inplace:
        for src,dst in output:
            shutil.copyfile(dst,src)
        return True

    # now, compare
    failed = []
    for src,dst in output:
        if filecmp.cmp(src,dst) == 0:
            failed.append([src, dst])
    if len(failed) > 0:
        if not diff:
            print 'Found %u badly indented files:' % len(failed)
            for src,dst in failed:
                print '  ' + src
        else:
            for src,dst in failed:
                s = open(src, 'r').readlines()
                d = open(dst, 'r').readlines()
                for line in difflib.unified_diff(s, d, fromfile=src, tofile=dst):
                    sys.stdout.write(line)
        return False
    return True 
Example #25
Source File: doc.py    From edgedb with Apache License 2.0 5 votes vote down vote up
def get_diff(
            cls, a, b, fromfile='', tofile='', fromfiledate='', tofiledate='',
            n=10):

        lines = difflib.unified_diff(
            a, b, fromfile, tofile, fromfiledate, tofiledate, n)
        lines = [line.rstrip() for line in lines]

        if lines:
            return cls(lines=lines)
        else:
            return Text(text='No differences') 
Example #26
Source File: test.py    From zxbasic with GNU General Public License v3.0 5 votes vote down vote up
def is_same_file(fname1, fname2, ignore_regexp=None, replace_regexp=None, replace_what='.', replace_with='.',
                 diff=None, is_binary=False, strip_blanks=True):
    """ Test if two files are the same.

    If ignore_regexp is passed, it must be a Regular Expression
    which will ignore matched lines on both files.

    If replace_regexp is passed, all lines matching RE (string) will perform
    a string substitution of A into B. This if done *AFTER* ignoreLinesRE.
    """
    if fname1 == fname2:
        return True

    if not os.path.exists(fname1) and not os.path.exists(fname2):
        return True

    if not os.path.exists(fname1) or not os.path.exists(fname2):
        return False

    if is_binary:
        return open(fname1, 'rb').read() == open(fname2, 'rb').read()

    r1 = get_file_lines(fname1, ignore_regexp, replace_regexp, replace_what, replace_with, strip_blanks)
    r2 = get_file_lines(fname2, ignore_regexp, replace_regexp, replace_what, replace_with, strip_blanks)
    result = (r1 == r2)

    if not result:
        if diff is None:
            diff = []
        diff.extend(difflib.unified_diff(r1, r2, fname1, fname2))

    if PRINT_DIFF and not result:
        if VIM_DIFF:
            systemExec('gvimdiff %s %s' % (fname1, fname2))
        else:
            sys.stdout.write(''.join(diff))

    return result 
Example #27
Source File: check-style.py    From ntu-dsi-dcn with GNU General Public License v2.0 5 votes vote down vote up
def indent_files(files, diff=False, debug=False, level=0, inplace=False):
    output = []
    for f in files:
        dst = indent(f, debug=debug, level=level)
        output.append([f,dst])

    # First, copy to inplace
    if inplace:
        for src,dst in output:
            shutil.copyfile(dst,src)
        return True

    # now, compare
    failed = []
    for src,dst in output:
        if filecmp.cmp(src,dst) == 0:
            failed.append([src, dst])
    if len(failed) > 0:
        if not diff:
            print 'Found %u badly indented files:' % len(failed)
            for src,dst in failed:
                print '  ' + src
        else:
            for src,dst in failed:
                s = open(src, 'r').readlines()
                d = open(dst, 'r').readlines()
                for line in difflib.unified_diff(s, d, fromfile=src, tofile=dst):
                    sys.stdout.write(line)
        return False
    return True 
Example #28
Source File: test_ipsec.py    From neutron-vpnaas with Apache License 2.0 5 votes vote down vote up
def generate_diff(a, b):
        """Generates unified diff of a and b."""
        a, b = list(a.splitlines(True)), list(b.splitlines(True))
        diff = difflib.unified_diff(a, b, fromfile="expected",
                                    tofile="actual")
        return diff 
Example #29
Source File: diff.py    From dcos-deploy with Apache License 2.0 5 votes vote down vote up
def compare_text(left, right):
    if not isinstance(left, str):
        left = left.decode("utf-8")
    if not isinstance(right, str):
        right = right.decode("utf-8")
    diff = list(difflib.unified_diff(left.splitlines(), right.splitlines(), lineterm=''))
    if diff:
        return "    " + '\n    '.join([color_diff_line(line) for line in diff])
    else:
        return None 
Example #30
Source File: mailer.py    From anytask with MIT License 5 votes vote down vote up
def __init__(self, label_from, label_to, from_file, to_file):
    import difflib
    self.seen_change = False
    fromlines = open(from_file, 'U').readlines()
    tolines = open(to_file, 'U').readlines()
    self.diff = difflib.unified_diff(fromlines, tolines,
                                     label_from, label_to)