예제 #1
0
파일: imp.py 프로젝트: IBM/microprobe
def import_operand_definition(
    defdict, yaml, key, base_module,
    regs, force=False
):
    """

    :param defdict:
    :param yaml:
    :param key:
    :param base_module:
    :param regs:

    """

    try:
        entry = defdict[key]
    except KeyError:
        raise MicroprobeArchitectureDefinitionError(
            "'%s' key in %s "
            "file missing or not defined "
            "correctly." % (key, yaml)
        )

    filenames = [yaml] + entry["YAML"]
    cache_filename = cache_file("%s.Operand" % (yaml))

    result = update_cache_needed(filenames, cachefile=cache_filename)
    result = result or force

    entry["YAML_inherits"] = entry.get("YAML_inherits", [])

    if not result:
        LOG.debug("Reading cache contents for Operand")
        try:
            return read_cache_data(cache_filename), result
        except ImportError:
            LOG.exception("Unable to read cache contents for Operand")
        except MicroprobeCacheError:
            LOG.debug("Cache error when reading cache contents for Operand")
    try:
        data = base_module.import_definition(
            entry["YAML"], entry["YAML_inherits"], regs
        )
    except KeyError:
        raise MicroprobeArchitectureDefinitionError(
            "'%s' key in %s "
            "file missing or not defined "
            "correctly." % (key, yaml)
        )

    try:
        write_cache_data(cache_filename, data)
    except MicroprobeCacheError:
        LOG.debug("Cache error when writing cache contents for Operand")

    return data, result
예제 #2
0
파일: imp.py 프로젝트: rommelsv/microprobe
def import_definition(defdict, yaml, key, base_module, args, force=False):
    """Import definition

    :param defdict:
    :param yaml:
    :param key:
    :param base_module:
    :param args:

    """

    try:
        entry = defdict[key]
        cls = get_object_from_module(entry["Class"], entry["Module"])
    except KeyError:
        raise MicroprobeArchitectureDefinitionError(
            "'%s' key in %s "
            "file missing or not defined "
            "correctly." % (key, yaml)
        )

    filenames = [yaml, entry["Module"]] + entry["YAML"]

    if issubclass(cls, PropertyHolder):
        for cfile in entry["YAML"]:
            filenames += list_property_files(cfile)

    cache_filename = cache_file("%s.%s" % (yaml, cls.__name__))

    result = update_cache_needed(filenames, cachefile=cache_filename)
    result = result or force

    if not result:
        LOG.debug("Reading cache contents for %s", cls.__name__)
        try:
            return read_cache_data(cache_filename), result
        except ImportError:
            LOG.exception("Unable to read cache contents for %s", cls.__name__)
        except MicroprobeCacheError:
            LOG.debug("Cache error when reading class %s", cls.__name__)

    try:
        data = base_module.import_definition(cls, entry["YAML"], args)
    except KeyError:
        raise MicroprobeArchitectureDefinitionError(
            "'%s' key in %s "
            "missing the YAML attribute." % (key, yaml)
        )

    try:
        write_cache_data(cache_filename, data)
    except MicroprobeCacheError:
        LOG.debug("Cache error when writing class %s", cls.__name__)

    return data, result
예제 #3
0
def _read_yaml(filename):
    """Reads a YAML file

    :param filename:

    """

    result = update_cache_needed([filename])
    if not result:
        try:
            return read_default_cache_data(filename)
        except MicroprobeCacheError:
            LOG.debug("Unable to read cache data for '%s'", filename)

    if not os.path.isfile(filename):
        raise MicroprobeYamlFormatError(
            "File '%s' referenced in YAML definition not found" % filename
        )

    with open(filename, 'r') as yaml_fd:
        raw_data = yaml_fd.read()
        try:
            data = yaml.load(raw_data)
        except yaml.composer.ComposerError as exc:
            raise MicroprobeYamlFormatError(
                "YAML parsing error while processing "
                "file '%s'. Error reported: '%s'" % (filename, str(exc))
            )

        except yaml.scanner.ScannerError as exc:
            raise MicroprobeYamlFormatError(
                "YAML parsing error while processing "
                "file '%s'. Error reported: '%s'" % (filename, str(exc))
            )

        except yaml.parser.ParserError as exc:
            raise MicroprobeYamlFormatError(
                "YAML parsing error while processing "
                "file '%s'. Error reported: '%s'" % (filename, str(exc))
            )
        except yaml.scanner.ScannerError as exc:
            raise MicroprobeYamlFormatError(
                "YAML parsing error while processing "
                "file '%s'. Error reported: '%s'" % (filename, str(exc))
            )

        try:
            write_default_cache_data(filename, data)
        except MicroprobeCacheError:
            LOG.debug("Unable to update cache data for '%s'", filename)

    return data
예제 #4
0
파일: yaml.py 프로젝트: IBM/microprobe
def read_yaml(data_file, schema_file):
    """Reads a file and checks it against the schema file. Returns
    the data

    :param data_file:
    :param schema_file:

    """

    LOG.debug("Start")
    LOG.debug("Data file: %s", data_file)
    LOG.debug("Schema file: %s", schema_file)

    result = update_cache_needed([data_file])
    result = result or update_cache_needed([schema_file])

    readed = False
    if not result:
        LOG.debug("Using cache contents for '%s'", data_file)
        try:
            data = read_default_cache_data(data_file)
            readed = True
        except MicroprobeCacheError:
            LOG.debug("Unable to read cache data for '%s'", data_file)
            readed = False

    if not readed:
        data = _read_yaml(data_file)

    if data is None:
        LOG.warning("No data found in file: %s", data_file)
        LOG.debug("End")
        return data

    schema = _create_yaml_schema(schema_file)

    if not schema.check(data):

        LOG.info("Schema not validated")

        if isinstance(data, list):

            LOG.info("Check each element to provide a nice hint to the error")

            for cdata in data:
                if not schema.check([cdata]):
                    LOG.info("Element failing:")
                    for line in yaml.dump(
                            cdata, default_flow_style=False).split('\n'):
                        LOG.info(line)
                    raise MicroprobeYamlFormatError(
                        "YAML definition file in"
                        "'%s' does not follow the "
                        "schema definition in '%s'" % (data_file, schema_file))
        else:
            raise MicroprobeYamlFormatError("YAML definition file in"
                                            "'%s' does not follow the "
                                            "schema definition in '%s'" %
                                            (data_file, schema_file))

    LOG.debug("End")
    return data