~linaro-graphics-wg/+junk/spandex-package

« back to all changes in this revision

Viewing changes to python/vrt.py

  • Committer: Alexandros Frantzis
  • Date: 2011-05-04 08:50:52 UTC
  • Revision ID: alexandros.frantzis@linaro.org-20110504085052-88gps4jrg317s6lc
Tags: upstream-1.1.3~git20110502.0ae20368
ImportĀ upstreamĀ versionĀ 1.1.3~git20110502.0ae20368

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# Spandex benchmark and test framework.
 
3
#
 
4
# Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
 
5
#
 
6
# Contact: Kari J. Kangas <kari.j.kangas@nokia.com>
 
7
#
 
8
#   This framework is free software; you can redistribute it and/or modify it
 
9
# under the terms of the GNU Lesser General Public License as published by the
 
10
# Free Software Foundation, version 2.1 of the License.
 
11
#
 
12
#   This framework is distributed in the hope that it will be useful, but
 
13
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 
14
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 
15
# for more details.
 
16
#
 
17
#   You should have received a copy of the GNU Lesser General Public License
 
18
# along with this framework; if not, see <http://www.gnu.org/licenses/>.
 
19
#
 
20
 
 
21
import sys
 
22
import shutil
 
23
from   clitool        import *
 
24
import tga2png
 
25
from   sct            import bmbuilder
 
26
from   sct.util       import textutil
 
27
from   sct.visualtest import image, report
 
28
 
 
29
################################################################################
 
30
# Util
 
31
class TestCase:
 
32
    def __init__( self, name, description='', status='UNDEFINED', priority='UNDEFINED' ):
 
33
        self.name        = name
 
34
        self.description = description
 
35
        self.status      = status
 
36
 
 
37
    def __str__( self ):
 
38
        return '%-50s Status: %10s' % (self.name, self.status)
 
39
 
 
40
def createTestCases( suite ):
 
41
    testCases = []
 
42
    for benchmark in suite.benchmarks:
 
43
        testCases.append( TestCase( benchmark.name, description=benchmark.__doc__ ) )
 
44
    return testCases
 
45
 
 
46
# def runSuite( suite, target ):
 
47
#     runDir         = os.path.join( 'run', target )
 
48
#     inputImagePath = os.path.join( runDir, 'images' )
 
49
#     fileutil.cleanDir( inputImagePath )
 
50
#     spandexInput = suite.generateSpandexInput( target )
 
51
#     # Create the image directory
 
52
#     imageDir = os.path.join( runDir, 'images' )
 
53
#     fileutil.safeCreateDir( imageDir )
 
54
#     runSpandex( spandexInput, runDir )
 
55
#     tga2png.tga2png( imageDir, imageDir )
 
56
 
 
57
class VisualTestCase( bmbuilder.Benchmark ):
 
58
    def __init__( self ):
 
59
        bmbuilder.Benchmark.__init__( self )
 
60
        self.repeats = 1
 
61
 
 
62
def permutate( stack, rest ):
 
63
    if not rest:
 
64
        yield stack
 
65
        return
 
66
    attr   = rest.keys()[0]
 
67
    values = rest[attr]
 
68
    rest.pop( attr )
 
69
    if isinstance( attr, str ):
 
70
        names = (attr,)
 
71
    else:
 
72
        names = attr
 
73
    for value in values:
 
74
        if len( names ) > 1:
 
75
            for name, value in zip( names, value ):
 
76
                stack[name] = value
 
77
        else:
 
78
            stack[names[0]] = value
 
79
        for x in permutate( stack, rest ):
 
80
            yield x
 
81
    for name in names:
 
82
        stack.pop( name )
 
83
    rest[attr] = values
 
84
        
 
85
class VisualTestSuite( bmbuilder.Suite ):
 
86
    def addTestCase( self, testCaseClass ):
 
87
        if hasattr( testCaseClass, 'testValues' ):
 
88
            for kwargs in permutate( {}, testCaseClass.testValues ):
 
89
                self.addBenchmark( testCaseClass( **kwargs ) )
 
90
        else:
 
91
            self.addBenchmark( testCaseClass() )
 
92
 
 
93
################################################################################
 
94
# Command handling
 
95
commandConfig.update( { 'compare'  : '<candidate image path> <reference image path>',
 
96
                        'fetch'    : '<path> <target>',
 
97
                        'generate' : '<target>',
 
98
                        #'prepare'  : '<path> <target>',
 
99
                        #'run'      : '<targets>',
 
100
                        #'show'     : '<image paths>'
 
101
                        } )
 
