Subversion Repositories HomeAutomation

Rev

Rev 1935 | Rev 1991 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

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