summaryrefslogtreecommitdiffstats
path: root/scripts/lib/resulttool/resultsutils.py
blob: 368786922c52516d26db76579155d924671eee4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# test result tool - utilities
#
# Copyright (c) 2019, Intel Corporation.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms and conditions of the GNU General Public License,
# version 2, as published by the Free Software Foundation.
#
# This program is distributed in the hope it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
# more details.
#
import os
import json
import scriptpath
scriptpath.add_oe_lib_path()
from oeqa.utils.git import GitRepo, GitError

def load_json_file(file):
    with open(file, "r") as f:
        return json.load(f)

def dump_json_data(write_dir, file_name, json_data):
    file_content = json.dumps(json_data, sort_keys=True, indent=4)
    file_path = os.path.join(write_dir, file_name)
    with open(file_path, 'w') as the_file:
        the_file.write(file_content)

def get_dict_value(logger, dict, key):
    try:
        return dict[key]
    except KeyError:
        if logger:
            logger.debug('Faced KeyError exception: dict=%s: key=%s' % (dict, key))
        return None
    except TypeError:
        if logger:
            logger.debug('Faced TypeError exception: dict=%s: key=%s' % (dict, key))
        return None

def pop_dict_element(logger, dict, key):
    try:
        dict.pop(key)
    except KeyError:
        if logger:
            logger.debug('Faced KeyError exception: dict=%s: key=%s' % (dict, key))
    except AttributeError:
        if logger:
            logger.debug('Faced AttributeError exception: dict=%s: key=%s' % (dict, key))

def checkout_git_dir(git_dir, git_branch):
    try:
        repo = GitRepo(git_dir, is_topdir=True)
        repo.run_cmd('checkout %s' % git_branch)
        return True
    except GitError:
        return False

def get_directory_files(source_dir, excludes, file):
    files_in_dir = []
    for root, dirs, files in os.walk(source_dir, topdown=True):
        [dirs.remove(d) for d in list(dirs) if d in excludes]
        for name in files:
            if name == file:
                files_in_dir.append(os.path.join(root, name))
    return files_in_dir