forked from mupen64plus/mupen64plus-ui-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.py
435 lines (366 loc) · 15.6 KB
/
setup.py
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
#!/usr/bin/env python
import fnmatch
import glob
import os
import shutil
import subprocess
import sys
import tempfile
import urllib
import zipfile
import distutils
import distutils.command.build as distutils_build
import distutils.command.clean as distutils_clean
import setuptools
# Add the src folder to the path
sys.path.insert(0, os.path.realpath("src"))
from m64py.core.defs import FRONTEND_VERSION
BASE_DIR = os.path.dirname(os.path.realpath(__file__))
class BuildQt(setuptools.Command):
description = "Build the QT interface"
boolean_options = []
user_options = []
def initialize_options(self):
pass
def finalize_options(self):
pass
def compile_rc(self, qrc_file):
import PySide2
py_file = os.path.splitext(qrc_file)[0] + "_rc.py"
if not distutils.dep_util.newer(qrc_file, py_file):
return
origpath = os.getenv("PATH")
path = origpath.split(os.pathsep)
path.append(os.path.dirname(PySide2.__file__))
os.putenv("PATH", os.pathsep.join(path))
if subprocess.call(["pyside2-rcc", qrc_file, "-o", py_file]) > 0:
self.warn("Unable to compile resource file {}".format(qrc_file))
if not os.path.exists(py_file):
sys.exit(1)
os.putenv("PATH", origpath)
def compile_ui(self, ui_file):
from pyside2uic import compileUi
py_file = os.path.splitext(ui_file)[0] + "_ui.py"
if not distutils.dep_util.newer(ui_file, py_file):
return
with open(py_file, "w") as a_file:
compileUi(ui_file, a_file, from_imports=True)
def run(self):
basepath = os.path.join(os.path.dirname(__file__), "src", "m64py", "ui")
for dirpath, _, filenames in os.walk(basepath):
for filename in filenames:
if filename.endswith('.ui'):
self.compile_ui(os.path.join(dirpath, filename))
elif filename.endswith('.qrc'):
self.compile_rc(os.path.join(dirpath, filename))
def set_rthook():
import PyInstaller
hook_file = ""
module_dir = os.path.dirname(PyInstaller.__file__)
rthook = os.path.join(module_dir, "loader", "rthooks", "pyi_rth_qt5plugins.py")
with open(rthook, "r") as hook:
data = hook.read()
if 'sys._MEIPASS, "lib"' not in data:
lines = data.split("\n")
for line in lines:
if "MEIPASS" in line:
hook_file += "d = os.path.join(sys._MEIPASS, \"lib\", d)\n"
else:
hook_file += line + "\n"
with open(rthook, "w") as hook:
hook.write(hook_file)
class BuildDmg(setuptools.Command):
description = "Generate a .dmg file for distribution"
user_options = []
dist_dir = os.path.join(BASE_DIR, "dist", "macosx")
def initialize_options(self):
pass
def finalize_options(self):
pass
def copy_emulator(self):
src_path = os.path.join(self.dist_dir, "mupen64plus", "Contents")
dest_path = os.path.join(self.dist_dir, "dmg", "M64Py.app", "Contents")
distutils.dir_util.copy_tree(src_path, dest_path)
def copy_files(self):
dest_path = os.path.join(self.dist_dir, "dmg")
if not os.path.exists(dest_path):
os.mkdir(dest_path)
shutil.move(os.path.join(self.dist_dir, "M64Py.app"), dest_path)
for file_name in ["AUTHORS", "ChangeLog", "COPYING", "LICENSES", "README.rst"]:
shutil.copy(os.path.join(BASE_DIR, file_name), dest_path)
shutil.copy(os.path.join(BASE_DIR, "test", "mupen64plus.v64"), dest_path)
def remove_files(self):
dest_path = os.path.join(self.dist_dir, "dmg", "M64Py.app", "Contents", "MacOS")
for dir_name in ["include", "lib"]:
shutil.rmtree(os.path.join(dest_path, dir_name), True)
os.remove(os.path.join(self.dist_dir, "dmg", "M64Py.app", "Contents",
"Resources", "icon-windowed.icns"))
def run_build(self):
import PyInstaller.building.build_main
work_path = os.path.join(self.dist_dir, "build")
spec_file = os.path.join(self.dist_dir, "m64py.spec")
os.environ["BASE_DIR"] = BASE_DIR
os.environ["DIST_DIR"] = self.dist_dir
opts = {"distpath": self.dist_dir,
"workpath": work_path,
"clean_build": True,
"upx_dir": None,
"debug": False}
PyInstaller.building.build_main.main(None, spec_file, True, **opts)
def run_build_dmg(self):
src_path = os.path.join(self.dist_dir, "dmg")
dst_path = os.path.join(self.dist_dir, "m64py-{}.dmg".format(FRONTEND_VERSION))
subprocess.call(["hdiutil", "create", dst_path, "-srcfolder", src_path])
def set_plist(self):
info_plist = os.path.join(self.dist_dir, "dmg", "M64Py.app", "Contents", "Info.plist")
shutil.copy(os.path.join(self.dist_dir, "m64py.icns"),
os.path.join(self.dist_dir, "dmg", "M64Py.app", "Contents", "Resources"))
shutil.copy(os.path.join(self.dist_dir, "m64py.sh"),
os.path.join(self.dist_dir, "dmg", "M64Py.app", "Contents", "MacOS"))
with open(info_plist, "r") as opts:
data = opts.read()
plist_file = ""
lines = data.split("\n")
for line in lines:
if "0.0.0" in line:
line = line.replace("0.0.0", FRONTEND_VERSION)
elif "icon-windowed.icns" in line:
line = line.replace("icon-windowed.icns", "m64py.icns")
elif "MacOS/m64py" in line:
line = line.replace("MacOS/m64py", "m64py.sh")
plist_file += line + "\n"
with open(info_plist, "w") as opts:
opts.write(plist_file)
def run(self):
self.run_command("build_qt")
self.run_build()
self.copy_files()
self.copy_emulator()
self.remove_files()
self.set_plist()
self.run_build_dmg()
class BuildExe(setuptools.Command):
"""
Requires PySide2, rarfile, PyLZMA, PyWin32, PyInstaller and Inno
Setup 5.
"""
description = "Generate a .exe file for distribution"
boolean_options = []
user_options = []
arch = "i686-w64-mingw32.static"
url = "https://bitbucket.org/ecsv/mupen64plus-mxe-daily/get/master.zip"
dist_dir = os.path.join(BASE_DIR, "dist", "windows")
def initialize_options(self):
pass
def finalize_options(self):
pass
def copy_emulator(self):
tempdir = tempfile.mkdtemp()
zippath = os.path.join(tempdir, os.path.basename(self.url))
urllib.request.urlretrieve(self.url, zippath)
zip_file = zipfile.ZipFile(zippath)
for name in zip_file.namelist():
if self.arch in name:
dirn = os.path.basename(os.path.dirname(name))
filen = os.path.basename(name)
if not filen:
continue
dest_path = os.path.join(self.dist_dir, "m64py")
if dirn == self.arch:
fullpath = os.path.join(dest_path, filen)
else:
fullpath = os.path.join(dest_path, dirn, filen)
if not os.path.exists(os.path.join(dest_path, dirn)):
os.makedirs(os.path.join(dest_path, dirn))
unpacked = open(fullpath, "wb")
unpacked.write(zip_file.read(name))
unpacked.close()
zip_file.close()
shutil.rmtree(tempdir)
def copy_files(self):
dest_path = os.path.join(self.dist_dir, "m64py")
rar_dir = os.path.join(os.environ["ProgramFiles(x86)"], "Unrar")
if not os.path.isfile(os.path.join(rar_dir, "UnRAR.exe")):
tempdir = tempfile.mkdtemp()
urllib.request.urlretrieve("http://www.rarlab.com/rar/unrarw32.exe",
os.path.join(tempdir, "unrar.exe"))
subprocess.call([os.path.join(tempdir, "unrar.exe"), "-s"])
shutil.rmtree(tempdir)
shutil.copy(os.path.join(rar_dir, "UnRAR.exe"), dest_path)
shutil.copy(os.path.join(rar_dir, "license.txt"),
os.path.join(dest_path, "doc", "unrar-license.txt"))
for file_name in ["AUTHORS", "ChangeLog", "COPYING", "LICENSES", "README.rst"]:
shutil.copy(os.path.join(BASE_DIR, file_name), dest_path)
import PySide2
qt5_dir = os.path.dirname(PySide2.__file__)
qwindows = os.path.join(qt5_dir, "Qt", "plugins", "platforms", "qwindows.dll")
qwindows_dest = os.path.join(dest_path, "lib", "qt5_plugins", "platforms")
if not os.path.exists(qwindows_dest):
os.makedirs(qwindows_dest)
shutil.copy(qwindows, qwindows_dest)
def move_files(self):
dest_path = os.path.join(self.dist_dir, "m64py", "lib")
plugins_path = os.path.join(self.dist_dir, "m64py", "qt5_plugins")
shutil.copytree(plugins_path, os.path.join(dest_path, "qt5_plugins"))
shutil.rmtree(plugins_path)
for file_name in glob.glob(os.path.join(self.dist_dir, "m64py", "*.pyd")):
if "PySide2" not in file_name:
shutil.move(file_name, dest_path)
for file_name in glob.glob(os.path.join(self.dist_dir, "m64py", "api*.dll")):
shutil.move(file_name, dest_path)
for file_name in glob.glob(os.path.join(self.dist_dir, "m64py", "*.dll")):
print(file_name)
if "python3" not in file_name and "mupen64plus" not in os.path.basename(file_name):
shutil.move(file_name, dest_path)
def remove_files(self):
dest_path = os.path.join(self.dist_dir, "m64py")
for dir_name in ["api", "man6", "applications", "apps"]:
shutil.rmtree(os.path.join(dest_path, dir_name), True)
for file_name in glob.glob(os.path.join(dest_path, "glide*.exe")):
os.remove(file_name)
def run_build(self):
import PyInstaller.building.build_main
work_path = os.path.join(self.dist_dir, "build")
spec_file = os.path.join(self.dist_dir, "m64py.spec")
os.environ["BASE_DIR"] = BASE_DIR
os.environ["DIST_DIR"] = self.dist_dir
opts = {"distpath": self.dist_dir,
"workpath": work_path,
"clean_build": True,
"upx_dir": None,
"debug": False}
PyInstaller.building.build_main.main(None, spec_file, True, **opts)
def run_build_installer(self):
iss_file = ""
iss_in = os.path.join(self.dist_dir, "m64py.iss.in")
iss_out = os.path.join(self.dist_dir, "m64py.iss")
with open(iss_in, "r") as iss:
data = iss.read()
lines = data.split("\n")
for line in lines:
line = line.replace("{ICON}", os.path.realpath(os.path.join(self.dist_dir, "m64py")))
line = line.replace("{VERSION}", FRONTEND_VERSION)
iss_file += line + "\n"
with open(iss_out, "w") as iss:
iss.write(iss_file)
iscc = os.path.join(os.environ["ProgramFiles(x86)"], "Inno Setup 5", "ISCC.exe")
subprocess.call([iscc, iss_out])
def run(self):
self.run_command("build_qt")
set_rthook()
self.run_build()
self.copy_emulator()
self.move_files()
self.copy_files()
self.remove_files()
self.run_build_installer()
class BuildZip(BuildExe):
description = "Generate a .zip file for distribution"
def run_build_zip(self):
os.rename(os.path.join(self.dist_dir, "m64py"),
os.path.join(self.dist_dir, "m64py-{}".format(FRONTEND_VERSION)))
shutil.make_archive(os.path.join(self.dist_dir,
"m64py-{}-portable".format(FRONTEND_VERSION)),
"zip",
self.dist_dir, "m64py-{}".format(FRONTEND_VERSION),
True)
@staticmethod
def set_config_path():
core_file = ""
core_path = os.path.join(BASE_DIR, "src", "m64py", "core", "core.py")
with open(core_path, "r") as core:
data = core.read()
lines = data.split("\n")
for line in lines:
if "C.c_int(CORE_API_VERSION)" in line:
line = line.replace("None", "C.c_char_p(os.getcwd().encode())")
core_file += line + "\n"
with open(core_path, "w") as core:
core.write(core_file)
settings_file = ""
settings_path = os.path.join(BASE_DIR, "src", "m64py", "frontend", "settings.py")
with open(settings_path, "r") as core:
data = core.read()
lines = data.split("\n")
for line in lines:
if "QSettings(" in line:
line = line.replace("QSettings(\"m64py\", \"m64py\")",
"QSettings(os.path.join(os.getcwd(), \"m64py.ini\"), QSettings.IniFormat)")
settings_file += line + "\n"
with open(settings_path, "w") as core:
core.write(settings_file)
def run(self):
self.run_command("build_qt")
set_rthook()
self.set_config_path()
self.run_build()
self.copy_emulator()
self.move_files()
self.copy_files()
self.remove_files()
self.run_build_zip()
class CleanLocal(setuptools.Command):
description = "Clean the local project directory"
wildcards = ['*.py[co]', '*_ui.py', '*_rc.py', '__pycache__']
excludedirs = ['.git', 'build', 'dist']
user_options = []
def initialize_options(self):
pass
def finalize_options(self):
pass
def _walkpaths(self, path):
for root, dirs, files in os.walk(path):
for excluded_dir in self.excludedirs:
abs_excluded_dir = os.path.join(path, excluded_dir)
if root == abs_excluded_dir or root.startswith(abs_excluded_dir + os.sep):
continue
for a_dir in dirs:
file_path = os.path.join(root, a_dir)
if any(fnmatch.fnmatch(a_dir, pattern) for pattern in self.wildcards):
yield file_path
for a_file in files:
file_path = os.path.join(root, a_file)
if any(fnmatch.fnmatch(file_path, pattern) for pattern in self.wildcards):
yield file_path
def run(self):
for a_path in self._walkpaths('.'):
if os.path.isdir(a_path):
shutil.rmtree(a_path)
else:
os.remove(a_path)
class MyBuild(distutils_build.build):
def run(self):
self.run_command("build_qt")
distutils_build.build.run(self)
class MyClean(distutils_clean.clean):
def run(self):
self.run_command("clean_local")
distutils_clean.clean.run(self)
setuptools.setup(
name="m64py",
version=FRONTEND_VERSION,
description="A frontend for Mupen64Plus",
long_description="A Qt5 front-end (GUI) for Mupen64Plus, a cross-platform plugin-based Nintendo 64 emulator.",
author="Milan Nikolic",
author_email="gen2brain@gmail.com",
license="GNU GPLv3",
url="http://m64py.sourceforge.net",
package_dir={'': "src"},
packages=["m64py", "m64py.core", "m64py.frontend", "m64py.ui"],
scripts=["bin/m64py"],
requires=["PySide2", "PySDL2"],
platforms=["Linux", "Windows", "Darwin"],
cmdclass={
'build': MyBuild,
'build_dmg': BuildDmg,
'build_exe': BuildExe,
'build_qt': BuildQt,
'build_zip': BuildZip,
'clean': MyClean,
'clean_local': CleanLocal
},
data_files=[
("share/pixmaps", ["xdg/m64py.png"]),
("share/applications", ["xdg/m64py.desktop"]),
]
)