/
run.py
executable file
·144 lines (119 loc) · 4.83 KB
/
run.py
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
#!/usr/bin/env python
import os
import sys
import getopt
import json
import requests
import time
import regex
import difflib
from pyangbind.lib.xpathhelper import YANGPathHelper
from pyangbind.lib.pybindJSON import dumps
from pyangbind.lib.serialise import pybindIETFJSONEncoder, pybindJSONEncoder
from pyangbind.lib.yangtypes import YANGBool
TESTNAME = "json-serialise"
# generate bindings in this folder
def main():
try:
opts, args = getopt.getopt(sys.argv[1:], "k", ["keepfiles"])
except getopt.GetoptError as e:
print str(e)
sys.exit(127)
k = False
for o, a in opts:
if o in ["-k", "--keepfiles"]:
k = True
pythonpath = os.environ.get("PATH_TO_PYBIND_TEST_PYTHON") if \
os.environ.get('PATH_TO_PYBIND_TEST_PYTHON') is not None \
else sys.executable
pyangpath = os.environ.get('PYANGPATH') if \
os.environ.get('PYANGPATH') is not None else False
pyangbindpath = os.environ.get('PYANGBINDPATH') if \
os.environ.get('PYANGBINDPATH') is not None else False
assert pyangpath is not False, "could not find path to pyang"
assert pyangbindpath is not False, "could not resolve pyangbind directory"
OC = "https://raw.githubusercontent.com/openconfig/" + \
"public/master/release/models/"
RFC = "https://raw.githubusercontent.com/robshakir/" + \
"yang/master/standard/ietf/RFC/"
FETCH_FILES = [
(OC + "openconfig-extensions.yang", "include"),
(OC + "types/openconfig-types.yang", "include"),
(OC + "types/openconfig-yang-types.yang", "include"),
(OC + "types/openconfig-inet-types.yang", "include"),
(OC + "interfaces/openconfig-interfaces.yang", "openconfig"),
(RFC + "ietf-inet-types.yang", "include"),
(RFC + "ietf-yang-types.yang", "include"),
]
this_dir = os.path.dirname(os.path.realpath(__file__))
del_dirs = []
for fn in FETCH_FILES:
wrdir = os.path.join(this_dir, fn[1])
if not os.path.exists(wrdir):
os.mkdir(wrdir)
if wrdir not in del_dirs:
del_dirs.append(wrdir)
wrpath = os.path.join(this_dir, fn[1], fn[0].split("/")[-1])
if not os.path.exists(wrpath):
got = False
for i in range(0, 4):
response = requests.get(fn[0])
if response.status_code != 200:
time.sleep(2)
else:
got = True
f = open(wrpath, 'w')
f.write(response.content)
f.close()
break
assert got is True, "Could not get file %s from GitHub (response: %s)" \
% (response.status_code, fn[0])
files_str = " ".join([os.path.join(this_dir, "openconfig", i) for i in
os.listdir(os.path.join(this_dir, "openconfig"))])
cmd = "%s " % pythonpath
cmd += "%s --plugindir %s/pyangbind/plugin" % (pyangpath, pyangbindpath)
cmd += " -f pybind --split-class-dir %s/ocbind" % this_dir
cmd += " -p %s" % this_dir
cmd += " -p %s" % os.path.join(this_dir, "include")
cmd += " --use-xpathhelper "
cmd += files_str
os.system(cmd)
from ocbind import openconfig_interfaces
for fn in os.listdir(os.path.join(this_dir, "json")):
jobj = json.load(open(os.path.join(this_dir, "json", fn), 'r'))
parameters = regex.sub(
'interfaces\_ph:(?P<pathhelper>[a-zA-Z]+)\-flt:(?P<filter>[a-zA-Z]+)\-m:(?P<mode>[a-zA-Z]+)\.json',
'\g<pathhelper>||\g<filter>||\g<mode>', fn).split("||")
path_helper, config_filter, mode = YANGBool(parameters[0]), YANGBool(parameters[1]), parameters[2]
if path_helper:
ph = YANGPathHelper()
i = openconfig_interfaces(path_helper=ph)
else:
i = openconfig_interfaces()
i.interfaces.interface.add("eth0")
jstr = json.loads(dumps(i, filter=bool(config_filter), mode=mode))
sys.stdout.flush()
assert jstr == jobj, "Generated JSON did not match expected object for %s" % fn \
+ " diff:\n%s" % (diff(jstr, jobj))
passed = True
try:
jstr = json.loads(json.dumps(pybindIETFJSONEncoder.generate_element(i), cls=pybindIETFJSONEncoder))
except Exception as e:
passed = False
assert passed, "Serialisation test for object with pybindIETFJSONEncoder threw an error"
jstr = json.loads(json.dumps(i, cls=pybindJSONEncoder))
if not k:
del_dirs.append(os.path.join(this_dir, "ocbind"))
for dirname in del_dirs:
for root, dirs, files in os.walk(os.path.join(dirname), topdown=False):
for name in files:
os.remove(os.path.join(root, name))
for name in dirs:
os.rmdir(os.path.join(root, name))
os.rmdir(dirname)
def diff(a, b):
ja = json.dumps(a, indent=4, sort_keys=True).split("\n")
jb = json.dumps(b, indent=4, sort_keys=True).split("\n")
return '\n'.join(difflib.unified_diff(ja, jb))
if __name__ == '__main__':
main()