Skip to content

Commit

Permalink
add suggestions from PR
Browse files Browse the repository at this point in the history
  • Loading branch information
jpbusch committed Nov 18, 2024
1 parent ede90af commit b0ee71b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 27 deletions.
24 changes: 13 additions & 11 deletions utils/codegen/codegen-rust/asn1ToConversionHeader.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import shutil
import subprocess
import tempfile
from typing import List


def parseCli():
Expand All @@ -54,23 +55,25 @@ def parseCli():

return args

def generate_required_conversion_files(parent_file_path: str, type: str, file_list: list = []) -> list:
def findDependenciesOfConversionHeaders(parent_file_path: str, type: str, file_list: List[str] = []) -> List[str]:
# duplicate list to avoid modifying the original list
new_file_list = file_list.copy()

# load contents of msg file
# load contents of conversion file
with open(parent_file_path, 'r') as file:
lines = file.readlines()
for line in lines:

if line.startswith(f"#include <etsi_its_{type}_conversion/convert"):
msg_type = line.split("/")[1].split(".")[0]
if msg_type not in file_list and os.path.isfile(f"{os.path.dirname(parent_file_path)}/{msg_type}.h"):
file_list.append(msg_type)
generate_required_conversion_files(f"{os.path.dirname(parent_file_path)}/{msg_type}.h", type, file_list)
if msg_type not in new_file_list and os.path.isfile(f"{os.path.dirname(parent_file_path)}/{msg_type}.h"):
new_file_list.append(msg_type)
new_file_list = findDependenciesOfConversionHeaders(f"{os.path.dirname(parent_file_path)}/{msg_type}.h", type, new_file_list)

# make sure there are no duplicates and sort alphabetically
file_list = list(set(file_list))
new_file_list = sorted(list(set(new_file_list)))

return file_list
return new_file_list

def main():

Expand Down Expand Up @@ -127,11 +130,10 @@ def main():
elif args.type == "vam_ts":
msg_type = "VAM"

msg_files = [f"convert{msg_type}"]
generate_required_conversion_files(os.path.join(args.output_dir, f"convert{msg_type}.h"), args.type, msg_files)

header_files = findDependenciesOfConversionHeaders(os.path.join(args.output_dir, f"convert{msg_type}.h"), args.type, [f"convert{msg_type}"])

for f in glob.glob(os.path.join(args.output_dir, "*.h")):
if os.path.basename(f).split(".")[0] not in msg_files:
if os.path.splitext(os.path.basename(f))[0] not in header_files:
os.remove(f)

if __name__ == "__main__":
Expand Down
31 changes: 15 additions & 16 deletions utils/codegen/codegen-rust/asn1ToRosMsg.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,28 +93,29 @@ def asn1Definitions(files: List[str]) -> Dict[str, str]:

return asn1_raw

def generate_required_msgs(parent_file_path: str, file_list: list = []) -> list:
def findDependenciesOfRosMessageType(parent_file_path: str, file_list: List[str] = []) -> List[str]:
# duplicate list to avoid modifying the original list
new_file_list = file_list.copy()

# load contents of msg file
with open(parent_file_path, 'r') as file:
lines = file.readlines()
for line in lines:
# if line doesnt starts with # or empty line, get first word
# if line doesnt start with # or empty line, get first word
if not line.startswith("#") and line.strip() != "":
msg_type = line.split()[0]
# if ends with message type ends with [], remove []
# if message type ends with [], remove []
msg_type = msg_type[:-2] if msg_type.endswith("[]") else msg_type
if msg_type not in file_list and os.path.isfile(f"{os.path.dirname(parent_file_path)}/{msg_type}.msg"):
file_list.append(msg_type)
generate_required_msgs(f"{os.path.dirname(parent_file_path)}/{msg_type}.msg", file_list)
if msg_type not in new_file_list and os.path.isfile(f"{os.path.dirname(parent_file_path)}/{msg_type}.msg"):
new_file_list.append(msg_type)
new_file_list = findDependenciesOfRosMessageType(f"{os.path.dirname(parent_file_path)}/{msg_type}.msg", new_file_list)

# make sure there are no duplicates and sort alphabetically
file_list = list(set(file_list))
new_file_list = sorted(list(set(new_file_list)))


return file_list
return new_file_list

def generate_cmakelists(msg_files: list, file_path: str, type: str) -> None:
def generateCMakeLists(msg_files: list, file_path: str, type: str) -> None:
with open(file_path, "w") as f:
msg_file_lines = "\n".join([f" \"msg/{msg_file}.msg\"" for msg_file in msg_files])

Expand Down Expand Up @@ -203,7 +204,7 @@ def main():
with open(f, "r") as file:
msg = file.read()

type = os.path.basename(f).split('.')[0]
type = os.path.splitext(os.path.basename(f))[0]
raw_def = asn1_raw[type]
comments = "# --- Auto-generated by asn1ToRosMsg.py ----------------------------------------\n\n" +\
"# --- ASN.1 Definition ---------------------------------------------------------\n" +\
Expand All @@ -230,13 +231,11 @@ def main():
elif args.type == "vam_ts":
msg_type = "VAM"

msg_files = [msg_type]
generate_required_msgs(os.path.join(args.output_dir, f"{msg_type}.msg"), msg_files)
msg_files.sort()
generate_cmakelists(msg_files, os.path.join(args.output_dir, "../CMakeLists.txt"), args.type)
msg_files = findDependenciesOfRosMessageType(os.path.join(args.output_dir, f"{msg_type}.msg"), [msg_type])
generateCMakeLists(msg_files, os.path.join(args.output_dir, "../CMakeLists.txt"), args.type)

for f in glob.glob(os.path.join(args.output_dir, "*.msg")):
if os.path.basename(f).split('.')[0] not in msg_files:
if os.path.splitext(os.path.basename(f))[0] not in msg_files:
os.remove(f)

if __name__ == "__main__":
Expand Down

0 comments on commit b0ee71b

Please sign in to comment.