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 from bertos_utils import isBertosDir, loadCpuInfos, getTagSet, loadModuleData, setEnabledModules
43 class BProject(object):
45 Simple class for store and retrieve project informations.
48 def __init__(self, project_file="", info_dict={}):
50 self._cached_queries = {}
52 self.loadBertosProject(project_file, info_dict)
54 def loadBertosProject(self, project_file, info_dict):
55 project_dir = os.path.dirname(project_file)
56 project_data = pickle.loads(open(project_file, "r").read())
57 # If PROJECT_NAME is not defined it use the directory name as PROJECT_NAME
58 # NOTE: this can throw an Exception if the user has changed the directory containing the project
59 self.infos["PROJECT_NAME"] = project_data.get("PROJECT_NAME", os.path.basename(project_dir))
60 self.infos["PROJECT_PATH"] = os.path.dirname(project_file)
61 # Check for the Wizard version
62 wizard_version = project_data.get("WIZARD_VERSION", 0)
63 # Ignore the SOURCES_PATH inside the project file
64 project_data["SOURCES_PATH"] = project_dir
65 if "SOURCES_PATH" in info_dict:
66 project_data["SOURCES_PATH"] = info_dict["SOURCES_PATH"]
67 if os.path.exists(project_data["SOURCES_PATH"]):
68 self.infos["SOURCES_PATH"] = project_data["SOURCES_PATH"]
70 raise VersionException(self)
71 if not isBertosDir(project_dir):
72 version_file = open(os.path.join(const.DATA_DIR, "vtemplates/VERSION"), "r").read()
73 open(os.path.join(project_dir, "VERSION"), "w").write(version_file.replace("$version", "").strip())
75 cpu_name = project_data["CPU_NAME"]
76 self.infos["CPU_NAME"] = cpu_name
77 cpu_info = loadCpuInfos(self)
79 if cpu["CPU_NAME"] == cpu_name:
80 self.infos["CPU_INFOS"] = cpu
82 tag_list = getTagSet(cpu_info)
83 # Create, fill and store the dict with the tags
85 for element in tag_list:
86 tag_dict[element] = False
87 infos = self.info("CPU_INFOS")
89 if tag in infos["CPU_TAGS"] + [infos["CPU_NAME"], infos["TOOLCHAIN"]]:
93 self.infos["ALL_CPU_TAGS"] = tag_dict
94 if "TOOLCHAIN" in info_dict:
95 project_data["TOOLCHAIN"] = info_dict["TOOLCHAIN"]
96 if os.path.exists(project_data["TOOLCHAIN"]["path"]):
97 self.infos["TOOLCHAIN"] = project_data["TOOLCHAIN"]
99 raise ToolchainException(self)
100 self.infos["SELECTED_FREQ"] = project_data["SELECTED_FREQ"]
101 self.infos["OUTPUT"] = project_data["OUTPUT"]
102 loadModuleData(self, True)
103 setEnabledModules(self, project_data["ENABLED_MODULES"])
105 def setInfo(self, key, value):
107 Store the given value with the name key.
109 self.infos[key] = value
111 def info(self, key, default=None):
113 Retrieve the value associated with the name key.
115 if key in self.infos:
116 return copy.deepcopy(self.infos[key])
119 def loadSourceTree(self):
120 # Index only the SOURCES_PATH/bertos content
121 bertos_sources_dir = os.path.join(self.info("SOURCES_PATH"), 'bertos')
123 if os.path.exists(bertos_sources_dir):
124 for element in os.walk(bertos_sources_dir):
126 file_dict[f] = file_dict.get(f, []) + [element[0]]
127 self.infos["FILE_DICT"] = file_dict
129 def searchFiles(self, filename):
130 file_dict = self.infos["FILE_DICT"]
131 return [(filename, dirname) for dirname in file_dict.get(filename, [])]
133 def findDefinitions(self, ftype):
134 definitions = self._cached_queries.get(ftype, None)
135 if definitions is not None:
137 file_dict = self.infos["FILE_DICT"]
139 for filename in file_dict:
140 if fnmatch.fnmatch(filename, ftype):
141 definitions += [(filename, dirname) for dirname in file_dict.get(filename, [])]
142 self._cached_queries[ftype] = definitions
146 return repr(self.infos)