python: Document CHDR Test Generation

The script_test.py script is used to generate data .cpp files from a
wireshark trace for the C++ CHDR Parser tests. This commit expands the
script to also generate the data .py files for the Python CHDR Parser
tests.

Signed-off-by: Samuel O'Brien <sam.obrien@ni.com>
This commit is contained in:
Samuel O'Brien 2020-07-01 09:00:53 -05:00 committed by Aaron Rossetto
parent 919a147afc
commit 9a75dcf3a3
2 changed files with 45 additions and 20 deletions

View File

@ -21,4 +21,4 @@ It contains many Data packets, as well as some Stream Status and Stream Command
5. Save the trace with a descriptive name in the `host/tests/common/chdr_resource` directory as a `.c` file
6. In the `host/tests/common/chdr_resource` directory, run `./format_trace.py {filename}`, where `{filename}` is the name of the `.c` file you save the trace as.
This will create a `.cpp` file of the same name, which contains the trace data inside a namespace matching the filename. The trace can now be accessed in unit tests with `#include <chdr_resource/{filename}.cpp>`.
This will create a `.cpp` file and a `.py` file of the same name, which contains the trace data inside a namespace matching the filename. The trace can now be accessed in unit tests with `#include <chdr_resource/{filename}.cpp>` in C++ and `from chdr_resource import {filename}` in Python.

View File

@ -1,6 +1,6 @@
#!/usr/bin/env python3
#
# Copyright 2017 Ettus Research, a National Instruments Company
# Copyright 2020 Ettus Research, a National Instruments Brand
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
@ -10,20 +10,21 @@ import re
if len(sys.argv) < 2:
print("Please supply an input filename!")
exit()
sys.exit()
input_arg = sys.argv[1]
filename_pat = re.compile(r"(.*)\.c")
m = filename_pat.match(input_arg)
if not m:
print("Please supply a .c file as input!")
exit()
sys.exit()
filename = m.group(1)
input_file = open(filename + ".c", "r")
output_file = open(filename + ".cpp", "w")
cpp_file = open(filename + ".cpp", "w")
python_file = open(filename + ".py", "w")
output_header = """//
OUTPUT_HEADER_CPP = """//
// Copyright 2020 Ettus Research, a National Instruments Brand
//
// SPDX-License-Identifier: GPL-3.0-or-later
@ -33,6 +34,14 @@ output_header = """//
"""
OUTPUT_HEADER_PYTHON = """#
# Copyright 2020 Ettus Research, a National Instruments Company
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
"""
# Matches a line defining a new packet, e.g
# char peer1_19[] = { /* Packet 3153 */
# Group 1 is the array name, e.g. "peer1_19"
@ -40,7 +49,7 @@ output_header = """//
define_pat = re.compile(r"^char (peer[0|1]_\d+)\[\] = \{ \/\* (Packet \d+) \*\/$")
# Matches a line in the middle of a packet or at the end, e.g.
# 0x01, 0x00, 0x18, 0x00, 0x02, 0x00, 0x80, 0x00,
# 0x01, 0x00, 0x18, 0x00, 0x02, 0x00, 0x80, 0x00,
# 0x08, 0x00, 0x0f, 0x00 };
# No groups, use full match
other_pat = re.compile(r"^((?:0x[0-9a-f]{2}, )*(?:0x[0-9a-f]{2}))(?: \};|, )$")
@ -48,8 +57,10 @@ other_pat = re.compile(r"^((?:0x[0-9a-f]{2}, )*(?:0x[0-9a-f]{2}))(?: \};|, )$")
# Used to seperate the two parties in the conversation (peer0 and peer1)
var_sort_pat = re.compile(r"^peer0_\d+$")
output_file.write(output_header)
output_file.write("namespace {} {{\n\n".format(filename))
cpp_file.write(OUTPUT_HEADER_CPP)
cpp_file.write("namespace {} {{\n\n".format(filename))
python_file.write(OUTPUT_HEADER_PYTHON)
var_names_peer_0 = []
var_names_peer_1 = []
@ -60,7 +71,8 @@ while True:
break
m = define_pat.match(line)
if m:
output_file.write("uint8_t {}[] = {{ // {}\n".format(m.group(1), m.group(2)))
cpp_file.write("uint8_t {}[] = {{ // {}\n".format(m.group(1), m.group(2)))
python_file.write("{} = bytes([ # {}\n".format(m.group(1), m.group(2)))
var_sort_match = var_sort_pat.match(m.group(1))
if var_sort_match:
var_names_peer_0.append(m.group(1))
@ -69,18 +81,31 @@ while True:
continue
m = other_pat.match(line)
if m:
output_file.write(line)
cpp_file.write(line)
python_file.write(line.replace("};", "])"))
continue
print("Encountered unexpected line:\n{}".format(line))
exit()
sys.exit()
for peer_name, var_names in [("peer0", var_names_peer_0), ("peer1", var_names_peer_1)]:
output_file.write("\n")
output_file.write("size_t {}_len = {};\n".format(peer_name, len(var_names)))
output_file.write("std::tuple<uint8_t*, size_t> {}[] = {{\n".format(peer_name))
for var_name in var_names:
output_file.write("\tstd::make_tuple({0}, sizeof({0})),\n".format(var_name))
output_file.write("};\n")
cpp_file.write("\n")
cpp_file.write("size_t {}_len = {};\n".format(peer_name, len(var_names)))
cpp_file.write("std::tuple<uint8_t*, size_t> {}[] = {{\n".format(peer_name))
output_file.write("\n")
output_file.write("}} // namespace {}\n".format(filename))
python_file.write("\n")
python_file.write("{} = [\n\t".format(peer_name))
for var_name in var_names:
cpp_file.write("\tstd::make_tuple({0}, sizeof({0})),\n".format(var_name))
cpp_file.write("};\n")
for i, var_name in enumerate(var_names):
python_file.write(var_name)
if i + 1 < len(var_names):
python_file.write(",\t")
if (i + 1) % 10 == 0:
python_file.write("\n\t")
python_file.write("\n]\n")
cpp_file.write("\n")
cpp_file.write("}} // namespace {}\n".format(filename))