• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ARMmbed / mbed-os-tools / #457

24 Aug 2024 09:15PM UTC coverage: 0.0% (-59.9%) from 59.947%
#457

push

coveralls-python

web-flow
Merge 7c6dbce13 into c467d6f14

0 of 4902 relevant lines covered (0.0%)

0.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/mbed_os_tools/test/mbed_greentea_dlm.py
1
# Copyright (c) 2018, Arm Limited and affiliates.
2
# SPDX-License-Identifier: Apache-2.0
3
#
4
# Licensed under the Apache License, Version 2.0 (the "License");
5
# you may not use this file except in compliance with the License.
6
# You may obtain a copy of the License at
7
#
8
#     http://www.apache.org/licenses/LICENSE-2.0
9
#
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
15

16
import os
×
17
import json
×
18
import uuid
×
19
import lockfile
×
20
from os.path import expanduser
×
21

22
HOME_DIR = expanduser("~")
×
23
GREENTEA_HOME_DIR = ".mbed-greentea"
×
24
GREENTEA_GLOBAL_LOCK = "glock.lock"
×
25
GREENTEA_KETTLE = "kettle.json" # active Greentea instances
×
26
GREENTEA_KETTLE_PATH = os.path.join(HOME_DIR, GREENTEA_HOME_DIR, GREENTEA_KETTLE)
×
27

28

29
def greentea_home_dir_init():
×
30
    """ Initialize data in home directory for locking features
31
    """
32
    if not os.path.isdir(os.path.join(HOME_DIR, GREENTEA_HOME_DIR)):
×
33
        os.mkdir(os.path.join(HOME_DIR, GREENTEA_HOME_DIR))
×
34

35
def greentea_get_app_sem():
×
36
    """ Obtain locking mechanism info
37
    """
38
    greentea_home_dir_init()
×
39
    gt_instance_uuid = str(uuid.uuid4())   # String version
×
40
    gt_file_sem_name = os.path.join(HOME_DIR, GREENTEA_HOME_DIR, gt_instance_uuid)
×
41
    gt_file_sem = lockfile.LockFile(gt_file_sem_name)
×
42
    return gt_file_sem, gt_file_sem_name, gt_instance_uuid
×
43

44
def greentea_get_target_lock(target_id):
×
45
    greentea_home_dir_init()
×
46
    file_path = os.path.join(HOME_DIR, GREENTEA_HOME_DIR, target_id)
×
47
    lock = lockfile.LockFile(file_path)
×
48
    return lock
×
49

50
def greentea_get_global_lock():
×
51
    greentea_home_dir_init()
×
52
    file_path = os.path.join(HOME_DIR, GREENTEA_HOME_DIR, GREENTEA_GLOBAL_LOCK)
×
53
    lock = lockfile.LockFile(file_path)
×
54
    return lock
×
55

56
def greentea_update_kettle(greentea_uuid):
×
57
    from time import gmtime, strftime
×
58

59
    with greentea_get_global_lock():
×
60
        current_brew = get_json_data_from_file(GREENTEA_KETTLE_PATH)
×
61
        if not current_brew:
×
62
            current_brew = {}
×
63
        current_brew[greentea_uuid] = {
×
64
            "start_time" : strftime("%Y-%m-%d %H:%M:%S", gmtime()),
65
            "cwd" : os.getcwd(),
66
            "locks" : []
67
        }
68
        with open(GREENTEA_KETTLE_PATH, 'w') as kettle_file:
×
69
            json.dump(current_brew, kettle_file, indent=4)
×
70

71
def greentea_clean_kettle(greentea_uuid):
×
72
    """ Clean info in local file system config file
73
    """
74
    with greentea_get_global_lock():
×
75
        current_brew = get_json_data_from_file(GREENTEA_KETTLE_PATH)
×
76
        if not current_brew:
×
77
            current_brew = {}
×
78
        current_brew.pop(greentea_uuid, None)
×
79
        with open(GREENTEA_KETTLE_PATH, 'w') as kettle_file:
×
80
            json.dump(current_brew, kettle_file, indent=4)
