4 # This file is part of BeRTOS.
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.
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.
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
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.
29 # Copyright 2008 Develer S.r.l. (http://www.develer.com/)
33 # Author: Lorenzo Berni <duplo@develer.com>
41 import DefineException
43 from LoadException import VersionException, ToolchainException
47 from bertos_utils import (
49 isBertosDir, getTagSet, setEnabledModules, getInfos,
50 loadConfigurationInfos, loadDefineLists, loadModuleDefinition,
51 getCommentList, updateConfigurationValues,
54 ParseError, SupportedException
57 class BProject(object):
59 Simple class for store and retrieve project informations.
62 def __init__(self, project_file="", info_dict={}):
64 self._cached_queries = {}
66 self.loadBertosProject(project_file, info_dict)
68 def loadBertosProject(self, project_file, info_dict):
69 project_dir = os.path.dirname(project_file)
70 project_data = pickle.loads(open(project_file, "r").read())
71 # If PROJECT_NAME is not defined it use the directory name as PROJECT_NAME
72 # NOTE: this can throw an Exception if the user has changed the directory containing the project
73 self.infos["PROJECT_NAME"] = project_data.get("PROJECT_NAME", os.path.basename(project_dir))
74 self.infos["PROJECT_PATH"] = os.path.dirname(project_file)
75 # Check for the Wizard version
76 wizard_version = project_data.get("WIZARD_VERSION", 0)
77 # Ignore the SOURCES_PATH inside the project file
78 project_data["SOURCES_PATH"] = project_dir
79 if "SOURCES_PATH" in info_dict:
80 project_data["SOURCES_PATH"] = info_dict["SOURCES_PATH"]
81 if os.path.exists(project_data["SOURCES_PATH"]):
82 self.infos["SOURCES_PATH"] = project_data["SOURCES_PATH"]
84 raise VersionException(self)
85 if not isBertosDir(project_dir):
86 version_file = open(os.path.join(const.DATA_DIR, "vtemplates/VERSION"), "r").read()
87 open(os.path.join(project_dir, "VERSION"), "w").write(version_file.replace("$version", "").strip())
89 cpu_name = project_data["CPU_NAME"]
90 self.infos["CPU_NAME"] = cpu_name
91 cpu_info = self.loadCpuInfos()
93 if cpu["CPU_NAME"] == cpu_name:
94 self.infos["CPU_INFOS"] = cpu
96 tag_list = getTagSet(cpu_info)
97 # Create, fill and store the dict with the tags
99 for element in tag_list:
100 tag_dict[element] = False
101 infos = self.info("CPU_INFOS")
103 if tag in infos["CPU_TAGS"] + [infos["CPU_NAME"], infos["TOOLCHAIN"]]:
106 tag_dict[tag] = False
107 self.infos["ALL_CPU_TAGS"] = tag_dict
108 if "TOOLCHAIN" in info_dict:
109 project_data["TOOLCHAIN"] = info_dict["TOOLCHAIN"]
110 if os.path.exists(project_data["TOOLCHAIN"]["path"]):
111 self.infos["TOOLCHAIN"] = project_data["TOOLCHAIN"]
113 raise ToolchainException(self)
114 self.infos["SELECTED_FREQ"] = project_data["SELECTED_FREQ"]
115 self.infos["OUTPUT"] = project_data["OUTPUT"]
116 self.loadModuleData(True)
117 setEnabledModules(self, project_data["ENABLED_MODULES"])
119 def loadProjectPresets(self, preset_directory):
120 preset_prj_file = os.path.join(preset_directory, 'project.bertos')
121 preset_data = pickle.loads(open(preset_prj_file, "r").read())
123 def loadModuleData(self, edit=False):
124 module_info_dict = {}
126 configuration_info_dict = {}
128 for filename, path in self.findDefinitions("*.h") + self.findDefinitions("*.c") + self.findDefinitions("*.s") + self.findDefinitions("*.S"):
129 comment_list = getCommentList(open(path + "/" + filename, "r").read())
130 if len(comment_list) > 0:
132 configuration_info = {}
134 to_be_parsed, module_dict = loadModuleDefinition(comment_list[0])
135 except ParseError, err:
136 raise DefineException.ModuleDefineException(path, err.line_number, err.line)
137 for module, information in module_dict.items():
138 if "depends" not in information:
139 information["depends"] = ()
140 information["depends"] += (filename.split(".")[0],)
141 information["category"] = os.path.basename(path)
142 if "configuration" in information and len(information["configuration"]):
143 configuration = module_dict[module]["configuration"]
145 configuration_info[configuration] = loadConfigurationInfos(self.infos["SOURCES_PATH"] + "/" + configuration)
146 except ParseError, err:
147 raise DefineException.ConfigurationDefineException(self.infos["SOURCES_PATH"] + "/" + configuration, err.line_number, err.line)
150 path = self.infos["PROJECT_NAME"]
151 directory = self.infos["PROJECT_PATH"]
152 user_configuration = loadConfigurationInfos(directory + "/" + configuration.replace("bertos", path))
153 configuration_info[configuration] = updateConfigurationValues(configuration_info[configuration], user_configuration)
154 except ParseError, err:
155 raise DefineException.ConfigurationDefineException(directory + "/" + configuration.replace("bertos", path))
156 module_info_dict.update(module_dict)
157 configuration_info_dict.update(configuration_info)
160 list_dict = loadDefineLists(comment_list[1:])
161 list_info_dict.update(list_dict)
162 except ParseError, err:
163 raise DefineException.EnumDefineException(path, err.line_number, err.line)
164 for filename, path in self.findDefinitions("*_" + self.infos["CPU_INFOS"]["TOOLCHAIN"] + ".h"):
165 comment_list = getCommentList(open(path + "/" + filename, "r").read())
166 list_info_dict.update(loadDefineLists(comment_list))
167 for tag in self.infos["CPU_INFOS"]["CPU_TAGS"]:
168 for filename, path in self.findDefinitions("*_" + tag + ".h"):
169 comment_list = getCommentList(open(path + "/" + filename, "r").read())
170 list_info_dict.update(loadDefineLists(comment_list))
171 self.infos["MODULES"] = module_info_dict
172 self.infos["LISTS"] = list_info_dict
173 self.infos["CONFIGURATIONS"] = configuration_info_dict
174 self.infos["FILES"] = file_dict
176 def loadCpuInfos(self):
178 for definition in self.findDefinitions(const.CPU_DEFINITION):
179 cpuInfos.append(getInfos(definition))
182 def reloadCpuInfo(self):
183 for cpu_info in self.loadCpuInfos():
184 if cpu_info["CPU_NAME"] == self.infos["CPU_NAME"]:
185 self.infos["CPU_INFOS"] = cpu_info
187 def setInfo(self, key, value):
189 Store the given value with the name key.
191 self.infos[key] = value
193 def info(self, key, default=None):
195 Retrieve the value associated with the name key.
197 if key in self.infos:
198 return copy.deepcopy(self.infos[key])
201 def loadSourceTree(self):
202 # Index only the SOURCES_PATH/bertos content
203 bertos_sources_dir = os.path.join(self.info("SOURCES_PATH"), 'bertos')
205 if os.path.exists(bertos_sources_dir):
206 for element in os.walk(bertos_sources_dir):
208 file_dict[f] = file_dict.get(f, []) + [element[0]]
209 self.infos["FILE_DICT"] = file_dict
211 def searchFiles(self, filename):
212 file_dict = self.infos["FILE_DICT"]
213 return [(filename, dirname) for dirname in file_dict.get(filename, [])]
215 def findDefinitions(self, ftype):
216 # Maintain a cache for every scanned SOURCES_PATH
217 definitions_dict = self._cached_queries.get(self.infos["SOURCES_PATH"], {})
218 definitions = definitions_dict.get(ftype, None)
219 if definitions is not None:
221 file_dict = self.infos["FILE_DICT"]
223 for filename in file_dict:
224 if fnmatch.fnmatch(filename, ftype):
225 definitions += [(filename, dirname) for dirname in file_dict.get(filename, [])]
227 # If no cache for the current SOURCES_PATH create an empty one
228 if not definitions_dict:
229 self._cached_queries[self.infos["SOURCES_PATH"]] = {}
230 # Fill the empty cache with the result
231 self._cached_queries[self.infos["SOURCES_PATH"]][ftype] = definitions
235 return repr(self.infos)