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