Add implementation for the isComplete method override.
[bertos.git] / wizard / BProject.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 copy
39 import pickle
40
41 import DefineException
42
43 from LoadException import VersionException, ToolchainException
44
45 import const
46
47 from bertos_utils import (
48                             # Utility functions
49                             isBertosDir, getTagSet, setEnabledModules, getInfos,
50                             loadConfigurationInfos, loadDefineLists, loadModuleDefinition,
51                             getCommentList, updateConfigurationValues,
52
53                             # Custom exceptions
54                             ParseError, SupportedException
55                         )
56
57 class BProject(object):
58     """
59     Simple class for store and retrieve project informations.
60     """
61
62     def __init__(self, project_file="", info_dict={}):
63         self.infos = {}
64         self._cached_queries = {}
65         if project_file:
66             self.loadBertosProject(project_file, info_dict)
67
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"]
83         else:
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())
88         self.loadSourceTree()
89         cpu_name = project_data["CPU_NAME"]
90         self.infos["CPU_NAME"] = cpu_name
91         cpu_info = self.loadCpuInfos()
92         for cpu in cpu_info:
93             if cpu["CPU_NAME"] == cpu_name:
94                 self.infos["CPU_INFOS"] = cpu
95                 break
96         tag_list = getTagSet(cpu_info)
97         # Create, fill and store the dict with the tags
98         tag_dict = {}
99         for element in tag_list:
100             tag_dict[element] = False
101         infos = self.info("CPU_INFOS")
102         for tag in tag_dict:
103             if tag in infos["CPU_TAGS"] + [infos["CPU_NAME"], infos["TOOLCHAIN"]]:
104                 tag_dict[tag] = True
105             else:
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"]
112         else:
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"])
118
119     def loadProjectPresets(self):
120         """
121         Load the default presets (into the const.PREDEFINED_BOARDS_DIR).
122         """
123         # NOTE: this method does nothing (for now).
124         preset_path = os.path.join(self.infos["SOURCES_PATH"], const.PREDEFINED_BOARDS_DIR)
125         preset_tree = {}
126         if os.path.exists(preset_path):
127             preset_tree = self._loadProjectPresetTree(preset_path)
128         self.infos["PRESET_TREE"] = preset_tree
129
130     def _loadProjectPresetTree(self, path):
131         _tree = {}
132         _tree['info'] = self._loadPresetInfo(os.path.join(path, const.PREDEFINED_BOARD_SPEC_FILE))
133         _tree['info']['filename'] = os.path.basename(path)
134         _tree['info']['path'] = path
135         _tree['children'] = []
136         entries = set(os.listdir(path))
137         for entry in entries:
138             _path = os.path.join(path, entry)
139             if os.path.isdir(_path):
140                 sub_entries = set(os.listdir(_path))
141                 if const.PREDEFINED_BOARD_SPEC_FILE in sub_entries:
142                     _tree['children'].append(self._loadProjectPresetTree(_path))
143         # Add into the info dict the dir type (dir/project)
144         if _tree['children']:
145             _tree['info']['type'] = 'dir'
146         else:
147             _tree['info']['type'] = 'project'
148         return _tree
149
150     def _loadPresetInfo(self, preset_spec_file):
151         D = {}
152         execfile(preset_spec_file, {}, D)
153         return D
154
155     def loadModuleData(self, edit=False):
156         module_info_dict = {}
157         list_info_dict = {}
158         configuration_info_dict = {}
159         file_dict = {}
160         for filename, path in self.findDefinitions("*.h") + self.findDefinitions("*.c") + self.findDefinitions("*.s") + self.findDefinitions("*.S"):
161             comment_list = getCommentList(open(path + "/" + filename, "r").read())
162             if len(comment_list) > 0:
163                 module_info = {}
164                 configuration_info = {}
165                 try:
166                     to_be_parsed, module_dict = loadModuleDefinition(comment_list[0])
167                 except ParseError, err:
168                     raise DefineException.ModuleDefineException(path, err.line_number, err.line)
169                 for module, information in module_dict.items():
170                     if "depends" not in information:
171                         information["depends"] = ()
172                     information["depends"] += (filename.split(".")[0],)
173                     information["category"] = os.path.basename(path)
174                     if "configuration" in information and len(information["configuration"]):
175                         configuration = module_dict[module]["configuration"]
176                         try:
177                             configuration_info[configuration] = loadConfigurationInfos(self.infos["SOURCES_PATH"] + "/" + configuration)
178                         except ParseError, err:
179                             raise DefineException.ConfigurationDefineException(self.infos["SOURCES_PATH"] + "/" + configuration, err.line_number, err.line)
180                         if edit:
181                             try:
182                                 path = self.infos["PROJECT_NAME"]
183                                 directory = self.infos["PROJECT_PATH"]
184                                 user_configuration = loadConfigurationInfos(directory + "/" + configuration.replace("bertos", path))
185                                 configuration_info[configuration] = updateConfigurationValues(configuration_info[configuration], user_configuration)
186                             except ParseError, err:
187                                 raise DefineException.ConfigurationDefineException(directory + "/" + configuration.replace("bertos", path))
188                 module_info_dict.update(module_dict)
189                 configuration_info_dict.update(configuration_info)
190                 if to_be_parsed:
191                     try:
192                         list_dict = loadDefineLists(comment_list[1:])
193                         list_info_dict.update(list_dict)
194                     except ParseError, err:
195                         raise DefineException.EnumDefineException(path, err.line_number, err.line)
196         for tag in self.infos["CPU_INFOS"]["CPU_TAGS"]:
197             for filename, path in self.findDefinitions("*_" + tag + ".h"):
198                 comment_list = getCommentList(open(path + "/" + filename, "r").read())
199                 list_info_dict.update(loadDefineLists(comment_list))
200         self.infos["MODULES"] = module_info_dict
201         self.infos["LISTS"] = list_info_dict
202         self.infos["CONFIGURATIONS"] = configuration_info_dict
203         self.infos["FILES"] = file_dict
204
205     def loadCpuInfos(self):
206         cpuInfos = []
207         for definition in self.findDefinitions(const.CPU_DEFINITION):
208             cpuInfos.append(getInfos(definition))
209         return cpuInfos
210
211     def reloadCpuInfo(self):
212         for cpu_info in self.loadCpuInfos():
213             if cpu_info["CPU_NAME"] == self.infos["CPU_NAME"]:
214                 self.infos["CPU_INFOS"] = cpu_info
215
216     def setInfo(self, key, value):
217         """
218         Store the given value with the name key.
219         """
220         self.infos[key] = value
221
222     def info(self, key, default=None):
223         """
224         Retrieve the value associated with the name key.
225         """
226         if key in self.infos:
227             return copy.deepcopy(self.infos[key])
228         return default
229
230     def loadSourceTree(self):
231         # Index only the SOURCES_PATH/bertos content
232         bertos_sources_dir = os.path.join(self.info("SOURCES_PATH"), 'bertos')
233         file_dict = {}
234         if os.path.exists(bertos_sources_dir):
235             for element in os.walk(bertos_sources_dir):
236                 for f in element[2]:
237                     file_dict[f] = file_dict.get(f, []) + [element[0]]
238         self.infos["FILE_DICT"] = file_dict
239
240     def searchFiles(self, filename):
241         file_dict = self.infos["FILE_DICT"]
242         return [(filename, dirname) for dirname in file_dict.get(filename, [])]
243
244     def findDefinitions(self, ftype):
245         # Maintain a cache for every scanned SOURCES_PATH
246         definitions_dict = self._cached_queries.get(self.infos["SOURCES_PATH"], {})
247         definitions = definitions_dict.get(ftype, None)
248         if definitions is not None:
249             return definitions
250         file_dict = self.infos["FILE_DICT"]
251         definitions = []
252         for filename in file_dict:
253             if fnmatch.fnmatch(filename, ftype):
254                 definitions += [(filename, dirname) for dirname in file_dict.get(filename, [])]
255
256         # If no cache for the current SOURCES_PATH create an empty one
257         if not definitions_dict:
258             self._cached_queries[self.infos["SOURCES_PATH"]] = {}
259         # Fill the empty cache with the result
260         self._cached_queries[self.infos["SOURCES_PATH"]][ftype] = definitions
261         return definitions
262
263     def __repr__(self):
264         return repr(self.infos)