Update preset.
[bertos.git] / wizard / bertos.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 import sys
37 from distutils.dep_util import newer
38
39 from PyQt4.QtCore import *
40 from PyQt4.QtGui import *
41
42 import exception_handler
43
44 from BProject import BProject
45
46 from BWizard import BWizard
47
48 from BIntroPage import BIntroPage
49 from BFolderPage import BFolderPage
50 from BBoardPage import BBoardPage
51 from BProjectPresets import BProjectPresets
52 from BOpenPage import BOpenPage
53 from BVersionPage import BVersionPage
54 from BCpuPage import BCpuPage
55 from BToolchainPage import BToolchainPage
56 from BModulePage import BModulePage
57 from BCreationPage import BCreationPage
58 from BFinalPage import BFinalPage
59
60 from BEditingDialog import BEditingDialog, BVersionDialog, BToolchainDialog
61
62 from const import DATA_DIR
63
64 from LoadException import VersionException, ToolchainException
65
66 def newProject():
67     QApplication.instance().project = BProject()
68     page_list = [BIntroPage, BFolderPage, BVersionPage, BBoardPage, BProjectPresets, BCpuPage, BToolchainPage, BModulePage, BCreationPage, BFinalPage]
69     wizard = BWizard(page_list)
70     wizard.show()
71     wizard.exec_()
72     project = QApplication.instance().project
73     to_be_opened = project.info("TO_BE_OPENED")
74     command_lines = project.info("COMMAND_LINES")
75     relevant_files = project.info("RELEVANT_FILES")
76     if to_be_opened:
77         for ide in to_be_opened:
78             command_line = command_lines[ide]
79             relevant_file = relevant_files[ide]
80             QProcess.startDetached(command_line, [relevant_file,])
81     sys.exit()
82
83 def editProject(project_file):
84     info_dict = {}
85     # Progress dialog.
86     p_dialog = QProgressDialog(QApplication.instance().tr("Wait project loading"), QApplication.instance().tr("Cancel"), 0, 0)
87     while(True):
88         p_dialog.show()
89         # Empty project is the default fallback.
90         QApplication.instance().project = BProject()
91         try:
92             QApplication.instance().project = BProject(project_file, info_dict)
93         except VersionException:
94             p_dialog.hide()
95             QMessageBox.critical(
96                 None,
97                 QObject().tr("BeRTOS version not found!"),
98                 QObject().tr("The selected BeRTOS version was not found, please select another one...")
99             )
100             dialog = BVersionDialog()
101             if dialog.exec_():
102                 version = dialog.version_page.currentVersion()
103                 info_dict["BERTOS_PATH"] = version
104             continue
105         except ToolchainException, exc:
106             p_dialog.hide()
107             QMessageBox.critical(
108                 None,
109                 QObject().tr("Toolchain not found!"),
110                 QObject().tr("The selected toolchain was not found, please select another one...")
111             )
112             QApplication.instance().project = exc.partial_project
113             dialog = BToolchainDialog()
114             if dialog.exec_():
115                 toolchain = dialog.toolchain_page.currentToolchain()
116                 info_dict["TOOLCHAIN"] = toolchain
117             continue
118         break
119     dialog = BEditingDialog()
120     p_dialog.hide()
121     dialog.exec_()
122
123 def main():
124     app = QApplication(sys.argv)
125     app.settings = QSettings("Develer", "Bertos Configurator")
126     # Development utility lines, to be removed for production
127     datadir = DATA_DIR
128
129     # Something seems to not work, on Windows, using pyrcc4 with BeRTOS Wizard
130     # resources. So I'm restoring the old rcc-based resource generation
131     # system.
132     #
133     # qrc, bertos_rc = os.path.join(datadir, 'bertos.qrc'), os.path.join(datadir, 'bertos_rc.py')
134     # if not (hasattr(sys, "frozen") and sys.frozen) and newer(qrc, bertos_rc):
135     #     os.system("pyrcc4 \"%s\" -o \"%s\"" %(qrc, bertos_rc))
136     # import bertos_rc
137
138     qrc, rcc = os.path.join(datadir, 'bertos.qrc'), os.path.join(datadir, 'bertos.rcc') 
139     if not (hasattr(sys, "frozen") and sys.frozen) and newer(qrc, rcc): 
140         os.system("rcc -binary \"%s\" -o \"%s\"" %(qrc, rcc)) 
141     QResource.registerResource(rcc) 
142     
143     if len(sys.argv) == 3 and sys.argv[1] == "--edit":
144         editProject(os.path.abspath(sys.argv[2]))
145     else:
146         newProject()
147
148
149 if __name__ == '__main__':
150     main()
151