Subversion Repositories HomeAutomation

Rev

Rev 1994 | Rev 2006 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
979 linlun 1
#!/usr/bin/python
825 migan 2
 
819 migan 3
# Written by Mattias Runge 2008-05-13
4
 
5
import os
6
import sys
7
import getopt
8
 
852 migan 9
moddir = sys.path[0] + "/../../EmbeddedSoftware/AVR/module"
10
localmoddir = os.getcwd() + "/modules"
819 migan 11
 
12
def getModules():
13
	modules = []
14
	for fileName in os.listdir(moddir):
887 migan 15
		if fileName[0] != '.' and fileName != "template":
819 migan 16
			modules.append(fileName)
17
 
1994 linlun 18
	return sorted(modules)
819 migan 19
 
20
 
21
def getLocalModules():
22
	modules = []
23
	for fileName in os.listdir(localmoddir):
24
		if fileName[0] != '.':
25
			modules.append(fileName)
26
 
1994 linlun 27
	return sorted(modules)
819 migan 28
 
29
 
30
def parseModuleIdFromFile(moduleName, fileName):
31
	config_inc = open(fileName, 'r') 
32
	config_inc_lines = config_inc.readlines()
33
	config_inc.close()
1572 linlun 34
	takenIds = []
819 migan 35
	for line in config_inc_lines:
36
		line = line.strip("\n").strip(" ")
827 migan 37
 
819 migan 38
		if line.find(moduleName + "_ID") != -1:
39
			parts = line.split("=")
1572 linlun 40
			if parts[1].strip(" ") != -1 and parts[1].strip(" ") != "<ID>":
41
				takenIds.append(int(parts[1].strip(" "), 16))
2003 linlun 42
				print "Found id: "+parts[1].strip(" ")+" in file: "+fileName+" for module: "+moduleName
1572 linlun 43
	return takenIds
819 migan 44
 
45
 
1572 linlun 46
def getModuleIdsFromFile(moduleName):
819 migan 47
	takenIds = []
1572 linlun 48
	for applicationName in os.listdir("../"):	
819 migan 49
		if applicationName[0] != '.' and os.path.exists("../" + applicationName + "/config.inc"):
1572 linlun 50
			takenIds = parseModuleIdFromFile(moduleName, "../" + applicationName + "/config.inc")
1574 linlun 51
 
1572 linlun 52
	return takenIds
53
 
2003 linlun 54
def parseAllModuleIdFromFile(fileName):
55
	modules = getModules()
56
	config_inc = open(fileName, 'r') 
57
	config_inc_lines = config_inc.readlines()
58
	config_inc.close()
59
	for line in config_inc_lines:
60
		line = line.strip("\n").strip(" ")
61
 
62
		if line.find("_ID") != -1:
63
			parts = line.split("=")
64
			if (parts[0][:-3] in modules):
65
				if parts[1].strip(" ") != -1 and parts[1].strip(" ") != "<ID>":
66
					print "\tModule: "+parts[0][:-3]+"\t"+parts[1].strip(" ")
67
 
68
def getAllModuleIdsFromFile():
69
	for applicationName in os.listdir("../"):	
70
		if applicationName[0] != '.' and os.path.exists("../" + applicationName + "/config.inc"):
71
			print "Found node: "+applicationName
72
			parseAllModuleIdFromFile("../" + applicationName + "/config.inc")
819 migan 73
 
1572 linlun 74
def getFreeModuleId(takenIds):
819 migan 75
	if len(takenIds) == 0:
1572 linlun 76
		return 1
819 migan 77
	takenIds.sort()
78
 
79
	lastId = 0
80
 
81
	for takenId in takenIds:
82
		if takenId != lastId+1:
1572 linlun 83
			return lastId+1
827 migan 84
		lastId = takenId
819 migan 85
 
1572 linlun 86
	return lastId+1
819 migan 87
 
1572 linlun 88
 
89
 
819 migan 90
def compileMainFile():
91
	print "Compiling new main.c..."
