1 #! /usr/local/bin/python
3 # "Freeze" a Python script into a binary.
4 # Usage: see variable usage_msg below (before the imports!)
7 # - Edit the lines marked XXX below to localize.
8 # - You must have done "make inclinstall libainstall" in the Python
10 # - The script should not use dynamically loaded modules
11 # (*.so on most systems).
17 usage: freeze [-p prefix] [-e extension] ... script [module] ...
19 -p prefix: This is the prefix used when you ran
20 'Make inclinstall libainstall' in the Python build directory.
21 (If you never ran this, freeze won't work.)
22 The default is /usr/local.
24 -e extension: A directory containing additional .o files that
25 may be used to resolve modules. This directory
26 should also have a Setup file describing the .o files.
27 More than one -e option may be given.
29 script: The Python script to be executed by the resulting binary.
31 module ...: Additional Python modules (referenced by pathname)
32 that will be included in the resulting binary. These
33 may be .py or .pyc files.
37 # XXX Change the following line to point to your Tools/freeze directory
38 PACK
= '/ufs/guido/src/python/Tools/freeze'
40 # XXX Change the following line to point to your install prefix
44 # Import standard modules
54 # Set the directory to look for the freeze-private modules
56 dir = os
.path
.dirname(sys
.argv
[0])
64 # Import the freeze-private modules
66 import checkextensions
78 prefix
= PREFIX
# settable with -p option
85 target
= 'a.out' # normally derived from script name
90 opts
, args
= getopt
.getopt(sys
.argv
[1:], 'e:p:')
91 except getopt
.error
, msg
:
92 usage('getopt error: ' + str(msg
))
94 # proces option arguments
101 # locations derived from options
102 binlib
= os
.path
.join(prefix
, 'lib/python/lib')
103 incldir
= os
.path
.join(prefix
, 'include/Py')
104 config_c_in
= os
.path
.join(binlib
, 'config.c.in')
105 frozenmain_c
= os
.path
.join(binlib
, 'frozenmain.c')
106 makefile_in
= os
.path
.join(binlib
, 'Makefile')
107 defines
= ['-DHAVE_CONFIG_H', '-DUSE_FROZEN', '-DNO_MAIN',
108 '-DPYTHONPATH=\\"$(PYTHONPATH)\\"']
109 includes
= ['-I' + incldir
, '-I' + binlib
]
111 # sanity check of directories and files
112 for dir in [prefix
, binlib
, incldir
] + extensions
:
113 if not os
.path
.exists(dir):
114 usage('needed directory %s not found' % dir)
115 if not os
.path
.isdir(dir):
116 usage('%s: not a directory' % dir)
117 for file in config_c_in
, makefile_in
, frozenmain_c
:
118 if not os
.path
.exists(file):
119 usage('needed file %s not found' % file)
120 if not os
.path
.isfile(file):
121 usage('%s: not a plain file' % file)
122 for dir in extensions
:
123 setup
= os
.path
.join(dir, 'Setup')
124 if not os
.path
.exists(setup
):
125 usage('needed file %s not found' % setup
)
126 if not os
.path
.isfile(setup
):
127 usage('%s: not a plain file' % setup
)
129 # check that enough arguments are passed
131 usage('at least one filename argument required')
133 # check that file arguments exist
135 if not os
.path
.exists(arg
):
136 usage('argument %s not found' % arg
)
137 if not os
.path
.isfile(arg
):
138 usage('%s: not a plain file' % arg
)
140 # process non-option arguments
144 # derive target name from script name
145 base
= os
.path
.basename(scriptfile
)
146 base
, ext
= os
.path
.splitext(base
)
148 if base
!= scriptfile
:
151 target
= base
+ '.bin'
153 # Actual work starts here...
155 dict = findmodules
.findmodules(scriptfile
, modules
, path
)
157 backup
= frozen_c
+ '~'
159 os
.rename(frozen_c
, backup
)
162 outfp
= open(frozen_c
, 'w')
164 makefreeze
.makefreeze(outfp
, dict)
168 if cmp.cmp(backup
, frozen_c
):
169 sys
.stderr
.write('%s not changed, not written\n' %
171 os
.rename(backup
, frozen_c
)
178 if dict[mod
] == '<builtin>':
180 elif dict[mod
] == '<unknown>':
185 addfiles
, addmods
= \
186 checkextensions
.checkextensions(unknown
, extensions
)
189 builtins
= builtins
+ addmods
191 sys
.stderr
.write('Warning: unknown modules remain: %s\n' %
192 string
.join(unknown
))
195 infp
= open(config_c_in
)
196 backup
= config_c
+ '~'
198 os
.rename(config_c
, backup
)
201 outfp
= open(config_c
, 'w')
203 makeconfig
.makeconfig(infp
, outfp
, builtins
)
208 if cmp.cmp(backup
, config_c
):
209 sys
.stderr
.write('%s not changed, not written\n' %
211 os
.rename(backup
, config_c
)
213 cflags
= defines
+ includes
+ ['$(OPT)']
215 for n
in 'Modules', 'Python', 'Objects', 'Parser':
217 n
= os
.path
.join(binlib
, n
)
220 makevars
= parsesetup
.getmakevars(makefile_in
)
222 for key
in makevars
.keys():
223 somevars
[key
] = makevars
[key
]
225 somevars
['CFLAGS'] = string
.join(cflags
) # override
226 files
= ['$(OPT)', config_c
, frozen_c
, frozenmain_c
] + \
228 ['$(MODLIBS)', '$(LIBS)', '$(SYSLIBS)']
230 outfp
= open(makefile
, 'w')
232 makemakefile
.makemakefile(outfp
, somevars
, files
, target
)
238 print 'Now run make to build the target:', target
241 # Print usage message and exit
243 def usage(msg
= None):
245 sys
.stderr
.write(str(msg
) + '\n')
246 sys
.stderr
.write(usage_msg
)