[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

Re: [Libosinfo] [PATCH osinfo-db 0/4] treeinfo data fixes



On 3/1/19 3:54 PM, Cole Robinson wrote:
> I wrote a script which iterates over the database and pulls out any
> <tree> URLs that have associated <treeinfo> metadata, then passes those
> URLs to osinfo_tree_create_from_location + osinfo_guess_os_from_tree,
> to ensure that we detect the URL as the same OS that it's listed with.
> I'll attach the script in a follow up mail.
> 

Script attached. This could be a starting point for a testcase I guess.

I think we would also want a test suite with raw treeinfo files though
which would be easier to unit test, in playing with this stuff it's
clear that it's easy to add a new treeinfo annotation which breaks
detection for an old URL

- Cole
#!/usr/bin/env python3

import argparse
import os
import re
import sys

import gi
gi.require_version('Libosinfo', '1.0')
from gi.repository import Libosinfo


loader = Libosinfo.Loader()
loader.process_path(os.path.join(os.path.dirname(__file__), "data"))


# natural/human sorting
# https://stackoverflow.com/questions/5967500/how-to-correctly-sort-a-string-with-a-number-inside

def atof(text):
    try:
        retval = float(text)
    except ValueError:
        retval = text
    return retval


def natural_keys(text):
    return [atof(c) for c in
            re.split(r'[+-]?([0-9]+(?:[.][0-9]*)?|[.][0-9]+)', text)]


def _os_get_treeinfo_trees(osobj):
    ret = []
    treelist = osobj.get_tree_list()
    for idx in range(treelist.get_length()):
        treeobj = treelist.get_nth(idx)
        if treeobj.get_url() and treeobj.get_treeinfo_family():
            ret.append(treeobj)
    return ret


def _find_all_url_trees(match):
    """
    Return a list of Os objects that match the passed short-id starting string,
    and have both a tree URL and treeinfo family
    """
    oslist = loader.get_db().get_os_list()
    ret = []
    for idx in range(oslist.get_length()):
        osobj = oslist.get_nth(idx)
        if match and not osobj.get_short_id().startswith(match):
            continue
        if _os_get_treeinfo_trees(osobj):
            ret.append(osobj)

    ret.sort(key=lambda o: natural_keys(o.get_short_id()))
    return ret


def _test_os_url(osobj):
    for tree in _os_get_treeinfo_trees(osobj):
        url = tree.get_url()
        arch = tree.get_architecture()
        kernelpath = tree.get_kernel_path()
        initrdpath = tree.get_initrd_path()
        print("Checking id=%s arch=%s" % (osobj.get_short_id(), arch))
        checktree = Libosinfo.Tree.create_from_location(url, None)
        foundos, foundtree = loader.get_db().guess_os_from_tree(checktree)

        try:
            if not foundos:
                raise AssertionError("Didn't find any matching OS")
            if foundos.get_short_id() != osobj.get_short_id():
                raise AssertionError("os=%s but foundos=%s" %
                    (osobj.get_short_id(), foundos.get_short_id()))
            if foundtree.get_url() != url:
                raise AssertionError("url=%s but foundurl=%s" %
                    (url, foundtree.get_url()))
            if foundtree.get_kernel_path() != kernelpath:
                raise AssertionError("kernel=%s but foundkernel=%s" %
                    (kernelpath, foundtree.get_kernel_path()))
            if foundtree.get_initrd_path() != initrdpath:
                raise AssertionError("initrd=%s but foundinitrd=%s" %
                    (initrdpath, foundtree.get_initrd_path()))
        except AssertionError as e:
            print("FAILED: %s\nURL: %s\n" % (e, url))


##############################
# main() and option handling #
##############################

def _parse_args():
    desc = None
    usage = None
    parser = argparse.ArgumentParser(usage=usage, description=desc)

    parser.add_argument("match", nargs="?",
            help="Only test short-ids that start with this string")

    options = parser.parse_args()
    return options


def _main():
    """
    This is a template for new command line programs. Copy and edit it!
    """
    options = _parse_args()

    oslist = _find_all_url_trees(options.match)
    for osobj in oslist:
        _test_os_url(osobj)

    return 0


if __name__ == '__main__':
    sys.exit(_main())

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]