92
	main_c_template = open('src/main.c.template', 'r') 
93
	main_c_template_lines = main_c_template.readlines()
94
	main_c_template.close()
95
 
96
	modules = getLocalModules()
97
 
98
	main_c = open('src/main.c', 'w') 
99
 
100
	for main_c_template_line in main_c_template_lines:
101
		pos_include = main_c_template_line.find('%INCLUDE')
102
		pos_init = main_c_template_line.find('%INIT')
103
		pos_process = main_c_template_line.find('%PROCESS')
104
		pos_list = main_c_template_line.find('%LIST')
105
		pos_handlemsg = main_c_template_line.find('%HANDLEMSG')
106
 
107
		if pos_include != -1:
108
			for moduleName in modules:
822 migan 109
				main_c.write(main_c_template_line[:pos_include] + "#include \"../modules/" + moduleName + "/" + moduleName + ".h\"\n")
819 migan 110
		elif pos_init != -1:
111
			for moduleName in modules:
839 migan 112
				main_c.write(main_c_template_line[:pos_init] + moduleName + "_Init();\n")
819 migan 113
		elif pos_process != -1:
114
			for moduleName in modules:
839 migan 115
				main_c.write(main_c_template_line[:pos_process] + moduleName + "_Process();\n")
819 migan 116
		elif pos_list != -1:
117
			count = 1
118
			for moduleName in modules:
839 migan 119
				main_c.write(main_c_template_line[:pos_list] + moduleName + "_List(" + str(count) + ");\n")
819 migan 120
				count += 1
121
		elif pos_handlemsg != -1:
122
			for moduleName in modules:
839 migan 123
				main_c.write(main_c_template_line[:pos_handlemsg] + moduleName + "_HandleMessage(&rxMsg);\n")
819 migan 124
		else:
125
			main_c.write(main_c_template_line)
126
 
127
	main_c.close()
128
 
129
	print "Creation of main.c complete"
130
 
131
def readConfigSection(fileName, sectionName):
132
	fileInstance = open(fileName, 'r')
133
	lines = fileInstance.readlines()
134
	fileInstance.close()
135
 
136
	sectionLines = []
137
 
138
	inSection = False
139
 
140
	for line in lines:
141
		if not inSection:
1727 cougar 142
			if line.find("## Section " + sectionName + " --") != -1:
819 migan 143
				inSection = True
144
				sectionLines.append(line.strip("\n"))
145
		else:
1727 cougar 146
			if line.find("## End section " + sectionName + " --") != -1:
819 migan 147
				inSection = False
148
 
149
			sectionLines.append(line.strip("\n"))
150
 
151
	return sectionLines
152
 
153
 
154
def updateConfigFile():
155
	print "Updating config.inc..."
156
	modules = getLocalModules()
157
	moduleSections = {}
158
	applicationSection = []
1574 linlun 159
	moduleName="none"
819 migan 160
	if not os.path.exists("config.inc"):
161
		applicationSection = readConfigSection("src/config.inc.template", "application")
162
 
163
		for moduleName in modules:
164
			moduleSections[moduleName] = readConfigSection(localmoddir + "/" + moduleName + "/config.inc.template", moduleName)
165
	else:
166
		applicationSection = readConfigSection("config.inc", "application")
167
 
168
		for moduleName in modules:
169
			moduleSections[moduleName] = readConfigSection("config.inc", moduleName)
170
 
171
			if len(moduleSections[moduleName]) <= 1:
172
				moduleSections[moduleName] = readConfigSection(localmoddir + "/" + moduleName + "/config.inc.template", moduleName)
173
 
174
	timers = 0
1125 linlun 175
	pcints = 0
1572 linlun 176
 
177
	moduleIds = getModuleIdsFromFile(moduleName)
178
 
819 migan 179
	for moduleName, moduleSection in moduleSections.iteritems():
180
		c = 0
181
		for line in moduleSection:
1259 runge 182
			if len(line) > 0:
183
				if line[0] != '#':
184
					if line.find("<ID>") != -1:
