summaryrefslogtreecommitdiff
path: root/docs/_ext/testcoverage.py
blob: 70714d6bc2c1cd6f0d3214f8a8ce4695152fc4ce (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
'''
generate json with all commands from xml for vyos documentation coverage

'''


import sys
import os
import json
import re
import logging

from io import BytesIO
from lxml import etree as ET
import shutil

default_constraint_err_msg = "Invalid value"
validator_dir = ""


input_data = [
    {
        "kind": "cfgcmd",
        "input_dir": "_include/vyos-1x/interface-definitions/",
        "schema_file": "_include/vyos-1x/schema/interface_definition.rng",
        "files": []
    },
    {
        "kind": "opcmd",
        "input_dir": "_include/vyos-1x/op-mode-definitions/",
        "schema_file": "_include/vyos-1x/schema/op-mode-definition.rng",
        "files": []
    }
]

node_data = {
    'cfgcmd': {},
    'opcmd': {},
}

def get_properties(p):
    props = {}
    props['valueless'] = False

    try:
        if p.find("valueless") is not None:
            props['valueless'] = True
    except:
        pass

    if p is None:
        return props

    # Get the help string
    try:
        props["help"] = p.find("help").text
    except:
        pass

    # Get value help strings
    try:
        vhe = p.findall("valueHelp")
        vh = []
        for v in vhe:
            vh.append( (v.find("format").text, v.find("description").text) )
        props["val_help"] = vh
    except:
        props["val_help"] = []

    # Get the constraint statements
    error_msg = default_constraint_err_msg
    # Get the error message if it's there
    try:
        error_msg = p.find("constraintErrorMessage").text
    except:
        pass
    

    vce = p.find("constraint")
    vc = []
    if vce is not None:
        # The old backend doesn't support multiple validators in OR mode
        # so we emulate it

        regexes = []
        regex_elements = vce.findall("regex")
        if regex_elements is not None:
            regexes = list(map(lambda e: e.text.strip(), regex_elements))
        if "" in regexes:
            print("Warning: empty regex, node will be accepting any value")

        validator_elements = vce.findall("validator")
        validators = []
        if validator_elements is not None:
            for v in validator_elements:
                v_name = os.path.join(validator_dir, v.get("name"))

                # XXX: lxml returns None for empty arguments
                v_argument = None
                try:
                    v_argument = v.get("argument")
                except:
                    pass
                if v_argument is None:
                    v_argument = ""

                validators.append("{0} {1}".format(v_name, v_argument))


        regex_args = " ".join(map(lambda s: "--regex \\\'{0}\\\'".format(s), regexes))
        validator_args = " ".join(map(lambda s: "--exec \\\"{0}\\\"".format(s), validators))
        validator_script = '${vyos_libexec_dir}/validate-value.py'
        validator_string = "exec \"{0} {1} {2} --value \\\'$VAR(@)\\\'\"; \"{3}\"".format(validator_script, regex_args, validator_args, error_msg)

        props["constraint"] = validator_string

    # Get the completion help strings
    try:
        che = p.findall("completionHelp")
        ch = ""
        for c in che:
            scripts = c.findall("script")
            paths = c.findall("path")
            lists = c.findall("list")

            # Current backend doesn't support multiple allowed: tags
            # so we get to emulate it
            comp_exprs = []
            for i in lists:
                comp_exprs.append("echo \"{0}\"".format(i.text))
            for i in paths:
                comp_exprs.append("/bin/cli-shell-api listNodes {0}".format(i.text))
            for i in scripts:
                comp_exprs.append("sh -c \"{0}\"".format(i.text))
            comp_help = " && ".join(comp_exprs)
            props["comp_help"] = comp_help
    except:
        props["comp_help"] = []

    # Get priority
    try:
        props["priority"] = p.find("priority").text
    except:
        pass

    # Get "multi"
    if p.find("multi") is not None:
        props["multi"] = True

    # Get "valueless"
    if p.find("valueless") is not None:
        props["valueless"] = True

    return props

def process_node(n, f):

    props_elem = n.find("properties")
    children = n.find("children")
    command = n.find("command")
    children_nodes = []
    owner = n.get("owner")
    node_type = n.tag

    name = n.get("name")
    props = get_properties(props_elem)

    if node_type != "node":
        if "valueless" not in props.keys():
            props["type"] = "txt"
    if node_type == "tagNode":
        props["tag"] = "True"
    
    if node_type == "node" and children is not None:
        inner_nodes = children.iterfind("*")
        index_child = 0
        for inner_n in inner_nodes:
            children_nodes.append(process_node(inner_n, f))
            index_child = index_child + 1

    if node_type == "tagNode" and children is not None:
        inner_nodes = children.iterfind("*")
        index_child = 0
        for inner_n in inner_nodes:
            children_nodes.append(process_node(inner_n, f))
            index_child = index_child + 1
    else:
        # This is a leaf node
        pass
    
    if command is not None:
        test_command = True
    else:
        test_command = False
    node = {
        'name': name,
        'type': node_type,
        'children': children_nodes,
        'props': props,
        'command': test_command,
        'filename': f
    }
    return node



def create_commands(data, parent_list=[], level=0):
    result = []
    command = {
        'name': [],
        'help': None,
        'tag_help': [],
        'level': level,
        'no_childs': False,
        'filename': None
    }
    command['filename'] = data['filename']
    command['name'].extend(parent_list)
    command['name'].append(data['name'])

    if data['type'] == 'tagNode':
        command['name'].append("<" + data['name'] + ">")

    if 'val_help' in data['props'].keys():
        for val_help in data['props']['val_help']:
            command['tag_help'].append(val_help)
    
    if len(data['children']) == 0:
        command['no_childs'] = True
    
    if data['command']:
        command['no_childs'] = True
    
    try:
        help_text = data['props']['help']
        command['help'] = re.sub(r"[\n\t]*", "", help_text)
        
    except:
        command['help'] = ""
    
    command['valueless'] = data['props']['valueless']
    
    if 'children' in data.keys():
        children_bool = True
        for child in data['children']:
            result.extend(create_commands(child, command['name'], level + 1))
    
    if command['no_childs']:
        result.append(command)
    


    return result


def include_file(line, input_dir):
    string = ""
    if "#include <include" in line.strip():
        include_filename = line.strip().split('<')[1][:-1]
        with open(input_dir + include_filename) as ifp:
            iline = ifp.readline()
            while iline:
                string = string + include_file(iline.strip(), input_dir)
                iline = ifp.readline()
    else:
        string = line
    return string


def get_working_commands():
    for entry in input_data:
        for (dirpath, dirnames, filenames) in os.walk(entry['input_dir']):
            entry['files'].extend(filenames)
            break

        for f in entry['files']:

            string = ""
            with open(entry['input_dir'] + f) as fp:
                line = fp.readline()
                while line:                
                    string = string + include_file(line.strip(), entry['input_dir'])
                    line = fp.readline()
            
            try:
                xml = ET.parse(BytesIO(bytes(string, 'utf-8')))
            except Exception as e:
                print("Failed to load interface definition file {0}".format(f))
                print(e)
                sys.exit(1)
            
            try:
                relaxng_xml = ET.parse(entry['schema_file'])
                validator = ET.RelaxNG(relaxng_xml)

                if not validator.validate(xml):
                    print(validator.error_log)
                    print("Interface definition file {0} does not match the schema!".format(f))
                    sys.exit(1)
            except Exception as e:
                print("Failed to load the XML schema {0}".format(entry['schema_file']))
                print(e)
                sys.exit(1)

            root = xml.getroot()
            nodes = root.iterfind("*")
            for n in nodes:
                node_data[entry['kind']][f] = process_node(n, f)

    # build config tree and sort

    config_tree_new = {
        'cfgcmd': {},
        'opcmd': {},
    }

    for kind in node_data:
        for entry in node_data[kind]:
            node_0 = node_data[kind][entry]['name']
            
            if node_0 not in config_tree_new[kind].keys():
                config_tree_new[kind][node_0] = {
                    'name': node_0,
                    'type': node_data[kind][entry]['type'],
                    'props': node_data[kind][entry]['props'],
                    'children': [],
                    'command': node_data[kind][entry]['command'],
                    'filename': node_data[kind][entry]['filename'],
                }
            config_tree_new[kind][node_0]['children'].extend(node_data[kind][entry]['children'])
    
    result = {
        'cfgcmd': [],
        'opcmd': [],
    }
    for kind in  config_tree_new:
        for e in config_tree_new[kind]:
            result[kind].extend(create_commands(config_tree_new[kind][e]))
    
    for cmd in result['cfgcmd']:
        cmd['cmd'] = " ".join(cmd['name'])
    for cmd in result['opcmd']:
        cmd['cmd'] = " ".join(cmd['name'])
    return result



if __name__ == "__main__":
    res = get_working_commands()
    print(json.dumps(res))
    #print(res['cfgcmd'][0])