Add the function for retrieve the module information and the enum informations (broke...
[bertos.git] / wizard / bertos_utils.py
1 #!/usr/bin/env python
2 # encoding: utf-8
3 #
4 # Copyright 2008 Develer S.r.l. (http://www.develer.com/)
5 # All rights reserved.
6 #
7 # $Id:$
8 #
9 # Author: Lorenzo Berni <duplo@develer.com>
10 #
11
12 import os
13 import fnmatch
14 import glob
15 import re
16
17 import const
18
19 def isBertosDir(directory):
20    return os.path.exists(directory + "/VERSION")
21
22 def bertosVersion(directory):
23    return open(directory + "/VERSION").readline().strip()
24
25 def createBertosProject(directory):
26     if not os.path.isdir(directory):
27         os.mkdir(directory)
28     open(directory + "/project.bertos", "w")
29
30 def getSystemPath():
31     path = os.environ["PATH"]
32     if os.name == "nt":
33         path = path.split(";")
34     else:
35         path = path.split(":")
36     return path
37
38 def findToolchains(pathList):
39     toolchains = []
40     for element in pathList:
41         for toolchain in glob.glob(element+ "/" + const.GCC_NAME):
42             if not os.path.islink(toolchain):
43                 toolchains.append(toolchain)
44     return list(set(toolchains))
45
46 def getToolchainInfo(output):
47     info = {}
48     expr = re.compile("Target: .*")
49     target = expr.findall(output)
50     if len(target) == 1:
51         info["target"] = target[0].split("Target: ")[1]
52     expr = re.compile("gcc version [0-9,.]*")
53     version = expr.findall(output)
54     if len(version) == 1:
55         info["version"] = version[0].split("gcc version ")[1]
56     expr = re.compile("gcc version [0-9,.]* \(.*\)")
57     build = expr.findall(output)
58     if len(build) == 1:
59         build = build[0].split("gcc version ")[1]
60         build = build[build.find("(") + 1 : build.find(")")]
61         info["build"] = build
62     expr = re.compile("Configured with: .*")
63     configured = expr.findall(output)
64     if len(configured) == 1:
65         info["configured"] = configured[0].split("Configured with: ")[1]
66     expr = re.compile("Thread model: .*")
67     thread = expr.findall(output)
68     if len(thread) == 1:
69         info["thread"] = thread[0].split("Thread model: ")[1]
70     return info
71
72 def findDefinitions(ftype, path):
73     L = os.walk(path)
74     for element in L:
75         for filename in element[2]:
76             if fnmatch.fnmatch(filename, ftype):
77                 yield (filename, element[0])
78
79 def loadCpuInfos(path):
80     cpuInfos = []
81     for definition in findDefinitions(const.CPU_DEFINITION, path):
82         cpuInfos.append(getInfos(definition))
83     return cpuInfos
84
85 def getInfos(definition):
86     D = {}
87     D.update(const.CPU_DEF)
88     def include(filename, dict = D, directory=definition[1]):
89         execfile(directory + "/" + filename, {}, D)
90     D["include"] = include
91     include(definition[0], D)
92     D["CPU_NAME"] = definition[0].split(".")[0]
93     D["DEFINITION_PATH"] = definition[1] + "/" + definition[0]
94     del D["include"]
95     return D
96
97 def getDefinitionBlocks(text):
98     """
99     Take a text and return a list of tuple (description, name-value).
100     """
101     block = []
102     block_tmp = re.findall(r"/\*{2}\s*([^*]*\*(?:[^/*][^*]*\*+)*)/\s*#define\s+((?:[^/]*?/?)+)\s*?(?:/{2,3}[^<].*?)?$", text, re.MULTILINE)
103     for comment, define in block_tmp:
104         block.append((" ".join(re.findall(r"^\s*\*?\s*(.*?)\s*?(?:/{2}.*?)?$", comment, re.MULTILINE)).strip(), define))
105     block += re.findall(r"/{3}\s*([^<].*?)\s*#define\s+((?:[^/]*?/?)+)\s*?(?:/{2,3}[^<].*?)?$", text, re.MULTILINE)
106     block += [(comment, define) for define, comment in re.findall(r"#define\s*(.*?)\s*/{3}<\s*(.+?)\s*?(?:/{2,3}[^<].*?)?$", text, re.MULTILINE)]
107     return block
108
109 def formatParamNameValue(text):
110     """
111     Take the given string and return a tuple with the name of the parameter in the first position
112     and the value in the second.
113     """
114     block = re.findall("\s*([^\s]+)\s*(.+?)\s*$", text, re.MULTILINE)
115     return block[0]
116
117 def getDescriptionInformations(text): 
118     """ 
119     Take the doxygen comment and strip the wizard informations, returning the tuple 
120     (comment, wizard_informations) 
121     """ 
122     index = text.find("$WIZARD") 
123     if index != -1: 
124         exec(text[index + 1:]) 
125         informations = WIZARD 
126         return text[:index].strip(), informations
127     else:
128         return text.strip(), {}
129
130 def loadConfigurationInfos(path):
131     """
132     Return the module configurations found in the given file as a dict with the
133     parameter name as key and a dict containig the fields above as value:
134         "value": the value of the parameter
135         "description": the description of the parameter
136         "informations": a dict containig optional informations:
137             "type": "int" | "boolean" | "enum"
138             "min": the minimum value for integer parameters
139             "max": the maximum value for integer parameters
140             "long": boolean indicating if the num is a long
141             "value_list": the name of the enum for enum parameters
142     """
143     configurationInfos = {}
144     for comment, define in getDefinitionBlocks(open(path, "r").read()):
145         name, value = formatParamNameValue(define)
146         description, informations = getDescriptionInformations(comment)
147         configurationInfos[name] = {}
148         configurationInfos[name]["value"] = value
149         configurationInfos[name]["informations"] = informations
150         configurationInfos[name]["description"] = description
151     return configurationInfos
152
153 def loadModuleInfos(path):
154     """
155     Return the module infos found in the given file as a dict with the module
156     name as key and a dict containig the fields above as value or an empty dict
157     if the given file is not a BeRTOS module:
158         "depends": a list of modules needed by this module
159         "configuration": the cfg_*.h with the module configurations
160     """
161     moduleInfos = {}
162     string = open(path, "r").read()
163     commentList = re.findall(r"/\*{2}\s*([^*]*\*(?:[^/*][^*]*\*+)*)/", string)
164     commentList = [" ".join(re.findall(r"^\s*\*?\s*(.*?)\s*?(?:/{2}.*?)?$", comment, re.MULTILINE)).strip() for comment in commentList]
165     for comment in commentList:
166         index = comment.find("$WIZARD_MODULE")
167         if index != -1:
168             exec(comment[index + 1:])
169             moduleInfos[WIZARD_MODULE["name"]] = {"depends": WIZARD_MODULE["depends"], "configuration": WIZARD_MODULE["configuration"]}
170             return moduleInfos
171     return {}
172
173 def loadDefineLists(path):
174     """
175     Return a dict with the name of the list as key and a list of string as value
176     """
177     string = open(path, "r").read()
178     commentList = re.findall(r"/\*{2}\s*([^*]*\*(?:[^/*][^*]*\*+)*)/", string)
179     commentList = [" ".join(re.findall(r"^\s*\*?\s*(.*?)\s*?(?:/{2}.*?)?$", comment, re.MULTILINE)).strip() for comment in commentList]
180     listDict = {}
181     for comment in commentList:
182         index = comment.find("$WIZARD_LIST")
183         if index != -1:
184             exec(comment[index + 1:])
185             listDict.update(WIZARD_LIST)
186     return listDict