forked from hazelcast/hazelcast-client-protocol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.py
executable file
·228 lines (191 loc) · 7.25 KB
/
generator.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
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
#!/usr/bin/env python3
import argparse
import time
from os.path import abspath, dirname, exists, join, realpath
from jinja2 import TemplateNotFound
from binary.util import binary_output_directories, test_output_directories
from binary_generator import get_binary_templates, save_binary_files, save_test_files
from util import *
start = time.time()
parser = argparse.ArgumentParser(
description="Hazelcast Code Generator generates code of client protocol across languages."
)
parser.add_argument(
"-r",
"--root-dir",
dest="root_dir",
action="store",
metavar="ROOT_DIRECTORY",
default=None,
type=str,
help="Root directory for the generated codecs (default value is ./output/[LANGUAGE])",
)
parser.add_argument(
"-l",
"--lang",
dest="lang",
action="store",
metavar="LANGUAGE",
default="java",
choices=[lang.value for lang in SupportedLanguages],
type=str,
help="Language to generate codecs for (default default is java)",
)
parser.add_argument(
"-p",
"--protocol-definitions-path",
dest="proto_path",
action="store",
metavar="PROTOCOL_DEFS_PATH",
default=None,
type=str,
help="Path of protocol definitions directory (default value is ./protocol-definitions)",
)
parser.add_argument(
"-o",
"--output-dir",
dest="out_dir",
action="store",
metavar="OUTPUT_DIRECTORY",
default=None,
type=str,
help="Path of the output directory relative to the "
"root directory (default value is set according to the selected "
"language)",
)
parser.add_argument(
"-n",
"--namespace",
dest="namespace",
action="store",
metavar="NAMESPACE",
default=None,
type=str,
help="Namespace for the generated codecs (default value is inferred from the "
"selected language)",
)
parser.add_argument(
"-b",
"--binary-output-dir",
dest="bin_out_dir",
action="store",
metavar="BINARY_OUTPUT_DIRECTORY",
default=None,
type=str,
help="Path of the output directory relative to the "
"root directory for the binary file.(default value is set according to the selected "
"language)",
)
parser.add_argument(
"-t",
"--test-output-dir",
dest="test_out_dir",
action="store",
metavar="TEST_OUTPUT_DIRECTORY",
default=None,
type=str,
help="Path of the output directory relative to the "
"root directory for the binary compatibility test files.(default value is "
"set according to the selected language)",
)
parser.add_argument(
"--no-binary",
dest="no_binary",
action="store_true",
default=False,
help="Flag to signal that binary compatibility files and tests"
"should not be generated. These files are generated by default.",
)
parser.add_argument(
"--no-id-check",
dest="no_id_check",
action="store_true",
default=False,
help="Flag to signal that no sequential id check for service or method definitions "
"should be performed. These checks are done by default.",
)
args = parser.parse_args()
lang = SupportedLanguages[args.lang.upper()]
curr_dir = dirname(realpath(__file__))
root_dir = args.root_dir or join(curr_dir, "output", lang.value)
relative_output_dir = args.out_dir or codec_output_directories[lang]
codec_output_dir = join(root_dir, relative_output_dir)
protocol_defs_path = args.proto_path or join(curr_dir, "protocol-definitions")
custom_protocol_defs_path = join(protocol_defs_path, "custom")
schema_path = join(curr_dir, "schema", "protocol-schema.json")
custom_codec_schema_path = join(curr_dir, "schema", "custom-codec-schema.json")
protocol_defs = load_services(protocol_defs_path)
custom_protocol_defs = None
if exists(custom_protocol_defs_path):
custom_protocol_defs = load_services(custom_protocol_defs_path)
protocol_versions = sorted(
get_protocol_versions(protocol_defs, custom_protocol_defs),
key=lambda ver: get_version_as_number(ver),
)
protocol_versions_as_numbers = list(map(get_version_as_number, protocol_versions))
protocol_defs = sorted(protocol_defs, key=lambda proto_def: proto_def["id"])
if not validate_services(
protocol_defs, schema_path, args.no_id_check, protocol_versions_as_numbers
):
exit(-1)
if custom_protocol_defs and not validate_custom_protocol_definitions(
custom_protocol_defs, custom_codec_schema_path, protocol_versions_as_numbers
):
exit(-1)
print("Hazelcast Client Binary Protocol version", protocol_versions[-1])
env = create_environment(lang, args.namespace)
if lang != SupportedLanguages.MD:
codec_template = env.get_template("codec-template.%s.j2" % lang.value)
generate_codecs(protocol_defs, custom_protocol_defs, codec_template, codec_output_dir, lang, env)
print("Generated codecs are at '%s'" % abspath(codec_output_dir))
if custom_protocol_defs:
if lang != SupportedLanguages.MD and lang != SupportedLanguages.CPP:
custom_codec_template = env.get_template("custom-codec-template.%s.j2" % lang.value)
relative_custom_codec_output_dir = args.out_dir or custom_codec_output_directories[lang]
custom_codec_output_dir = join(root_dir, relative_custom_codec_output_dir)
generate_custom_codecs(
custom_protocol_defs,
custom_codec_template,
custom_codec_output_dir,
lang,
env,
)
print("Generated custom codecs are at '%s'" % custom_codec_output_dir)
elif lang == SupportedLanguages.MD:
documentation_template = env.get_template("documentation-template.j2")
generate_documentation(
protocol_defs,
custom_protocol_defs,
documentation_template,
codec_output_dir,
)
print("Generated documentation is at '%s'" % abspath(codec_output_dir))
if not args.no_binary and lang != SupportedLanguages.MD:
relative_test_output_dir = args.test_out_dir or test_output_directories.get(lang, None)
relative_binary_output_dir = args.bin_out_dir or binary_output_directories.get(lang, None)
# If both of them are not defined, that means the
# protocol binary compatibility tests are not implemented
# for that language yet.
not_implemented = relative_binary_output_dir is None or relative_test_output_dir is None
try:
if not_implemented:
raise NotImplementedError()
test_output_dir = join(root_dir, relative_test_output_dir)
binary_output_dir = join(root_dir, relative_binary_output_dir)
binary_templates = get_binary_templates(lang)
for version in protocol_versions:
save_test_files(test_output_dir, lang, version, protocol_defs, binary_templates)
save_binary_files(binary_output_dir, protocol_defs_path, version, protocol_defs)
print("Generated binary compatibility files are at '%s'" % binary_output_dir)
print("Generated binary compatibility tests are at '%s'" % test_output_dir)
except TemplateNotFound as e:
print(
"Binary compatibility test cannot be generated because the templates for the selected "
"language cannot be loaded. Verify that the '%s' exists." % e
)
except NotImplementedError:
pass
except Exception as e:
print("Binary compatibility tests cannot be generated. Error:", e)
end = time.time()
print("Generator took: %d secs" % (end - start))