main.py 3.79 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# main.py
#
# Copyright (C) 2011 Carlos Garcia Campos <carlosgc@gnome.org>
#
# 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 2 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, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
from __future__ import absolute_import, division, print_function
19 20 21 22 23 24 25 26

import sys
import argparse
import commands
import backends
import os
from Config import Config

27 28
from multiprocessing import cpu_count

29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
class ListAction(argparse.Action):
    def __call__(self, parser, namespace, values, option_string = None):
        setattr(namespace, self.dest, values.split(','))

class HelpAction(argparse.Action):
    def __call__(self, parser, namespace, values, option_string = None):
        if option_string == '--help-command':
            commands.run([values, '--help'])
            sys.exit(0)

        parser.print_help()
        commands.print_help()

        sys.exit(0)

def main(args):
45 46
    n_cpus = cpu_count()

47 48 49
    parser = argparse.ArgumentParser(
        description = 'Poppler regression tests',
        prog = 'poppler-regtest',
50
        usage = '%(prog)s [options ...] command [command-options ...] tests',
51 52 53 54 55 56
        add_help = False)
    parser.add_argument('-h', '--help',
                        action = HelpAction, nargs = 0)
    parser.add_argument('--help-command', metavar = 'COMMAND',
                        action = HelpAction,
                        help = 'Show help for a given command')
57 58 59
    parser.add_argument('-v', '--verbose',
                        action = 'store_true', dest = 'verbose', default = False,
                        help = 'Run in verbose mode')
60
    parser.add_argument('--utils-dir',
61
                        action = 'store', dest = 'utils_dir', default = os.path.abspath("../build/utils"),
62 63 64 65
                        help = 'Directory of poppler utils used for the tests')
    parser.add_argument('-b', '--backends',
                        action = ListAction, dest = 'backends',
                        help = 'List of backends that will be used (separated by comma)')
66 67 68
    parser.add_argument('--skip', metavar = 'FILE',
                        action = 'store', dest = 'skipped_file',
                        help = 'File containing tests to skip')
69 70 71
    parser.add_argument('-p', '--passwords', metavar = 'FILE',
                        action = 'store', dest = 'passwords_file',
                        help = 'File containing the documents passwords')
72
    parser.add_argument('-t', '--threads',
73 74
                        action = 'store', dest = 'threads', type = int, default = n_cpus,
                        help = 'Number of worker threads (Default: %d)' % n_cpus)
75 76 77 78 79 80

    ns, args = parser.parse_known_args(args)
    if not args:
        parser.print_help()
        sys.exit(0)

81
    c = Config(vars(ns))
82

83
    if c.threads <= 0:
84
        c.threads = n_cpus - c.threads
85

86
    try:
87
        return commands.run(args)
88 89 90
    except commands.UnknownCommandError:
        sys.stderr.write("Unknown command: %s\n" % (args[0]))
        commands.print_help()
91
        return 1
92
    except backends.UnknownBackendError as e:
93 94
        sys.stderr.write(str(e) + "\n")
        sys.stdout.write("Backends are: %s\n" % (", ".join([backend.get_name() for backend in backends.get_all_backends()])))
95
        return 1
96 97 98

if __name__ == '__main__':
    import sys
99
    sys.exit(main(sys.argv[1:]))