697355e8ddb859db43bb2c4cc41fd60e27b731b8
[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
40 from bertos_utils import *
41
42 class BProject(object):
43     """
44     Simple class for store and retrieve project informations.
45     """
46     
47     def __init__(self, project_file="", info_dict={}):
48         self.infos = {}
49         self._cached_queries = {}
50         if project_file:
51             self.loadBertosProject(project_file, info_dict)
52
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"]
68         else:
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())
73         self.loadSourceTree()
74         cpu_name = project_data["CPU_NAME"]
75         self.infos["CPU_NAME"] = cpu_name
76         cpu_info = loadCpuInfos(self)
77         for cpu in cpu_info:
78             if cpu["CPU_NAME"] == cpu_name:
79                 self.infos["CPU_INFOS"] = cpu
80                 break
81         tag_list = getTagSet(cpu_info)
82         # Create, fill and store the dict with the tags
83         tag_dict = {}
84         for element in tag_list:
85             tag_dict[element] = False
86         infos = self.info("CPU_INFOS")
87         for tag in tag_dict:
88             if tag in infos["CPU_TAGS"] + [infos["CPU_NAME"], infos["TOOLCHAIN"]]:
89                 tag_dict[tag] = True
90             else:
91                 tag_dict[tag] = False
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"]
97         else:
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"])
103
104     def setInfo(self, key, value):
105         """
106         Store the given value with the name key.
107         """
108         self.infos[key] = value
109     
110     def info(self, key, default=None):
111         """
112         Retrieve the value associated with the name key.
113         """
114         if key in self.infos:
115             return copy.deepcopy(self.infos[key])
116         return default
117
118     def loadSourceTree(self):
119         # Index only the SOURCES_PATH/bertos content
120         bertos_sources_dir = os.path.join(self.info("SOURCES_PATH"), 'bertos')
121         file_dict = {}
122         if os.path.exists(bertos_sources_dir):
123             for element in os.walk(bertos_sources_dir):
124                 for f in element[2]:
125                     file_dict[f] = file_dict.get(f, []) + [element[0]]
126         self.infos["FILE_DICT"] = file_dict
127
128     def searchFiles(self, filename):
129         file_dict = self.infos["FILE_DICT"]
130         return [(filename, dirname) for dirname in file_dict.get(filename, [])]
131
132     def findDefinitions(self, ftype):
133         definitions = self._cached_queries.get(ftype, None)
134         if definitions is not None:
135             return definitions
136         file_dict = self.infos["FILE_DICT"]
137         definitions = []
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
142         return definitions
143
144     def __repr__(self):
145         return repr(self.infos)