102
 
 
103
def handleCompareCommand( suite, options, candidateImagePath, referenceImagePath ):
 
104
    testCases = createTestCases( suite )
 
105
    comparer  = image.getImageComparer( 'exact' )
 
106
 
 
107
    candidate_image_loader = image.TestCaseImageLoader( candidateImagePath )
 
108
    reference_image_loader = image.TestCaseImageLoader( referenceImagePath )
 
109
 
 
110
    if options.summary:
 
111
      report.create_image_comparison_report( options.reportName, testCases, reference_image_loader, candidate_image_loader, comparer, options.summary )
 
112
    else:
 
113
      report.create_image_comparison_report( options.reportName, testCases, reference_image_loader, candidate_image_loader, comparer, options.summary )
 
114
    
 
115
def handleFetchCommand( suite, options, path, target ):
 
116
    srcImagePath = os.path.join( path )
 
117
    dstImagePath = os.path.join( 'images', target )
 
118
    if not os.path.exists( srcImagePath ):
 
119
        console.exitWithError( 'Folder %s does not exist' % srcImagePath )
 
120
    if os.path.exists( dstImagePath ):
 
121
        if not console.confirm( 'ALL the images at %s will DELETED and images from %s will be copied there, OK?' % (dstImagePath, srcImagePath) ):
 
122
            console.exitWithError( 'Stopped by user request' )
 
123
        shutil.rmtree( dstImagePath )
 
124
    os.makedirs( dstImagePath )
 
125
    tga2png.tga2png( srcImagePath, dstImagePath )
 
126
        
 
127
def handleGenerateCommand( suite, options, target ):
 
128
    if options.separate:
 
129
        suite.generateSpandexInputToFiles( target )
 
130
    else:
 
131
        sys.stdout.write( suite.generateSpandexInput( target ) )
 
132
 
 
133
# def handlePrepareCommand( suite, options, path, target ):
 
134
#     if os.path.exists( path ):
 
135
#         if not console.confirm( '%s/benchmark.txt and everything under %s/Images will be deleted, OK?' % (path, path) ):
 
136
#             console.exitWithError( 'Stopped by user request' )
 
137
#         os.system( 'rm -f %s/benchmark.txt %s/images/*.*' % (path, path) )
 
138
#     else:
 
139
#         os.makedirs( '%s/images' % path )
 
140
#         console.printInfo( '%s/images created' % path )
 
141
#     # Generate benchmark.txt
 
142
#     file( '%s/benchmark.txt' % path, 'w' ).write( suite.generateSpandexInput( target ) )
 
143
 
 
144
# def handleRunCommand( suite, options, targets ):
 
145
#     for target in targets:
 
146
#         runSuite( suite, target )
 
147
 
 
148
#     if options.compare:
 
149
#         if len( targets ) != 2:
 
150
#             console.exitWithError( 'Need exactly two targets to compare' )
 
151
#         candidateImagePath = os.path.join( 'run', targets[0], 'images' )
 
152
#         referenceImagePath = os.path.join( 'run', targets[1], 'images' )
 
153
#         handleCompareCommand( options, candidateImagePath, referenceImagePath )
 
154
 
 
155
class VRTCLI( CLI ):
 
156
    def __init__( self ):
 
157
        CLI.__init__( self, VisualTestSuite() )
 
158
 
 
159
        self.parser.add_option( '-c', '--compare',
 
160
                                action='store_true',
 
161
                                dest='compare',
 
162
                                default=False,
 
163
                                help='produce comparison report when running two targets' )
 
164
        self.parser.add_option( '-r', '--report',
 
165
                                action='store_true',
 
166
                                dest='report',
 
167
                                default=False,
 
168
                                help='generate report' )
 
169
        self.parser.add_option( '--report-name',
 
170
                                dest='reportName',
 
171
                                type='string',
 
172
                                default='report',
 
173
                                help='name of the report' )
 
174
        self.parser.add_option( '-v', '--verbose',
 
175
                                action='store_true',
 
176
                                dest='verbose',
 
177
                                default=False,
 
178
                                help='print verbose progress information' )
 
179
        self.parser.add_option( '', '--separate',
 
180
                                action='store_true',
 
181
                                dest='separate',
 
182
                                default=False,
 
183
                                help='write benchmarks to separate files' )
 
184
        self.parser.add_option( '', '--summary',
 
185
                                action='store_true',
 
186
                                dest='summary',
 
187
                                default=False,
 
188
                                help='Create summary without images' )
 
189
 
 
190
def init():
 
191
    return VRTCLI().run()
 
192