-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPluginCxFreeze.py
More file actions
390 lines (337 loc) · 13.1 KB
/
PluginCxFreeze.py
File metadata and controls
390 lines (337 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
# -*- coding: utf-8 -*-
# Copyright (c) 2010 - 2020 Detlev Offenbach <detlev@die-offenbachs.de>
#
"""
Module implementing the CxFreeze plugin.
"""
import os
import platform
from PyQt5.QtCore import QObject, QTranslator, QCoreApplication, QProcess
from PyQt5.QtWidgets import QDialog
from E5Gui import E5MessageBox
from E5Gui.E5Action import E5Action
from E5Gui.E5Application import e5App
import Utilities
# Start-of-Header
name = "CxFreeze Plugin"
author = "Detlev Offenbach <detlev@die-offenbachs.de>"
autoactivate = True
deactivateable = True
version = "7.0.0"
className = "CxFreezePlugin"
packageName = "CxFreeze"
shortDescription = "Show the CxFreeze dialogs."
longDescription = (
"""This plugin implements the CxFreeze dialogs."""
""" CxFreeze is used to generate a distribution package."""
)
needsRestart = False
pyqtApi = 2
# End-of-Header
error = ""
exePy3 = []
def exeDisplayDataList():
"""
Public method to support the display of some executable info.
@return dictionary containing the data to query the presence of
the executable
"""
dataList = []
data = {
"programEntry": True,
"header": QCoreApplication.translate(
"CxFreezePlugin", "Packagers - cx_freeze"),
"exe": 'dummyfreeze',
"versionCommand": '--version',
"versionStartsWith": 'dummyfreeze',
"versionPosition": -1,
"version": "",
"versionCleanup": None,
}
if _checkProgram():
for exePath in exePy3:
data["exe"] = exePath
data["versionStartsWith"] = "cxfreeze"
dataList.append(data.copy())
else:
dataList.append(data)
return dataList
def _findExecutable(majorVersion):
"""
Restricted function to determine the names of the executable.
@param majorVersion major python version of the executables (int)
@return names of the executable (list)
"""
# Determine Python Version
if majorVersion == 3:
minorVersions = range(10)
else:
return []
executables = set()
if Utilities.isWindowsPlatform():
#
# Windows
#
try:
import winreg
except ImportError:
import _winreg as winreg # __IGNORE_WARNING__
def getExePath(branch, access, versionStr):
try:
software = winreg.OpenKey(branch, 'Software', 0, access)
python = winreg.OpenKey(software, 'Python', 0, access)
pcore = winreg.OpenKey(python, 'PythonCore', 0, access)
version = winreg.OpenKey(pcore, versionStr, 0, access)
installpath = winreg.QueryValue(version, 'InstallPath')
exe = os.path.join(installpath, 'Scripts', 'cxfreeze.bat')
if os.access(exe, os.X_OK):
return exe
except WindowsError: # __IGNORE_WARNING__
return None
return None
versionSuffixes = ["", "-32", "-64"]
for minorVersion in minorVersions:
for versionSuffix in versionSuffixes:
versionStr = '{0}.{1}{2}'.format(majorVersion, minorVersion,
versionSuffix)
exePath = getExePath(
winreg.HKEY_CURRENT_USER,
winreg.KEY_WOW64_32KEY | winreg.KEY_READ, versionStr)
if exePath is not None:
executables.add(exePath)
exePath = getExePath(
winreg.HKEY_LOCAL_MACHINE,
winreg.KEY_WOW64_32KEY | winreg.KEY_READ, versionStr)
if exePath is not None:
executables.add(exePath)
# Even on Intel 64-bit machines it's 'AMD64'
if platform.machine() == 'AMD64':
exePath = getExePath(
winreg.HKEY_CURRENT_USER,
winreg.KEY_WOW64_64KEY | winreg.KEY_READ, versionStr)
if exePath is not None:
executables.add(exePath)
exePath = getExePath(
winreg.HKEY_LOCAL_MACHINE,
winreg.KEY_WOW64_64KEY | winreg.KEY_READ, versionStr)
if exePath is not None:
executables.add(exePath)
if not executables and majorVersion >= 3:
# check the PATH environment variable if nothing was found
# Python 3 only
path = Utilities.getEnvironmentEntry('PATH')
if path:
dirs = path.split(os.pathsep)
for directory in dirs:
for suffix in (".bat", ".exe"):
exe = os.path.join(directory, "cxfreeze" + suffix)
if os.access(exe, os.X_OK):
executables.add(exe)
else:
#
# Linux, Unix ...
cxfreezeScript = 'cxfreeze'
scriptSuffixes = ["", "-python{0}".format(majorVersion)]
for minorVersion in minorVersions:
scriptSuffixes.append(
"-python{0}.{1}".format(majorVersion, minorVersion))
# There could be multiple cxfreeze executables in the path
# e.g. for different python variants
path = Utilities.getEnvironmentEntry('PATH')
# environment variable not defined
if path is None:
return []
# step 1: determine possible candidates
exes = []
dirs = path.split(os.pathsep)
for directory in dirs:
for suffix in scriptSuffixes:
exe = os.path.join(directory, cxfreezeScript + suffix)
if os.access(exe, os.X_OK):
exes.append(exe)
# step 2: determine the Python variant
_exePy3 = set()
versionArgs = ["-c", "import sys; print(sys.version_info[0])"]
for exe in exes:
try:
f = open(exe, "r")
line0 = f.readline()
program = line0.replace("#!", "").strip()
process = QProcess()
process.start(program, versionArgs)
process.waitForFinished(5000)
# get a QByteArray of the output
versionBytes = process.readAllStandardOutput()
versionStr = str(versionBytes, encoding='utf-8').strip()
if versionStr == "3":
_exePy3.add(exe)
finally:
f.close()
executables = _exePy3
# sort items, the probably newest topmost
executables = list(executables)
executables.sort(reverse=True)
return executables
def _checkProgram():
"""
Restricted function to check the availability of cxfreeze.
@return flag indicating availability (boolean)
"""
global error, exePy3
exePy3 = _findExecutable(3)
if exePy3 == []:
if Utilities.isWindowsPlatform():
error = QCoreApplication.translate(
"CxFreezePlugin",
"The cxfreeze.bat executable could not be found."
"Did you run the cxfreeze-postinstall script?")
else:
error = QCoreApplication.translate(
"CxFreezePlugin",
"The cxfreeze executable could not be found.")
return False
else:
return True
class CxFreezePlugin(QObject):
"""
Class implementing the CxFreeze plugin.
"""
def __init__(self, ui):
"""
Constructor
@param ui reference to the user interface object (UI.UserInterface)
"""
QObject.__init__(self, ui)
self.__ui = ui
self.__initialize()
_checkProgram()
self.__translator = None
self.__loadTranslator()
def __initialize(self):
"""
Private slot to (re)initialize the plugin.
"""
self.__projectAct = None
self.__projectSeparator = None
def activate(self):
"""
Public method to activate this plugin.
@return tuple of None and activation status (boolean)
"""
global error
# There is already an error, don't activate
if error:
return None, False
# cxfreeze is only activated if it is available
if not _checkProgram():
return None, False
project = e5App().getObject("Project")
menu = project.getMenu("Packagers")
if menu:
self.__projectAct = E5Action(
self.tr('Use cx_freeze'),
self.tr('Use cx_&freeze'), 0, 0,
self, 'packagers_cxfreeze')
self.__projectAct.setStatusTip(
self.tr('Generate a distribution package using cx_freeze'))
self.__projectAct.setWhatsThis(self.tr(
"""<b>Use cx_freeze</b>"""
"""<p>Generate a distribution package using cx_freeze."""
""" The command is executed in the project path. All"""
""" files and directories must be given absolute or"""
""" relative to the project directory.</p>"""
))
self.__projectAct.triggered.connect(self.__cxfreeze)
project.addE5Actions([self.__projectAct])
self.__projectSeparator = menu.addSeparator()
menu.addAction(self.__projectAct)
project.showMenu.connect(self.__projectShowMenu)
error = ""
return None, True
def deactivate(self):
"""
Public method to deactivate this plugin.
"""
menu = e5App().getObject("Project").getMenu("Packagers")
if menu:
if self.__projectAct:
menu.removeAction(self.__projectAct)
e5App().getObject("Project").removeE5Actions(
[self.__projectAct])
if self.__projectSeparator:
menu.removeAction(self.__projectSeparator)
self.__initialize()
def __projectShowMenu(self, menuName, menu):
"""
Private slot called, when the the project menu or a submenu is
about to be shown.
@param menuName name of the menu to be shown (string)
@param menu reference to the menu (QMenu)
"""
if menuName == "Packagers":
if self.__projectAct is not None:
self.__projectAct.setEnabled(
e5App().getObject("Project").getProjectLanguage() ==
"Python3"
)
def __loadTranslator(self):
"""
Private method to load the translation file.
"""
if self.__ui is not None:
loc = self.__ui.getLocale()
if loc and loc != "C":
locale_dir = os.path.join(os.path.dirname(__file__),
"CxFreeze", "i18n")
translation = "cxfreeze_{0}".format(loc)
translator = QTranslator(None)
loaded = translator.load(translation, locale_dir)
if loaded:
self.__translator = translator
e5App().installTranslator(self.__translator)
else:
print("Warning: translation file '{0}' could not be"
" loaded.".format(translation))
print("Using default.")
def __cxfreeze(self):
"""
Private slot to handle the cxfreeze execution.
"""
project = e5App().getObject("Project")
if not project.getMainScript():
# no main script defined
E5MessageBox.critical(
self.__ui,
self.tr("cxfreeze"),
self.tr(
"""There is no main script defined for the current"""
""" project."""),
E5MessageBox.StandardButtons(E5MessageBox.Abort))
return
majorVersionStr = project.getProjectLanguage()
exe = {"Python3": exePy3}.get(majorVersionStr)
if exe == []:
E5MessageBox.critical(
self.__ui,
self.tr("cxfreeze"),
self.tr("""The cxfreeze executable could not be found."""))
return
# check if all files saved and errorfree before continue
if not project.checkAllScriptsDirty(reportSyntaxErrors=True):
return
from CxFreeze.CxfreezeConfigDialog import CxfreezeConfigDialog
parms = project.getData('PACKAGERSPARMS', "CXFREEZE")
dlg = CxfreezeConfigDialog(project, exe, parms)
if dlg.exec_() == QDialog.Accepted:
args, parms = dlg.generateParameters()
project.setData('PACKAGERSPARMS', "CXFREEZE", parms)
# now do the call
from CxFreeze.CxfreezeExecDialog import CxfreezeExecDialog
dia = CxfreezeExecDialog("cxfreeze")
dia.show()
res = dia.start(args, parms, project.ppath,
project.getMainScript())
if res:
dia.exec_()
#
# eflag: noqa = M801