-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor the code generator and update template
- Loading branch information
Showing
12 changed files
with
850 additions
and
653 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,101 @@ | ||
#!/usr/bin/env python3 | ||
|
||
from os import makedirs | ||
from os.path import join | ||
|
||
from binary.util import * | ||
from jinja2.exceptions import TemplateNotFound | ||
|
||
|
||
def get_binary_templates(lang): | ||
env = create_environment_for_binary_generator(lang) | ||
templates = {} | ||
try: | ||
templates['Client'] = env.get_template('client-binary-compatibility-template.j2') | ||
templates['Member'] = env.get_template('member-binary-compatibility-template.j2') | ||
except TemplateNotFound as err: | ||
return err, None | ||
return None, templates | ||
client_template = env.get_template("client-binary-compatibility-template.j2") | ||
member_template = env.get_template("member-binary-compatibility-template.j2") | ||
|
||
return { | ||
"Client": client_template, | ||
"Member": member_template, | ||
} | ||
|
||
|
||
def save_binary_files(binary_output_dir, protocol_defs_path, version, services): | ||
os.makedirs(binary_output_dir, exist_ok=True) | ||
with open(os.path.join(binary_output_dir, version + '.protocol.compatibility.binary'), 'wb') as binary_file: | ||
with open(os.path.join(binary_output_dir, version + '.protocol.compatibility.null.binary'), | ||
'wb') as null_binary_file: | ||
_generate_binary_files(binary_file, null_binary_file, protocol_defs_path, services, version) | ||
makedirs(binary_output_dir, exist_ok=True) | ||
binary_file_path = join(binary_output_dir, version + ".protocol.compatibility.binary") | ||
null_binary_file_path = join(binary_output_dir, version + ".protocol.compatibility.null.binary") | ||
|
||
with open(binary_file_path, "wb") as binary_file: | ||
with open(null_binary_file_path, "wb") as null_binary_file: | ||
_generate_binary_files( | ||
binary_file, null_binary_file, protocol_defs_path, services, version | ||
) | ||
|
||
|
||
def save_test_files(test_output_dir, lang, version, services, templates): | ||
os.makedirs(test_output_dir, exist_ok=True) | ||
makedirs(test_output_dir, exist_ok=True) | ||
class_name = binary_test_names[lang](version) | ||
|
||
for test_type in ['Client', 'Member']: | ||
for test_null_type in ['', 'Null']: | ||
with open(os.path.join(test_output_dir, class_name.format(type=test_type, null=test_null_type)), 'w', newline='\n') as f: | ||
f.write(templates[test_type].render(protocol_version=version, services=services, test_nullable=test_null_type == 'Null')) | ||
for test_type in ["Client", "Member"]: | ||
for test_null_type in ["", "Null"]: | ||
file_path = join( | ||
test_output_dir, class_name.format(type=test_type, null=test_null_type) | ||
) | ||
with open(file_path, "w", newline="\n") as f: | ||
f.write( | ||
templates[test_type].render( | ||
protocol_version=version, | ||
services=services, | ||
test_nullable=test_null_type == "Null", | ||
) | ||
) | ||
|
||
|
||
def _generate_binary_files(binary_file, null_binary_file, protocol_defs_path, services, version): | ||
encoder = Encoder(protocol_defs_path, version) | ||
version_as_number = get_version_as_number(version) | ||
for service in services: | ||
methods = service['methods'] | ||
methods = service["methods"] | ||
for method in methods: | ||
if get_version_as_number(method['since']) > version_as_number: | ||
if get_version_as_number(method["since"]) > version_as_number: | ||
continue | ||
|
||
method['request']['id'] = int(id_fmt % (service['id'], method['id'], 0), 16) | ||
method['response']['id'] = int(id_fmt % (service['id'], method['id'], 1), 16) | ||
events = method.get('events', None) | ||
method["request"]["id"] = int(id_fmt % (service["id"], method["id"], 0), 16) | ||
method["response"]["id"] = int(id_fmt % (service["id"], method["id"], 1), 16) | ||
events = method.get("events", None) | ||
if events is not None: | ||
for i in range(len(events)): | ||
method['events'][i]['id'] = int(id_fmt % (service['id'], method['id'], i + 2), 16) | ||
request = encoder.encode(method['request'], REQUEST_FIX_SIZED_PARAMS_OFFSET, set_partition_id=True) | ||
null_request = encoder.encode(method['request'], REQUEST_FIX_SIZED_PARAMS_OFFSET, | ||
set_partition_id=True, is_null_test=True) | ||
method["events"][i]["id"] = int( | ||
id_fmt % (service["id"], method["id"], i + 2), 16 | ||
) | ||
request = encoder.encode( | ||
method["request"], REQUEST_FIX_SIZED_PARAMS_OFFSET, set_partition_id=True | ||
) | ||
null_request = encoder.encode( | ||
method["request"], | ||
REQUEST_FIX_SIZED_PARAMS_OFFSET, | ||
set_partition_id=True, | ||
is_null_test=True, | ||
) | ||
request.write(binary_file) | ||
null_request.write(null_binary_file) | ||
response = encoder.encode(method['response'], RESPONSE_FIX_SIZED_PARAMS_OFFSET) | ||
null_response = encoder.encode(method['response'], RESPONSE_FIX_SIZED_PARAMS_OFFSET, is_null_test=True) | ||
response = encoder.encode(method["response"], RESPONSE_FIX_SIZED_PARAMS_OFFSET) | ||
null_response = encoder.encode( | ||
method["response"], RESPONSE_FIX_SIZED_PARAMS_OFFSET, is_null_test=True | ||
) | ||
response.write(binary_file) | ||
null_response.write(null_binary_file) | ||
if events is not None: | ||
for e in events: | ||
if get_version_as_number(e['since']) > version_as_number: | ||
if get_version_as_number(e["since"]) > version_as_number: | ||
continue | ||
|
||
event = encoder.encode(e, EVENT_FIX_SIZED_PARAMS_OFFSET, is_event=True, set_partition_id=True) | ||
null_event = encoder.encode(e, EVENT_FIX_SIZED_PARAMS_OFFSET, is_event=True, | ||
set_partition_id=True, is_null_test=True) | ||
event = encoder.encode( | ||
e, EVENT_FIX_SIZED_PARAMS_OFFSET, is_event=True, set_partition_id=True | ||
) | ||
null_event = encoder.encode( | ||
e, | ||
EVENT_FIX_SIZED_PARAMS_OFFSET, | ||
is_event=True, | ||
set_partition_id=True, | ||
is_null_test=True, | ||
) | ||
event.write(binary_file) | ||
null_event.write(null_binary_file) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.