diff options
Diffstat (limited to 'meta/lib/oeqa/utils/metadata.py')
-rw-r--r-- | meta/lib/oeqa/utils/metadata.py | 83 |
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 @@ | |||
1 | # Copyright (C) 2016 Intel Corporation | ||
2 | # | ||
3 | # Released under the MIT license (see COPYING.MIT) | ||
4 | # | ||
5 | # Functions to get metadata from the testing host used | ||
6 | # for analytics of test results. | ||
7 | |||
8 | from git import Repo, InvalidGitRepositoryError, NoSuchPathError | ||
9 | from collections import OrderedDict | ||
10 | from collections.abc import MutableMapping | ||
11 | from xml.dom.minidom import parseString | ||
12 | from xml.etree.ElementTree import Element, tostring | ||
13 | |||
14 | from oe.lsb import distro_identifier | ||
15 | from oeqa.utils.commands import runCmd, get_bb_var, get_bb_vars | ||
16 | |||
17 | metadata_vars = ['MACHINE', 'DISTRO', 'DISTRO_VERSION'] | ||
18 | |||
19 | def metadata_from_bb(): | ||
20 | """ Returns test's metadata as OrderedDict. | ||
21 | |||
22 | Data will be gathered using bitbake -e thanks to get_bb_vars. | ||
23 | """ | ||
24 | |||
25 | info_dict = OrderedDict() | ||
26 | hostname = runCmd('hostname') | ||
27 | info_dict['hostname'] = hostname.output | ||
28 | data_dict = get_bb_vars(metadata_vars) | ||
29 | for var in metadata_vars: | ||
30 | info_dict[var.lower()] = data_dict[var] | ||
31 | host_distro= distro_identifier() | ||
32 | host_distro, _, host_distro_release = host_distro.partition('-') | ||
33 | info_dict['host_distro'] = host_distro | ||
34 | info_dict['host_distro_release'] = host_distro_release | ||
35 | info_dict['layers'] = get_layers(get_bb_var('BBLAYERS')) | ||
36 | return info_dict | ||
37 | |||
38 | def metadata_from_data_store(d): | ||
39 | """ Returns test's metadata as OrderedDict. | ||
40 | |||
41 | Data will be collected from the provided data store. | ||
42 | """ | ||
43 | # TODO: Getting metadata from the data store would | ||
44 | # be useful when running within bitbake. | ||
45 | pass | ||
46 | |||
47 | def get_layers(layers): | ||
48 | """ Returns layer name, branch, and revision as OrderedDict. """ | ||
49 | |||
50 | layer_dict = OrderedDict() | ||
51 | for layer in layers.split(): | ||
52 | layer_name = os.path.basename(layer) | ||
53 | layer_dict[layer_name] = OrderedDict() | ||
54 | try: | ||
55 | repo = Repo(layer, search_parent_directories=True) | ||
56 | revision, branch = repo.head.object.name_rev.split() | ||
57 | layer_dict[layer_name]['branch'] = branch | ||
58 | layer_dict[layer_name]['revision'] = revision | ||
59 | except (InvalidGitRepositoryError, NoSuchPathError): | ||
60 | layer_dict[layer_name]['branch'] = 'unknown' | ||
61 | layer_dict[layer_name]['revision'] = 'unknown' | ||
62 | return layer_dict | ||
63 | |||
64 | def write_metadata_file(file_path, metadata): | ||
65 | """ Writes metadata to a XML file in directory. """ | ||
66 | |||
67 | xml = dict_to_XML('metadata', metadata) | ||
68 | xml_doc = parseString(tostring(xml).decode('UTF-8')) | ||
69 | with open(file_path, 'w') as f: | ||
70 | f.write(xml_doc.toprettyxml()) | ||
71 | |||
72 | def dict_to_XML(tag, dictionary): | ||
73 | """ Return XML element converting dicts recursively. """ | ||
74 | |||
75 | elem = Element(tag) | ||
76 | for key, val in dictionary.items(): | ||
77 | if isinstance(val, MutableMapping): | ||
78 | child = (dict_to_XML(key, val)) | ||
79 | else: | ||
80 | child = Element(key) | ||
81 | child.text = str(val) | ||
82 | elem.append(child) | ||
83 | return elem | ||