-
Notifications
You must be signed in to change notification settings - Fork 0
/
process_simulink_codegen.py
158 lines (131 loc) · 5.62 KB
/
process_simulink_codegen.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
import zipfile
import os
import sys
import json
from mako.template import Template
from pathlib import Path
import shutil
def copy_directory(src, dest):
"""
Copies the contents of the source directory to the destination directory.
Creates the destination directory if it doesn't exist.
Args:
src (str): Path to the source directory.
dest (str): Path to the destination directory.
"""
# Check if the source directory exists
if not os.path.isdir(src):
raise FileNotFoundError(f"Source directory '{src}' does not exist.")
# Create the destination directory if it doesn't exist
os.makedirs(dest, exist_ok=True)
# Copy the entire directory tree
try:
shutil.copytree(src, dest, dirs_exist_ok=True)
print(f"Directory copied from '{src}' to '{dest}'")
except shutil.Error as e:
print(f"Error during copying: {e}")
def read_zipped_files(json_file):
try:
with open(json_file, 'r') as f:
zip_files = json.load(f)
if isinstance(zip_files, list):
return zip_files
else:
raise ValueError("JSON content is not a list.")
except FileNotFoundError:
print(f"Error: File '{json_file}' not found.")
return []
except json.JSONDecodeError:
print(f"Error: Failed to parse '{json_file}' as JSON.")
return []
except ValueError as e:
print(f"Error: {e}")
return []
def unzip_and_rename(zip_path, output_dir):
if not os.path.isfile(zip_path):
print(f"Error: '{zip_path}' does not exist.")
return None
extraction_path = os.path.join(output_dir, Path(zip_path).stem)
os.makedirs(extraction_path, exist_ok=True) # Ensure the extraction directory exists
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
zip_ref.extractall(extraction_path)
print(f"Contents extracted to '{extraction_path}'.")
return extraction_path
def remove_specific_files(directory, filenames):
for root, dirs, files in os.walk(directory):
for filename in filenames:
if filename in files:
os.remove(os.path.join(root, filename))
print(f"Removed '{filename}' from '{root}'")
def list_directories(base_path):
directories = []
for root, _, _ in os.walk(base_path, topdown=True):
relative_dir = os.path.relpath(root, base_path)
if relative_dir != ".":
directories.append(relative_dir)
return directories
def organize_files(base_path):
src_path = os.path.join(base_path, 'src')
include_path = os.path.join(base_path, 'include')
os.makedirs(src_path, exist_ok=True)
os.makedirs(include_path, exist_ok=True)
for root, _, files in os.walk(base_path):
for file in files:
if file.endswith('.cpp'):
new_location = os.path.join(src_path, file)
os.rename(os.path.join(root, file), new_location)
print(f"Moved '{file}' to 'src/'")
elif file.endswith('.h'):
new_location = os.path.join(include_path, file)
os.rename(os.path.join(root, file), new_location)
print(f"Moved '{file}' to 'include/'")
for root, dirs, files in os.walk(base_path, topdown=False):
for file in files:
if not (file.endswith('.cpp') or file.endswith('.h')):
os.remove(os.path.join(root, file))
print(f"Removed '{file}' from '{root}'")
for dir in dirs:
dir_path = os.path.join(root, dir)
try:
os.rmdir(dir_path)
print(f"Removed empty directory '{dir_path}'")
except OSError:
pass
def format_sources(sources):
return " ".join(sources)
def remove_parent_directory(path):
return os.path.join(*path.split(os.sep)[1:])
def generate_cmakelists(libraries, output_dir):
template = Template(filename='CMakeLists.txt.mako')
rendered = template.render(libraries=libraries)
cmake_file_path = os.path.join(output_dir, 'CMakeLists.txt')
with open(cmake_file_path, 'w') as f:
f.write(rendered)
print(f"CMakeLists.txt generated at '{cmake_file_path}'.")
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: python process_simulink_codegen.py <output_directory>")
sys.exit(1)
output_directory = sys.argv[1]
# Read the zipped files
files = read_zipped_files('zipped_files.json')
libraries = [] # Store library info for CMake
for file in files:
extraction_path = unzip_and_rename(file, output_directory)
if extraction_path:
remove_specific_files(extraction_path, ["ert_main.cpp", "CMakeLists.txt"])
directories = list_directories(extraction_path)
organize_files(extraction_path) # Organize files into src and include folders
# Collect library information
library_name = Path(file).stem # Use the zip file name as library name
sources = []
for dir in directories:
# Assuming that .cpp files are in the src directory
src_dir = os.path.join(extraction_path, 'src')
sources.extend(remove_parent_directory(os.path.join(src_dir, f)) for f in os.listdir(src_dir) if f.endswith('.cpp'))
libraries.append({"name": library_name, "sources": sources})
else:
print("Failed to extract and process the zip file.")
# Generate the CMakeLists.txt with the collected libraries
generate_cmakelists(libraries, output_directory)
copy_directory('cmake/', os.path.join(output_directory, 'cmake'))