예제 #1
0
    def _get_dependency_level(self):
        if 'level' in self.options:
            self.options['level'] = helpers.extract_scalar(self.options['level'])

            if not helpers.exists_in_list(self.options['level'],
                                          PACKAGES_DEPENDENCY_LEVELS):
                raise HttpReqError(415, "invalid option 'level'")

            self.opts['level'] = helpers.extract_exists_in_list(self.options['level'],
                                                                PACKAGES_LIST[self.reqpkg].keys())

            if not self.opts['level']:
                return
        else:
            self.opts['level'] = helpers.extract_exists_in_list(PACKAGES_LIST[self.reqpkg].keys(),
                                                                PACKAGES_DEPENDENCY_LEVELS)
예제 #2
0
파일: lshw.py 프로젝트: Eyepea/xivo-skaro
def Lshw(args, options):    # pylint: disable-msg=W0613
    """
    GET /lshw
    
    Just returns the list hardware

    >>> lshw({}, {'class':  'network'})
    >>> lshw({}, {'class':  {0: 'network', 1: 'system'}})
    >>> lshw({}, {'class':  ['network', 'system']})
    """

    opts = {}

    if 'class' in options:
        options['class'] = helpers.extract_scalar(options['class'])

        if helpers.exists_in_list(options['class'], LSHW_CLASS_LIST):
            opts['class'] = options['class']
        else:
            raise HttpReqError(415, "invalid option 'class'")

    if 'disable' in options:
        options['disable'] = helpers.extract_scalar(options['disable'])

        if helpers.exists_in_list(options['disable'], LSHW_TEST_LIST):
            opts['disable'] = options['disable']
        else:
            raise HttpReqError(415, "invalid option 'disable'")

    if 'enable' in options:
        options['enable'] = helpers.extract_scalar(options['enable'])

        if helpers.exists_in_list(options['enable'], LSHW_TEST_LIST):
            opts['enable'] = options['enable']
        else:
            raise HttpReqError(415, "invalid option 'enable'")

    if 'sanitize' in options:
        opts['sanitize'] = None

    if 'numeric' in options:
        opts['numeric'] = None

    if not LSHWLOCK.acquire_read(LSHW_LOCK_TIMEOUT):
        raise HttpReqError(503, "unable to take LSHWLOCK for reading after %s seconds" % LSHW_LOCK_TIMEOUT)

    lshw_cmd = [LSHW_BIN, '-xml']

    for key, value in opts.iteritems():
        if isinstance(value, (tuple, list)):
            for x in value:
                lshw_cmd.append("-%s" % key)
                lshw_cmd.append(x)
        else:
            lshw_cmd.append("-%s" % key)

            if value is not None:
                lshw_cmd.append(value)

    try:
        lshw = subprocess.Popen(lshw_cmd,
                                stdout=subprocess.PIPE,
                                stderr=subprocess.PIPE,
                                close_fds=True)

        if lshw.wait():
            log.error(lshw.stderr.read())
            raise LshwExecutionError("A problem occurred while executing command. (command: %r, error: %r)"
                                     % (lshw_cmd, lshw.stderr.read()))

        data = lshw.stdout.read()
        match = LSHW_RE_XML_DECLARATION(data)

        # XXX Workaround when missing XML top-level
        if match:
            xml = "%s<lshw>%s</lshw>" % (match.group(1), match.group(2))
        else:
            xml = "<lshw>%s</lshw>" % data

        return xml2dict.Parse(xml)
    finally:
        LSHWLOCK.release()