Rev 826 | Rev 828 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 825 | migan | 1 | #!/bin/env python |
| 2 | |||
| 819 | migan | 3 | # Written by Mattias Runge 2008-05-13 |
| 4 | |||
| 5 | import os |
||
| 6 | import sys |
||
| 7 | import getopt |
||
| 8 | |||
| 9 | moddir = '../../module' |
||
| 10 | localmoddir = 'modules' |
||
| 11 | |||
| 12 | def getModules(): |
||
| 13 | modules = [] |
||
| 14 | for fileName in os.listdir(moddir): |
||
| 15 | if fileName[0] != '.': |
||
| 16 | modules.append(fileName) |
||
| 17 | |||
| 18 | return modules |
||
| 19 | |||
| 20 | |||
| 21 | def getLocalModules(): |
||
| 22 | modules = [] |
||
| 23 | for fileName in os.listdir(localmoddir): |
||
| 24 | if fileName[0] != '.': |
||
| 25 | modules.append(fileName) |
||
| 26 | |||
| 27 | return modules |
||
| 28 | |||
| 29 | |||
| 30 | def parseModuleIdFromFile(moduleName, fileName): |
||
| 31 | config_inc = open(fileName, 'r') |
||
| 32 | config_inc_lines = config_inc.readlines() |
||
| 33 | config_inc.close() |
||
| 34 | |||
| 35 | for line in config_inc_lines: |
||
| 36 | line = line.strip("\n").strip(" ") |
||
| 827 | migan | 37 | |
| 819 | migan | 38 | if line.find(moduleName + "_ID") != -1: |
| 39 | parts = line.split("=") |
||
| 40 | return parts[1].strip(" ") |
||
| 41 | |||
| 42 | return -1; |
||
| 43 | |||
| 44 | |||
| 45 | def getFreeModuleId(moduleName): |
||
| 46 | takenIds = [] |
||
| 827 | migan | 47 | |
| 819 | migan | 48 | for applicationName in os.listdir("../"): |
| 827 | migan | 49 | |
| 819 | migan | 50 | if applicationName[0] != '.' and os.path.exists("../" + applicationName + "/config.inc"): |
| 51 | takenId = parseModuleIdFromFile(moduleName, "../" + applicationName + "/config.inc") |
||
| 52 | if takenId != -1 and takenId != "<ID>": |
||
| 53 | takenIds.append(int(takenId, 16)) |
||
| 54 | |||
| 55 | if len(takenIds) == 0: |
||
| 56 | return "0x01" |
||
| 57 | |||
| 58 | takenIds.sort() |
||
| 59 | |||
| 60 | lastId = 0 |
||
| 61 | |||
| 62 | for takenId in takenIds: |
||
| 63 | if takenId != lastId+1: |
||
| 64 | return hex(lastId+1) |
||
| 827 | migan | 65 | lastId = takenId |
| 819 | migan | 66 | |
| 67 | return hex(lastId+1) |
||
| 68 | |||
| 69 | |||
| 70 | def compileMainFile(): |
||
| 71 | print "Compiling new main.c..." |
||
| 72 | main_c_template = open('src/main.c.template', 'r') |
||
| 73 | main_c_template_lines = main_c_template.readlines() |
||
| 74 | main_c_template.close() |
||
| 75 | |||
| 76 | modules = getLocalModules() |
||
| 77 | |||
| 78 | main_c = open('src/main.c', 'w') |
||
| 79 | |||
| 80 | for main_c_template_line in main_c_template_lines: |
||
| 81 | pos_include = main_c_template_line.find('%INCLUDE') |
||
| 82 | pos_init = main_c_template_line.find('%INIT') |
||
| 83 | pos_process = main_c_template_line.find('%PROCESS') |
||
| 84 | pos_list = main_c_template_line.find('%LIST') |
||
| 85 | pos_handlemsg = main_c_template_line.find('%HANDLEMSG') |
||
| 86 | |||
| 87 | if pos_include != -1: |
||
| 88 | for moduleName in modules: |
||
| 822 | migan | 89 | main_c.write(main_c_template_line[:pos_include] + "#include \"../modules/" + moduleName + "/" + moduleName + ".h\"\n") |
| 819 | migan | 90 | elif pos_init != -1: |
| 91 | for moduleName in modules: |
||
| 92 | main_c.write(main_c_template_line[:pos_init] + moduleName + "_Init();\n") |
||
| 93 | elif pos_process != -1: |
||
| 94 | for moduleName in modules: |
||
| 95 | main_c.write(main_c_template_line[:pos_process] + moduleName + "_Process();\n") |
||
| 96 | elif pos_list != -1: |
||
| 97 | count = 1 |
||
| 98 | for moduleName in modules: |
||
| 99 | main_c.write(main_c_template_line[:pos_list] + moduleName + "_List(" + str(count) + ");\n") |
||
| 100 | count += 1 |
||
| 101 | elif pos_handlemsg != -1: |
||
| 102 | for moduleName in modules: |
||
| 103 | main_c.write(main_c_template_line[:pos_handlemsg] + moduleName + "_HandleMessage(&rxMsg);\n") |
||
| 104 | else: |
||
| 105 | main_c.write(main_c_template_line) |
||
| 106 | |||
| 107 | main_c.close() |
||
| 108 | |||
| 109 | print "Creation of main.c complete" |
||
| 110 | |||
| 111 | def readConfigSection(fileName, sectionName): |
||
| 112 | fileInstance = open(fileName, 'r') |
||
| 113 | lines = fileInstance.readlines() |
||
| 114 | fileInstance.close() |
||
| 115 | |||
| 116 | sectionLines = [] |
||
| 117 | |||
| 118 | inSection = False |
||
| 119 | |||
| 120 | for line in lines: |
||
| 121 | if not inSection: |
||
| 122 | if line.find("## Section " + sectionName) != -1: |
||
| 123 | inSection = True |
||
| 124 | sectionLines.append(line.strip("\n")) |
||
| 125 | else: |
||
| 126 | if line.find("## End section " + sectionName) != -1: |
||
| 127 | inSection = False |
||
| 128 | |||
| 129 | sectionLines.append(line.strip("\n")) |
||
| 130 | |||
| 131 | return sectionLines |
||
| 132 | |||
| 133 | |||
| 134 | def updateConfigFile(): |
||
| 135 | print "Updating config.inc..." |
||
| 136 | |||
| 137 | modules = getLocalModules() |
||
| 138 | moduleSections = {} |
||
| 139 | applicationSection = [] |
||
| 140 | |||
| 141 | if not os.path.exists("config.inc"): |
||
| 142 | applicationSection = readConfigSection("src/config.inc.template", "application") |
||
| 143 | |||
| 144 | for moduleName in modules: |
||
| 145 | moduleSections[moduleName] = readConfigSection(localmoddir + "/" + moduleName + "/config.inc.template", moduleName) |
||
| 146 | else: |
||
| 147 | applicationSection = readConfigSection("config.inc", "application") |
||
| 148 | |||
| 149 | for moduleName in modules: |
||
| 150 | moduleSections[moduleName] = readConfigSection("config.inc", moduleName) |
||
| 151 | |||
| 152 | if len(moduleSections[moduleName]) <= 1: |
||
| 153 | moduleSections[moduleName] = readConfigSection(localmoddir + "/" + moduleName + "/config.inc.template", moduleName) |
||
| 154 | |||
| 155 | timers = 0 |
||
| 156 | |||
| 157 | for moduleName, moduleSection in moduleSections.iteritems(): |
||
| 158 | c = 0 |
||
| 159 | for line in moduleSection: |
||
| 160 | if line.find("<ID>") != -1: |
||
| 161 | line = line.replace("<ID>", getFreeModuleId(moduleName)) |
||
| 162 | elif line.find("<TIMER>") != -1: |
||
| 163 | line = line[:line.find("=")+1] + hex(timers) + " /* <TIMER> -- DO NOT REMOVE THIS COMMENT -- */" |
||
| 164 | timers += 1 |
||
| 165 | |||
| 166 | moduleSection[c] = line; |
||
| 167 | c += 1 |
||
| 168 | |||
| 169 | c = 0 |
||
| 170 | for line in applicationSection: |
||
| 171 | if line.find("<TIMER>") != -1: |
||
| 172 | line = line[:line.find("=")+1] + hex(timers) + " /* <TIMER> -- DO NOT REMOVE THIS COMMENT -- */" |
||
| 173 | timers += 1 |
||
| 174 | elif line.find("<NUMBER_OF_TIMERS>") != -1: |
||
| 175 | line = line.replace("<NUMBER_OF_TIMERS>", hex(timers)) |
||
| 176 | elif line.find("<NUMBER_OF_MODULES>") != -1: |
||
| 177 | line = line.replace("<NUMBER_OF_MODULES>", hex(len(modules))) |
||
| 178 | |||
| 179 | applicationSection[c] = line; |
||
| 180 | c += 1 |
||
| 181 | |||
| 182 | config_inc = open('config.inc', 'w') |
||
| 183 | |||
| 184 | for line in applicationSection: |
||
| 185 | config_inc.write(line + "\n") |
||
| 186 | |||
| 187 | for moduleName, moduleSection in moduleSections.iteritems(): |
||
| 188 | for line in moduleSection: |
||
| 189 | config_inc.write(line + "\n") |
||
| 190 | |||
| 191 | config_inc.close() |
||
| 192 | |||
| 193 | print "Update of config.inc complete" |
||
| 194 | |||
| 195 | |||
| 196 | def compileSourcesFile(): |
||
| 197 | print "Compiling sources.inc..." |
||
| 198 | |||
| 199 | uniqueSet = [] |
||
| 200 | |||
| 201 | sources_list_template = open('src/sources.list.template', 'r') |
||
| 202 | sources_list_template_lines = sources_list_template.readlines() |
||
| 203 | sources_list_template.close() |
||
| 204 | |||
| 205 | for sources_list_template_line in sources_list_template_lines: |
||
| 206 | if len(sources_list_template_line) > 0: |
||
| 207 | uniqueSet.append(sources_list_template_line.strip("\n")) |
||
| 208 | |||
| 209 | modules = getLocalModules() |
||
| 210 | |||
| 211 | for moduleName in modules: |
||
| 212 | sources_list = open(localmoddir + "/" + moduleName + "/sources.list", 'r') |
||
| 213 | sources_list_lines = sources_list.readlines() |
||
| 214 | sources_list.close() |
||
| 215 | |||
| 216 | for sources_list_line in sources_list_lines: |
||
| 217 | if len(sources_list_line) > 0: |
||
| 218 | uniqueSet.append(sources_list_line.strip("\n")) |
||
| 219 | |||
| 220 | sourcesString = "SOURCES = " |
||
| 221 | |||
| 222 | #FIXME: Make uniqueSet actually unique |
||
| 223 | |||
| 224 | for line in uniqueSet: |
||
| 225 | sourcesString += line + " " |
||
| 226 | |||
| 227 | sources_inc = open('sources.inc', 'w') |
||
| 228 | sources_inc.write(sourcesString.strip(" ") + "\n") |
||
| 229 | sources_inc.close() |
||
| 230 | |||
| 231 | print "Creation of sources.inc complete" |
||
| 232 | |||
| 233 | |||
| 234 | def regenerateModules(): |
||
| 235 | print "Regenerating modules..." |
||
| 236 | compileSourcesFile() |
||
| 237 | compileMainFile() |
||
| 238 | updateConfigFile() |
||
| 239 | print "Regenerating modules complete" |
||
| 240 | |||
| 241 | |||
| 242 | def addModule(moduleName): |
||
| 243 | print "Trying to add module " + moduleName |
||
| 244 | |||
| 245 | try: |
||
| 820 | migan | 246 | os.symlink("../" + moddir + "/" + moduleName, localmoddir + "/" + moduleName) |
| 247 | print "Added module successfully" |
||
| 819 | migan | 248 | except OSError, (errno, strerror): |
| 249 | if errno == 17: |
||
| 250 | print "A link, file or directory named " + moduleName + " is already present in the modules directory, not linking" |
||
| 251 | else: |
||
| 252 | raise |
||
| 253 | |||
| 254 | print "" |
||
| 255 | |||
| 256 | regenerateModules() |
||
| 257 | |||
| 258 | |||
| 824 | migan | 259 | def delModule(moduleName): |
| 260 | print "Trying to remove module " + moduleName |
||
| 261 | |||
| 262 | try: |
||
| 263 | os.unlink(localmoddir + "/" + moduleName) |
||
| 264 | print "Removed module successfully" |
||
| 265 | except OSError, (errno, strerror): |
||
| 266 | if errno == 2: |
||
| 267 | print "No such " + moduleName + " present in the modules directory, not unlinking" |
||
| 268 | else: |
||
| 269 | raise |
||
| 270 | |||
| 271 | print "" |
||
| 272 | |||
| 273 | regenerateModules() |
||
| 274 | |||
| 275 | |||
| 819 | migan | 276 | def usage(): |
| 277 | print "Options:" |
||
| 278 | print "\t--regenare\t\tRegenarate modules and files" |
||
| 279 | print "\t--list\t\t\tList avalible modules" |
||
| 280 | print "\t--help\t\t\tPrint this help" |
||
| 281 | print "\t--add=<module name>\tAdds a module to the application" |
||
| 282 | print "" |
||
| 283 | |||
| 284 | |||
| 285 | def main(): |
||
| 286 | try: |
||
| 824 | migan | 287 | opts, args = getopt.getopt(sys.argv[1:], "hlrd", ["help", "list", "regenerate", "add=", "del="]) |
| 819 | migan | 288 | except getopt.GetoptError, err: |
| 289 | # print help information and exit: |
||
| 290 | print str(err) # will print something like "option -a not recognized" |
||
| 291 | usage() |
||
| 292 | sys.exit(2) |
||
| 825 | migan | 293 | |
| 294 | if len(sys.argv) == 1: |
||
| 295 | usage() |
||
| 296 | sys.exit() |
||
| 297 | |||
| 819 | migan | 298 | for o, a in opts: |
| 299 | if o in ("-l", "--list"): |
||
| 300 | print getModules() |
||
| 824 | migan | 301 | |
| 819 | migan | 302 | elif o in ("-h", "--help"): |
| 303 | usage() |
||
| 304 | sys.exit() |
||
| 824 | migan | 305 | |
| 819 | migan | 306 | elif o in ("-r", "--regenerate"): |
| 307 | regenerateModules() |
||
| 824 | migan | 308 | |
| 819 | migan | 309 | elif o in ("--add"): |
| 310 | if len(a) == 0: |
||
| 311 | print "No modulename specified" |
||
| 312 | usage() |
||
| 313 | sys.exit() |
||
| 314 | |||
| 315 | if a not in getModules(): |
||
| 316 | print a + " is not a recognized module name" |
||
| 317 | sys.exit() |
||
| 318 | |||
| 319 | addModule(a) |
||
| 824 | migan | 320 | |
| 321 | elif o in ("--del"): |
||
| 322 | if len(a) == 0: |
||
| 323 | print "No modulename specified" |
||
| 324 | usage() |
||
| 325 | sys.exit() |
||
| 326 | |||
| 327 | if a not in getModules(): |
||
| 328 | print a + " is not a recognized module name" |
||
| 329 | sys.exit() |
||
| 330 | |||
| 331 | delModule(a) |
||
| 332 | |||
| 819 | migan | 333 | else: |
| 334 | assert False, "unhandled option" |
||
| 335 | |||
| 336 | |||
| 337 | if __name__ == "__main__": |
||
| 338 | main() |
||
| 339 |