1572 linlun 185
						new_ID = getFreeModuleId(moduleIds)
186
						print "id = "+hex(new_ID)
187
						moduleIds.append(new_ID)
188
						line = line.replace("<ID>", hex(new_ID))
1259 runge 189
					elif line.find("<TIMER>") != -1:
190
						line = line[:line.find("=")+1] + hex(timers) + " /* <TIMER> -- DO NOT REMOVE THIS COMMENT -- */"
191
						timers += 1
192
					elif line.find("<PCINT>") != -1:
193
						line = line[:line.find("=")+1] + hex(pcints) + " /* <PCINT> -- DO NOT REMOVE THIS COMMENT -- */"
194
						pcints += 1
195
 
196
			moduleSection[c] = line;
197
			c += 1
198
 
199
	c = 0
200
	for line in applicationSection:
201
		if len(line) > 0:
1258 runge 202
			if line[0] != '#':
1259 runge 203
				if line.find("<TIMER>") != -1:
1258 runge 204
					line = line[:line.find("=")+1] + hex(timers) + " /* <TIMER> -- DO NOT REMOVE THIS COMMENT -- */"
205
					timers += 1
1259 runge 206
				elif line.find("TIMER_NUM_TIMERS=") != -1:
207
					line = "TIMER_NUM_TIMERS=" + hex(timers)
208
				elif line.find("NUMBER_OF_MODULES=") != -1:
209
					line = "NUMBER_OF_MODULES=" + hex(len(modules))
1258 runge 210
				elif line.find("<PCINT>") != -1:
211
					line = line[:line.find("=")+1] + hex(pcints) + " /* <PCINT> -- DO NOT REMOVE THIS COMMENT -- */"
212
					pcints += 1
1259 runge 213
				elif line.find("PCINT_NUM_PCINTS=") != -1:
214
					line = "PCINT_NUM_PCINTS=" + hex(pcints)
819 migan 215
		applicationSection[c] = line;
216
		c += 1
217
 
218
	config_inc = open('config.inc', 'w')
219
 
220
	for line in applicationSection:
221
		config_inc.write(line + "\n")
222
 
223
	for moduleName, moduleSection in moduleSections.iteritems():
224
		for line in moduleSection:
225
			config_inc.write(line + "\n")
226
 
227
	config_inc.close()
228
 
229
	print "Update of config.inc complete"
230
 
231
 
232
def compileSourcesFile():
233
	print "Compiling sources.inc..."
234
 
235
	uniqueSet = []
1983 linlun 236
	uniqueSet2 = []
1991 linlun 237
	if os.path.isfile("src/libraries.list.template"):
238
		libraries_list_template = open('src/libraries.list.template', 'r')
239
		libraries_list_template_lines = libraries_list_template.readlines()
240
		libraries_list_template.close()
819 migan 241
 
242
	sources_list_template = open('src/sources.list.template', 'r')
243
	sources_list_template_lines = sources_list_template.readlines()
244
	sources_list_template.close()
1991 linlun 245
	if os.path.isfile("src/libraries.list.template"):
246
		for libraries_list_template_line in libraries_list_template_lines:
247
			line =libraries_list_template_line.strip("\n").strip(" ")
248
			if len(line) > 0:
249
				if line[0] != '#':
250
					uniqueSet2.append(line)
819 migan 251
 
252
	for sources_list_template_line in sources_list_template_lines:
847 migan 253
		line = sources_list_template_line.strip("\n").strip(" ")
254
		if len(line) > 0:
255
			if line[0] != '#':
256
				uniqueSet.append(line)
819 migan 257
 
258
	modules = getLocalModules()
259
 
260
	for moduleName in modules:
261
		sources_list = open(localmoddir + "/" + moduleName + "/sources.list", 'r')
262
		sources_list_lines = sources_list.readlines()
263
		sources_list.close()
264
 
1993 linlun 265
		if os.path.isfile(localmoddir + "/" + moduleName + "/libraries.list"):
