Back to index

python3.2  3.2.2
Functions
freeze Namespace Reference

Functions

def main
def usage

Function Documentation

def freeze.main ( void  )

Definition at line 110 of file freeze.py.

00110 
00111 def main():
00112     # overridable context
00113     prefix = None                       # settable with -p option
00114     exec_prefix = None                  # settable with -P option
00115     extensions = []
00116     exclude = []                        # settable with -x option
00117     addn_link = []      # settable with -l, but only honored under Windows.
00118     path = sys.path[:]
00119     modargs = 0
00120     debug = 1
00121     odir = ''
00122     win = sys.platform[:3] == 'win'
00123     replace_paths = []                  # settable with -r option
00124     error_if_any_missing = 0
00125 
00126     # default the exclude list for each platform
00127     if win: exclude = exclude + [
00128         'dos', 'dospath', 'mac', 'macpath', 'macfs', 'MACFS', 'posix',
00129         'os2', 'ce',
00130         ]
00131 
00132     fail_import = exclude[:]
00133 
00134     # output files
00135     frozen_c = 'frozen.c'
00136     config_c = 'config.c'
00137     target = 'a.out'                    # normally derived from script name
00138     makefile = 'Makefile'
00139     subsystem = 'console'
00140 
00141     # parse command line by first replacing any "-i" options with the
00142     # file contents.
00143     pos = 1
00144     while pos < len(sys.argv)-1:
00145         # last option can not be "-i", so this ensures "pos+1" is in range!
00146         if sys.argv[pos] == '-i':
00147             try:
00148                 options = open(sys.argv[pos+1]).read().split()
00149             except IOError as why:
00150                 usage("File name '%s' specified with the -i option "
00151                       "can not be read - %s" % (sys.argv[pos+1], why) )
00152             # Replace the '-i' and the filename with the read params.
00153             sys.argv[pos:pos+2] = options
00154             pos = pos + len(options) - 1 # Skip the name and the included args.
00155         pos = pos + 1
00156 
00157     # Now parse the command line with the extras inserted.
00158     try:
00159         opts, args = getopt.getopt(sys.argv[1:], 'r:a:dEe:hmo:p:P:qs:wX:x:l:')
00160     except getopt.error as msg:
00161         usage('getopt error: ' + str(msg))
00162 
00163     # proces option arguments
00164     for o, a in opts:
00165         if o == '-h':
00166             print(__doc__)
00167             return
00168         if o == '-d':
00169             debug = debug + 1
00170         if o == '-e':
00171             extensions.append(a)
00172         if o == '-m':
00173             modargs = 1
00174         if o == '-o':
00175             odir = a
00176         if o == '-p':
00177             prefix = a
00178         if o == '-P':
00179             exec_prefix = a
00180         if o == '-q':
00181             debug = 0
00182         if o == '-w':
00183             win = not win
00184         if o == '-s':
00185             if not win:
00186                 usage("-s subsystem option only on Windows")
00187             subsystem = a
00188         if o == '-x':
00189             exclude.append(a)
00190         if o == '-X':
00191             exclude.append(a)
00192             fail_import.append(a)
00193         if o == '-E':
00194             error_if_any_missing = 1
00195         if o == '-l':
00196             addn_link.append(a)
00197         if o == '-a':
00198             modulefinder.AddPackagePath(*a.split("=", 2))
00199         if o == '-r':
00200             f,r = a.split("=", 2)
00201             replace_paths.append( (f,r) )
00202 
00203     # modules that are imported by the Python runtime
00204     implicits = []
00205     for module in ('site', 'warnings', 'encodings.utf_8', 'encodings.latin_1'):
00206         if module not in exclude:
00207             implicits.append(module)
00208 
00209     # default prefix and exec_prefix
00210     if not exec_prefix:
00211         if prefix:
00212             exec_prefix = prefix
00213         else:
00214             exec_prefix = sys.exec_prefix
00215     if not prefix:
00216         prefix = sys.prefix
00217 
00218     # determine whether -p points to the Python source tree
00219     ishome = os.path.exists(os.path.join(prefix, 'Python', 'ceval.c'))
00220 
00221     # locations derived from options
00222     version = sys.version[:3]
00223     if win:
00224         extensions_c = 'frozen_extensions.c'
00225     if ishome:
00226         print("(Using Python source directory)")
00227         binlib = exec_prefix
00228         incldir = os.path.join(prefix, 'Include')
00229         config_h_dir = exec_prefix
00230         config_c_in = os.path.join(prefix, 'Modules', 'config.c.in')
00231         frozenmain_c = os.path.join(prefix, 'Python', 'frozenmain.c')
00232         makefile_in = os.path.join(exec_prefix, 'Makefile')
00233         if win:
00234             frozendllmain_c = os.path.join(exec_prefix, 'Pc\\frozen_dllmain.c')
00235     else:
00236         binlib = os.path.join(exec_prefix,
00237                               'lib', 'python%s' % version, 'config')
00238         incldir = os.path.join(prefix, 'include', 'python%s' % version)
00239         config_h_dir = os.path.join(exec_prefix, 'include',
00240                                     'python%s' % version)
00241         config_c_in = os.path.join(binlib, 'config.c.in')
00242         frozenmain_c = os.path.join(binlib, 'frozenmain.c')
00243         makefile_in = os.path.join(binlib, 'Makefile')
00244         frozendllmain_c = os.path.join(binlib, 'frozen_dllmain.c')
00245     supp_sources = []
00246     defines = []
00247     includes = ['-I' + incldir, '-I' + config_h_dir]
00248 
00249     # sanity check of directories and files
00250     check_dirs = [prefix, exec_prefix, binlib, incldir]
00251     if not win:
00252         # These are not directories on Windows.
00253         check_dirs = check_dirs + extensions
00254     for dir in check_dirs:
00255         if not os.path.exists(dir):
00256             usage('needed directory %s not found' % dir)
00257         if not os.path.isdir(dir):
00258             usage('%s: not a directory' % dir)
00259     if win:
00260         files = supp_sources + extensions # extensions are files on Windows.
00261     else:
00262         files = [config_c_in, makefile_in] + supp_sources
00263     for file in supp_sources:
00264         if not os.path.exists(file):
00265             usage('needed file %s not found' % file)
00266         if not os.path.isfile(file):
00267             usage('%s: not a plain file' % file)
00268     if not win:
00269         for dir in extensions:
00270             setup = os.path.join(dir, 'Setup')
00271             if not os.path.exists(setup):
00272                 usage('needed file %s not found' % setup)
00273             if not os.path.isfile(setup):
00274                 usage('%s: not a plain file' % setup)
00275 
00276     # check that enough arguments are passed
00277     if not args:
00278         usage('at least one filename argument required')
00279 
00280     # check that file arguments exist
00281     for arg in args:
00282         if arg == '-m':
00283             break
00284         # if user specified -m on the command line before _any_
00285         # file names, then nothing should be checked (as the
00286         # very first file should be a module name)
00287         if modargs:
00288             break
00289         if not os.path.exists(arg):
00290             usage('argument %s not found' % arg)
00291         if not os.path.isfile(arg):
00292             usage('%s: not a plain file' % arg)
00293 
00294     # process non-option arguments
00295     scriptfile = args[0]
00296     modules = args[1:]
00297 
00298     # derive target name from script name
00299     base = os.path.basename(scriptfile)
00300     base, ext = os.path.splitext(base)
00301     if base:
00302         if base != scriptfile:
00303             target = base
00304         else:
00305             target = base + '.bin'
00306 
00307     # handle -o option
00308     base_frozen_c = frozen_c
00309     base_config_c = config_c
00310     base_target = target
00311     if odir and not os.path.isdir(odir):
00312         try:
00313             os.mkdir(odir)
00314             print("Created output directory", odir)
00315         except os.error as msg:
00316             usage('%s: mkdir failed (%s)' % (odir, str(msg)))
00317     base = ''
00318     if odir:
00319         base = os.path.join(odir, '')
00320         frozen_c = os.path.join(odir, frozen_c)
00321         config_c = os.path.join(odir, config_c)
00322         target = os.path.join(odir, target)
00323         makefile = os.path.join(odir, makefile)
00324         if win: extensions_c = os.path.join(odir, extensions_c)
00325 
00326     # Handle special entry point requirements
00327     # (on Windows, some frozen programs do not use __main__, but
00328     # import the module directly.  Eg, DLLs, Services, etc
00329     custom_entry_point = None  # Currently only used on Windows
00330     python_entry_is_main = 1   # Is the entry point called __main__?
00331     # handle -s option on Windows
00332     if win:
00333         import winmakemakefile
00334         try:
00335             custom_entry_point, python_entry_is_main = \
00336                 winmakemakefile.get_custom_entry_point(subsystem)
00337         except ValueError as why:
00338             usage(why)
00339 
00340 
00341     # Actual work starts here...
00342 
00343     # collect all modules of the program
00344     dir = os.path.dirname(scriptfile)
00345     path[0] = dir
00346     mf = modulefinder.ModuleFinder(path, debug, exclude, replace_paths)
00347 
00348     if win and subsystem=='service':
00349         # If a Windows service, then add the "built-in" module.
00350         mod = mf.add_module("servicemanager")
00351         mod.__file__="dummy.pyd" # really built-in to the resulting EXE
00352 
00353     for mod in implicits:
00354         mf.import_hook(mod)
00355     for mod in modules:
00356         if mod == '-m':
00357             modargs = 1
00358             continue
00359         if modargs:
00360             if mod[-2:] == '.*':
00361                 mf.import_hook(mod[:-2], None, ["*"])
00362             else:
00363                 mf.import_hook(mod)
00364         else:
00365             mf.load_file(mod)
00366 
00367     # Add the main script as either __main__, or the actual module name.
00368     if python_entry_is_main:
00369         mf.run_script(scriptfile)
00370     else:
00371         mf.load_file(scriptfile)
00372 
00373     if debug > 0:
00374         mf.report()
00375         print()
00376     dict = mf.modules
00377 
00378     if error_if_any_missing:
00379         missing = mf.any_missing()
00380         if missing:
00381             sys.exit("There are some missing modules: %r" % missing)
00382 
00383     # generate output for frozen modules
00384     files = makefreeze.makefreeze(base, dict, debug, custom_entry_point,
00385                                   fail_import)
00386 
00387     # look for unfrozen modules (builtin and of unknown origin)
00388     builtins = []
00389     unknown = []
00390     mods = sorted(dict.keys())
00391     for mod in mods:
00392         if dict[mod].__code__:
00393             continue
00394         if not dict[mod].__file__:
00395             builtins.append(mod)
00396         else:
00397             unknown.append(mod)
00398 
00399     # search for unknown modules in extensions directories (not on Windows)
00400     addfiles = []
00401     frozen_extensions = [] # Windows list of modules.
00402     if unknown or (not win and builtins):
00403         if not win:
00404             addfiles, addmods = \
00405                       checkextensions.checkextensions(unknown+builtins,
00406                                                       extensions)
00407             for mod in addmods:
00408                 if mod in unknown:
00409                     unknown.remove(mod)
00410                     builtins.append(mod)
00411         else:
00412             # Do the windows thang...
00413             import checkextensions_win32
00414             # Get a list of CExtension instances, each describing a module
00415             # (including its source files)
00416             frozen_extensions = checkextensions_win32.checkextensions(
00417                 unknown, extensions, prefix)
00418             for mod in frozen_extensions:
00419                 unknown.remove(mod.name)
00420 
00421     # report unknown modules
00422     if unknown:
00423         sys.stderr.write('Warning: unknown modules remain: %s\n' %
00424                          ' '.join(unknown))
00425 
00426     # windows gets different treatment
00427     if win:
00428         # Taking a shortcut here...
00429         import winmakemakefile, checkextensions_win32
00430         checkextensions_win32.write_extension_table(extensions_c,
00431                                                     frozen_extensions)
00432         # Create a module definition for the bootstrap C code.
00433         xtras = [frozenmain_c, os.path.basename(frozen_c),
00434                  frozendllmain_c, os.path.basename(extensions_c)] + files
00435         maindefn = checkextensions_win32.CExtension( '__main__', xtras )
00436         frozen_extensions.append( maindefn )
00437         outfp = open(makefile, 'w')
00438         try:
00439             winmakemakefile.makemakefile(outfp,
00440                                          locals(),
00441                                          frozen_extensions,
00442                                          os.path.basename(target))
00443         finally:
00444             outfp.close()
00445         return
00446 
00447     # generate config.c and Makefile
00448     builtins.sort()
00449     infp = open(config_c_in)
00450     outfp = bkfile.open(config_c, 'w')
00451     try:
00452         makeconfig.makeconfig(infp, outfp, builtins)
00453     finally:
00454         outfp.close()
00455     infp.close()
00456 
00457     cflags = ['$(OPT)']
00458     cppflags = defines + includes
00459     libs = [os.path.join(binlib, 'libpython$(VERSION).a')]
00460 
00461     somevars = {}
00462     if os.path.exists(makefile_in):
00463         makevars = parsesetup.getmakevars(makefile_in)
00464         for key in makevars:
00465             somevars[key] = makevars[key]
00466 
00467     somevars['CFLAGS'] = ' '.join(cflags) # override
00468     somevars['CPPFLAGS'] = ' '.join(cppflags) # override
00469     files = [base_config_c, base_frozen_c] + \
00470             files + supp_sources +  addfiles + libs + \
00471             ['$(MODLIBS)', '$(LIBS)', '$(SYSLIBS)']
00472 
00473     outfp = bkfile.open(makefile, 'w')
00474     try:
00475         makemakefile.makemakefile(outfp, somevars, files, base_target)
00476     finally:
00477         outfp.close()
00478 
00479     # Done!
00480 
00481     if odir:
00482         print('Now run "make" in', odir, end=' ')
00483         print('to build the target:', base_target)
00484     else:
00485         print('Now run "make" to build the target:', base_target)
00486 
00487 
00488 # Print usage message and exit

Here is the call graph for this function:

Here is the caller graph for this function:

def freeze.usage (   msg)

Definition at line 489 of file freeze.py.

00489 
00490 def usage(msg):
00491     sys.stdout = sys.stderr
00492     print("Error:", msg)
00493     print("Use ``%s -h'' for help" % sys.argv[0])
00494     sys.exit(2)
00495 
00496 
00497 main()

Here is the call graph for this function:

Here is the caller graph for this function: