c903cd93e6670a60651ffdf56517a050be3d8c4d
[minwii.git] / setup_win_exe.py
1 # -*- coding: utf-8 -*-
2 # This will create a dist directory containing the executable file, all the data
3 # directories. All Libraries will be bundled in executable file.
4 #
5 # Run the build process by entering 'pygame2exe.py' or
6 # 'python pygame2exe.py' in a console prompt.
7 #
8 # To build exe, python, pygame, and py2exe have to be installed. After
9 # building exe none of this libraries are needed.
10
11 try:
12 from distutils.core import setup
13 import py2exe, pygame
14 from modulefinder import Module
15 import glob, fnmatch
16 import sys, os, shutil
17 import operator
18 except ImportError, message:
19 raise SystemExit, "Unable to load module. %s" % message
20
21 class pygame2exe(py2exe.build_exe.py2exe): #This hack make sure that pygame default font is copied: no need to modify code for specifying default font
22 def copy_extensions(self, extensions):
23 #Get pygame default font
24 pygamedir = os.path.split(pygame.base.__file__)[0]
25 pygame_default_font = os.path.join(pygamedir, pygame.font.get_default_font())
26
27 #Add font to list of extension to be copied
28 extensions.append(Module("pygame.font", pygame_default_font))
29 py2exe.build_exe.py2exe.copy_extensions(self, extensions)
30
31 class BuildExe:
32 def __init__(self):
33 #Name of starting .py
34 self.script = "src/minwii/start_win.py"
35
36 #Name of program
37 self.project_name = "MINWii"
38
39 #Project url
40 self.project_url = "about:none"
41
42 #Version of program
43 self.project_version = "0.0"
44
45 #License of the program
46 self.license = "MyApps License"
47
48 #Auhor of program
49 self.author_name = "Me"
50 self.author_email = "example@example.com"
51 self.copyright = "Copyright (c) 2009 Me."
52
53 #Description
54 self.project_description = "MyApps Description"
55
56 #Icon file (None will use pygame default icon)
57 self.icon_file = None
58
59 #Extra files/dirs copied to game
60 self.extra_datas = []
61
62 #Extra/excludes python modules
63 self.extra_modules = []
64 self.exclude_modules = []
65
66 #DLL Excludes
67 self.exclude_dll = ['']
68
69 #Zip file name (None will bundle files in exe instead of zip file)
70 self.zipfile_name = None
71
72 #Dist directory
73 self.dist_dir ='dist'
74
75 ## Code from DistUtils tutorial at http://wiki.python.org/moin/Distutils/Tutorial
76 ## Originally borrowed from wxPython's setup and config files
77 def opj(self, *args):
78 path = os.path.join(*args)
79 return os.path.normpath(path)
80
81 def find_data_files(self, srcdir, *wildcards, **kw):
82 # get a list of all files under the srcdir matching wildcards,
83 # returned in a format to be used for install_data
84 def walk_helper(arg, dirname, files):
85 if '.svn' in dirname:
86 return
87 names = []
88 lst, wildcards = arg
89 for wc in wildcards:
90 wc_name = self.opj(dirname, wc)
91 for f in files:
92 filename = self.opj(dirname, f)
93
94 if fnmatch.fnmatch(filename, wc_name) and not os.path.isdir(filename):
95 names.append(filename)
96 if names:
97 lst.append( (dirname, names ) )
98
99 file_list = []
100 recursive = kw.get('recursive', True)
101 if recursive:
102 os.path.walk(srcdir, walk_helper, (file_list, wildcards))
103 else:
104 walk_helper((file_list, wildcards),
105 srcdir,
106 [os.path.basename(f) for f in glob.glob(self.opj(srcdir, '*'))])
107 return file_list
108
109 def run(self):
110 if os.path.isdir(self.dist_dir): #Erase previous destination dir
111 shutil.rmtree(self.dist_dir)
112
113 #Use the default pygame icon, if none given
114 if self.icon_file == None:
115 path = os.path.split(pygame.__file__)[0]
116 self.icon_file = os.path.join(path, 'pygame.ico')
117
118 #List all data files to add
119 extra_datas = []
120 for data in self.extra_datas:
121 if os.path.isdir(data):
122 extra_datas.extend(self.find_data_files(data, '*'))
123 else:
124 extra_datas.append(('.', [data]))
125
126 setup(
127 cmdclass = {'py2exe': pygame2exe},
128 version = self.project_version,
129 description = self.project_description,
130 name = self.project_name,
131 url = self.project_url,
132 author = self.author_name,
133 author_email = self.author_email,
134 license = self.license,
135
136 # targets to build
137 windows = [{
138 'script': self.script,
139 'icon_resources': [(0, self.icon_file)],
140 'copyright': self.copyright
141 }],
142 options = {'py2exe': {#'optimize': 2,
143 'bundle_files': 1,
144 #'compressed': True,
145 'excludes': self.exclude_modules,
146 'packages': self.extra_modules,
147 'dll_excludes': self.exclude_dll}
148 },
149 zipfile = self.zipfile_name,
150 data_files = extra_datas,
151 dist_dir = self.dist_dir
152 )
153
154 #if os.path.isdir('build'): #Clean up build dir
155 # shutil.rmtree('build')
156
157 if __name__ == '__main__':
158 if operator.lt(len(sys.argv), 2):
159 sys.argv.append('py2exe')
160 BuildExe().run() #Run generation
161 #raw_input("Press any key to continue") #Pause to let user see that things ends