Subversion Repositories HomeAutomation

Rev

Details | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
819 migan 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:
822 migan 85
                main_c.write(main_c_template_line[:pos_include] + "#include \"../modules/" + moduleName + "/" + moduleName + ".h\"\n")
819 migan 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:
820 migan 242
        os.symlink("../" + moddir + "/" + moduleName, localmoddir + "/" + moduleName)
243
        print "Added module successfully"
819 migan 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 usage():
256
    print "Options:"
257
    print "\t--regenare\t\tRegenarate modules and files"
258
    print "\t--list\t\t\tList avalible modules"
259
    print "\t--help\t\t\tPrint this help"
260
    print "\t--add=<module name>\tAdds a module to the application"
261
    print ""
262
 
263
 
264
def main():
265
    try:
266
        opts, args = getopt.getopt(sys.argv[1:], "hlr", ["help", "list", "regenerate", "add="])
267
    except getopt.GetoptError, err:
268
        # print help information and exit:
269
        print str(err) # will print something like "option -a not recognized"
270
        usage()
271
        sys.exit(2)
272
 
273
    for o, a in opts:
274
        if o in ("-l", "--list"):
275
            print getModules()
276
        elif o in ("-h", "--help"):
277
            usage()
278
            sys.exit()
279
        elif o in ("-r", "--regenerate"):
280
            regenerateModules()
281
        elif o in ("--add"):
282
            if len(a) == 0:
283
                print "No modulename specified"
284
                usage()
285
                sys.exit()
286
 
287
            if a not in getModules():
288
                print a + " is not a recognized module name"
289
                sys.exit()
290
 
291
            addModule(a)
292
        else:
293
            assert False, "unhandled option"
294
 
295
 
296
if __name__ == "__main__":
297
    main()
298