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