0f292578cac5599cfb098915cb1588d1615401ad
[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
22 origIsSystemDLL = py2exe.build_exe.isSystemDLL
23 def isSystemDLL(pathname):
24 if os.path.basename(pathname).lower() in ["sdl_ttf.dll"]:
25 return 0
26 return origIsSystemDLL(pathname)
27 py2exe.build_exe.isSystemDLL = isSystemDLL
28
29
30 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
31 def copy_extensions(self, extensions):
32 #Get pygame default font
33 pygamedir = os.path.split(pygame.base.__file__)[0]
34 pygame_default_font = os.path.join(pygamedir, pygame.font.get_default_font())
35
36 #Add font to list of extension to be copied
37 extensions.append(Module("pygame.font", pygame_default_font))
38 py2exe.build_exe.py2exe.copy_extensions(self, extensions)
39
40 class BuildExe:
41 def __init__(self):
42 #Name of starting .py
43 self.script = "src/minwii/start_win.py"
44
45 #Name of program
46 self.project_name = "MINWii"
47
48 #Project url
49 self.project_url = "about:none"
50
51 #Version of program
52 self.project_version = "0.0"
53
54 #License of the program
55 self.license = "MyApps License"
56
57 #Auhor of program
58 self.author_name = "Me"
59 self.author_email = "example@example.com"
60 self.copyright = "Copyright (c) 2009 Me."
61
62 #Description
63 self.project_description = "MyApps Description"
64
65 #Icon file (None will use pygame default icon)
66 self.icon_file = None
67
68 #Extra files/dirs copied to game
69 self.extra_datas = []
70
71 #Extra/excludes python modules
72 self.extra_modules = []
73 self.exclude_modules = []
74
75 #DLL Excludes
76 self.exclude_dll = ['']
77
78 #Zip file name (None will bundle files in exe instead of zip file)
79 self.zipfile_name = None
80
81 #Dist directory
82 self.dist_dir ='dist'
83
84 ## Code from DistUtils tutorial at http://wiki.python.org/moin/Distutils/Tutorial
85 ## Originally borrowed from wxPython's setup and config files
86 def opj(self, *args):
87 path = os.path.join(*args)
88 return os.path.normpath(path)
89
90 def find_data_files(self, srcdir, *wildcards, **kw):
91 # get a list of all files under the srcdir matching wildcards,
92 # returned in a format to be used for install_data
93 def walk_helper(arg, dirname, files):
94 if '.svn' in dirname:
95 return
96 names = []
97 lst, wildcards = arg
98 for wc in wildcards:
99 wc_name = self.opj(dirname, wc)
100 for f in files:
101 filename = self.opj(dirname, f)
102
103 if fnmatch.fnmatch(filename, wc_name) and not os.path.isdir(filename):
104 names.append(filename)
105 if names:
106 lst.append( (dirname, names ) )
107
108 file_list = []
109 recursive = kw.get('recursive', True)
110 if recursive:
111 os.path.walk(srcdir, walk_helper, (file_list, wildcards))
112 else:
113 walk_helper((file_list, wildcards),
114 srcdir,
115 [os.path.basename(f) for f in glob.glob(self.opj(srcdir, '*'))])
116 return file_list
117
118 def run(self):
119 if os.path.isdir(self.dist_dir): #Erase previous destination dir
120 shutil.rmtree(self.dist_dir)
121
122 if os.path.isdir('build'): #Clean up build dir
123 shutil.rmtree('build')
124
125
126 #Use the default pygame icon, if none given
127 if self.icon_file == None:
128 path = os.path.split(pygame.__file__)[0]
129 self.icon_file = os.path.join(path, 'pygame.ico')
130
131 #List all data files to add
132 extra_datas = []
133 for data in self.extra_datas:
134 if os.path.isdir(data):
135 extra_datas.extend(self.find_data_files(data, '*'))
136 else:
137 extra_datas.append(('.', [data]))
138
139
140 setup(
141 cmdclass = {'py2exe': pygame2exe},
142 version = self.project_version,
143 description = self.project_description,
144 name = self.project_name,
145 url = self.project_url,
146 author = self.author_name,
147 author_email = self.author_email,
148 license = self.license,
149
150 # targets to build
151 windows = [{
152 'script': self.script,
153 'icon_resources': [(0, self.icon_file)],
154 'copyright': self.copyright
155 }],
156 options = {'py2exe': {#'optimize': 2,
157 'bundle_files': 1,
158 #'compressed': True,
159 'excludes': self.exclude_modules,
160 'packages': self.extra_modules,
161 'dll_excludes': self.exclude_dll}
162 },
163 zipfile = self.zipfile_name,
164 data_files = extra_datas,
165 dist_dir = self.dist_dir
166 )
167
168 #if os.path.isdir('build'): #Clean up build dir
169 # shutil.rmtree('build')
170
171 if __name__ == '__main__':
172 if operator.lt(len(sys.argv), 2):
173 sys.argv.append('py2exe')
174 BuildExe().run() #Run generation
175 #raw_input("Press any key to continue") #Pause to let user see that things ends