OSDN Git Service

revise amulet search to consider of seed1 value,
[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 MOVE_FILES = [u"data.zip", u"readme.txt", u"license.txt"]
19
20 SCRIPTFILE = u"amulettool.py"
21 APPNAME = u"AmuletToolsMH4"
22
23 MAKESPEC = u"pyi-makespec"
24 PYINSTALLER = u"pyinstaller"
25 ICONFILE_WIN = u"icon/amulettoolsmh4.ico"
26 ICONFILE_MAC = u"icon/amulettoolsmh4.icns"
27
28 SPECOPTIONS_WIN = [u"--onefile", u"--windowed", u"--icon=" + ICONFILE_WIN, u"--name=" + APPNAME]
29 SPECOPTIONS_MAC = [u"--onefile", u"--windowed", u"--icon=" + ICONFILE_MAC, u"--name=" + APPNAME]
30 SPECOPTIONS_LINUX = [u"--onefile", u"--windowed", u"--name=" + APPNAME]
31
32 def get_insert_lines():
33     result = []
34     for item in BUNDLE_RESOURCES:
35         result.append(ur'a.datas += [("{0}", r"{1}", "DATA")]'.format(item[0], item[1]) + "\n")
36     return result
37
38 if __name__ == "__main__":
39     print u"file path: ", os.path.dirname(os.path.abspath(__file__))
40
41     if sys.platform.startswith(u"win"):
42         cmd = [MAKESPEC] + SPECOPTIONS_WIN + [SCRIPTFILE]
43     elif sys.platform.startswith(u"linux"):
44         cmd = [MAKESPEC] + SPECOPTIONS_LINUX + [SCRIPTFILE]
45     elif sys.platform.startswith(u"darwin"):
46         cmd = [MAKESPEC] + SPECOPTIONS_MAC + [SCRIPTFILE]
47     else:
48         raise OSError(u"this operating system is not supported!")
49     print " ".join(cmd)
50     out = subprocess.check_output(cmd)
51     print out
52
53
54     # insert bundle data line to .spec
55     print "insert bundle resources to .spec file"
56     specfile = APPNAME + ".spec"
57     with tempfile.NamedTemporaryFile(delete=False) as tmp_file:
58         tmpname = tmp_file.name
59         with open(specfile, "r") as f:
60             for line in f:
61                 if line.startswith("pyz"):
62                     for l in get_insert_lines():
63                         tmp_file.write(l)
64                 tmp_file.write(line)
65
66     os.remove(specfile)
67     os.rename(tmpname, specfile)
68
69
70     # conduct pyinstall
71     cmd = [PYINSTALLER, specfile]
72     print " ".join(cmd)
73     out = subprocess.check_output(cmd)
74     print out
75
76     # copy file to dist 
77     file_dir = os.path.dirname(os.path.abspath(__file__))
78     for f in MOVE_FILES:
79         abs_path = os.path.abspath(f)
80         relative_path = os.path.relpath(abs_path, file_dir)
81         shutil.copy(abs_path, os.path.join("dist", relative_path))
82
83     print "Finished"