Subversion Repositories HomeAutomation

Rev

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