Refactor to use new protocol module and sipo.
[bertos.git] / wizard / toolchain_manager.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 #
32 # Author: Lorenzo Berni <duplo@develer.com>
33 #
34
35 import os
36
37 from PyQt4.QtCore import *
38 from PyQt4.QtGui import *
39
40 import qvariant_converter
41 from toolchain_validation import validateToolchain
42
43
44 class ToolchainManager(object):
45     def __init__(self):
46         self._app_settings = QApplication.instance().settings
47
48     def validateToolchain(self, toolchain):
49         toolchains = self.storedToolchainDict()
50         if toolchain in toolchains:
51             toolchains[toolchain] = True
52             self.setStoredToolchainDict(toolchains)
53             return self._validateToolchain(toolchain)
54         elif toolchain in self._predefined_toolchain_set:
55             return self._validateToolchain(toolchain)
56         else:
57             return None
58
59     def _validateToolchain(self, toolchain):
60         """
61         Returns information about the toolchain located in path
62         "toolchain". If the toolchain is not recognized, or it doesn't
63         exists anymore a empty dict is returned.
64
65         Example of toolchain information dict:
66         {
67             "target": "arm",
68             "version": "4.0.6",
69             "build": None,
70             "configured": None,
71             "thread": None,
72         }
73         """
74         return validateToolchain(toolchain)
75
76     def addToolchain(self, toolchain, validated=False):
77         if toolchain not in self.predefined_toolchains:
78             toolchains = self.storedToolchainDict()
79             toolchains[toolchain] = validated
80             self.setStoredToolchainDict(toolchains)
81
82     def removeToolchain(self, toolchain):
83         toolchains = self.storedToolchainDict()
84         if toolchain in toolchains:
85             del toolchains[toolchain]
86         self.setStoredToolchainDict(toolchains)
87
88     def storedToolchainDict(self):
89         toolchains = self._app_settings.value("toolchains", QVariant())
90         toolchains = qvariant_converter.getBoolDict(toolchains)
91         return toolchains
92
93     def setStoredToolchainDict(self, toolchain_dict):
94         toolchains = qvariant_converter.convertBoolDict(toolchain_dict)
95         self._app_settings.setValue("toolchains", toolchains)
96
97     @property
98     def toolchains(self):
99         toolchains = []
100         toolchain_dict = self.storedToolchainDict()
101         for toolchain, validated in toolchain_dict.items():
102             if validated:
103                 information = self._validateToolchain(toolchain)
104             else:
105                 information = None
106             toolchains.append((toolchain, information))
107         return toolchains
108
109     @property
110     def predefined_toolchains(self):
111         toolchains = []
112         stored_toolchains = self._predefined_toolchain_set
113         for toolchain in stored_toolchains:
114             toolchains.append((toolchain, self._validateToolchain(toolchain)))
115         return toolchains
116
117     @property
118     def _predefined_toolchain_set(self):
119         stored_toolchains = set()
120         if os.name == "nt":
121             import winreg_importer
122             stored_toolchains |= set(winreg_importer.getBertosToolchains())
123         return stored_toolchains
124
125     def suitableToolchains(self, target):
126         toolchains = self.toolchains
127         suitable_toolchains = []
128         for name, info in toolchains:
129             t = info.get("target", None)
130             if t and t.find(target) != -1:
131                 suitable_toolchains.append(name)
132         return suitable_toolchains
133