Home > py2exe, python > Python: py2exe setup.py sample

Python: py2exe setup.py sample

13 Maggio 2010

Ecco il sample che uso per creare eseguibili con py2exe.

Caratteristiche:
– unico file
– compressione attiva
– icona incorporata

Ho Utilizzato wx per la GUI del fileBrowser (che cerca il
file python sorgente) e la TextEntry per il nome
del file .exe di destinazione.

La dir di destinazione è la solita:
..\dist\

Ecco il sourcecode:

#!/usr/bin/python
"""py2exe custom setup Sample
author: bancaldo"""

import py2exe, sys, wx, os, glob
from distutils.core import setup

if len(sys.argv) == 1:
    sys.argv.append("py2exe")
    sys.argv.append("-q")

class FileBrowser(wx.FileDialog):
    '''Class for file browser'''
    def __init__(self):
        self.fin = None
        wildcard = "File di testo (*.py)|*.py|" \
            "Tutti i files (*.*)|*.*"
        wx.FileDialog.__init__(self, None, "scegli il file", os.getcwd(),
                               "", wildcard, wx.OPEN | wx.CHANGE_DIR)
        if self.ShowModal() == wx.ID_OK:  
            print(self.GetPath())
            self.file = self.GetPath()
            self.fin = open(self.file, 'r')
        else:
            print "operazione apertura annullata"
            self.Destroy()
        self.Destroy()

class Target(object):
    '''Terget'''
    def __init__(self, **kw):
        self.__dict__.update(kw)
        # info di versione
        self.version = "1.0.0"
        self.company_name = "Bancaldo TM"
        self.copyright = "no copyright"
        self.name = "py2exe sample files"


def main():
    '''py2exe setup starter'''
    app = wx.PySimpleApp()
    app.MainLoop()

    manifest_template = '''
    <?xml version='1.0' encoding='UTF-8' standalone='yes'?>
    <assembly xmlns='urn:schemas-microsoft-com:asm.v1' manifestVersion='1.0'>
      <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
        <security>
          <requestedPrivileges>
            <requestedExecutionLevel level='asInvoker' uiAccess='false' />
          </requestedPrivileges>
        </security>
      </trustInfo>
      <dependency>
        <dependentAssembly>
          <assemblyIdentity 
    	 type='win32' 
    	 name='Microsoft.VC90.CRT' 
    	 version='9.0.21022.8' 
    	 processorArchitecture='*' 
    	 publicKeyToken='1fc8b3b9a1e18e3b' />
        </dependentAssembly>
      </dependency>
      <dependency>
        <dependentAssembly>
          <assemblyIdentity
             type="win32"
             name="Microsoft.Windows.Common-Controls"
             version="6.0.0.0"
             processorArchitecture="*"
             publicKeyToken="6595b64144ccf1df"
             language="*" />
        </dependentAssembly>
      </dependency>
    </assembly>
    '''
    file_to_freeze = FileBrowser().file

    textentry = wx.TextEntryDialog(None, "nome file EXE?", '', '')
    if textentry.ShowModal() == wx.ID_OK:
        destname = textentry.GetValue()
    
    rt_manifest = 24
    
    explicit_incl = ["sqlalchemy.dialects.sqlite.base"]
    other_res = [(rt_manifest, 1, manifest_template % dict(prog = "Rubrica"))]

    setup_opts = {"py2exe": {"includes": explicit_incl,
                             "compressed": 1,
                             "optimize": 2,
                             #"ascii": 1,
                             "bundle_files": 1,}}
    
    test_wx = Target(description = "A GUI app",
                     script = file_to_freeze,
                     other_resources = other_res,
                     icon_resources = [(1, "images\\py.ico")],
                     dest_base = destname)

    # images    
    path = (os.getcwd() + "\\images\\")
    data_files = [('images', [png for png in glob.glob(path + '*.png')])]

    setup(data_files = data_files, # images
          options = setup_opts,
          zipfile = None,
          windows = [test_wx],
        )

if __name__ == '__main__':
    main()
Categorie:py2exe, python Tag:
I commenti sono chiusi.