×
81

82
def greentea_acquire_target_id(target_id, gt_instance_uuid):
×
83
    """ Acquire lock on target_id for given greentea UUID
84
    """
85
    with greentea_get_global_lock():
×
86
        current_brew = get_json_data_from_file(GREENTEA_KETTLE_PATH)
×
87
        if current_brew:
×
88
            current_brew[gt_instance_uuid]['locks'].append(target_id)
×
89
            with open(GREENTEA_KETTLE_PATH, 'w') as kettle_file:
×
90
                json.dump(current_brew, kettle_file, indent=4)
×
91

92
def greentea_acquire_target_id_from_list(possible_target_ids, gt_instance_uuid):
×
93
    """ Acquire lock on target_id from list of possible target_ids for given greentea UUID
94
    """
95
    target_id = None
×
96
    already_locked_target_ids = []
×
97
    with greentea_get_global_lock():
×
98
        current_brew = get_json_data_from_file(GREENTEA_KETTLE_PATH)
×
99
        # Get all already locked target_id
100
        for cb in current_brew:
×
101
            locks_list = current_brew[cb]['locks']
×
102
            already_locked_target_ids.extend(locks_list)
×
103

104
        # Remove from possible_target_ids elements from already_locked_target_ids
105
        available_target_ids = [item for item in possible_target_ids if item not in already_locked_target_ids]
×
106

107
        if available_target_ids:
×
108
            target_id = available_target_ids[0]
×
109
            current_brew[gt_instance_uuid]['locks'].append(target_id)
×
110
            with open(GREENTEA_KETTLE_PATH, 'w') as kettle_file:
×
111
                json.dump(current_brew, kettle_file, indent=4)
×
112
    return target_id
×
113

114
def greentea_release_target_id(target_id, gt_instance_uuid):
×
115
    """ Release target_id for given greentea UUID
116
    """
117
    with greentea_get_global_lock():
×
118
        current_brew = get_json_data_from_file(GREENTEA_KETTLE_PATH)
×
119
        if current_brew:
×
120
            current_brew[gt_instance_uuid]['locks'].remove(target_id)
×
121
            with open(GREENTEA_KETTLE_PATH, 'w') as kettle_file:
×
122
                json.dump(current_brew, kettle_file, indent=4)
×
123

124
def get_json_data_from_file(json_spec_filename):
×
125
    """ Loads from file JSON formatted string to data structure
126
    """
127
    result = None
×
128
    try:
×
129
        with open(json_spec_filename, 'r') as data_file:
×
130
            try:
×
131
                result = json.load(data_file)
×
132
            except ValueError:
×
133
                result = None
×
134
    except IOError:
×
135
        result = None
×
136
    return result
×
137

138
def greentea_kettle_info():
×
139
    """ generates human friendly info about current kettle state
140

141
    @details
142
    {
143
        "475a46d0-41fe-41dc-b5e6-5197a2fcbb28": {
144
            "locks": [],
145
            "start_time": "2015-10-23 09:29:54",
146
            "cwd": "c:\\Work\\mbed-drivers"
147
        }
148
    }
149
    """
150
    from prettytable import PrettyTable
×
151
    with greentea_get_global_lock():
×
152
        current_brew = get_json_data_from_file(GREENTEA_KETTLE_PATH)
×
153
        cols = ['greentea_uuid', 'start_time', 'cwd', 'locks']
×
154
        pt = PrettyTable(cols)
×
155

156
        for col in cols:
×
157
            pt.align[col] = "l"
×
158
        pt.padding_width = 1 # One space between column edges and contents (default)
×
159

160
        row = []
×
161
        for greentea_uuid in current_brew:
×
162
            kettle = current_brew[greentea_uuid]
×
163
            row.append(greentea_uuid)
×
164
            row.append(kettle['start_time'])
×
165
            row.append(kettle['cwd'])
×
166
            row.append('\n'.join(kettle['locks']))
×
167
            pt.add_row(row)
×
168
            row = []
×
169
    return pt.get_string()
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc