Subversion Repositories HomeAutomation

Rev

Rev 847 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 847 Rev 852
Line 4... Line 4...
4
 
4
 
5
import os
5
import os
6
import sys
6
import sys
7
import getopt
7
import getopt
8
 
8
 
9
moddir = '../../module' # FIXME: This is ugly
9
moddir = sys.path[0] + "/../../EmbeddedSoftware/AVR/module"
10
localmoddir = 'modules' # FIXME: This is ugly
10
localmoddir = os.getcwd() + "/modules"
11
 
11
 
12
def getModules():
12
def getModules():
13
	modules = []
13
	modules = []
14
	for fileName in os.listdir(moddir):
14
	for fileName in os.listdir(moddir):
15
		if fileName[0] != '.':
15
		if fileName[0] != '.':
Line 251... Line 251...
251
 
251
 
252
def addModule(moduleName):
252
def addModule(moduleName):
253
	print "Trying to add module " + moduleName
253
	print "Trying to add module " + moduleName
254
	
254
	
255
	try:
255
	try:
256
		os.symlink("../" + moddir + "/" + moduleName, localmoddir + "/" + moduleName)
256
		os.symlink(moddir + "/" + moduleName, localmoddir + "/" + moduleName)
257
		print "Added module successfully"
257
		print "Added module successfully"
258
	except OSError, (errno, strerror):
258
	except OSError, (errno, strerror):
259
		if errno == 17:
259
		if errno == 17:
260
			print "A link, file or directory named " + moduleName + " is already present in the modules directory, not linking"
260
			print "A link, file or directory named " + moduleName + " is already present in the modules directory, not linking"
261
		else:
261
		else:
Line 280... Line 280...
280
	
280
	
281
	print ""
281
	print ""
282
	
282
	
283
	regenerateModules()
283
	regenerateModules()
284
 
284
 
-
 
285
def createFile(moduleName, templateFileName, moduleFileName):
-
 
286
	print "Creating " + moduleName + "/" + templateFileName + "..."
-
 
287
	template_file = open(moddir + "/template/" + templateFileName, 'r')
-
 
288
	lines = template_file.readlines()
-
 
289
	template_file.close()
-
 
290
 
-
 
291
	module_file = open(moddir + "/" + moduleName + "/" + moduleFileName, 'w')
-
 
292
	
-
 
293
	for line in lines:
-
 
294
		line = line.replace("<template>", moduleName)
-
 
295
		line = line.replace("<TEMPLATE>", moduleName.upper())
-
 
296
 
-
 
297
		module_file.write(line)
-
 
298
 
-
 
299
	module_file.close()
-
 
300
	
-
 
301
def newModule(moduleName):
-
 
302
	print "Creating " + moddir + "/" + moduleName + " directory..."
-
 
303
	os.mkdir(moddir + "/" + moduleName + "/")
-
 
304
 
-
 
305
	createFile(moduleName, "config.inc.template", "config.inc.template")
-
 
306
	createFile(moduleName, "protocol.h", "protocol.h")
-
 
307
	createFile(moduleName, "sources.list", "sources.list")
-
 
308
	createFile(moduleName, "template.h", moduleName + ".h")
-
 
309
	createFile(moduleName, "template.c", moduleName + ".c")
285
 
310
 
286
def usage():
311
def usage():
287
	print "Syntax: ./ModuleManager [options]"
312
	print "Syntax: ./ModuleManager [options]"
288
	print "Options:"
313
	print "Options:"
289
	print "  --help               Print this help"
314
	print "  --help               Print this help"
290
	print "  --regenerate         Regenarate modules and files"
315
	print "  --regenerate         Regenarate modules and files"
291
	print "  --list               List avalible and installed modules"
316
	print "  --list               List avalible and installed modules"
292
	print "  --add=<module name>  Adds a module to the application"
317
	print "  --add=<module name>  Adds a module to the application"
293
	print "  --del=<module name>  Removes a module from the application"
