OSDN Git Service

add alchemy simulator
[amulettoolsmh4/main.git] / make.py
1 # -*- coding:utf-8 -*-
2
3 # generate pyinstaller's spec file & modify .spec file
4 # 2013/12/07 written by kei9
5
6 import subprocess
7 import tempfile
8 import os
9 import os.path
10 import sys
11 import shutil
12
13 import convertcoding
14 func = lambda path_list: os.path.join(convertcoding.convert_unicode(
15     os.path.dirname(os.path.abspath(__file__))), *path_list)
16 BUNDLE_RESOURCES = [(ur"view/mainframe.xrc", func([u"view", u"mainframe.xrc"])),
17        (ur"view/gaugedialog.xrc", func([u"view", u"gaugedialog.xrc"])),
18        (ur"view/memodialog.xrc", func([u"view", u"memodialog.xrc"]))]
19 MOVE_FILES = [u"data.zip", u"readme.txt", u"license.txt"]
20
21 SCRIPTFILE = u"amulettool.py"
22 APPNAME = u"AmuletToolsMH4"
23
24 MAKESPEC = u"pyi-makespec"
25 PYINSTALLER = u"pyinstaller"
26 ICONFILE_WIN = u"icon/amulettoolsmh4.ico"
27 ICONFILE_MAC = u"icon/amulettoolsmh4.icns"
28
29 SPECOPTIONS_WIN = [u"--onefile", u"--windowed", u"--icon=" + ICONFILE_WIN, u"--name=" + APPNAME]
30 SPECOPTIONS_MAC = [u"--onefile", u"--windowed", u"--icon=" + ICONFILE_MAC, u"--name=" + APPNAME]
31 SPECOPTIONS_LINUX = [u"--onefile", u"--windowed", u"--name=" + APPNAME]
32
33 def get_insert_lines():
34     result = []
35     for item in BUNDLE_RESOURCES:
36         result.append(ur'a.datas += [("{0}", r"{1}", "DATA")]'.format(item[0], item[1]) + "\n")
37     return result
38
39 if __name__ == "__main__":
40     print u"file path: ", os.path.dirname(os.path.abspath(__file__))
41
42     if sys.platform.startswith(u"win"):
43         cmd = [MAKESPEC] + SPECOPTIONS_WIN + [SCRIPTFILE]
44     elif sys.platform.startswith(u"linux"):
45         cmd = [MAKESPEC] + SPECOPTIONS_LINUX + [SCRIPTFILE]
46     elif sys.platform.startswith(u"darwin"):
47         cmd = [MAKESPEC] + SPECOPTIONS_MAC + [SCRIPTFILE]
48     else:
49         raise OSError(u"this operating system is not supported!")
50     print " ".join(cmd)
51     out = subprocess.check_output(cmd)
52     print out
53
54
55     # insert bundle data line to .spec
56     print "insert bundle resources to .spec file"
57     specfile = APPNAME + ".spec"
58     with tempfile.NamedTemporaryFile(delete=False) as tmp_file:
59         tmpname = tmp_file.name
60         with open(specfile, "r") as f:
61             for line in f:
62                 if line.startswith("pyz"):
63                     for l in get_insert_lines():
64                         tmp_file.write(l)
65                 tmp_file.write(line)
66
67     os.remove(specfile)
68     os.rename(tmpname, specfile)
69
70
71     # conduct pyinstall
72     cmd = [PYINSTALLER, specfile]
73     print " ".join(cmd)
74     out = subprocess.check_output(cmd)
75     print out
76
77     # copy file to dist 
78     file_dir = os.path.dirname(os.path.abspath(__file__))
79     for f in MOVE_FILES:
80         abs_path = os.path.abspath(f)
81         relative_path = os.path.relpath(abs_path, file_dir)
82         shutil.copy(abs_path, os.path.join("dist", relative_path))
83
84     print "Finished"