示例#1
0
文件: util.py 项目: nysan/koji
def apply_argspec(argspec, args, kwargs=None):
    """Apply an argspec to the given args and return a dictionary"""
    if kwargs is None:
        kwargs = {}
    f_args, f_varargs, f_varkw, f_defaults = argspec
    data = dict(zip(f_args, args))
    if len(args) > len(f_args):
        if not f_varargs:
            raise koji.ParameterError('too many args')
        data[f_varargs] = tuple(args[len(f_args):])
    elif f_varargs:
        data[f_varargs] = ()
    if f_varkw:
        data[f_varkw] = {}
    for arg in kwargs:
        if arg in data:
            raise koji.ParameterError('duplicate keyword argument %r' % arg)
        if arg in f_args:
            data[arg] = kwargs[arg]
        elif not f_varkw:
            raise koji.ParameterError("unexpected keyword argument %r" % arg)
        else:
            data[f_varkw][arg] = kwargs[arg]
    if f_defaults:
        for arg, val in zip(f_args[-len(f_defaults):], f_defaults):
            data.setdefault(arg, val)
    for n, arg in enumerate(f_args):
        if arg not in data:
            raise koji.ParameterError('missing required argument %r (#%i)'
                                        % (arg, n))
    return data
示例#2
0
文件: tasks.py 项目: kszakharov/koji
def parse_task_params(method, params):
    """Parse task params into a dictionary

    New tasks should already be dictionaries
    """

    # check for new style
    if (len(params) == 1 and isinstance(params[0], dict)
            and '__method__' in params[0]):
        ret = params[0].copy()
        del ret['__method__']
        return ret

    # otherwise sort out the legacy signatures
    args, kwargs = koji.decode_args(*params)

    if method not in LEGACY_SIGNATURES:
        raise TypeError("No legacy signature for %s" % method)

    err = None
    for argspec in LEGACY_SIGNATURES[method]:
        try:
            params = koji.util.apply_argspec(argspec, args, kwargs)
            break
        except koji.ParameterError as e:
            if not err:
                err = e.args[0]
    else:
        raise koji.ParameterError("Invalid signature for %s: %s" %
                                  (method, err))

    return params
示例#3
0
def call_with_argcheck(func, args, kwargs=None):
    """Call function, raising ParameterError if args do not match"""
    if kwargs is None:
        kwargs = {}
    try:
        return func(*args, **kwargs)
    except TypeError as e:
        if sys.exc_info()[2].tb_next is None:
            # The stack is only one high, so the error occurred in this function.
            # Therefore, we assume the TypeError is due to a parameter mismatch
            # in the above function call.
            raise koji.ParameterError(str(e))
        raise
示例#4
0
def osbuildImage(name, version, distro, image_types, target, arches, opts=None, priority=None):
    """Create an image via osbuild"""
    context.session.assertPerm("image")
    args = [name, version, distro, image_types, target, arches, opts]
    task = {"channel": "image"}

    try:
        jsonschema.validate(args, OSBUILD_IMAGE_SCHMEA)
    except jsonschema.exceptions.ValidationError as err:
        raise koji.ParameterError(str(err)) from None

    if priority and priority < 0 and not context.session.hasPerm('admin'):
        raise koji.ActionNotAllowed('only admins may create high-priority tasks')

    return kojihub.make_task('osbuildImage', args, **task)