Use a defaultdict instead of a standard dict for storing the toolchain's infos
[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 collections
17
18 import const
19
20 def isBertosDir(directory):
21    return os.path.exists(directory + "/VERSION")
22
23 def bertosVersion(directory):
24    return open(directory + "/VERSION").readline().strip()
25
26 def createBertosProject(directory):
27     if not os.path.isdir(directory):
28         os.mkdir(directory)
29     open(directory + "/project.bertos", "w")
30
31 def getSystemPath():
32     path = os.environ["PATH"]
33     if os.name == "nt":
34         path = path.split(";")
35     else:
36         path = path.split(":")
37     return path
38
39 def findToolchains(pathList):
40     toolchains = []
41     for element in pathList:
42         for toolchain in glob.glob(element+ "/" + const.GCC_NAME):
43             if not os.path.islink(toolchain):
44                 toolchains.append(toolchain)
45     return list(set(toolchains))
46
47 def getToolchainInfo(output):
48     info = collections.defaultdict(unicode)
49     expr = re.compile("Target: .*")
50     target = expr.findall(output)
51     if len(target) == 1:
52         info["target"] = target[0].split("Target: ")[1]
53     expr = re.compile("gcc version [0-9,.]*")
54     version = expr.findall(output)
55     if len(version) == 1:
56         info["version"] = version[0].split("gcc version ")[1]
57     expr = re.compile("gcc version [0-9,.]* \(.*\)")
58     build = expr.findall(output)
59     if len(build) == 1:
60         build = build[0].split("gcc version ")[1]
61         build = build[build.find("(") + 1 : build.find(")")]
62         info["build"] = build
63     expr = re.compile("Configured with: .*")
64     configured = expr.findall(output)
65     if len(configured) == 1:
66         info["configured"] = configured[0].split("Configured with: ")[1]
67     expr = re.compile("Thread model: .*")
68     thread = expr.findall(output)
69     if len(thread) == 1:
70         info["thread"] = thread[0].split("Thread model: ")[1]
71     return info
72
73 def findDefinitions(ftype, path):
74     L = os.walk(path)
75     for element in L:
76         for filename in element[2]:
77             if fnmatch.fnmatch(filename, ftype):
78                 yield (filename, element[0])
79
80 def loadCpuInfos(path):
81     cpuInfos = []
82     for definition in findDefinitions(const.CPU_DEFINITION, path):
83         cpuInfos.append(getInfos(definition))
84     return cpuInfos
85
86 def getInfos(definition):
87     D = {}
88     D.update(const.CPU_DEF)
89     def include(filename, dict = D, directory=definition[1]):
90         execfile(directory + "/" + filename, {}, D)
91     D["include"] = include
92     include(definition[0], D)
93     D["CPU_NAME"] = definition[0].split(".")[0]
94     D["DEFINITION_PATH"] = definition[1] + "/" + definition[0]
95     del D["include"]
96     return D