-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcreate.py
More file actions
119 lines (91 loc) · 3.94 KB
/
Copy pathcreate.py
File metadata and controls
119 lines (91 loc) · 3.94 KB
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
import argparse
import shutil
import os
args = argparse.ArgumentParser()
args.add_argument("--name", help="Name of the class/app to add", required=True)
args.add_argument(
"--type",
help=(
"Type of the extension to add: PhysicsObject, Event, HistogramsFiller, "
"app, printer, histogrammer, task4_histogrammer"
),
required=True
)
args = args.parse_args()
def replace_string_in_file(file_path, old_string, new_string):
with open(file_path, 'r') as file:
content = file.read()
new_content = content.replace(old_string, new_string)
with open(file_path, 'w') as file:
file.write(new_content)
def insert_cast(class_name, type):
file_path = "libs/user_extensions/include/UserExtensionsHelpers.hpp"
old_string = "#include \"PhysicsObject.hpp\""
new_string = f"#include \"{class_name}.hpp\"\n"
new_string += "#include \"PhysicsObject.hpp\""
replace_string_in_file(file_path, old_string, new_string)
old_string = "#endif /* UserExtensionsHelpers_hpp */"
new_string = f"inline std::shared_ptr<{class_name}> as{class_name}(const std::shared_ptr<{type}> physicsObject) {{\n"
new_string += " if(!physicsObject) return nullptr;\n"
new_string += f" return std::make_shared<{class_name}>(physicsObject);\n"
new_string += "}\n\n"
new_string += "#endif /* UserExtensionsHelpers_hpp */"
replace_string_in_file(file_path, old_string, new_string)
print(f"Added conversion from {type} to {class_name} in {file_path}")
def create_parent_directories(file_path):
parent_dir = os.path.dirname(file_path)
if not os.path.exists(parent_dir):
os.makedirs(parent_dir)
def remove_path(path):
if os.path.isdir(path):
shutil.rmtree(path)
elif os.path.isfile(path):
os.remove(path)
def copy_template_file(source, destination, name):
create_parent_directories(destination)
shutil.copy(source, destination)
replace_string_in_file(destination, "TemplateName", name)
print(f"Added file: {destination}")
def main():
class_name = args.name
class_type = args.type
print(f"Adding {class_type}: {class_name}")
files_to_copy = {
"PhysicsObject": (
("tea/templates/PhysicsObject.template.cpp", f"libs/user_extensions/src/{class_name}.cpp"),
("tea/templates/PhysicsObject.template.hpp", f"libs/user_extensions/include/{class_name}.hpp"),
),
"HistogramsFiller": (
("tea/templates/HistogramsFiller.template.cpp", f"libs/user_extensions/src/{class_name}.cpp"),
("tea/templates/HistogramsFiller.template.hpp", f"libs/user_extensions/include/{class_name}.hpp"),
),
"Event": (
("tea/templates/Event.template.cpp", f"libs/user_extensions/src/{class_name}.cpp"),
("tea/templates/Event.template.hpp", f"libs/user_extensions/include/{class_name}.hpp"),
),
"app": (
("tea/templates/app.template.cpp", f"apps/{class_name}.cpp"),
("tea/templates/config.template.py", f"configs/{class_name}_config.py"),
),
"printer": (
("tea/templates/printer.template.cpp", f"apps/{class_name}.cpp"),
("tea/templates/printer_config.template.py", f"configs/{class_name}_config.py"),
),
"histogrammer": (
("tea/templates/histogrammer.template.cpp", f"apps/{class_name}.cpp"),
("tea/templates/histogrammer_config.template.py", f"configs/{class_name}_config.py"),
),
"task4_histogrammer": (
("tea/templates/task4_histogrammer.template.cpp", f"apps/{class_name}.cpp"),
("tea/configs/das_exercises/task4_advanced_histograms.py", f"configs/{class_name}.py"),
),
}
classes_with_cast = ("PhysicsObject", "Event")
for i, entry in enumerate(files_to_copy[class_type]):
copy_template_file(entry[0], entry[1], class_name)
if i == 0 and class_type in classes_with_cast:
insert_cast(class_name, class_type)
remove_path("build/CMakeFiles/")
remove_path("build/CMakeCache.txt")
if __name__ == "__main__":
main()