Beispiel #1
0
def make_application(options, filepath):
    # options handling
    if options.verbosity > 0:
        LOGGER.setLevel(logging.INFO)
    if options.verbosity > 1:
        LOGGER.setLevel(logging.DEBUG)

    # So many projects contain its modules and packages at
    # the top level directory, modipyd inserts current directory
    # in ``sys.path`` module search path variable for convenience.
    sys.path.insert(0, os.getcwd())

    # Create Application instance, Install plugins
    application = Application(filepath)
    for plugin in options.plugins:
        application.install_plugin(plugin)

    # Predefine variables
    variables = {}
    for var in options.defines:
        i = var.find('=')
        if i == -1:
            variables[var] = ''
        else:
            variables[var[:i]] = var[i+1:]

    if variables:
        import pprint
        application.update_variables(variables)
        LOGGER.info(
            "Predefined variables: %s" % pprint.pformat(variables))

    # Load configuration (startup) file
    for rcfile in find_startup_files(os.environ, options.rcfile):
        LOGGER.info("Loading startup file from %s" % rcfile)
        execfile(rcfile, globals(), {'application': application})

    return application

if __name__ == "__main__":
    parser = OptionParser(usage="usage: %prog [options] file1, file2, ...")
    parser.add_option(
        "-r",
        "--runner",
        default="unittest.TextTestRunner",
        action="store",
        dest="runner",
        metavar="CLASS_NAME",
        help="qualified name of the unittest.TestRunner subclass " "(default: unittest.TextTestRunner)",
    )
    parser.add_option(
        "--loglevel",
        action="store",
        type="int",
        dest="loglevel",
        metavar="LOG_LEVEL",
        help="Specifies the lowest-severity log message a logger will handle",
    )

    options, args = parser.parse_args()

    if options.loglevel is not None:
        LOGGER.setLevel(options.loglevel)

    LOGGER.debug("Execute modipyd.tools.unittest_runner: %s" % " ".join(args))
    sys.path.insert(0, os.getcwd())
    main(args, options.runner)