summaryrefslogtreecommitdiff
path: root/piglit
blob: 0bc7534020794669d93d771448bb9e4c31e9fd36 (plain)
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
#!/usr/bin/env python3

# Copyright (c) 2014 Intel Corporation

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

""" Wrapper for piglit executables

This imports functions from the framework and calls them with the argument
parts that the parser defined here doesn't know how to parse.

It is very important that the final parser not generate a help message
(add_help=False in the constructor arguments), otherwise this parser will
capture -h/--help and the results will not be useful.

"""

from __future__ import (
    absolute_import, division, print_function, unicode_literals
)
import argparse
import os
import os.path as path
import sys

import six


def setup_module_search_path():
    """Add Piglit's data directory to Python's module search path.

    This enables Python to import Piglit's framework module.

    CAUTION: This script must import the framework that *belongs to this
    script*. Mayhem occurs if this script accidentally imports a framework
    module that belongs to a different Piglit source tree or belongs to
    a different Piglit installation.

    CAUTION: This script file must be located in the Piglit source tree or in
    an installed location.  Otherwise this function may fail to find the
    framework module or, worse, it may succeed in finding a different Piglit's
    framework module.
    """

    # To avoid accidentally importing a framework module that belongs to
    # a different Piglit, base the search for Piglit's data directory on the
    # absolute path of the this script and *not* on the process's working
    # directory.
    abs_script_dir = os.path.abspath(os.path.dirname(__file__))

    tested_piglit_data_dirs = []

    def is_piglit_data_dir(dirpath):
        tested_piglit_data_dirs.append(dirpath)
        return path.exists(path.join(dirpath, 'framework'))

    # This script may be in two valid locations:
    #
    #   - At the top of a Piglit source tree, as below.  In this case, Piglit's
    #     data directory is the source directory itself.
    #
    #       ${piglit_source_dir}/${script_name}     -> This script.
    #       ${piglit_source_dir}/                   -> Piglit's data directory.
    #       ${piglit_source_dir}/framework          -> Piglit's framework module
    #
    if is_piglit_data_dir(abs_script_dir):
        sys.path.append(abs_script_dir)
        return

    # Or...
    #
    #   - In an installed location.  Piglit's installation layout looks like
    #     this, where ${ext} may be empty or ".py":
    #
    #       ${prefix}/${bindir}/${script_name}${ext}     -> This script.
    #       ${prefix}/${libdir}/${script_name}/          -> Piglit's data directory.
    #       ${prefix}/${libdir}/${script_name}/framework -> Piglit framework module.
    #
    abs_bindir = abs_script_dir
    script_basename_noext = os.path.splitext(os.path.basename(__file__))[0]
    for libdir in ('lib64', 'lib32', 'lib'):
        abs_libdir = path.normpath(path.join(abs_bindir, '..', libdir))
        abs_data_dir = path.join(abs_libdir, script_basename_noext)
        if is_piglit_data_dir(abs_data_dir):
            sys.path.insert(0, abs_data_dir)
            return

    print('error: failed to find piglit data directory. exiting...', file=sys.stderr)
    for test_dir in tested_piglit_data_dirs:
        print('error: tested directory {0!r}'.format(test_dir), file=sys.stderr)
    sys.exit(1)


setup_module_search_path()
import framework.programs.run as run
import framework.programs.summary as summary
import framework.programs.print_commands as pc


def main():
    """ Parse argument and call other executables """
    if six.PY2:
        input_ = [i.decode('utf-8') for i in sys.argv[1:]]
    elif six.PY3:
        input_ = sys.argv[1:]

    parser = argparse.ArgumentParser()
    subparsers = parser.add_subparsers()
    print_cmd = subparsers.add_parser('print-cmd',
                                      add_help=False,
                                      help="Print piglit commands, one per line.")
    print_cmd.set_defaults(func=pc.main)

    parse_run = subparsers.add_parser('run',
                                      add_help=False,
                                      help="Run a piglit test")
    parse_run.set_defaults(func=run.run)
    resume = subparsers.add_parser('resume',
                                   add_help=False,
                                   help="resume an interrupted piglit run")
    resume.set_defaults(func=run.resume)
    parse_summary = subparsers.add_parser('summary', help='summary generators')
    summary_parser = parse_summary.add_subparsers()
    html = summary_parser.add_parser('html',
                                     add_help=False,
                                     help='generate html reports from results')
    html.set_defaults(func=summary.html)
    console = summary_parser.add_parser('console',
                                        add_help=False,
                                        help='print results to terminal')
    console.set_defaults(func=summary.console)
    csv = summary_parser.add_parser('csv',
                                    add_help=False,
                                    help='generate csv from results')
    csv.set_defaults(func=summary.csv)
    formatted = summary_parser.add_parser('formatted',
                                    add_help=False,
                                    help='generate formatted output from results')
    formatted.set_defaults(func=summary.formatted)
    aggregate = summary_parser.add_parser('aggregate',
                                          add_help=False,
                                          help="Aggregate incomplete piglit run.")
    aggregate.set_defaults(func=summary.aggregate)
    feature = summary_parser.add_parser('feature',
                                        add_help=False,
                                        help="generate feature readiness html report.")
    feature.set_defaults(func=summary.feature)

    # Parse the known arguments (piglit run or piglit summary html for
    # example), and then pass the arguments that this parser doesn't know about
    # to that executable
    parsed, args = parser.parse_known_args(input_)
    try:
        runner = parsed.func
    except AttributeError:
        parser.print_help()
        sys.exit(1)
    sys.exit(runner(args))


if __name__ == '__main__':
    main()