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