Subversion Repositories HomeAutomation

Rev

Rev 822 | Go to most recent revision | Blame | Last modification | View Log | SVN | RSS feed

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