266
			libraries_list = open(localmoddir + "/" + moduleName + "/libraries.list", 'r')
267
			libraries_list_lines = libraries_list.readlines()
268
			libraries_list.close()
269
 
270
			for libraries_list_line in libraries_list_lines:
271
				line = libraries_list_line.strip("\n").strip(" ")
272
				if len(line) > 0:
273
					if line[0] != '#':
274
						uniqueSet2.append("../../../module/" + moduleName + line)
819 migan 275
		for sources_list_line in sources_list_lines:
847 migan 276
			line = sources_list_line.strip("\n").strip(" ")
277
			if len(line) > 0:
278
				if line[0] != '#':
279
					uniqueSet.append(line)
280
 
819 migan 281
 
282
	sourcesString = "SOURCES = "
283
 
284
	#FIXME: Make uniqueSet actually unique
285
 
286
	for line in uniqueSet:
287
		sourcesString += line + " "
1983 linlun 288
	sourcesString += "\n"
289
	sourcesString += "LDLIBS = "
1993 linlun 290
	for line in uniqueSet2:
291
		sourcesString += line + " "
819 migan 292
	sources_inc = open('sources.inc', 'w')
293
	sources_inc.write(sourcesString.strip(" ") + "\n")
294
	sources_inc.close()
295
 
296
	print "Creation of sources.inc complete"
297
 
298
def regenerateModules():
299
	print "Regenerating modules..."
836 migan 300
	print ""
819 migan 301
	compileSourcesFile()
836 migan 302
	print ""
819 migan 303
	compileMainFile()
836 migan 304
	print ""
819 migan 305
	updateConfigFile()
836 migan 306
	print ""
819 migan 307
	print "Regenerating modules complete"
836 migan 308
	print ""
819 migan 309
 
2003 linlun 310
def listUsedModules():
311
	print "Listing modules..."
312
	print ""
313
	getAllModuleIdsFromFile()
314
	print ""
315
	print "Listing modules complete"
316
	print ""
819 migan 317
 
318
def addModule(moduleName):
319
	print "Trying to add module " + moduleName
320
 
321
	try:
1935 arune 322
		# the following creates symlinks to <path to modulemanager>/../../EmbeddedSoftware/AVR/module
323
		# and <path to modulemanager> is an absolute path which makes the symlink similar to this:
324
		# act_dimmer230 -> /home/arune/Documents/HomeAutomation/PcSoftware/scripts/../../EmbeddedSoftware/AVR/module/act_dimmer230
325
		#os.symlink(moddir + "/" + moduleName, localmoddir + "/" + moduleName)
326
		# instead symlink to relative path instead
327
		os.symlink("../../../module/" + moduleName, localmoddir + "/" + moduleName)
820 migan 328
		print "Added module successfully"
819 migan 329
	except OSError, (errno, strerror):
330
		if errno == 17:
331
			print "A link, file or directory named " + moduleName + " is already present in the modules directory, not linking"
332
		else:
333
			raise
334
 
335
	print ""
336
 
337
	regenerateModules()
338
 
339
 
824 migan 340
def delModule(moduleName):
341
	print "Trying to remove module " + moduleName
342
 
343
	try:
344
		os.unlink(localmoddir + "/" + moduleName)
345
		print "Removed module successfully"
346
	except OSError, (errno, strerror):
347
		if errno == 2:
348
			print "No such " + moduleName + " present in the modules directory, not unlinking"
349
		else:
350
			raise
351
 
352
	print ""
353
 
354
	regenerateModules()
355
 
852 migan 356
def createFile(moduleName, templateFileName, moduleFileName):
357
	print "Creating " + moduleName + "/" + templateFileName + "..."
358
	template_file = open(moddir + "/template/" + templateFileName, 'r')
359
	lines = template_file.readlines()
360
	template_file.close()
824 migan 361
 
852 migan 362
	module_file = open(moddir + "/" + moduleName + "/" + moduleFileName, 'w')
363
 
364
	for line in lines:
365
		line = line.replace("<template>", moduleName)
