-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathadapt_files.py
322 lines (239 loc) · 9.37 KB
/
adapt_files.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import os
import re
import shutil
import subprocess
import sys
FILE = "CMakeLists.txt"
FILE2 = "examples/CMakeLists.txt"
comment_files = [
"helper/cooperativePerception-helper.cc",
"model/Facilities/vdpopencda.cc",
"model/Applications/cooperativePerception.cc",
"model/utilities/opencda-sensor.cc",
"helper/cooperativePerception-helper.h",
"model/Applications/cooperativePerception.h",
"model/Facilities/vdpopencda.h",
"model/utilities/opencda-sensor.h",
]
uncomment_files = [
"helper/v2xEmulator-helper.cc",
"helper/simpleCAMSender-helper.cc",
"model/Applications/simpleCAMSender-gps-tc.cc",
"model/Applications/v2xEmulator.cc",
"helper/v2xEmulator-helper.h",
"helper/simpleCAMSender-helper.h",
"model/Applications/simpleCAMSender-gps-tc.h",
"model/BTP/simple-btp-application.h",
]
header_content = """find_package(protobuf REQUIRED)
include_directories(${Protobuf_INCLUDE_DIRS})
link_libraries(${Protobuf_LIBRARIES})
find_package(gRPC REQUIRED)
include_directories(${gRPC_INCLUDE_DIRS})
link_libraries(${gRPC_LIBRARIES})
link_libraries (protobuf::libprotobuf
gRPC::grpc
gRPC::grpc++)"""
def comment_out(file, pattern):
with open(file, 'r') as f:
lines = f.readlines()
with open(file, 'w') as f:
for line in lines:
if re.match(rf'^[^#]*{pattern}.*', line):
f.write('# ' + line)
else:
f.write(line)
def uncomment(file, pattern):
with open(file, 'r') as f:
lines = f.readlines()
with open(file, 'w') as f:
for line in lines:
f.write(re.sub(rf'^# (.*{pattern}.*)', r'\1', line))
def comment_sections(file_path, section_names):
# Read the content of the CMakeLists.txt file
with open(file_path, 'r') as file:
content = file.read()
# Iterate over each section name to comment out
for section in section_names:
# Regular expression to find the block to comment out
pattern = re.compile(rf'(build_lib_example\(\s*NAME\s*{re.escape(section)}\s*.*?LIBRARIES_TO_LINK\s*.*?\))', re.DOTALL)
# Add comments to the matched block
content = pattern.sub(lambda match: "\n".join([f"# {line}" for line in match.group(0).splitlines()]), content)
# Write the modified content back to the file
with open(file_path, 'w') as file:
file.write(content)
def comment_section_examples(file_path):
inside_block = False
block_lines = []
libcarla_found = False
with open(file_path, 'r') as file:
lines = file.readlines()
new_lines = []
for line in lines:
# Detect the start of the build_lib_example block
if 'build_lib_example(' in line:
inside_block = True
block_lines = [line] # Start capturing the block
libcarla_found = False # Reset libcarla_found flag
elif inside_block:
block_lines.append(line)
# Detect if ${libcarla} is in the block
if '${libcarla}' in line:
libcarla_found = True
# Detect the end of the build_lib_example block
if ')' in line:
inside_block = False
# If ${libcarla} was found, comment out the whole block
if libcarla_found:
block_lines = [f"# {l}" if not l.startswith("#") else l for l in block_lines]
# Add the block to the final lines
new_lines.extend(block_lines)
else:
new_lines.append(line)
# Write the modified content back to the file
with open(file_path, 'w') as file:
file.writelines(new_lines)
def uncomment_sections(file_path, section_names):
# Read the content of the CMakeLists.txt file
with open(file_path, 'r') as file:
content = file.read()
# Iterate over each section name to uncomment
for section in section_names:
# Regular expression to find the block to uncomment
pattern = re.compile(rf'(build_lib_example\(\s*NAME\s*{re.escape(section)}\s*.*?LIBRARIES_TO_LINK\s*.*?\))', re.DOTALL)
# Remove comments from the matched block
content = pattern.sub(lambda match: "\n".join([line.lstrip('# ') if line.lstrip().startswith('#') else line for line in match.group(0).splitlines()]), content)
# Write the modified content back to the file
with open(file_path, 'w') as file:
file.write(content)
def uncomment_section_examples(file_path):
inside_block = False
block_lines = []
libcarla_found = False
with open(file_path, 'r') as file:
lines = file.readlines()
new_lines = []
for line in lines:
# Detect the start of the build_lib_example block
if 'build_lib_example(' in line:
inside_block = True
block_lines = [line] # Start capturing the block
libcarla_found = False # Reset libcarla_found flag
elif inside_block:
block_lines.append(line)
# Detect if ${libcarla} is in the block
if '${libcarla}' in line:
libcarla_found = True
# Detect the end of the build_lib_example block
if ')' in line:
inside_block = False
# If ${libcarla} was found, uncomment the whole block
if libcarla_found:
block_lines = [l.lstrip('# ') for l in block_lines]
# Add the block (either uncommented or left as is) to the final lines
new_lines.extend(block_lines)
else:
# If we're not inside a block, add the line as-is
new_lines.append(line)
# Write the modified content back to the file
with open(file_path, 'w') as file:
file.writelines(new_lines)
def prepend_header_if_absent(file, header_content):
with open(file, 'r') as f:
content = f.read()
if "find_package(protobuf REQUIRED)" not in content:
with open(file, 'w') as f:
f.write(header_content + "\n" + content)
def delete_header_if_present(file, header_content):
with open(file, 'r') as f:
content = f.read()
if header_content in content:
with open(file, 'w') as f:
f.write(content.replace(header_content, ""))
def append_after_pattern(file, pattern, content):
with open(file, 'r') as f:
lines = f.readlines()
with open(file, 'w') as f:
for line in lines:
if line == " " + content + "\n":
continue
f.write(line)
if re.search(pattern, line):
f.write(" " + content + "\n")
def remove_after_pattern(file, pattern):
with open(file, 'r') as f:
lines = f.readlines()
# remove the line with the pattern
with open(file, 'w') as f:
for line in lines:
if line == " " + pattern + "\n":
continue
f.write(line)
def copytree_compat(src, dst):
if os.path.exists(dst):
shutil.rmtree(dst)
shutil.copytree(src, dst)
def switch_to_carla():
ns_3_dir = os.getcwd()
# Switch CMakeLists.txt and PRRsup
os.chdir('src/automotive/')
copytree_compat('model/Measurements/', 'aux-files/Measurements-base/')
copytree_compat('aux-files/Measurements-CARLA/', 'model/Measurements/')
for pattern in uncomment_files:
comment_out(FILE, pattern)
for pattern in comment_files:
uncomment(FILE, pattern)
sections_to_comment = [
'v2x-emulator',
'v2v-80211p-gps-tc-example',
'v2v-simple-cam-exchange-80211p'
]
comment_sections(FILE2, sections_to_comment)
uncomment_section_examples(FILE2)
# prepend_header_if_absent(FILE, header_content)
append_after_pattern(FILE, r'\${libtraci}', r'${libcarla}')
if os.path.exists("aux-files/current-mode.txt"):
os.remove("aux-files/current-mode.txt")
with open("aux-files/current-mode.txt", 'w') as f:
f.write("CARLA\n")
os.chdir(ns_3_dir)
os.chdir("src/carla/proto/")
subprocess.run(["./buildProto.sh"])
os.chdir(ns_3_dir)
print("Successfully switched to ms-van3t-CARLA!")
def switch_to_base():
ns_3_dir = os.getcwd()
# Switch CMakeLists.txt and PRRsup
os.chdir('src/automotive/')
copytree_compat('model/Measurements/', 'aux-files/Measurements-CARLA/')
copytree_compat('aux-files/Measurements-base/', 'model/Measurements/')
for pattern in comment_files:
comment_out(FILE, pattern)
for pattern in uncomment_files:
uncomment(FILE, pattern)
sections_to_comment = [
'v2x-emulator',
'v2v-80211p-gps-tc-example',
'v2v-simple-cam-exchange-80211p'
]
uncomment_sections(FILE2, sections_to_comment)
comment_section_examples(FILE2)
# delete_header_if_present(FILE, header_content)
remove_after_pattern(FILE, '${libcarla}')
if os.path.exists("aux-files/current-mode.txt"):
os.remove("aux-files/current-mode.txt")
with open("aux-files/current-mode.txt", 'w') as f:
f.write("BASE\n")
os.chdir(ns_3_dir)
os.chdir("src/carla/proto/")
subprocess.run(["./buildProto.sh"])
os.chdir(ns_3_dir)
print("Successfully switched to base ms-van3t!")
def main():
# if the first argument is 'CARLA', switch to CARLA mode
if len(sys.argv) > 1 and sys.argv[1] == 'CARLA':
switch_to_carla()
elif len(sys.argv) > 1 and sys.argv[1] == 'BASE':
switch_to_base()
if __name__ == "__main__":
main()