Example #1
0
def roman_build(base_path,
                course_id,
                course_key,
                instance_key,
                build_number,
                config_filename=None):
    shepherd_builder_observer = ShepherdObserver(
        [course_id, build_number, BuildStep.BUILD.name, None, ""])
    source_path = join(base_path, 'builds', course_key, instance_key,
                       build_number)
    if not exists(source_path):
        logger.error("Cannot find source file for building at %s", source_path)
        return 1
    # Get config
    try:
        project_config = ProjectConfig.find_from(source_path)
    except ValidationError as e:
        logger.error(render_error(e))
        return 1
    except ProjectConfigError as e:
        logger.error('Invalid project configuration: %s', e)
        return 1
    # Get global settings
    try:
        global_settings = GlobalSettings.load(
            config_filename if config_filename is not None else
            GlobalSettings.get_config_path(),
            allow_missing=True)
    except ValidationError as e:
        logger.error(render_error(e))
        return 1
    except OSError as e:
        logger.error(str(e))
        return 1
    # Get engine
    try:
        engine = Engine(settings=global_settings)
    except ImportError:
        logger.error("Unable to find backend %s", str(global_settings))
        return 1
    builder = engine.create_builder(project_config, shepherd_builder_observer)
    if not project_config.steps:
        logger.error("Nothing to build.")
        return 1
    try:
        result = builder.build()
    except KeyError as err:
        logger.error("No step named %s", err.args[0])
        return 1
    except IndexError as err:
        logger.error(
            "Index %s is out of range. There are %d steps. Indexing begins ar 0.",
            err.args[0], len(project_config.steps))
    return result.code
Example #2
0
def validate_schema_action(context):
    Container = Document.bind(schema=context.args.schema_name).Container
    files = chain.from_iterable(glob(s) for s in context.args.data_files)
    max_version = context.args.schema_version

    def print2(msg):
        print(" ", msg)

    if max_version is None:
        get_docs = lambda x: x
    else:

        def get_docs(container):
            try:
                doc = container.get_latest(max_version, validate=False)
            except KeyError:
                print2(
                    _("No elements with max version %s found!") % max_version)
                return ()
            if doc.version is None:
                doc.version = max_version
            return (doc, )

    errors = 0
    documents = 0
    for file_ in files:
        container = Container(file_)
        print("%s:" % (container.path, ))
        if not container:
            print2(_("The data file is empty!"))
            continue

        for doc in get_docs(container):
            try:
                doc.validate(quiet=True)
            except ValidationError as e:
                print2(
                    _("Document %d failed against %s") %
                    (doc.index, doc.validator_id))
                print('\n' + '\n'.join('    ' + s
                                       for s in render_error(e)) + '\n')
                errors += 1
            else:
                print2(
                    _("Document %d validates against %s") %
                    (doc.index, doc.validator_id))
            documents += 1
        print()
    if errors > 0:
        print(
            _("Found total of %d errors in %d documents.") %
            (errors, documents))
        return 1
    print(_("All %d documents are valid.") % documents)
    return 0
Example #3
0
def get_config(context):
    try:
        if context.args.project_config:
            project_config = abspath(
                expanduser(expandvars(context.args.project_config)))
            return ProjectConfig.load_from(project_config)
        try:
            return ProjectConfig.find_from(getcwd())
        except FileNotFoundError as err:
            msg = _(
                "You can create a configuration file with '{} init'.").format(
                    context.parser.prog)
            exit(1, "{}\n{}".format(err, msg))
    except ValidationError as e:
        exit(1, '\n'.join(render_error(e)))
    except ProjectConfigError as e:
        exit(1, _("Invalid project configuration: {}").format(e))
Example #4
0
def parse_actioncontext(parser, *, args=None):
    args = parser.parse_args(args=args)

    # set logging level
    max_level = len(LOG_LEVELS) - 1
    if args.debug or args.verbose >= max_level:
        args.debug = True
        args.verbose = max_level
    logging.getLogger().setLevel(LOG_LEVELS[args.verbose])

    # set working directory
    if args.directory:
        dirs = [expanduser(expandvars(d)) for d in args.directory]
        chdir(path_join(*dirs))

    # load settings
    config = (args.config
              if args.config is not None else GlobalSettings.get_config_path())

    logger.debug(_("Loading settings from '%s'"), config)

    try:
        settings = GlobalSettings.load(config, allow_missing=True)
    except ValidationError as e:
        exit(1, '\n'.join(render_error(e)))
    except OSError as e:
        exit(1, str(e))

    if args.config is not None and not settings.container.exists():
        warning(_("File {} doesn't exist.").format(args.config))

    settings.update_from_namespace(args)

    # post process rest of the args
    if args.steps and any(step == '?' for step in args.steps):
        args.list_steps = True

    return ActionContext(parser, args, settings, parser.get_callback(args))
Example #5
0
def step_add_action(context):
    args = context.args
    env = args.env
    check_env(env)
    step = {
        'img': args.img,
        'cmd': args.cmd,
        'mnt': args.mnt,
        'env': env,
        'name': args.name
    }
    step = {k: v for k, v in step.items() if v}
    config = get_config(context)
    try:
        config.add_step(step)
    except ValueError as err:
        exit(1, str(err))
    try:
        config.validate()
    except ValidationError as err:
        exit(1, '\n'.join(render_error(err)))
    config.save()
    print("Step successfully added to config.")