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