root/trunk/src/win_inno_setup.py

Revision 176, 7.6 kB (checked in by timw, 2 years ago)

'py2exe

Line 
1 # A setup script showing how to extend py2exe.
2 #
3 # In this case, the py2exe command is subclassed to create an installation
4 # script for InnoSetup, which can be compiled with the InnoSetup compiler
5 # to a single file windows installer.
6 #
7 # By default, the installer will be created as dist\Output\setup.exe.
8
9 from distutils.core import setup
10 import matplotlib
11 import py2exe
12 import sys
13 import glob
14 import os
15
16 #Build tree of files given a dir (for appending to py2exe data_files)
17 #Taken from http://osdir.com/ml/python.py2exe/2006-02/msg00085.html
18 def tree(src):
19     list = [(root, map(lambda f: os.path.join(root, f), files)) for (root, dirs, files) in os.walk(os.path.normpath(src))]
20     new_list = []
21     for (root, files) in list:
22     #print "%s , %s" % (root,files)
23         if len(files) > 0 and root.count('.svn') == 0:
24             new_list.append((root, files))
25     return new_list
26
27 ################################################################
28
29 class InnoScript:
30     def __init__(self,
31                  name,
32                  lib_dir,
33                  dist_dir,
34                  windows_exe_files = [],
35                  lib_files = [],
36                  version = ""):
37         self.lib_dir = lib_dir
38         self.dist_dir = dist_dir
39         if not self.dist_dir[-1] in "\\/":
40             self.dist_dir += "\\"
41         self.name = name
42         self.version = version
43         self.windows_exe_files = [self.chop(p) for p in windows_exe_files]
44         self.lib_files = [self.chop(p) for p in lib_files]
45
46     def chop(self, pathname):
47         assert pathname.startswith(self.dist_dir)
48         return pathname[len(self.dist_dir):]
49    
50     def create(self, pathname="dist\\Delphos.iss"):
51         self.pathname = pathname
52         ofi = self.file = open(pathname, "w")
53         print >> ofi, "; WARNING: This script has been created by py2exe. Changes to this script"
54         print >> ofi, "; will be overwritten the next time py2exe is run!"
55         print >> ofi, r"[Setup]"
56         print >> ofi, r"AppName=%s" % self.name
57         print >> ofi, r"AppVerName=%s %s" % (self.name, self.version)
58         print >> ofi, r"DefaultDirName={pf}\%s" % self.name
59         print >> ofi, r"DefaultGroupName=%s" % self.name
60         print >> ofi, r"VersionInfoVersion=%s" % self.version
61         print >> ofi, r"VersionInfoCompany=Ecotrust/COBI/WWF"
62         print >> ofi, r"VersionInfoDescription=Delphos"
63         print >> ofi, r"VersionInfoCopyright=Ecotrust"
64         print >> ofi, r"AppCopyright=Ecotrust"
65         print >> ofi, r"InfoAfterFile=README.TXT"
66         print >> ofi, r"LicenseFile=LICENSE.TXT"
67         print >> ofi, r"WizardImageBackColor=clBlack"
68         print >> ofi, r"WizardImageFile=images\delphos_vert.bmp"
69         print >> ofi, r"WizardSmallImageFile=images\delphos_upper_right.bmp"
70         print >> ofi, r"SetupIconFile=images\delphos_icon.ico"
71         print >> ofi
72
73         print >> ofi, r"[Files]"
74         for path in self.windows_exe_files + self.lib_files:
75             if (path == '.\\Delphos.exe.log'):
76                 #Special case need world write access to error log
77                 print >> ofi, r'Source: "%s"; DestDir: "{app}\%s"; Flags: ignoreversion; Permissions: users-modify' % (path, os.path.dirname(path))
78             else:
79                 print >> ofi, r'Source: "%s"; DestDir: "{app}\%s"; Flags: ignoreversion' % (path, os.path.dirname(path))
80
81         print >> ofi, r'Source: lib\MSVCP71.dll; DestDir: {app}\lib; Flags: ignoreversion'
82         print >> ofi
83
84         print >> ofi, r"[Icons]"
85         for path in self.windows_exe_files:
86             print >> ofi, r'Name: "{group}\%s"; Filename: "{app}\%s"; IconFilename: "{app}\images\delphos_icon.ico"; WorkingDir: {app}' % \
87                   (self.name, path)
88                  
89         print >> ofi, r'Name: "{group}\Delphos Fisheries Documentation - English"; Filename: "{app}\documentation\fisheries\english\documentation.html"'
90         print >> ofi, r'Name: "{group}\Delphos Fisheries Documentation - Spanish"; Filename: "{app}\documentation\fisheries\spanish\documentation.html"'                 
91        
92         print >> ofi, 'Name: "{group}\Uninstall %s"; Filename: "{uninstallexe}"' % self.name
93
94     def compile(self):
95         try:
96             import ctypes
97         except ImportError:
98             try:
99                 import win32api
100             except ImportError:
101                 import os
102                 os.startfile(self.pathname)
103             else:
104                 print "Ok, using win32api."
105                 win32api.ShellExecute(0, "compile",
106                                                 self.pathname,
107                                                 None,
108                                                 None,
109                                                 0)
110         else:
111             print "Cool, you have ctypes installed."
112             res = ctypes.windll.shell32.ShellExecuteA(0, "compile",
113                                                       self.pathname,
114                                                       None,
115                                                       None,
116                                                       0)
117             if res < 32:
118                 raise RuntimeError, "ShellExecute failed, error %d" % res
119
120
121 ################################################################
122
123 from py2exe.build_exe import py2exe
124
125 class build_installer(py2exe):
126     # This class first builds the exe file(s), then creates a Windows installer.
127     # You need InnoSetup for it.
128     def run(self):
129         # First, let py2exe do it's work.
130         py2exe.run(self)
131
132         lib_dir = self.lib_dir
133         dist_dir = self.dist_dir
134        
135         # create the Installer, using the files py2exe has created.
136         script = InnoScript("Delphos 0.4",
137                             lib_dir,
138                             dist_dir,
139                             self.windows_exe_files,
140                             self.lib_files)
141         print "*** creating the inno setup script***"
142         script.create()
143         print "*** compiling the inno setup script***"
144         script.compile()
145         # Note: By default the final setup.exe will be in an Output subdirectory.
146
147 ######################## py2exe setup options ########################################
148
149 zipfile = r"lib\shardlib"
150
151 options = {
152     "py2exe": {
153         "compressed": 1,
154         "optimize": 2,
155         "includes": ['sip', 'PyQt4', 'matplotlib.numerix.random_array', 'matplotlib.backends.backend_tkagg', 'sqlalchemy.databases.sqlite'],
156         "excludes": ['backend_gtkagg', 'backend_wxagg'],
157         "dll_excludes": ['libgdk_pixbuf-2.0-0.dll', 'libgobject-2.0-0.dll', 'libgdk-win32-2.0-0.dll'],
158         "packages": ["sqlalchemy", "matplotlib", "pytz", "PyQt4"],
159         "dist_dir": "dist",
160     }
161 }
162
163 matplotlib_data_files = tree('lib\matplotlibdata')
164 tcl_data_files = tree("lib\\tcl")
165 doc_data_files = tree('documentation')
166 sample_data_files = tree('sample')
167 base_files = [(".",[".\\LICENSE.txt", ".\\README.txt", ".\\Delphos.exe.log"])]
168 lib_files = [("lib",["lib\\MSVCP71.dll"])]
169 image_files = [("images",["images\\delphos_icon.ico","images\\delphos_upper_right.bmp","images\\delphos_vert.bmp"])]
170 data_files = matplotlib_data_files + tcl_data_files + doc_data_files + sample_data_files + base_files + lib_files + image_files
171  
172 setup(
173     options = options,
174     # The lib directory contains everything except the executables and the python dll.
175     zipfile = zipfile,
176     windows=[{"script": "Delphos.py"}],
177     # use out build_installer class as extended py2exe build command
178     cmdclass = {"py2exe": build_installer},
179     data_files = data_files
180 )
Note: See TracBrowser for help on using the browser.