Move to codelite_project module the codelite specific project creation functions
[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 import shutil
17
18 import const
19 import codelite_project
20 import DefineException
21
22 def isBertosDir(directory):
23    return os.path.exists(directory + "/VERSION")
24
25 def bertosVersion(directory):
26    return open(directory + "/VERSION").readline().strip()
27
28 def createBertosProject(projectInfo):
29     directory = projectInfo.info("PROJECT_PATH")
30     sourcesDir = projectInfo.info("SOURCES_PATH")
31     if not os.path.isdir(directory):
32         os.mkdir(directory)
33     f = open(directory + "/project.bertos", "w")
34     f.write(repr(projectInfo))
35     f.close()
36     ## Destination source dir
37     srcdir = directory + "/bertos"
38     shutil.rmtree(srcdir, True)
39     shutil.copytree(sourcesDir + "/bertos", srcdir)
40     ## Destination makefile
41     makefile = directory + "/Makefile"
42     if os.path.exists(makefile):
43         os.remove(makefile)
44     makefile = open("mktemplates/Makefile").read()
45     makefile = makefileGenerator(projectInfo, makefile)
46     open(directory + "/Makefile", "w").write(makefile)
47     ## Destination project dir
48     prjdir = directory + "/" + os.path.basename(directory)
49     shutil.rmtree(prjdir, True)
50     os.mkdir(prjdir)
51     ## Destination configurations files
52     cfgdir = prjdir + "/cfg"
53     shutil.rmtree(cfgdir, True)
54     os.mkdir(cfgdir)
55     for key, value in projectInfo.info("CONFIGURATIONS").items():
56         string = open(sourcesDir + "/" + key, "r").read()
57         for parameter, infos in value.items():
58             value = infos["value"]
59             if "unsigned" in infos["informations"].keys() and infos["informations"]["unsigned"]:
60                 value += "U"
61             if "long" in infos["informations"].keys() and infos["informations"]["long"]:
62                 value += "L"
63             string = sub(string, parameter, value)
64         f = open(cfgdir + "/" + os.path.basename(key), "w")
65         f.write(string)
66         f.close()
67     ## Destinatio mk file
68     makefile = open("mktemplates/template.mk", "r").read()
69     makefile = mkGenerator(projectInfo, makefile)
70     open(prjdir + "/" + os.path.basename(prjdir) + ".mk", "w").write(makefile)
71     workspace = codeliteWorkspaceGenerator(projectInfo)
72     open(directory + "/" + os.path.basename(prjdir) + ".workspace", "w").write(workspace)
73     project = codeliteProjectGenerator(projectInfo)
74     open(directory + "/" + os.path.basename(prjdir) + ".project", "w").write(project)
75
76 def mkGenerator(projectInfo, makefile):
77     """
78     Generates the mk file for the current project.
79     """
80     mkData = {}
81     mkData["$pname"] = os.path.basename(projectInfo.info("PROJECT_PATH"))
82     mkData["$cpuname"] = projectInfo.info("CPU_INFOS")["CPU_NAME"]
83     mkData["$cflags"] = " ".join(projectInfo.info("CPU_INFOS")["C_FLAGS"])
84     mkData["$ldflags"] = " ".join(projectInfo.info("CPU_INFOS")["LD_FLAGS"])
85     mkData["$csrc"] = csrcGenerator(projectInfo)
86     mkData["$prefix"] = os.path.basename(projectInfo.info("TOOLCHAIN")["path"]).split("gcc")[0]
87     mkData["$suffix"] = os.path.basename(projectInfo.info("TOOLCHAIN")["path"]).split("gcc")[1]
88     for key in mkData:
89         while makefile.find(key) != -1:
90             makefile = makefile.replace(key, mkData[key])
91     return makefile
92
93 def makefileGenerator(projectInfo, makefile):
94     """
95     Generate the Makefile for the current project.
96     """
97     # TODO: write a general function that works for both the mk file and the Makefile
98     while makefile.find("project_name") != -1:
99         makefile = makefile.replace("project_name", os.path.basename(projectInfo.info("PROJECT_PATH")))
100     return makefile
101
102 def csrcGenerator(projectInfo):
103     modules = projectInfo.info("MODULES")
104     files = []
105     for module, information in modules.items():
106         if information["enabled"]:
107             for filename, path in findDefinitions(module + ".c", projectInfo):
108                 files.append(path + "/" + filename)
109             for filename, path in findDefinitions(module + "_" + projectInfo.info("CPU_INFOS")["TOOLCHAIN"] + ".c", projectInfo):
110                 files.append(path + "/" + filename)
111             for tag in projectInfo.info("CPU_INFOS")["CPU_TAGS"]:
112                 for filename, path in findDefinitions(module + "_" + tag + ".c", projectInfo):
113                     files.append(path + "/" + filename)
114     csrc = " \\\n\t".join(files) + " \\"
115     return csrc
116
117 def codeliteProjectGenerator(projectInfo):
118     template = open("cltemplates/bertos.project").read()
119     filelist = "\n".join(codelite_project.clFiles(codelite_project.findSources(projectInfo.info("PROJECT_PATH")), projectInfo.info("PROJECT_PATH")))
120     while template.find("$filelist") != -1:
121         template = template.replace("$filelist", filelist)
122     return template
123
124 def codeliteWorkspaceGenerator(projectInfo):
125     template = open("cltemplates/bertos.workspace").read()
126     projectName = os.path.basename(projectInfo.info("PROJECT_PATH"))
127     while template.find("$project") != -1:
128         template = template.replace("$project", projectName)
129     return template
130     
131 def getSystemPath():
132     path = os.environ["PATH"]
133     if os.name == "nt":
134         path = path.split(";")
135     else:
136         path = path.split(":")
137     return path
138
139 def findToolchains(pathList):
140     toolchains = []
141     for element in pathList:
142         for toolchain in glob.glob(element+ "/" + const.GCC_NAME):
143             toolchains.append(toolchain)
144     return list(set(toolchains))
145
146 def getToolchainInfo(output):
147     info = {}
148     expr = re.compile("Target: .*")
149     target = expr.findall(output)
150     if len(target) == 1:
151         info["target"] = target[0].split("Target: ")[1]
152     expr = re.compile("gcc version [0-9,.]*")
153     version = expr.findall(output)
154     if len(version) == 1:
155         info["version"] = version[0].split("gcc version ")[1]
156     expr = re.compile("gcc version [0-9,.]* \(.*\)")
157     build = expr.findall(output)
158     if len(build) == 1:
159         build = build[0].split("gcc version ")[1]
160         build = build[build.find("(") + 1 : build.find(")")]
161         info["build"] = build
162     expr = re.compile("Configured with: .*")
163     configured = expr.findall(output)
164     if len(configured) == 1:
165         info["configured"] = configured[0].split("Configured with: ")[1]
166     expr = re.compile("Thread model: .*")
167     thread = expr.findall(output)
168     if len(thread) == 1:
169         info["thread"] = thread[0].split("Thread model: ")[1]
170     return info
171
172 def loadSourceTree(project):
173     fileList = [f for f in os.walk(project.info("SOURCES_PATH"))]
174     project.setInfo("FILE_LIST", fileList)
175
176 def findDefinitions(ftype, project):
177     L = project.info("FILE_LIST")
178     definitions = []
179     for element in L:
180         for filename in element[2]:
181             if fnmatch.fnmatch(filename, ftype):
182                 definitions.append((filename, element[0]))
183     return definitions
184
185 def loadCpuInfos(project):
186     cpuInfos = []
187     for definition in findDefinitions(const.CPU_DEFINITION, project):
188         cpuInfos.append(getInfos(definition))
189     return cpuInfos
190
191 def getInfos(definition):
192     D = {}
193     D.update(const.CPU_DEF)
194     def include(filename, dict = D, directory=definition[1]):
195         execfile(directory + "/" + filename, {}, D)
196     D["include"] = include
197     include(definition[0], D)
198     D["CPU_NAME"] = definition[0].split(".")[0]
199     D["DEFINITION_PATH"] = definition[1] + "/" + definition[0]
200     del D["include"]
201     return D
202
203 def getCommentList(string):
204     commentList = re.findall(r"/\*{2}\s*([^*]*\*(?:[^/*][^*]*\*+)*)/", string)
205     commentList = [re.findall(r"^\s*\* *(.*?)$", comment, re.MULTILINE) for comment in commentList]
206     return commentList
207
208 def loadModuleDefinition(first_comment):
209     toBeParsed = False
210     moduleDefinition = {}
211     for num, line in enumerate(first_comment):
212         index = line.find("$WIZ$")
213         if index != -1:
214             toBeParsed = True
215             try:
216                 exec line[index + len("$WIZ$ "):] in {}, moduleDefinition
217             except:
218                 raise ParseError(num, line[index:])
219         elif line.find("\\brief") != -1:
220             moduleDefinition["module_description"] = line[line.find("\\brief") + len("\\brief "):]
221     moduleDict = {}
222     if "module_name" in moduleDefinition.keys():
223         moduleDict[moduleDefinition["module_name"]] = {}
224         if "module_depends" in moduleDefinition.keys():
225             if type(moduleDefinition["module_depends"]) == str:
226                 moduleDefinition["module_depends"] = (moduleDefinition["module_depends"],)
227             moduleDict[moduleDefinition["module_name"]]["depends"] = moduleDefinition["module_depends"]
228         else:
229             moduleDict[moduleDefinition["module_name"]]["depends"] = ()
230         if "module_configuration" in moduleDefinition.keys():
231             moduleDict[moduleDefinition["module_name"]]["configuration"] = moduleDefinition["module_configuration"]
232         else:
233             moduleDict[moduleDefinition["module_name"]]["configuration"] = ""
234         if "module_description" in moduleDefinition.keys():
235             moduleDict[moduleDefinition["module_name"]]["description"] = moduleDefinition["module_description"]
236         moduleDict[moduleDefinition["module_name"]]["enabled"] = False
237     return toBeParsed, moduleDict
238
239 def loadDefineLists(commentList):
240     defineList = {}
241     for comment in commentList:
242         for num, line in enumerate(comment):
243             index = line.find("$WIZ$")
244             if index != -1:
245                 try:
246                     exec line[index + len("$WIZ$ "):] in {}, defineList
247                 except:
248                     raise ParseError(num, line[index:])
249     for key, value in defineList.items():
250         if type(value) == str:
251             defineList[key] = (value,)
252     return defineList
253
254 def getDescriptionInformations(comment): 
255     """ 
256     Take the doxygen comment and strip the wizard informations, returning the tuple 
257     (comment, wizard_information) 
258     """
259     brief = ""
260     description = ""
261     information = {}
262     for num, line in enumerate(comment):
263         index = line.find("$WIZ$")
264         if index != -1:
265             if len(brief) == 0:
266                 brief += line[:index].strip()
267             else:
268                 description += " " + line[:index]
269             try:
270                 exec line[index + len("$WIZ$ "):] in {}, information
271             except:
272                 raise ParseError(num, line[index:])
273         else:
274             if len(brief) == 0:
275                 brief += line.strip()
276             else:
277                 description += " " + line
278                 description = description.strip()
279     return brief.strip(), description.strip(), information
280
281 def getDefinitionBlocks(text):
282     """
283     Take a text and return a list of tuple (description, name-value).
284     """
285     block = []
286     block_tmp = re.findall(r"/\*{2}\s*([^*]*\*(?:[^/*][^*]*\*+)*)/\s*#define\s+((?:[^/]*?/?)+)\s*?(?:/{2,3}[^<].*?)?$", text, re.MULTILINE)
287     for comment, define in block_tmp:
288         # Only the first element is needed
289         block.append(([re.findall(r"^\s*\* *(.*?)$", line, re.MULTILINE)[0] for line in comment.splitlines()], define))
290     for comment, define in re.findall(r"/{3}\s*([^<].*?)\s*#define\s+((?:[^/]*?/?)+)\s*?(?:/{2,3}[^<].*?)?$", text, re.MULTILINE):
291         block.append(([comment], define))
292     for define, comment in re.findall(r"#define\s*(.*?)\s*/{3}<\s*(.+?)\s*?(?:/{2,3}[^<].*?)?$", text, re.MULTILINE):
293         block.append(([comment], define))
294     return block
295
296 def loadModuleData(project):
297     moduleInfoDict = {}
298     listInfoDict = {}
299     configurationInfoDict = {}
300     for filename, path in findDefinitions("*.h", project):
301         commentList = getCommentList(open(path + "/" + filename, "r").read())
302         if len(commentList) > 0:
303             moduleInfo = {}
304             configurationInfo = {}
305             try:
306                 toBeParsed, moduleDict = loadModuleDefinition(commentList[0])
307             except ParseError, err:
308                 raise DefineException.ModuleDefineException(path, err.line_number, err.line)
309             for module, information in moduleDict.items():
310                 information["category"] = os.path.basename(path)
311                 if "configuration" in information.keys() and len(information["configuration"]):
312                     configuration = moduleDict[module]["configuration"]
313                     try:
314                         configurationInfo[configuration] = loadConfigurationInfos(project.info("SOURCES_PATH") + "/" + configuration)
315                     except ParseError, err:
316                         raise DefineException.ConfigurationDefineException(project.info("SOURCES_PATH") + "/" + configuration, err.line_number, err.line)
317             moduleInfoDict.update(moduleDict)
318             configurationInfoDict.update(configurationInfo)
319             if toBeParsed:
320                 try:
321                     listDict = loadDefineLists(commentList[1:])
322                     listInfoDict.update(listDict)
323                 except ParseError, err:
324                     raise DefineException.EnumDefineException(path, err.line_number, err.line)
325     for filename, path in findDefinitions("*_" + project.info("CPU_INFOS")["TOOLCHAIN"] + ".h", project):
326         commentList = getCommentList(open(path + "/" + filename, "r").read())
327         listInfoDict.update(loadDefineLists(commentList))
328     for tag in project.info("CPU_INFOS")["CPU_TAGS"]:
329         for filename, path in findDefinitions("*_" + tag + ".h", project):
330             commentList = getCommentList(open(path + "/" + filename, "r").read())
331             listInfoDict.update(loadDefineLists(commentList))
332     project.setInfo("MODULES", moduleInfoDict)
333     project.setInfo("LISTS", listInfoDict)
334     project.setInfo("CONFIGURATIONS", configurationInfoDict)
335     
336 def formatParamNameValue(text):
337     """
338     Take the given string and return a tuple with the name of the parameter in the first position
339     and the value in the second.
340     """
341     block = re.findall("\s*([^\s]+)\s*(.+?)\s*$", text, re.MULTILINE)
342     return block[0]
343
344 def loadConfigurationInfos(path):
345     """
346     Return the module configurations found in the given file as a dict with the
347     parameter name as key and a dict containig the fields above as value:
348         "value": the value of the parameter
349         "description": the description of the parameter
350         "informations": a dict containig optional informations:
351             "type": "int" | "boolean" | "enum"
352             "min": the minimum value for integer parameters
353             "max": the maximum value for integer parameters
354             "long": boolean indicating if the num is a long
355             "value_list": the name of the enum for enum parameters
356     """
357     configurationInfos = {}
358     for comment, define in getDefinitionBlocks(open(path, "r").read()):
359         name, value = formatParamNameValue(define)
360         brief, description, informations = getDescriptionInformations(comment)
361         configurationInfos[name] = {}
362         configurationInfos[name]["value"] = value
363         configurationInfos[name]["informations"] = informations
364         if ("type" in configurationInfos[name]["informations"].keys() and
365                 configurationInfos[name]["informations"]["type"] == "int" and
366                 configurationInfos[name]["value"].find("L") != -1):
367             configurationInfos[name]["informations"]["long"] = True
368             configurationInfos[name]["value"] = configurationInfos[name]["value"].replace("L", "")
369         if ("type" in configurationInfos[name]["informations"].keys() and
370                 configurationInfos[name]["informations"]["type"] == "int" and
371                 configurationInfos[name]["value"].find("U") != -1):
372             configurationInfos[name]["informations"]["unsigned"] = True
373             configurationInfos[name]["value"] = configurationInfos[name]["value"].replace("U", "")
374         configurationInfos[name]["description"] = description
375         configurationInfos[name]["brief"] = brief
376     return configurationInfos
377
378 def sub(string, parameter, value):
379     """
380     Substitute the given value at the given parameter define in the given string
381     """
382     return re.sub(r"(?P<define>#define\s+" + parameter + r"\s+)([^\s]+)", r"\g<define>" + value, string)
383
384 def isInt(informations):
385     """
386     Return True if the value is a simple int.
387     """
388     if ("long" not in informatios.keys() or not informations["long"]) and ("unsigned" not in informations.keys() or informations["unsigned"]):
389         return True
390     else:
391         return False
392
393 def isLong(informations):
394     """
395     Return True if the value is a long.
396     """
397     if "long" in informations.keys() and informations["long"] and "unsigned" not in informations.keys():
398         return True
399     else:
400         return False
401
402 def isUnsigned(informations):
403     """
404     Return True if the value is an unsigned.
405     """
406     if "unsigned" in informations.keys() and informations["unsigned"] and "long" not in informations.keys():
407         return True
408     else:
409         return False
410
411 def isUnsignedLong(informations):
412     """
413     Return True if the value is an unsigned long.
414     """
415     if "unsigned" in informations.keys() and "long" in informations.keys() and informations["unsigned"] and informations["long"]:
416         return True
417     else:
418         return False
419
420 class ParseError(Exception):
421     def __init__(self, line_number, line):
422         Exception.__init__(self)
423         self.line_number = line_number
424         self.line = line