Subversion Repositories HomeAutomation

Rev

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

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