File: benchmark_diff.py

package info (click to toggle)
pysvn 1.5.0dfsg-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,524 kB
  • ctags: 2,281
  • sloc: cpp: 9,846; python: 2,662; sh: 430; makefile: 101; ansic: 9
file content (220 lines) | stat: -rw-r--r-- 7,486 bytes parent folder | download | duplicates (2)
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
'''
 ====================================================================
 Copyright (c) 2005-2006 Barry A Scott.  All rights reserved.

 This software is licensed as described in the file LICENSE.txt,
 which you should have received as part of this distribution.

 ====================================================================
'''

import sys, os, cStringIO, re, pprint, string
import difflib

_debug = 0

class LiteralMatch:
    def __init__(self,start,end):
        self.start_pos = start
        self.end_pos = end

    def start(self):
        return self.start_pos
    def end(self):
        return self.end_pos


class LiteralSearch:
    def __init__(self, substring):
        self.substring = substring

    def search(self,line):
        try:
            start_pos = line.index( self.substring )
            end_pos = start_pos + len(self.substring)
            return LiteralMatch( start_pos, end_pos )
        except ValueError, detail:
            return None

class LiteralCaseBlindSearch:
    def __init__(self, substring):
        self.substring = substring.lower()

    def search(self,line):
        try:
            start_pos = line.lower().index( self.substring )
            end_pos = start_pos + len(self.substring)
            return LiteralMatch( start_pos, end_pos )
        except ValueError, detail:
            return None

class ReplaceDirtInString:
    def __init__(self, lines_list):
        self.lines_list = lines_list
        self.workdir = self.find( 'WorkDir' )
        self.python = self.find( 'PYTHON' )
        self.username = self.find( 'Username' )
        self.username_spaces = self.username
        while len(self.username_spaces) < 10:
            self.username_spaces = self.username_spaces + ' '

        # ------------------------------------------------------------------------
        # Version strings:
        # Date/Timestamps:
        #   2001-03-27-15-36-10
        #   19-Mar-01 14:52:12
        #   Jan 20 14:35
        #   Jan 20  2004
        # UUID:
        #   467a5469-d6df-e448-9de8-282096145563
        # Directory path:
        #   Drawn from TestRoot: xxxx
        # Username, Hostname:
        #   Drawn from Username: xxx & Hostname: xxx
        #

        dateAlphaNumeric_re = re.compile(r'\d+-[JFMASOND][a-z][a-z]-\d+ [ 0-9]\d:\d\d:\d\d')
        dateNumeric_re = re.compile(r'\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d(.\d+)?Z?')
        dateUnixLs1_re = re.compile(r'[JFMASOND][a-z][a-z] \d+ \d\d:\d\d')
        dateUnixLs2_re = re.compile(r'[JFMASOND][a-z][a-z]  \d\d\d\d')
        uuid_re = re.compile(r'[0-9a-z]{8}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{12}')
        checksum_re = re.compile(r'[0-9a-z]{32}')

        self.replacement_list = \
                    [(dateAlphaNumeric_re,          '<alpha-date-and-time>'),
                     (dateNumeric_re,               '<numeric-date-and-time>'),
                     (uuid_re,                      '<UUID>'),
                     (checksum_re,                  '<checksum>'),
                     (dateUnixLs1_re,               '<ls-date-and-time>'),
                     (dateUnixLs2_re,               '<ls-date-and-time>'),
                    ]


        if self.workdir:
                workdir_re1 = LiteralCaseBlindSearch( self.workdir )
                workdir_re2 = LiteralCaseBlindSearch( os.path.realpath( self.workdir ) )
                self.replacement_list.append(
                     (workdir_re1,        '<workdir>') )
                self.replacement_list.append(
                     (workdir_re2,        '<workdir>') )

        if self.python:
                python_re = LiteralCaseBlindSearch( self.python )
                self.replacement_list.append(
                     (python_re,        '<PYTHON>') )

        if self.username:
                username_spaces_re = LiteralSearch( self.username_spaces )
                self.replacement_list.append(
                        (username_spaces_re,        '<username>') )
                username_re = LiteralSearch( self.username )
                self.replacement_list.append(
                        (username_re,        '<username>') )

    def find( self, keyword ):
        for line in self.lines_list:
            parts = string.split( line, ':' )
            if parts[0] == keyword:
                value = string.strip( string.join( parts[1:], ':' ) )
                if _debug: print 'find(',keyword,') ->',value
                return value
        return ''

    def execute(self):
            return map(self.replace, self.lines_list)

    def replace(self,line):
        if _debug: print 'Processing: ',line
        for re_expr, replacement_text in self.replacement_list:
            while 1:
                if _debug: print '...trying:',replacement_text
                match = re_expr.search( line )
                if match == None:
                    break
                line = line[0:match.start()] + replacement_text + line[match.end():]
        return line


def stripDirty(filename):

    f = open(filename, 'r')
    contents = f.read()
    f.close()

    lines = string.split( contents.replace( '\r\n', '\n' ).replace( '\r', '\n' ), '\n' )
    replace = ReplaceDirtInString( lines )
    stripped_lines = replace.execute()

    return stripped_lines


# ------------------------------------------------------------------------
# main
 
def main(argv):
 
    try:

        BenchmarkFile = argv[1]
        ResultsFile = argv[2]

        print 'Info: Comparing', BenchmarkFile
        benchmark = stripDirty(BenchmarkFile)

        print 'Info: Against  ', ResultsFile
        results = stripDirty(ResultsFile)
        if _debug:
            print 'Debug: results after we called stripDirty'
            pprint.pprint(results)


        f = open(ResultsFile + '.clean','w')
        for line in results:
            f.write( line + '\n' )
        f.close()

        f = open(BenchmarkFile + '.clean','w')
        for line in benchmark:
            f.write( line + '\n' )
        f.close()



        if results != benchmark:
            print 'Error: Test failed - %s' % ResultsFile

            sm = difflib.SequenceMatcher()
            sm.set_seq1( benchmark )
            sm.set_seq2( results )
            opcodes = sm.get_opcodes()

            for tag, i1, i2, j1, j2 in opcodes:
                if tag in ['delete','insert','replace']:
                    print 'Error: --------------------------------------------------------------------------------'
                if tag in ['delete','replace']:
                    for line_index in range(i1,i2):
                        prefix = 'Benchmark(%d) %7s' % (line_index+1, tag)
                        print 'Error: %26s %s' % (prefix, benchmark[line_index])
                if tag in ['insert','replace']:
                    for line_index in range(j1,j2):
                        prefix = 'Result(%d) %7s' % (line_index+1, tag)
                        print 'Error: %26s %s' % (prefix, results[line_index])

            print 'Error: --------------------------------------------------------------------------------'
            return 1
        else:
            print 'Info: Test succeeded'
            return 0

#    except KeyboardInterrupt:
#        print 'Interrupted by Ctrl-C'
    except IOError, detail:
        print 'Error:', detail

    return 2

# ------------------------------------------------------------------------
 
if __name__ == "__main__":
    sys.exit(main( sys.argv ))