Ejemplo n.º 1
0
def diff(file1, file2, move=False, parent=False):

    # get AST of file1
    f1 = open(file1, 'r')
    lines1 = f1.read()
    f1.close()

    try:
        node1 = parse(lines1)
    except (SyntaxError, Exception):
        print('file %s cannot be parsed' % file1)
        raise
    improve_ast(node1, lines1, file1, 'left', parent)

    # get AST of file2
    f2 = open(file2, 'r');
    lines2 = f2.read()
    f2.close()

    try:
        node2 = parse(lines2)
    except (SyntaxError, Exception):
        print('file %s cannot be parsed' % file2)
        raise

    improve_ast(node2, lines2, file2, 'right', parent)

    # get the changes
    (changes, cost) = diff_node(node1, node2, move)
    return changes
Ejemplo n.º 2
0
def diffstring(str1, str2, move=False, parent=False):

    try:
        node1 = parse(str1)
    except (SyntaxError, Exception):
        print('string -- %s -- cannot be parsed' % str1)
        raise

    improve_ast(node1, str1, None, 'left', parent)

    try:
        node2 = parse(str2)
    except (SyntaxError, Exception):
        print('string -- %s -- cannot be parsed' % str2)
        raise

    improve_ast(node2, str2, None, 'right', parent)

    # get the changes
    (changes, cost) = diff_node(node1, node2, move)
    return changes, cost
Ejemplo n.º 3
0
def diff(file1, file2, move=True):

    print("\nJob started at %s, %s\n" % (time.ctime(), time.tzname[0]))
    start_time = time.time()
    checkpoint(start_time)

    cleanup()

    # base files names
    base1 = base_name(file1)
    base2 = base_name(file2)

    # get AST of file1
    f1 = open(file1, 'r')
    lines1 = f1.read()
    f1.close()
    node1 = parse(lines1)
    improve_ast(node1, lines1, file1, 'left')

    # get AST of file2
    f2 = open(file2, 'r')
    lines2 = f2.read()
    f2.close()
    node2 = parse(lines2)
    improve_ast(node2, lines2, file2, 'right')

    print("[parse] finished in %s. Now start to diff." %
          sec_to_min(checkpoint()))

    # get the changes

    (changes, cost) = diff_node(node1, node2, nil, nil, 0, False)

    print("\n[diff] processed %d nodes in %s." %
          (stat.diff_count, sec_to_min(checkpoint())))

    if move:
        (changes, cost) = find_all_moves((changes, cost))

        print("\nfinished in %s." % sec_to_min(checkpoint()))

    #---------------------- print final stats ---------------------
    size1 = node_size(node1)
    size2 = node_size(node2)
    total = size1 + size2

    report = ""
    report += (
        "\n--------------------- summary -----------------------") + "\n"
    report += ("- total changes (chars):  %d" % cost) + "\n"
    report += ("- total code size:        %d (left: %d  right: %d)" %
               (total, size1, size2)) + "\n"
    report += ("- total moved pieces:     %d" % stat.move_count) + "\n"
    report += ("- percentage of change:   %.1f%%" %
               (div(cost, total) * 100)) + "\n"
    report += ("-----------------------------------------------------") + "\n"

    print report

    #---------------------- generation HTML ---------------------

    htmlize(changes, file1, file2, lines1, lines2)

    dur = time.time() - start_time
    print("\n[summary] Job finished at %s, %s" %
          (time.ctime(), time.tzname[0]))
    print("\n\tTotal duration: %s" % sec_to_min(dur))
