-
Notifications
You must be signed in to change notification settings - Fork 1
/
create_package.py
341 lines (275 loc) · 13 KB
/
create_package.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import sys
from PyQt4.QtGui import *
from PyQt4.uic import *
import os
import datetime
class MyWindow(QMainWindow):
def __init__(self):
super(MyWindow, self).__init__()
loadUi('main.ui', self)
self.show()
#nodes
self.AddNodeButton.clicked.connect(self.addNodeButtonCallback)
self.DeleteNodeButton.clicked.connect(self.deleteNodeButtonCallback)
#libs
self.AddLibraryButton.clicked.connect(self.addLibraryButtonCallback)
self.DeleteLibraryButton.clicked.connect(self.deleteLibraryButtonCallback)
#msgs
self.AddMessageButton.clicked.connect(self.addMessageButtonCallback)
self.DeleteMessageButton.clicked.connect(self.deleteMessageButtonCallback)
#srvs
self.AddServiceButton.clicked.connect(self.addServiceButtonCallback)
self.DeleteServiceButton.clicked.connect(self.deleteServiceButtonCallback)
#depndencies
self.AddDependencyButton.clicked.connect(self.addDependencyButtonCallback)
self.DeleteDependencyButton.clicked.connect(self.deleteDependencyButtonCallback)
self.CreatePackageButton.clicked.connect(self.createPackageCallback)
self.templates_dir = 'templates'
def addNodeButtonCallback(self):
text, ok = QInputDialog.getText(self, 'Node Name','Enter node name: ')
if ok:
text = text.toLower()
self.NodesList.addItem(text)
def deleteNodeButtonCallback(self):
for item in self.NodesList.selectedItems():
self.NodesList.takeItem(self.NodesList.row(item))
def addLibraryButtonCallback(self):
text, ok = QInputDialog.getText(self, 'Library Name','Enter library name:')
if ok:
text = text.toLower()
text.replace(" ", "")
if not self.isValidName(text):
return
self.LibraryList.addItem(text)
def deleteLibraryButtonCallback(self):
for item in self.LibraryList.selectedItems():
self.LibraryList.takeItem(self.LibraryList.row(item))
def addMessageButtonCallback(self):
text, ok = QInputDialog.getText(self, 'Message Name','Enter message name:')
if ok:
text = text.toLower()
text.replace(" ", "")
if not self.isValidName(text):
return
self.MessageList.addItem(text)
def deleteMessageButtonCallback(self):
for item in self.MessageList.selectedItems():
self.MessageList.takeItem(self.MessageList.row(item))
def addServiceButtonCallback(self):
text, ok = QInputDialog.getText(self, 'Service Name','Enter service name:')
if ok:
text = text.toLower()
text.replace(" ", "")
if not self.isValidName(text):
return
self.ServiceList.addItem(text)
def deleteServiceButtonCallback(self):
for item in self.ServiceList.selectedItems():
self.ServiceList.takeItem(self.ServiceList.row(item))
def addDependencyButtonCallback(self):
text, ok = QInputDialog.getText(self, 'Dependency Name','Enter dependency name:')
if ok:
text = text.toLower()
text.replace(" ", "")
if not self.isValidName(text):
return
self.DependencyList.addItem(text)
def deleteDependencyButtonCallback(self):
for item in self.DependencyList.selectedItems():
self.DependencyList.takeItem(self.DependencyList.row(item))
def createPackageCallback(self):
main_dir = str(QFileDialog.getExistingDirectory(self, "Select Directory"))
if not main_dir:
return
package_name = self.PackageNameLineEdit.text()
package_name.replace(" ", "")
package_name = package_name.toLower()
self.author_name = str(self.AuthorNameLineEdit.text())
self.author_email = str(self.AuthorEmailLineEdit.text())
self.pacakge_description = str(self.PackageDescriptionLineEdit.text())
if not self.author_name:
print "Error: author name should not be empty"
return
if not self.author_email:
print "Error: author_email should not be empty"
return
if not self.pacakge_description:
print "Error: pacakge_descriptionshould not be empty"
return
namespace = self.NamespaceLineEdit.text()
namespace.replace(" ", "")
namespace = namespace.toLower()
self.namespace = str(namespace)
if not self.isValidName(package_name):
self.PackageNameLineEdit.clear()
return
package_name = str(package_name)
self.packge_name = package_name
main_dir = main_dir + '/' + package_name
src_dir = main_dir + '/src'
include_dir = main_dir + '/include/' + package_name
msg_dir = main_dir + '/msg'
srv_dir = main_dir + '/srv'
if not os.path.exists(main_dir):
os.makedirs(main_dir)
#create nodes and libs
if self.NodesList.count() > 0 or self.LibraryList.count() > 0:
if not os.path.exists(src_dir):
os.makedirs(src_dir)
if not os.path.exists(include_dir):
os.makedirs(include_dir)
nodes_list = []
for i in range(self.NodesList.count()):
node_name = str(self.NodesList.item(i).text())
nodes_list.append(node_name)
self.createNodeSrcFile(src_dir, node_name)
libs_list = []
for i in range(self.LibraryList.count()):
lib_name = str(self.LibraryList.item(i).text())
libs_list.append(lib_name)
self.createLibrarySrcFile(src_dir, lib_name)
self.createLibraryHeaderFile(include_dir, lib_name)
#create msgs
if self.MessageList.count() > 0:
if not os.path.exists(msg_dir):
os.makedirs(msg_dir)
for i in range(self.MessageList.count()):
msg_name = str(self.MessageList.item(i).text())
self.createMessageFile(msg_dir, msg_name)
#create srvs
if self.ServiceList.count() > 0:
if not os.path.exists(srv_dir):
os.makedirs(srv_dir)
for i in range(self.ServiceList.count()):
srv_name = str(self.ServiceList.item(i).text())
self.createServiceFile(srv_dir, srv_name)
#create package.xml
dependency_string = ""
for i in range(self.DependencyList.count()):
dependency = str(self.DependencyList.item(i).text())
dependency_string = dependency_string + " <depend>"+dependency+"</depend>\n"
self.createPackageXML(main_dir, dependency_string)
#create cmakelists.txt
self.createCmakeLists(main_dir, libs_list, nodes_list)
msg = QMessageBox()
msg.setIcon(QMessageBox.Information)
msg.setText("Success")
msg.setInformativeText("Successfully created package in \n" + main_dir)
msg.setWindowTitle("Message")
msg.setStandardButtons(QMessageBox.Ok)
msg.exec_()
def isValidName(self, string):
if not string.at(0).isLetter():
print "Error: invalid name"
return False
return True
def createNodeSrcFile(self, dir, node_name):
file_name = dir + '/' + node_name + '_node.cpp'
file = open(file_name, 'w')
template_file = open(self.templates_dir + '/NODE_SRC', 'r')
for line in template_file:
now = datetime.datetime.now()
new_line = line.replace('@###Year####', str(now.year))
new_line = new_line.replace('@###PackageAuthor####', self.author_name)
new_line = new_line.replace('@###AuthorEmail####', self.author_email)
tmp1 = node_name.replace("_", " ")
tmp2 = tmp1.replace("-", " ")
tmp3 = tmp2.title()
camel_case_name = tmp3.replace(" ", "")
camel_case_name = camel_case_name + "Node"
new_line = new_line.replace('@###NodeName####', camel_case_name)
file.write(new_line)
file.close()
template_file.close()
def createLibrarySrcFile(self, dir, lib_name):
file_name = dir + '/' + lib_name + '.cpp'
file = open(file_name, 'w')
template_file = open(self.templates_dir + '/LIB_SRC', 'r')
for line in template_file:
now = datetime.datetime.now()
new_line = line.replace('@###Year####', str(now.year))
new_line = new_line.replace('@###PackageAuthor####', self.author_name)
new_line = new_line.replace('@###AuthorEmail####', self.author_email)
new_line = new_line.replace('@###Namespace####', self.namespace)
new_line = new_line.replace('@###PackageName####', self.packge_name)
new_line = new_line.replace('@###LibraryHeaderFileName####', lib_name + '.h')
tmp1 = lib_name.replace("_", " ")
tmp2 = tmp1.replace("-", " ")
tmp3 = tmp2.title()
camel_case_name = tmp3.replace(" ", "")
new_line = new_line.replace('@###LibraryName####', camel_case_name)
file.write(new_line)
file.close()
template_file.close()
def createLibraryHeaderFile(self, dir, lib_name):
file_name = dir + '/' + lib_name + '.h'
file = open(file_name, 'w')
template_file = open(self.templates_dir + '/LIB_HEADER', 'r')
for line in template_file:
now = datetime.datetime.now()
new_line = line.replace('@###Year####', str(now.year))
new_line = new_line.replace('@###PackageAuthor####', self.author_name)
new_line = new_line.replace('@###AuthorEmail####', self.author_email)
new_line = new_line.replace('@###Namespace####', self.namespace)
include_guard = self.packge_name + '_include_'+ self.packge_name + '_' + lib_name + '_h'
include_guard = include_guard.upper()
new_line = new_line.replace('@###IncludeGuard####', include_guard)
tmp1 = lib_name.replace("_", " ")
tmp2 = tmp1.replace("-", " ")
tmp3 = tmp2.title()
camel_case_name = tmp3.replace(" ", "")
new_line = new_line.replace('@###LibraryName####', camel_case_name)
file.write(new_line)
file.close()
template_file.close()
def createMessageFile(self, dir, msg_name):
file_name = dir + '/' + msg_name + '.msg'
file = open(file_name, 'w')
file.close()
def createServiceFile(self, dir, srv_name):
file_name = dir + '/' + srv_name + '.srv'
file = open(file_name, 'w')
file.close()
def createPackageXML(self, dir, dependency_string):
file_name = dir + '/' + 'package.xml'
file = open(file_name, 'w')
template_file = open(self.templates_dir + '/PACKAGE_XML', 'r')
for line in template_file:
new_line = line.replace('@###PackageName####', self.packge_name)
new_line = new_line.replace('@###PackageAuthor####', self.author_name)
new_line = new_line.replace('@###AuthorEmail####', self.author_email)
new_line = new_line.replace('@###PackageDescription####', self.pacakge_description)
new_line = new_line.replace('@###DependenciesHere####', dependency_string)
file.write(new_line)
file.close()
template_file.close()
def createCmakeLists(self, dir, libs_list, nodes_list):
add_libs_string = "\n"
for lib in libs_list:
add_libs_string = add_libs_string + "cs_add_library(" + lib + "_lib " + "src/" + lib + ".cpp)\n"
add_exec_string = "\n"
for node in nodes_list:
add_exec_string = add_exec_string + "cs_add_executable(" + node + "_node " + "src/" + node + "_node.cpp)\n"
linking_string = "\n"
for node in nodes_list:
for lib in libs_list:
linking_string = linking_string + "target_link_libraries(" + node + "_node " + lib + "_lib)\n"
#if there is msg or srv, add depenency on exported target to compile in the correct order
if self.MessageList.count() > 0 or self.ServiceList.count() > 0:
for lib in libs_list:
linking_string = linking_string + "add_dependencies(" + lib + "_lib " + "${PROJECT_NAME}_EXPORTED_TARGETS)\n"
file_name = dir + '/' + 'CMakeLists.txt'
file = open(file_name, 'w')
template_file = open(self.templates_dir + '/CMAKELISTS', 'r')
for line in template_file:
new_line = line.replace('@###PackageName####', self.packge_name)
new_line = new_line.replace('@###AddLibrariesHere####', add_libs_string)
new_line = new_line.replace('@###AddExecutablesHere####', add_exec_string)
new_line = new_line.replace('@###LinkLibrariesHere####', linking_string)
file.write(new_line)
file.close()
template_file.close()
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MyWindow()
sys.exit(app.exec_())