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>
40 from bertos_utils import *
42 class BProject(object):
44 Simple class for store and retrieve project informations.
47 def __init__(self, project_file="", info_dict={}):
49 self._cached_queries = {}
51 self.loadBertosProject(project_file, info_dict)
53 def loadBertosProject(self, project_file, info_dict):
54 project_dir = os.path.dirname(project_file)
55 project_data = pickle.loads(open(project_file, "r").read())
56 # If PROJECT_NAME is not defined it use the directory name as PROJECT_NAME
57 # NOTE: this can throw an Exception if the user has changed the directory containing the project
58 self.infos["PROJECT_NAME"] = project_data.get("PROJECT_NAME", os.path.basename(project_dir))
59 self.infos["PROJECT_PATH"] = os.path.dirname(project_file)
60 # Check for the Wizard version
61 wizard_version = project_data.get("WIZARD_VERSION", 0)
62 # Ignore the SOURCES_PATH inside the project file
63 project_data["SOURCES_PATH"] = project_dir
64 if "SOURCES_PATH" in info_dict:
65 project_data["SOURCES_PATH"] = info_dict["SOURCES_PATH"]
66 if os.path.exists(project_data["SOURCES_PATH"]):
67 self.infos["SOURCES_PATH"] = project_data["SOURCES_PATH"]
69 raise VersionException(self)
70 if not isBertosDir(project_dir):
71 version_file = open(os.path.join(const.DATA_DIR, "vtemplates/VERSION"), "r").read()
72 open(os.path.join(project_dir, "VERSION"), "w").write(version_file.replace("$version", "").strip())
74 cpu_name = project_data["CPU_NAME"]
75 self.infos["CPU_NAME"] = cpu_name
76 cpu_info = loadCpuInfos(self)
78 if cpu["CPU_NAME"] == cpu_name:
79 self.infos["CPU_INFOS"] = cpu
81 tag_list = getTagSet(cpu_info)
82 # Create, fill and store the dict with the tags
84 for element in tag_list:
85 tag_dict[element] = False
86 infos = self.info("CPU_INFOS")
88 if tag in infos["CPU_TAGS"] + [infos["CPU_NAME"], infos["TOOLCHAIN"]]:
92 self.infos["ALL_CPU_TAGS"] = tag_dict
93 if "TOOLCHAIN" in info_dict:
94 project_data["TOOLCHAIN"] = info_dict["TOOLCHAIN"]
95 if os.path.exists(project_data["TOOLCHAIN"]["path"]):
96 self.infos["TOOLCHAIN"] = project_data["TOOLCHAIN"]
98 raise ToolchainException(self)
99 self.infos["SELECTED_FREQ"] = project_data["SELECTED_FREQ"]
100 self.infos["OUTPUT"] = project_data["OUTPUT"]
101 loadModuleData(self, True)
102 setEnabledModules(self, project_data["ENABLED_MODULES"])
104 def setInfo(self, key, value):
106 Store the given value with the name key.
108 self.infos[key] = value
110 def info(self, key, default=None):
112 Retrieve the value associated with the name key.
114 if key in self.infos:
115 return copy.deepcopy(self.infos[key])
118 def loadSourceTree(self):
119 # Index only the SOURCES_PATH/bertos content
120 bertos_sources_dir = os.path.join(self.info("SOURCES_PATH"), 'bertos')
122 if os.path.exists(bertos_sources_dir):
123 for element in os.walk(bertos_sources_dir):
125 file_dict[f] = file_dict.get(f, []) + [element[0]]
126 self.infos["FILE_DICT"] = file_dict
128 def searchFiles(self, filename):
129 file_dict = self.infos["FILE_DICT"]
130 return [(filename, dirname) for dirname in file_dict.get(filename, [])]
132 def findDefinitions(self, ftype):
133 definitions = self._cached_queries.get(ftype, None)
134 if definitions is not None:
136 file_dict = self.infos["FILE_DICT"]
138 for filename in file_dict:
139 if fnmatch.fnmatch(filename, ftype):
140 definitions += [(filename, dirname) for dirname in file_dict.get(filename, [])]
141 self._cached_queries[ftype] = definitions
145 return repr(self.infos)