Add stub of preset load method.
[bertos.git] / wizard / bertos_utils.py
1 #!/usr/bin/env python
2 # encoding: utf-8
3 #
4 # This file is part of BeRTOS.
5 #
6 # Bertos is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19 #
20 # As a special exception, you may use this file as part of a free software
21 # library without restriction.  Specifically, if other files instantiate
22 # templates or use macros or inline functions from this file, or you compile
23 # this file and link it with other files to produce an executable, this
24 # file does not by itself cause the resulting executable to be covered by
25 # the GNU General Public License.  This exception does not however
26 # invalidate any other reasons why the executable file might be covered by
27 # the GNU General Public License.
28 #
29 # Copyright 2008 Develer S.r.l. (http://www.develer.com/)
30 #
31 # $Id$
32 #
33 # Author: Lorenzo Berni <duplo@develer.com>
34 #
35
36 import os
37 import fnmatch
38 import glob
39 import re
40 import shutil
41 # Use custom copytree function
42 import copytree
43 import pickle
44
45 import const
46 import plugins
47 import DefineException
48
49 from _wizard_version import WIZARD_VERSION
50
51 from LoadException import VersionException, ToolchainException
52
53 def isBertosDir(directory):
54    return os.path.exists(directory + "/VERSION")
55
56 def bertosVersion(directory):
57    return open(directory + "/VERSION").readline().strip()
58
59 def setEnabledModules(project_info, enabled_modules):
60     modules = project_info.info("MODULES")
61     files = {}
62     for module, information in modules.items():
63         information["enabled"] = module in enabled_modules
64         if information["enabled"]:
65             for dependency in information["depends"]:
66                 if not dependency in modules:
67                     files[dependency] = files.get(dependency, 0) + 1
68     project_info.setInfo("MODULES", modules)
69     project_info.setInfo("FILES", files)
70
71 def enabledModules(project_info):
72     enabled_modules = []
73     for name, module in project_info.info("MODULES").items():
74         if module["enabled"]:
75             enabled_modules.append(name)
76     return enabled_modules
77
78 def presetList(directory):
79     """
80     Return the list of the preset found in the selected BeRTOS Version.
81     """
82     abspath = os.path.join(directory, const.PREDEFINED_BOARDS_DIR)
83     preset_list = dict([
84         (os.path.join(abspath, preset_dir), presetInfo(os.path.join(abspath, preset_dir)))
85         for preset_dir in os.listdir(os.path.join(directory, const.PREDEFINED_BOARDS_DIR))
86     ])
87     return preset_list
88
89 def presetInfo(preset_dir):
90     """
91     Return the preset-relevant info contined into the project_file.
92     """
93     preset_info = pickle.loads(open(os.path.join(preset_dir, "project.bertos"), "r").read())
94     try:
95         description = open(os.path.join(preset_dir, "description"), "r").read()
96     except IOError:
97         # No description file found.
98         description = ""
99     relevant_info = {
100         "CPU_NAME": preset_info.get("CPU_NAME"),
101         "SELECTED_FREQ": preset_info.get("SELECTED_FREQ"),
102         "WIZARD_VERSION": preset_info.get("WIZARD_VERSION", None),
103         "PRESET_NAME": preset_info.get("PROJECT_NAME"),
104         "PRESET_DESCRIPTION": description.decode("utf-8"),
105     }
106     return relevant_info
107
108 def mergeSources(srcdir, new_sources, old_sources):
109     # The current mergeSources function provide only a raw copy of the sources in the
110     # created project.
111     #
112     # TODO: implement the three way merge algorithm
113     #
114     shutil.rmtree(srcdir, True)
115     copytree.copytree(os.path.join(new_sources, "bertos"), srcdir, ignore_list=const.IGNORE_LIST)
116
117 def projectFileGenerator(project_info):
118     directory = project_info.info("PROJECT_PATH")
119     project_data = {}
120     enabled_modules = []
121     for module, information in project_info.info("MODULES").items():
122         if information["enabled"]:
123             enabled_modules.append(module)
124     project_data["ENABLED_MODULES"] = enabled_modules
125     # Use the local BeRTOS version instead of the original one
126     # project_data["SOURCES_PATH"] = project_info.info("SOURCES_PATH")
127     project_data["SOURCES_PATH"] = directory
128     project_data["PROJECT_NAME"] = project_info.info("PROJECT_NAME", os.path.basename(directory))
129     project_data["TOOLCHAIN"] = project_info.info("TOOLCHAIN")
130     project_data["CPU_NAME"] = project_info.info("CPU_NAME")
131     project_data["SELECTED_FREQ"] = project_info.info("SELECTED_FREQ")
132     project_data["OUTPUT"] = project_info.info("OUTPUT")
133     project_data["WIZARD_VERSION"] = WIZARD_VERSION
134     return pickle.dumps(project_data)
135
136 def createBertosProject(project_info, edit=False):
137     directory = project_info.info("PROJECT_PATH")
138     sources_dir = project_info.info("SOURCES_PATH")
139     old_sources_dir = project_info.info("OLD_SOURCES_PATH")
140     if not edit:
141         if os.path.isdir(directory):
142             shutil.rmtree(directory, True)        
143         os.makedirs(directory)
144     # Write the project file
145     f = open(directory + "/project.bertos", "w")
146     f.write(projectFileGenerator(project_info))
147     f.close()
148     # VERSION file
149     version_file = open(os.path.join(const.DATA_DIR, "vtemplates/VERSION"), "r").read()
150     open(directory + "/VERSION", "w").write(versionFileGenerator(project_info, version_file))
151     # Destination source dir
152     srcdir = directory + "/bertos"
153     if not edit:
154         # If not in editing mode it copies all the bertos sources in the /bertos subdirectory of the project
155         shutil.rmtree(srcdir, True)
156         copytree.copytree(sources_dir + "/bertos", srcdir, ignore_list=const.IGNORE_LIST)
157     elif old_sources_dir:
158         # If in editing mode it merges the current bertos sources with the selected ones
159         # TODO: implement the three way merge algotihm
160         #
161         mergeSources(srcdir, sources_dir, old_sources_dir)
162     # Destination makefile
163     makefile = directory + "/Makefile"
164     makefile = open(os.path.join(const.DATA_DIR, "mktemplates/Makefile"), 'r').read()
165     makefile = makefileGenerator(project_info, makefile)
166     open(directory + "/Makefile", "w").write(makefile)
167     # Destination project dir
168     # prjdir = directory + "/" + os.path.basename(directory)
169     prjdir = os.path.join(directory, project_info.info("PROJECT_NAME"))
170     if not edit:
171         shutil.rmtree(prjdir, True)
172         os.mkdir(prjdir)
173     # Destination hw files
174     hwdir = prjdir + "/hw"
175     if not edit:
176         shutil.rmtree(hwdir, True)
177         os.mkdir(hwdir)
178     # Copy all the hw files
179     for module, information in project_info.info("MODULES").items():
180         for hwfile in information["hw"]:
181             string = open(sources_dir + "/" + hwfile, "r").read()
182             hwfile_path = hwdir + "/" + os.path.basename(hwfile)
183             if not edit or not os.path.exists(hwfile_path):
184                 # If not in editing mode it copies all the hw files. If in
185                 # editing mode it copies only the files that don't exist yet
186                 open(hwdir + "/" + os.path.basename(hwfile), "w").write(string)
187     # Destination configurations files
188     cfgdir = prjdir + "/cfg"
189     if not edit:
190         shutil.rmtree(cfgdir, True)
191         os.mkdir(cfgdir)
192     # Set properly the autoenabled parameters
193     for module, information in project_info.info("MODULES").items():
194         if "configuration" in information and information["configuration"] != "":
195             configurations = project_info.info("CONFIGURATIONS")
196             configuration = configurations[information["configuration"]]
197             for start, parameter in configuration["paramlist"]:
198                 if "type" in configuration[parameter]["informations"] and configuration[parameter]["informations"]["type"] == "autoenabled":
199                     configuration[parameter]["value"] = "1" if information["enabled"] else "0"
200             project_info.setInfo("CONFIGURATIONS", configurations)
201     # Copy all the configuration files
202     for configuration, information in project_info.info("CONFIGURATIONS").items():
203         string = open(sources_dir + "/" + configuration, "r").read()
204         for start, parameter in information["paramlist"]:
205             infos = information[parameter]
206             value = infos["value"]
207             if "unsigned" in infos["informations"] and infos["informations"]["unsigned"]:
208                 value += "U"
209             if "long" in infos["informations"] and infos["informations"]["long"]:
210                 value += "L"
211             string = sub(string, parameter, value)
212         f = open(cfgdir + "/" + os.path.basename(configuration), "w")
213         f.write(string)
214         f.close()
215     if not edit:
216         # Destination user mk file (only on project creation)
217         makefile = open(os.path.join(const.DATA_DIR, "mktemplates/template.mk"), "r").read()
218         # Deadly performances loss was here :(
219         makefile = userMkGenerator(project_info, makefile)
220         open(prjdir + "/" + os.path.basename(prjdir) + ".mk", "w").write(makefile)
221     # Destination wizard mk file
222     makefile = open(os.path.join(const.DATA_DIR, "mktemplates/template_wiz.mk"), "r").read()
223     makefile = mkGenerator(project_info, makefile)
224     open(prjdir + "/" + os.path.basename(prjdir) + "_wiz.mk", "w").write(makefile)
225     # Destination main.c file
226     if not edit:
227         main = open(os.path.join(const.DATA_DIR, "srctemplates/main.c"), "r").read()
228         open(prjdir + "/main.c", "w").write(main)
229     # Files for selected plugins
230     relevants_files = {}
231     for plugin in project_info.info("OUTPUT"):
232         module = loadPlugin(plugin)
233         relevants_files[plugin] = module.createProject(project_info)
234     project_info.setInfo("RELEVANT_FILES", relevants_files)
235
236 def loadPlugin(plugin):
237     """
238     Returns the given plugin module.
239     """
240     return getattr(__import__("plugins", {}, {}, [plugin]), plugin)
241
242 def versionFileGenerator(project_info, version_file):
243     version = bertosVersion(project_info.info("SOURCES_PATH"))
244     return version_file.replace('$version', version)
245
246 def userMkGenerator(project_info, makefile):
247     mk_data = {}
248     mk_data["$pname"] = os.path.basename(project_info.info("PROJECT_PATH"))
249     mk_data["$main"] = os.path.basename(project_info.info("PROJECT_PATH")) + "/main.c"
250     for key in mk_data:
251         while makefile.find(key) != -1:
252             makefile = makefile.replace(key, mk_data[key])
253     return makefile
254
255 def mkGenerator(project_info, makefile):
256     """
257     Generates the mk file for the current project.
258     """
259     mk_data = {}
260     mk_data["$pname"] = os.path.basename(project_info.info("PROJECT_PATH"))
261     mk_data["$cpuclockfreq"] = project_info.info("SELECTED_FREQ")
262     cpu_mk_parameters = []
263     for key, value in project_info.info("CPU_INFOS").items():
264         if key.startswith(const.MK_PARAM_ID):
265             cpu_mk_parameters.append("%s = %s" %(key.replace("MK", mk_data["$pname"]), value))
266     mk_data["$cpuparameters"] = "\n".join(cpu_mk_parameters)
267     mk_data["$csrc"], mk_data["$pcsrc"], mk_data["$cppasrc"], mk_data["$cxxsrc"], mk_data["$asrc"], mk_data["$constants"] = csrcGenerator(project_info)
268     mk_data["$prefix"] = replaceSeparators(project_info.info("TOOLCHAIN")["path"].split("gcc")[0])
269     mk_data["$suffix"] = replaceSeparators(project_info.info("TOOLCHAIN")["path"].split("gcc")[1])
270     mk_data["$main"] = os.path.basename(project_info.info("PROJECT_PATH")) + "/main.c"
271     for key in mk_data:
272         while makefile.find(key) != -1:
273             makefile = makefile.replace(key, mk_data[key])
274     return makefile
275
276 def makefileGenerator(project_info, makefile):
277     """
278     Generate the Makefile for the current project.
279     """
280     # TODO write a general function that works for both the mk file and the Makefile
281     while makefile.find("$pname") != -1:
282         makefile = makefile.replace("$pname", os.path.basename(project_info.info("PROJECT_PATH")))
283     return makefile
284
285 def csrcGenerator(project_info):
286     modules = project_info.info("MODULES")
287     files = project_info.info("FILES")
288     if "harvard" in project_info.info("CPU_INFOS")["CPU_TAGS"]:
289         harvard = True
290     else:
291         harvard = False
292     # file to be included in CSRC variable
293     csrc = []
294     # file to be included in PCSRC variable
295     pcsrc = []
296     # files to be included in CPPASRC variable
297     cppasrc = []
298     # files to be included in CXXSRC variable
299     cxxsrc = []
300     # files to be included in ASRC variable
301     asrc = []
302     # constants to be included at the beginning of the makefile
303     constants = {}
304     for module, information in modules.items():
305         module_files = set([])
306         dependency_files = set([])
307         # assembly sources
308         asm_files = set([])
309         hwdir = os.path.basename(project_info.info("PROJECT_PATH")) + "/hw" 
310         if information["enabled"]:
311             if "constants" in information:
312                 constants.update(information["constants"])
313             cfiles, sfiles = findModuleFiles(module, project_info)
314             module_files |= set(cfiles)
315             asm_files |= set(sfiles)
316             for file in information["hw"]:
317                 if file.endswith(".c"):
318                     module_files |= set([hwdir + "/" + os.path.basename(file)])
319             for file_dependency in information["depends"] + tuple(files.keys()):
320                     dependencyCFiles, dependencySFiles = findModuleFiles(file_dependency, project_info)
321                     dependency_files |= set(dependencyCFiles)
322                     asm_files |= set(dependencySFiles)
323             for file in module_files:
324                 if not harvard or information.get("harvard", "both") == "both":
325                     csrc.append(file)
326                 if harvard and "harvard" in information:
327                     pcsrc.append(file)
328             for file in dependency_files:
329                 csrc.append(file)
330             for file in project_info.info("CPU_INFOS")["C_SRC"]:
331                 csrc.append(file)
332             for file in project_info.info("CPU_INFOS")["PC_SRC"]:
333                 pcsrc.append(file)
334             for file in asm_files:
335                 cppasrc.append(file)
336     for file in project_info.info("CPU_INFOS")["CPPA_SRC"]:
337         cppasrc.append(file)
338     for file in project_info.info("CPU_INFOS")["CXX_SRC"]:
339         cxxsrc.append(file)
340     for file in project_info.info("CPU_INFOS")["ASRC"]:
341         asrc.append(file)
342     csrc = set(csrc)
343     csrc = " \\\n\t".join(csrc) + " \\"
344     pcsrc = set(pcsrc)
345     pcsrc = " \\\n\t".join(pcsrc) + " \\"
346     cppasrc = set(cppasrc)
347     cppasrc = " \\\n\t".join(cppasrc) + " \\"
348     cxxsrc = set(cxxsrc)
349     cxxsrc = " \\\n\t".join(cxxsrc) + " \\"
350     asrc = set(asrc)
351     asrc = " \\\n\t".join(asrc) + " \\"
352     constants = "\n".join([os.path.basename(project_info.info("PROJECT_PATH")) + "_" + key + " = " + unicode(value) for key, value in constants.items()])
353     return csrc, pcsrc, cppasrc, cxxsrc, asrc, constants
354
355 def findModuleFiles(module, project_info):
356     # Find the files related to the selected module
357     cfiles = []
358     sfiles = []
359     # .c files related to the module and the cpu architecture
360     for filename, path in project_info.searchFiles(module + ".c"):
361         path = path.replace(project_info.info("SOURCES_PATH") + os.sep, "")
362         path = replaceSeparators(path)
363         cfiles.append(path + "/" + filename)
364     # .s files related to the module and the cpu architecture
365     for filename, path in project_info.searchFiles(module + ".s") + \
366             project_info.searchFiles(module + ".S"):
367         path = path.replace(project_info.info("SOURCES_PATH") + os.sep, "")
368         path = replaceSeparators(path)
369         sfiles.append(path + "/" + filename)
370     # .c and .s files related to the module and the cpu tags
371     tags = project_info.info("CPU_INFOS")["CPU_TAGS"]
372
373     # Awful, but secure check for version
374     # TODO: split me in a method/function
375     try:
376         version_string = bertosVersion(project_info.info("SOURCES_PATH"))
377         version_list = [int(i) for i in version_string.split()[-1].split('.')]
378         if version_list < [2, 5]:
379             # For older versions of BeRTOS add the toolchain to the tags
380             tags.append(project_info.info("CPU_INFOS")["TOOLCHAIN"])
381     except ValueError:
382         # If the version file hasn't a valid version number do nothing
383         pass
384
385     for tag in tags:
386         for filename, path in project_info.searchFiles(module + "_" + tag + ".c"):
387             path = path.replace(project_info.info("SOURCES_PATH") + os.sep, "")
388             if os.sep != "/":
389                 path = replaceSeparators(path)
390             cfiles.append(path + "/" + filename)
391         for filename, path in project_info.searchFiles(module + "_" + tag + ".s") + \
392                 project_info.searchFiles(module + "_" + tag + ".S"):
393             path = path.replace(project_info.info("SOURCES_PATH") + os.sep, "")
394             path = replaceSeparators(path)
395             sfiles.append(path + "/" + filename)
396     return cfiles, sfiles
397
398 def replaceSeparators(path):
399     """
400     Replace the separators in the given path with unix standard separator.
401     """
402     if os.sep != "/":
403         while path.find(os.sep) != -1:
404             path = path.replace(os.sep, "/")
405     return path
406
407 def getSystemPath():
408     path = os.environ["PATH"]
409     if os.name == "nt":
410         path = path.split(";")
411     else:
412         path = path.split(":")
413     return path
414
415 def findToolchains(path_list):
416     toolchains = []
417     for element in path_list:
418         for toolchain in glob.glob(element+ "/" + const.GCC_NAME):
419             toolchains.append(toolchain)
420     return list(set(toolchains))
421
422 def getToolchainInfo(output):
423     info = {}
424     expr = re.compile("Target: .*")
425     target = expr.findall(output)
426     if len(target) == 1:
427         info["target"] = target[0].split("Target: ")[1]
428     expr = re.compile("gcc version [0-9,.]*")
429     version = expr.findall(output)
430     if len(version) == 1:
431         info["version"] = version[0].split("gcc version ")[1]
432     expr = re.compile("gcc version [0-9,.]* \(.*\)")
433     build = expr.findall(output)
434     if len(build) == 1:
435         build = build[0].split("gcc version ")[1]
436         build = build[build.find("(") + 1 : build.find(")")]
437         info["build"] = build
438     expr = re.compile("Configured with: .*")
439     configured = expr.findall(output)
440     if len(configured) == 1:
441         info["configured"] = configured[0].split("Configured with: ")[1]
442     expr = re.compile("Thread model: .*")
443     thread = expr.findall(output)
444     if len(thread) == 1:
445         info["thread"] = thread[0].split("Thread model: ")[1]
446     return info
447
448 def getToolchainName(toolchain_info):
449     name = "GCC " + toolchain_info["version"] + " - " + toolchain_info["target"].strip()
450     return name
451
452 def getTagSet(cpu_info):
453     tag_set = set([])
454     for cpu in cpu_info:
455         tag_set |= set([cpu["CPU_NAME"]])
456         tag_set |= set(cpu["CPU_TAGS"])
457         tag_set |= set([cpu["TOOLCHAIN"]])
458     return tag_set
459         
460
461 def getInfos(definition):
462     D = {}
463     D.update(const.CPU_DEF)
464     def include(filename, dict = D, directory=definition[1]):
465         execfile(directory + "/" + filename, {}, D)
466     D["include"] = include
467     include(definition[0], D)
468     D["CPU_NAME"] = definition[0].split(".")[0]
469     D["DEFINITION_PATH"] = definition[1] + "/" + definition[0]
470     del D["include"]
471     return D
472
473 def getCommentList(string):
474     comment_list = re.findall(r"/\*{2}\s*([^*]*\*(?:[^/*][^*]*\*+)*)/", string)
475     comment_list = [re.findall(r"^\s*\* *(.*?)$", comment, re.MULTILINE) for comment in comment_list]
476     return comment_list
477
478 def loadModuleDefinition(first_comment):
479     to_be_parsed = False
480     module_definition = {}
481     for num, line in enumerate(first_comment):
482         index = line.find("$WIZ$")
483         if index != -1:
484             to_be_parsed = True
485             try:
486                 exec line[index + len("$WIZ$ "):] in {}, module_definition
487             except:
488                 raise ParseError(num, line[index:])
489         elif line.find("\\brief") != -1:
490             module_definition["module_description"] = line[line.find("\\brief") + len("\\brief "):]
491     module_dict = {}
492     if "module_name" in module_definition:
493         module_name = module_definition[const.MODULE_DEFINITION["module_name"]]
494         del module_definition[const.MODULE_DEFINITION["module_name"]]
495         module_dict[module_name] = {}
496         if const.MODULE_DEFINITION["module_depends"] in module_definition:
497             depends = module_definition[const.MODULE_DEFINITION["module_depends"]]
498             del module_definition[const.MODULE_DEFINITION["module_depends"]]
499             if type(depends) == str:
500                 depends = (depends,)
501             module_dict[module_name]["depends"] = depends
502         else:
503             module_dict[module_name]["depends"] = ()
504         if const.MODULE_DEFINITION["module_configuration"] in module_definition:
505             module_dict[module_name]["configuration"] = module_definition[const.MODULE_DEFINITION["module_configuration"]]
506             del module_definition[const.MODULE_DEFINITION["module_configuration"]]
507         else:
508             module_dict[module_name]["configuration"] = ""
509         if "module_description" in module_definition:
510             module_dict[module_name]["description"] = module_definition["module_description"]
511             del module_definition["module_description"]
512         if const.MODULE_DEFINITION["module_harvard"] in module_definition:
513             harvard = module_definition[const.MODULE_DEFINITION["module_harvard"]]
514             module_dict[module_name]["harvard"] = harvard
515             del module_definition[const.MODULE_DEFINITION["module_harvard"]]
516         if const.MODULE_DEFINITION["module_hw"] in module_definition:
517             hw = module_definition[const.MODULE_DEFINITION["module_hw"]]
518             del module_definition[const.MODULE_DEFINITION["module_hw"]]
519             if type(hw) == str:
520                 hw = (hw, )
521             module_dict[module_name]["hw"] = hw
522         else:
523             module_dict[module_name]["hw"] = ()
524         if const.MODULE_DEFINITION["module_supports"] in module_definition:
525             supports = module_definition[const.MODULE_DEFINITION["module_supports"]]
526             del module_definition[const.MODULE_DEFINITION["module_supports"]]
527             module_dict[module_name]["supports"] = supports
528         module_dict[module_name]["constants"] = module_definition
529         module_dict[module_name]["enabled"] = False
530     return to_be_parsed, module_dict
531
532 def isSupported(project, module=None, property_id=None):
533     if not module and property_id:
534         item = project.info("CONFIGURATIONS")[property_id[0]][property_id[1]]["informations"]
535     else:
536         item = project.info("MODULES")[module]
537     tag_dict = project.info("ALL_CPU_TAGS")
538     if "supports" in item:
539         support_string = item["supports"]
540         supported = {}
541         try:
542             exec "supported = " + support_string in tag_dict, supported
543         except:
544             raise SupportedException(support_string)
545         return supported["supported"]
546     else:
547         return True
548
549 def loadDefineLists(comment_list):
550     define_list = {}
551     for comment in comment_list:
552         for num, line in enumerate(comment):
553             index = line.find("$WIZ$")
554             if index != -1:
555                 try:
556                     exec line[index + len("$WIZ$ "):] in {}, define_list
557                 except:
558                     raise ParseError(num, line[index:])
559     for key, value in define_list.items():
560         if type(value) == str:
561             define_list[key] = (value,)
562     return define_list
563
564 def getDescriptionInformations(comment):
565     """
566     Take the doxygen comment and strip the wizard informations, returning the tuple
567     (comment, wizard_information)
568     """
569     brief = ""
570     description = ""
571     information = {}
572     for num, line in enumerate(comment):
573         index = line.find("$WIZ$")
574         if index != -1:
575             if len(brief) == 0:
576                 brief += line[:index].strip()
577             else:
578                 description += " " + line[:index]
579             try:
580                 exec line[index + len("$WIZ$ "):] in {}, information
581             except:
582                 raise ParseError(num, line[index:])
583         else:
584             if len(brief) == 0:
585                 brief += line.strip()
586             else:
587                 description += " " + line
588                 description = description.strip()
589     return brief.strip(), description.strip(), information
590
591 def getDefinitionBlocks(text):
592     """
593     Take a text and return a list of tuple (description, name-value).
594     """
595     block = []
596     block_tmp = re.finditer(r"/\*{2}\s*([^*]*\*(?:[^/*][^*]*\*+)*)/\s*#define\s+((?:[^/]*?/?)+)\s*?(?:/{2,3}[^<].*?)?$", text, re.MULTILINE)
597     for match in block_tmp:
598         # Only the first element is needed
599         comment = match.group(1)
600         define = match.group(2)
601         start = match.start()
602         block.append(([re.findall(r"^\s*\* *(.*?)$", line, re.MULTILINE)[0] for line in comment.splitlines()], define, start))
603     for match in re.finditer(r"/{3}\s*([^<].*?)\s*#define\s+((?:[^/]*?/?)+)\s*?(?:/{2,3}[^<].*?)?$", text, re.MULTILINE):
604         comment = match.group(1)
605         define = match.group(2)
606         start = match.start()
607         block.append(([comment], define, start))
608     for match in re.finditer(r"#define\s*(.*?)\s*/{3}<\s*(.+?)\s*?(?:/{2,3}[^<].*?)?$", text, re.MULTILINE):
609         comment = match.group(2)
610         define = match.group(1)
611         start = match.start()
612         block.append(([comment], define, start))
613     return block
614
615 def formatParamNameValue(text):
616     """
617     Take the given string and return a tuple with the name of the parameter in the first position
618     and the value in the second.
619     """
620     block = re.findall("\s*([^\s]+)\s*(.+?)\s*$", text, re.MULTILINE)
621     return block[0]
622
623 def loadConfigurationInfos(path):
624     """
625     Return the module configurations found in the given file as a dict with the
626     parameter name as key and a dict containig the fields above as value:
627         "value": the value of the parameter
628         "description": the description of the parameter
629         "informations": a dict containig optional informations:
630             "type": "int" | "boolean" | "enum"
631             "min": the minimum value for integer parameters
632             "max": the maximum value for integer parameters
633             "long": boolean indicating if the num is a long
634             "unsigned": boolean indicating if the num is an unsigned
635             "value_list": the name of the enum for enum parameters
636             "conditional_deps": the list of conditional dependencies for boolean parameters
637     """
638     configuration_infos = {}
639     configuration_infos["paramlist"] = []
640     for comment, define, start in getDefinitionBlocks(open(path, "r").read()):
641         name, value = formatParamNameValue(define)
642         brief, description, informations = getDescriptionInformations(comment)
643         configuration_infos["paramlist"].append((start, name))
644         configuration_infos[name] = {}
645         configuration_infos[name]["value"] = value
646         configuration_infos[name]["informations"] = informations
647         if not "type" in configuration_infos[name]["informations"]:
648             configuration_infos[name]["informations"]["type"] = findParameterType(configuration_infos[name])
649         if ("type" in configuration_infos[name]["informations"] and
650                 configuration_infos[name]["informations"]["type"] == "int" and
651                 configuration_infos[name]["value"].find("L") != -1):
652             configuration_infos[name]["informations"]["long"] = True
653             configuration_infos[name]["value"] = configuration_infos[name]["value"].replace("L", "")
654         if ("type" in configuration_infos[name]["informations"] and
655                 configuration_infos[name]["informations"]["type"] == "int" and
656                 configuration_infos[name]["value"].find("U") != -1):
657             configuration_infos[name]["informations"]["unsigned"] = True
658             configuration_infos[name]["value"] = configuration_infos[name]["value"].replace("U", "")
659         if "conditional_deps" in configuration_infos[name]["informations"]:
660             if (type(configuration_infos[name]["informations"]["conditional_deps"]) == str or
661                     type(configuration_infos[name]["informations"]["conditional_deps"]) == unicode):
662                 configuration_infos[name]["informations"]["conditional_deps"] = (configuration_infos[name]["informations"]["conditional_deps"], )
663             elif type(configuration_infos[name]["informations"]["conditional_deps"]) == tuple:
664                 pass
665             else:
666                 configuration_infos[name]["informations"]["conditional_deps"] = ()
667         configuration_infos[name]["description"] = description
668         configuration_infos[name]["brief"] = brief
669     return configuration_infos
670
671 def updateConfigurationValues(def_conf, user_conf):
672     for param in def_conf["paramlist"]:
673         if param[1] in user_conf and "value" in user_conf[param[1]]:
674             def_conf[param[1]]["value"] = user_conf[param[1]]["value"]
675     return def_conf
676
677 def findParameterType(parameter):
678     if "value_list" in parameter["informations"]:
679         return "enum"
680     if "min" in parameter["informations"] or "max" in parameter["informations"] or re.match(r"^\d+U?L?$", parameter["value"]) != None:
681         return "int"
682
683 def sub(string, parameter, value):
684     """
685     Substitute the given value at the given parameter define in the given string
686     """
687     return re.sub(r"(?P<define>#define\s+" + parameter + r"\s+)([^\s]+)", r"\g<define>" + value, string)
688
689 def isInt(informations):
690     """
691     Return True if the value is a simple int.
692     """
693     if ("long" not in informatios or not informations["long"]) and ("unsigned" not in informations or informations["unsigned"]):
694         return True
695     else:
696         return False
697
698 def isLong(informations):
699     """
700     Return True if the value is a long.
701     """
702     if "long" in informations and informations["long"] and "unsigned" not in informations:
703         return True
704     else:
705         return False
706
707 def isUnsigned(informations):
708     """
709     Return True if the value is an unsigned.
710     """
711     if "unsigned" in informations and informations["unsigned"] and "long" not in informations:
712         return True
713     else:
714         return False
715
716 def isUnsignedLong(informations):
717     """
718     Return True if the value is an unsigned long.
719     """
720     if "unsigned" in informations and "long" in informations and informations["unsigned"] and informations["long"]:
721         return True
722     else:
723         return False
724
725 class ParseError(Exception):
726     def __init__(self, line_number, line):
727         Exception.__init__(self)
728         self.line_number = line_number
729         self.line = line
730
731 class SupportedException(Exception):
732     def __init__(self, support_string):
733         Exception.__init__(self)
734         self.support_string = support_string