366
		line = line.replace("<TEMPLATE>", moduleName.upper())
367
 
368
		module_file.write(line)
369
 
370
	module_file.close()
371
 
372
def newModule(moduleName):
373
	print "Creating " + moddir + "/" + moduleName + " directory..."
374
	os.mkdir(moddir + "/" + moduleName + "/")
375
 
376
	createFile(moduleName, "config.inc.template", "config.inc.template")
377
	createFile(moduleName, "sources.list", "sources.list")
378
	createFile(moduleName, "template.h", moduleName + ".h")
379
	createFile(moduleName, "template.c", moduleName + ".c")
1106 linlun 380
	createFile(moduleName, "template_eeprom.h", moduleName + "_eeprom.h")
852 migan 381
 
819 migan 382
def usage():
836 migan 383
	print "Syntax: ./ModuleManager [options]"
819 migan 384
	print "Options:"
836 migan 385
	print "  --help               Print this help"
386
	print "  --regenerate         Regenarate modules and files"
387
	print "  --list               List avalible and installed modules"
388
	print "  --add=<module name>  Adds a module to the application"
389
	print "  --del=<module name>  Removes a module from the application"
852 migan 390
	print "  --new=<module name>  Creates a new module from the module template"
2003 linlun 391
	print "  --modules            Lists all used modules in your personal folder"
819 migan 392
	print ""
393
 
394
 
395
def main():
396
	try:
2003 linlun 397
		opts, args = getopt.getopt(sys.argv[1:], "hlrcm", ["help", "list", "modules", "regenerate", "add=", "del=", "new="])
819 migan 398
	except getopt.GetoptError, err:
399
		# print help information and exit:
400
		print str(err) # will print something like "option -a not recognized"
401
		usage()
402
		sys.exit(2)
825 migan 403
 
404
	if len(sys.argv) == 1:
405
		usage()
406
		sys.exit()
407
 
819 migan 408
	for o, a in opts:
885 migan 409
		if o in ("-c"):
836 migan 410
			modules = getModules()
824 migan 411
 
885 migan 412
			for module in modules:
413
				print module
414
 
415
		elif o in ("-l", "--list"):
416
			modules = getModules()
836 migan 417
			print "Available modules"
418
			print "================="
419
			for module in modules:
420
				print module
421
 
885 migan 422
 
423
			if os.path.exists(localmoddir):
424
				print ""
836 migan 425
 
885 migan 426
				modules = getLocalModules()
836 migan 427
 
885 migan 428
				print "Modules used in this application"
429
				print "================================"
430
				for module in modules:
431
					print module
432
 
819 migan 433
		elif o in ("-h", "--help"):
434
			usage()
435
			sys.exit()
824 migan 436
 
819 migan 437
		elif o in ("-r", "--regenerate"):
438
			regenerateModules()
824 migan 439
 
2003 linlun 440
		elif o in ("-m", "--modules"):
441
			listUsedModules()
442
 
819 migan 443
		elif o in ("--add"):
444
			if len(a) == 0:
445
				print "No modulename specified"
446
				usage()
447
				sys.exit()
448
 
449
			if a not in getModules():
450
				print a + " is not a recognized module name"
451
				sys.exit()
452
 
453
			addModule(a)
824 migan 454
 
455
		elif o in ("--del"):
456
			if len(a) == 0:
457
				print "No modulename specified"
458
				usage()
459
				sys.exit()
460
 
461
			if a not in getModules():
462
				print a + " is not a recognized module name"
463
				sys.exit()
464
 
465
			delModule(a)
466
 
852 migan 467
		elif o in ("--new"):
468
			if len(a) == 0:
469
				print "No modulename specified"
470
				usage()
471
				sys.exit()
472
 
473
			if a in getModules():
474
				print a + " already exists"
475
				sys.exit()
476
 
477
			newModule(a)
478
 
819 migan 479
		else:
480
			assert False, "unhandled option"
481
 
482
 
483
if __name__ == "__main__":
484
	main()
485