|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2010 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the CxFreeze plugin. |
|
8 """ |
|
9 |
|
10 import os |
|
11 import sys |
|
12 |
|
13 from PyQt4.QtCore import QObject, SIGNAL, QTranslator, QCoreApplication |
|
14 from PyQt4.QtGui import QDialog, QMessageBox |
|
15 |
|
16 from E5Gui.E5Action import E5Action |
|
17 from E5Gui.E5Application import e5App |
|
18 |
|
19 from CxFreeze.CxfreezeConfigDialog import CxfreezeConfigDialog |
|
20 from CxFreeze.CxfreezeExecDialog import CxfreezeExecDialog |
|
21 |
|
22 import Utilities |
|
23 |
|
24 # Start-of-Header |
|
25 name = "CxFreeze Plugin" |
|
26 author = "Detlev Offenbach <detlev@die-offenbachs.de>" |
|
27 autoactivate = True |
|
28 deactivateable = True |
|
29 version = "5.0.0" |
|
30 className = "CxFreezePlugin" |
|
31 packageName = "CxFreeze" |
|
32 shortDescription = "Show the CxFreeze dialogs." |
|
33 longDescription = """This plugin implements the CxFreeze dialogs.""" \ |
|
34 """ CxFreeze is used to generate a distribution package.""" |
|
35 needsRestart = False |
|
36 pyqtApi = 2 |
|
37 # End-of-Header |
|
38 |
|
39 error = "" |
|
40 |
|
41 def exeDisplayData(): |
|
42 """ |
|
43 Public method to support the display of some executable info. |
|
44 |
|
45 @return dictionary containing the data to query the presence of |
|
46 the executable |
|
47 """ |
|
48 data = { |
|
49 "programEntry" : True, |
|
50 "header" : QCoreApplication.translate("CxFreezePlugin", |
|
51 "Packagers - cx_freeze"), |
|
52 "exe" : 'dummyfreeze', |
|
53 "versionCommand" : '--version', |
|
54 "versionStartsWith" : 'dummyfreeze', |
|
55 "versionPosition" : -1, |
|
56 "version" : "", |
|
57 "versionCleanup" : None, |
|
58 } |
|
59 |
|
60 exe = _findExecutable() |
|
61 if exe: |
|
62 data["exe"] = exe |
|
63 if exe.startswith("FreezePython"): |
|
64 data["versionStartsWith"] = "FreezePython" |
|
65 elif exe.startswith("cxfreeze"): |
|
66 data["versionStartsWith"] = "cxfreeze" |
|
67 |
|
68 return data |
|
69 |
|
70 def _findExecutable(): |
|
71 """ |
|
72 Restricted function to determine the name of the executable. |
|
73 |
|
74 @return name of the executable (string) |
|
75 """ |
|
76 # step 1: check for version 4.x |
|
77 exe = 'cxfreeze' |
|
78 if sys.platform == "win32": |
|
79 exe += '.bat' |
|
80 if Utilities.isinpath(exe): |
|
81 return exe |
|
82 |
|
83 return None |
|
84 |
|
85 def _checkProgram(): |
|
86 """ |
|
87 Restricted function to check the availability of cxfreeze. |
|
88 |
|
89 @return flag indicating availability (boolean) |
|
90 """ |
|
91 global error |
|
92 |
|
93 if _findExecutable() is None: |
|
94 error = QCoreApplication.translate("CxFreezePlugin", |
|
95 "The cxfreeze executable could not be found.") |
|
96 return False |
|
97 else: |
|
98 return True |
|
99 _checkProgram() |
|
100 |
|
101 class CxFreezePlugin(QObject): |
|
102 """ |
|
103 Class implementing the CxFreeze plugin. |
|
104 """ |
|
105 def __init__(self, ui): |
|
106 """ |
|
107 Constructor |
|
108 |
|
109 @param ui reference to the user interface object (UI.UserInterface) |
|
110 """ |
|
111 QObject.__init__(self, ui) |
|
112 self.__ui = ui |
|
113 self.__initialize() |
|
114 _checkProgram() |
|
115 |
|
116 self.__translator = None |
|
117 self.__loadTranslator() |
|
118 |
|
119 def __initialize(self): |
|
120 """ |
|
121 Private slot to (re)initialize the plugin. |
|
122 """ |
|
123 self.__projectAct = None |
|
124 |
|
125 def activate(self): |
|
126 """ |
|
127 Public method to activate this plugin. |
|
128 |
|
129 @return tuple of None and activation status (boolean) |
|
130 """ |
|
131 global error |
|
132 |
|
133 # cxfreeze is only activated if it is available |
|
134 if not _checkProgram(): |
|
135 return None, False |
|
136 |
|
137 menu = e5App().getObject("Project").getMenu("Packagers") |
|
138 if menu: |
|
139 self.__projectAct = E5Action(self.trUtf8('Use cx_freeze'), |
|
140 self.trUtf8('Use cx_&freeze'), 0, 0, |
|
141 self, 'packagers_cxfreeze') |
|
142 self.__projectAct.setStatusTip( |
|
143 self.trUtf8('Generate a distribution package using cx_freeze')) |
|
144 self.__projectAct.setWhatsThis(self.trUtf8( |
|
145 """<b>Use cx_freeze</b>""" |
|
146 """<p>Generate a distribution package using cx_freeze.""" |
|
147 """ The command is executed in the project path. All""" |
|
148 """ files and directories must be given absolute or""" |
|
149 """ relative to the project directory.</p>""" |
|
150 )) |
|
151 self.connect(self.__projectAct, SIGNAL('triggered()'), self.__cxfreeze) |
|
152 e5App().getObject("Project").addE5Actions([self.__projectAct]) |
|
153 menu.addAction(self.__projectAct) |
|
154 |
|
155 error = "" |
|
156 return None, True |
|
157 |
|
158 def deactivate(self): |
|
159 """ |
|
160 Public method to deactivate this plugin. |
|
161 """ |
|
162 menu = e5App().getObject("Project").getMenu("Packagers") |
|
163 if menu: |
|
164 if self.__projectAct: |
|
165 menu.removeAction(self.__projectAct) |
|
166 e5App().getObject("Project").removeE5Actions([self.__projectAct]) |
|
167 self.__initialize() |
|
168 |
|
169 def __loadTranslator(self): |
|
170 """ |
|
171 Private method to load the translation file. |
|
172 """ |
|
173 if self.__ui is not None: |
|
174 loc = self.__ui.getLocale() |
|
175 if loc and loc != "C": |
|
176 locale_dir = \ |
|
177 os.path.join(os.path.dirname(__file__), "CxFreeze", "i18n") |
|
178 translation = "cxfreeze_%s" % loc |
|
179 translator = QTranslator(None) |
|
180 loaded = translator.load(translation, locale_dir) |
|
181 if loaded: |
|
182 self.__translator = translator |
|
183 e5App().installTranslator(self.__translator) |
|
184 else: |
|
185 print("Warning: translation file '%s' could not be loaded." % \ |
|
186 translation) |
|
187 print("Using default.") |
|
188 |
|
189 def __cxfreeze(self): |
|
190 """ |
|
191 Private slot to handle the cxfreeze execution. |
|
192 """ |
|
193 project = e5App().getObject("Project") |
|
194 if len(project.pdata["MAINSCRIPT"]) == 0: |
|
195 # no main script defined |
|
196 QMessageBox.critical(None, |
|
197 self.trUtf8("cxfreeze"), |
|
198 self.trUtf8( |
|
199 """There is no main script defined for the current project."""), |
|
200 QMessageBox.StandardButtons( |
|
201 QMessageBox.Abort)) |
|
202 return |
|
203 |
|
204 parms = project.getData('PACKAGERSPARMS', "CXFREEZE") |
|
205 exe = _findExecutable() |
|
206 if exe is None: |
|
207 QMessageBox.critical(None, |
|
208 self.trUtf8("cxfreeze"), |
|
209 self.trUtf8("""The cxfreeze executable could not be found.""")) |
|
210 return |
|
211 |
|
212 dlg = CxfreezeConfigDialog(project, exe, parms) |
|
213 if dlg.exec_() == QDialog.Accepted: |
|
214 args, parms = dlg.generateParameters() |
|
215 project.setData('PACKAGERSPARMS', "CXFREEZE", parms) |
|
216 |
|
217 # now do the call |
|
218 dia = CxfreezeExecDialog("cxfreeze") |
|
219 dia.show() |
|
220 res = dia.start(args, |
|
221 os.path.join(project.ppath, project.pdata["MAINSCRIPT"][0])) |
|
222 if res: |
|
223 dia.exec_() |