318
	print "  --del=<module name>  Removes a module from the application"
-
 
319
	print "  --new=<module name>  Creates a new module from the module template"
294
	print ""
320
	print ""
295
 
321
 
296
 
322
 
297
def main():
323
def main():
298
	try:
324
	try:
299
		opts, args = getopt.getopt(sys.argv[1:], "hlr", ["help", "list", "regenerate", "add=", "del="])
325
		opts, args = getopt.getopt(sys.argv[1:], "hlr", ["help", "list", "regenerate", "add=", "del=", "new="])
300
	except getopt.GetoptError, err:
326
	except getopt.GetoptError, err:
301
		# print help information and exit:
327
		# print help information and exit:
302
		print str(err) # will print something like "option -a not recognized"
328
		print str(err) # will print something like "option -a not recognized"
303
		usage()
329
		usage()
304
		sys.exit(2)
330
		sys.exit(2)
Line 308... Line 334...
308
		sys.exit()
334
		sys.exit()
309
	
335
	
310
	for o, a in opts:
336
	for o, a in opts:
311
		if o in ("-l", "--list"):
337
		if o in ("-l", "--list"):
312
			modules = getModules()
338
			modules = getModules()
313
			
339
			
314
			print "Available modules"
340
			print "Available modules"
315
			print "================="
341
			print "================="
316
			for module in modules:
342
			for module in modules:
317
				print module
343
				print module
318
				
344
				
319
			print ""
345
			print ""
320
			
346
			
321
			modules = getLocalModules()
347
			modules = getLocalModules()
322
			
348
			
323
			print "Modules used in this application"
349
			print "Modules used in this application"
324
			print "================================"
350
			print "================================"
325
			for module in modules:
351
			for module in modules:
326
				print module
352
				print module
327
			
353
			
328
		elif o in ("-h", "--help"):
354
		elif o in ("-h", "--help"):
329
			usage()
355
			usage()
330
			sys.exit()
356
			sys.exit()
331
			
357
			
332
		elif o in ("-r", "--regenerate"):
358
		elif o in ("-r", "--regenerate"):
333
			regenerateModules()
359
			regenerateModules()
334
			
360
			
335
		elif o in ("--add"):
361
		elif o in ("--add"):
336
			if len(a) == 0:
362
			if len(a) == 0:
-
 
363
				print "No modulename specified"
-
 
364
				usage()
-
 
365
				sys.exit()
-
 
366
			
-
 
367
			if a not in getModules():
-
 
368
				print a + " is not a recognized module name"
-
 
369
				sys.exit()
-
 
370
				
-
 
371
			addModule(a)
-
 
372
			
-
 
373
		elif o in ("--del"):
-
 
374
			if len(a) == 0:
337
				print "No modulename specified"
375
				print "No modulename specified"
338
				usage()
376
				usage()
339
				sys.exit()
377
				sys.exit()
340
			
378
			
341
			if a not in getModules():
379
			if a not in getModules():
342
				print a + " is not a recognized module name"
380
				print a + " is not a recognized module name"
343
				sys.exit()
381
				sys.exit()
344
				
382
				
345
			addModule(a)
383
			delModule(a)
346
			
384
			
347
		elif o in ("--del"):
385
		elif o in ("--new"):
348
			if len(a) == 0:
386
			if len(a) == 0:
349
				print "No modulename specified"
387
				print "No modulename specified"
350
				usage()
388
				usage()
351
				sys.exit()
389
				sys.exit()
352
			
390
			
353
			if a not in getModules():
391
			if a in getModules():
354
				print a + " is not a recognized module name"
392
				print a + " already exists"
355
				sys.exit()
393
				sys.exit()
356
				
394
				
357
			delModule(a)
395
			newModule(a)
358
			
396
			
359
		else:
397
		else:
360
			assert False, "unhandled option"
398
			assert False, "unhandled option"
361
 
399
 
362
 
400