summaryrefslogtreecommitdiff
path: root/meta
diff options
context:
space:
mode:
authorMariano Lopez <mariano.lopez@linux.intel.com>2016-11-27 13:49:29 -0600
committerRichard Purdie <richard.purdie@linuxfoundation.org>2016-12-13 22:47:26 +0000
commit10b05794254886e55c76f29f7778d783c550befa (patch)
tree8a6cf67ef11d9741fe1e4aa571ad19eb7c4079ef /meta
parent22b7fa24fefcc3974806d1b282c93b8c5880f6a4 (diff)
downloadopenembedded-core-10b05794254886e55c76f29f7778d783c550befa.tar.gz
openembedded-core-10b05794254886e55c76f29f7778d783c550befa.tar.bz2
openembedded-core-10b05794254886e55c76f29f7778d783c550befa.zip
oeqa/utils/metadata.py: Add metadata library
Adds functions to get metadata from the host running the tests. [YOCTO #9954] Signed-off-by: Mariano Lopez <mariano.lopez@linux.intel.com> Signed-off-by: Ross Burton <ross.burton@intel.com>
Diffstat (limited to 'meta')
-rw-r--r--meta/lib/oeqa/utils/metadata.py83
1 files changed, 83 insertions, 0 deletions
diff --git a/meta/lib/oeqa/utils/metadata.py b/meta/lib/oeqa/utils/metadata.py
new file mode 100644
index 0000000000..ecbe763c0f
--- /dev/null
+++ b/meta/lib/oeqa/utils/metadata.py
@@ -0,0 +1,83 @@
+# Copyright (C) 2016 Intel Corporation
+#
+# Released under the MIT license (see COPYING.MIT)
+#
+# Functions to get metadata from the testing host used
+# for analytics of test results.
+
+from git import Repo, InvalidGitRepositoryError, NoSuchPathError
+from collections import OrderedDict
+from collections.abc import MutableMapping
+from xml.dom.minidom import parseString
+from xml.etree.ElementTree import Element, tostring
+
+from oe.lsb import distro_identifier
+from oeqa.utils.commands import runCmd, get_bb_var, get_bb_vars
+
+metadata_vars = ['MACHINE', 'DISTRO', 'DISTRO_VERSION']
+
+def metadata_from_bb():
+ """ Returns test's metadata as OrderedDict.
+
+ Data will be gathered using bitbake -e thanks to get_bb_vars.
+ """
+
+ info_dict = OrderedDict()
+ hostname = runCmd('hostname')
+ info_dict['hostname'] = hostname.output
+ data_dict = get_bb_vars(metadata_vars)
+ for var in metadata_vars:
+ info_dict[var.lower()] = data_dict[var]
+ host_distro= distro_identifier()
+ host_distro, _, host_distro_release = host_distro.partition('-')
+ info_dict['host_distro'] = host_distro
+ info_dict['host_distro_release'] = host_distro_release
+ info_dict['layers'] = get_layers(get_bb_var('BBLAYERS'))
+ return info_dict
+
+def metadata_from_data_store(d):
+ """ Returns test's metadata as OrderedDict.
+
+ Data will be collected from the provided data store.
+ """
+ # TODO: Getting metadata from the data store would
+ # be useful when running within bitbake.
+ pass
+
+def get_layers(layers):
+ """ Returns layer name, branch, and revision as OrderedDict. """
+
+ layer_dict = OrderedDict()
+ for layer in layers.split():
+ layer_name = os.path.basename(layer)
+ layer_dict[layer_name] = OrderedDict()
+ try:
+ repo = Repo(layer, search_parent_directories=True)
+ revision, branch = repo.head.object.name_rev.split()
+ layer_dict[layer_name]['branch'] = branch
+ layer_dict[layer_name]['revision'] = revision
+ except (InvalidGitRepositoryError, NoSuchPathError):
+ layer_dict[layer_name]['branch'] = 'unknown'
+ layer_dict[layer_name]['revision'] = 'unknown'
+ return layer_dict
+
+def write_metadata_file(file_path, metadata):
+ """ Writes metadata to a XML file in directory. """
+
+ xml = dict_to_XML('metadata', metadata)
+ xml_doc = parseString(tostring(xml).decode('UTF-8'))
+ with open(file_path, 'w') as f:
+ f.write(xml_doc.toprettyxml())
+
+def dict_to_XML(tag, dictionary):
+ """ Return XML element converting dicts recursively. """
+
+ elem = Element(tag)
+ for key, val in dictionary.items():
+ if isinstance(val, MutableMapping):
+ child = (dict_to_XML(key, val))
+ else:
+ child = Element(key)
+ child.text = str(val)
+ elem.append(child)
+ return elem