-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
144 lines (120 loc) · 4.69 KB
/
utils.py
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
# -*- coding: UTF-8 -*-
# Copyright (C) 2008 Sylvain Taverne <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Import from the Standard Library
from operator import attrgetter
from difflib import ndiff
from re import compile
from sys import stderr, stdout
# Import from itools
from itools.handlers import get_handler
from itools.odf.odf import ODFFile
from itools.uri import get_uri_path, resolve_uri
from itools import vfs
def get_tests():
"""Returns list of tuples:
[(odf_path, po_handler), ...]
"""
# We traverse all ODF documents
for odf_uri in vfs.traverse('./documents/'):
odf_handler = get_handler(odf_uri)
if not isinstance(odf_handler, ODFFile):
continue
# We found a ODF Document, We search the corresponding PO file
po_uri = resolve_uri(odf_uri, 'testDoc.po')
yield get_uri_path(odf_uri), get_handler(po_uri)
def progress(current, total):
"""Permit to calculate the work progression.
"""
status = current*50/total
str = '\r[%s>%s] %d/%d' %((status-1)*'=', (50-status)*' ', current, total)
stdout.write(str)
def write_diff_file(test_number, diff_filename, odf_uri, po_uri, odf_sources,
po_sources):
"""Write a diff file which summarize the failure of a specific test.
"""
# Create or open the diff file
diff_file_path = 'results/%s' % diff_filename
diff_file = vfs.make_file(diff_file_path)
# Write the header
diff_file.write(25*'=' + 'Test %d FAILED' %test_number + 25*'=' + '\n')
diff_file.write('INPUT: ' + str(odf_uri) + '\n')
diff_file.write('OUTPUT: ' + str(po_uri) + '\n\n')
diff_file.write(
"The '-' sign correspond to a line that is expected but don't "
"appears well or not at all after the extraction.\n"
"The '+' sign correspond to a line that appears after the extraction "
"but is not expected.\n")
diff_file.write(25*'=' + 'Test %d FAILED' %test_number + 25*'=' + '\n')
# Write the diff summary
for line in ndiff(po_sources, odf_sources):
diff_file.write(line.encode('utf-8') + '\n')
diff_file.close()
def clean_sources(sources, re_tags=compile('<.*?>')):
return [ re_tags.sub('', x) for x in sources ]
def start_test(odf_handler):
# Create the 'results' folder
if vfs.exists('results'):
vfs.remove('results')
vfs.make_folder('results')
# Find test files
print 'Searching for ODF files...'
unitests = get_tests()
unitests = list(unitests)
nb_tests = len(unitests)
print 'Number of files found: %d' % nb_tests
test_number = 0
msgs_error = []
for odf_path, po_handler in unitests:
# Progress bar
test_number += 1
progress(test_number, nb_tests)
# Call ODF handler
try:
odf_sources = odf_handler(odf_path)
except:
print 'ERROR with test %d / "%s"' % (test_number, odf_path)
continue
# Post-process the results
odf_sources = clean_sources(odf_sources)
odf_sources = list(set(odf_sources)) # Remove duplicates
odf_sources.sort()
# Expected results
po_sources = [ u''.join(x.source) for x in po_handler.get_units() ]
po_sources = clean_sources(po_sources)
po_sources.sort()
# Check
if odf_sources == po_sources:
continue
# Error. Create a diff report for the current test.
test_name = odf_path.split('/')[-2]
filename = 'test_%d_%s.txt' % (test_number, test_name)
po_path = get_uri_path(po_handler.uri)
write_diff_file(test_number, filename, odf_path, po_path, odf_sources,
po_sources)
# Inform the user that there where failures
msgs_error.append('results/%s\n' % filename)
# Summurarizes the results
print
print 'Results:'
print '--------'
print ' %s Tests' % nb_tests
print ' %s Errors' % len(msgs_error)
print
if msgs_error:
stderr.write('Generated error files\n')
stderr.write('---------------------\n')
for msg in msgs_error:
stderr.write(msg)