OSDN Git Service

Merge branch 'release-0.1.2-prepare'
[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 shutil
11
12 BUNDLE_RESOURCES = [(r"view/mainframe.xrc", r"d:\\Git\\AmuletToolsMH4\\view\\mainframe.xrc"),
13        (r"view/gaugedialog.xrc", r"d:\\Git\\AmuletToolsMH4\\view\\gaugedialog.xrc")]
14 MOVE_FILES = ["data.zip", "readme.txt", "license.txt"]
15
16
17 SCRIPTFILE = "amulettool.py"
18 APPNAME = "AmuletToolsMH4"
19
20 MAKESPEC = "pyi-makespec"
21 PYINSTALLER = "pyinstaller"
22
23 SPECOPTIONS = ["--onefile", "--windowed", "--name=" + APPNAME]
24
25 def get_insert_lines():
26     result = []
27     for item in BUNDLE_RESOURCES:
28         result.append(r'a.datas += [("{0}", "{1}", "DATA")]'.format(item[0], item[1]) + "\n")
29     return result
30
31 if __name__ == "__main__":
32     cmd = [MAKESPEC] + SPECOPTIONS + [SCRIPTFILE]
33     print " ".join(cmd)
34     out = subprocess.check_output(cmd)
35     print out
36
37     # insert bundle data line to .spec
38     print "insert bundle resources to .spec file"
39     specfile = APPNAME + ".spec"
40     with tempfile.NamedTemporaryFile(delete=False) as tmp_file:
41         tmpname = tmp_file.name
42         with open(specfile, "r") as f:
43             for line in f:
44                 if line.startswith("pyz"):
45                     for l in get_insert_lines():
46                         tmp_file.write(l)
47                 tmp_file.write(line)
48
49     os.remove(specfile)
50     os.rename(tmpname, specfile)
51
52     # conduct pyinstall
53     cmd = [PYINSTALLER, specfile]
54     print " ".join(cmd)
55     out = subprocess.check_output(cmd)
56     print out
57
58     # copy file to dist 
59     file_dir = os.path.dirname(os.path.abspath(__file__))
60     for f in MOVE_FILES:
61         abs_path = os.path.abspath(f)
62         relative_path = os.path.relpath(abs_path, file_dir)
63         shutil.copy(abs_path, os.path.join("dist", relative_path))
64
65     print "Finished"