Ejemplo n.º 4
0
def diff(file1, file2, move=True):

    print("File 1: %s" % file1)
    print("File 2: %s" % file2)
    print("Start time: %s, %s" % (time.ctime(), time.tzname[0]))
    start_time = time.time()
    checkpoint(start_time)

    cleanup()

    # base files names
    base1 = base_name(file1)
    base2 = base_name(file2)

    # get AST of file1
    f1 = open(file1, 'r');
    lines1 = f1.read()
    f1.close()

    try:
        node1 = parse(lines1)
    except Exception:
        print('file %s cannot be parsed' % file1)
        exit(-1)

    improve_ast(node1, lines1, file1, 'left')

    # get AST of file2
    f2 = open(file2, 'r');
    lines2 = f2.read()
    f2.close()

    try:
        node2 = parse(lines2)
    except Exception:
        print('file %s cannot be parsed' % file2)
        exit(-1)

    improve_ast(node2, lines2, file2, 'right')

    print("Parse finished in %s. Now start to diff." % sec_to_min(checkpoint()))

    # get the changes

    (changes, cost) = diff_node(node1, node2, 0, False)

    print("\n[diff] processed %d nodes in %s."
          % (stat.diff_count, sec_to_min(checkpoint())))


    #---------------------- print final stats ---------------------
    size1 = node_size(node1)
    size2 = node_size(node2)
    total = size1 + size2

    report = ""
    report += ("\n--------------------- summary -----------------------") + "\n"
    report += ("- total changes (chars):  %d" % cost)                  + "\n"
    report += ("- total code size:        %d (left: %d  right: %d)"
               % (total, size1, size2))                                + "\n"
    report += ("- total moved pieces:     %d" % stat.move_count)        + "\n"
    report += ("- percentage of change:   %.1f%%"
               % (div(cost, total) * 100))                             + "\n"
    report += ("-----------------------------------------------------")   + "\n"

    print(report)


    #---------------------- generation HTML ---------------------

    htmlize(changes, file1, file2, lines1, lines2)

    dur = time.time() - start_time
    print("\n[summary] Job finished at %s, %s" %
          (time.ctime(), time.tzname[0]))
    print("\n\tTotal duration: %s" % sec_to_min(dur))
Ejemplo n.º 5
0
def diff(file1, file2, move=True):

    print("File 1: {}".format(file1))
    print("File 2: {}".format(file2))
    print("Start time: {}, {}".format(time.ctime(), time.tzname[0]))

    start_time = time.time()
    checkpoint(start_time)

    cleanup()

    # base files names
    base1 = base_name(file1)
    base2 = base_name(file2)

    print(base1, base2)

    def file(file_name):
        f = ''
        with open(file_name, 'r') as f1:
            f = f1.read()
            f1.close()
        return f

    def node_by_file(file):
        node = None
        try:
            node = parse(file)
        except Exception:
            print('file %s cannot be parsed'.format(file))
            exit(-1)
        return node


    lines1 = file(file1)
    lines2 = file(file2)
    node1 = node_by_file(lines1)
    node2 = node_by_file(lines2)
    # get AST of file1
    improve_ast(node1, lines1, file1, 'left')
    # get AST of file2
    improve_ast(node2, lines2, file2, 'right')

    print("Parse finished in %s. Now start to diff." % sec_to_min(checkpoint()))

    # get the changes

    (changes, cost) = diff_node(node1, node2, 0, False)

    print("\n[diff] processed {} nodes in {}.".format(stat.diff_count, sec_to_min(checkpoint())))

    #---------------------- print final stats ---------------------
    size1 = node_size(node1)
    size2 = node_size(node2)
    total = size1 + size2

    report = ""
    report += ("\n--------------------- summary -----------------------") + "\n"
    report += ("- total changes (chars):  %d" % cost)                  + "\n"
    report += ("- total code size:        %d (left: %d  right: %d)"
               % (total, size1, size2))                                + "\n"
    report += ("- total moved pieces:     %d" % stat.move_count)        + "\n"
    report += ("- percentage of change:   %.1f%%"
               % (div(cost, total) * 100))                             + "\n"
    report += ("-----------------------------------------------------")   + "\n"

    print(report)


    #---------------------- generation HTML ---------------------

    htmlize(changes, file1, file2, lines1, lines2)

    dur = time.time() - start_time
    print("\n[summary] Job finished at %s, %s" %
          (time.ctime(), time.tzname[0]))
    print("\n\tTotal duration: %s" % sec_to_min(dur))