File: aug_comparator.py

package info (click to toggle)
augustus 3.4.0%2Bdfsg2-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 758,480 kB
  • sloc: cpp: 65,451; perl: 21,436; python: 3,927; ansic: 1,240; makefile: 1,032; sh: 189; javascript: 32
file content (81 lines) | stat: -rwxr-xr-x 2,268 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env python3

import sys
import os
import difflib


def compare_folder(reffolder, currentfolder, html=False, outputfolder='output_html/'):
    if not os.path.isdir(reffolder):
        return 'Reference folder not found: ' + reffolder
    if not os.path.isdir(currentfolder):
        return 'Current folder not found: ' + currentfolder

    res = ''

    for subdir, dirs, files in os.walk(reffolder):
        for file in files:
            currentdir = subdir.replace(reffolder, currentfolder)

            diff = compare_files(os.path.join(subdir, file),
                                 os.path.join(currentdir, file),
                                 html=html, outputfolder=outputfolder)
            res += diff

    return res


def compare_files(reffile, currentfile, html=False, outputfolder='output_html/'):
    if not os.path.isfile(reffile):
        return 'Reference file not found: ' + reffile
    if not os.path.isfile(currentfile):
        return 'Current file not found: ' + currentfile

    with open(reffile) as ff:
        reflines = ff.readlines()
    with open(currentfile) as tf:
        currentlines = tf.readlines()

    diff = ''.join(
        difflib.context_diff(reflines, currentlines, reffile, currentfile,
                             n=0))

    if html and diff:
        generate_html(reflines, currentlines, reffile,
                      currentfile, outputfolder)

    return diff


def generate_html(reflines, currentlines, reffile, currentfile, outputfolder):
    if not os.path.exists(outputfolder):
        os.mkdir(outputfolder)

    diff = difflib.HtmlDiff().make_file(reflines, currentlines, reffile,
                                        currentfile)

    filename = create_html_filename(reffile)
    with open(os.path.join(outputfolder, filename), 'w') as html:
        html.write(diff)


def create_html_filename(name):
    html_name = ''
    elements = []
    parts = name.split('/')

    for p in reversed(parts):
        elements.append(p)
        if 'test_' in p:
            break

    for e in reversed(elements):
        html_name += e + '_'

    return html_name + 'diff.html'


if __name__ == '__main__':
    reference = sys.argv[1]
    current = sys.argv[2]
    print(compare_files(reference, current))