cx_Freeze Compile-Utility

Code-Stücke können hier veröffentlicht werden.
Antworten
snakeseven
User
Beiträge: 408
Registriert: Freitag 7. Oktober 2005, 14:37
Wohnort: Berlin
Kontaktdaten:

Hi,
Mit cx_Freeze (http://starship.python.net/crew/atuining/cx_Freeze/) lassen sich unter Linux Executables aus Python-Scripten herstellen. Falls man ein größeres Projekt hat, das aus vielen Scripten besteht, kann man die Scriptnamen + Zielordner in eine Textdatei schreiben und mit folgendem Utility in einem Rutsch durchcompilieren lassen.

Code: Alles auswählen

#!/usr/bin/env python
#########################################################################################################################
# cxf_remote.py
# Remote for cx_freeze (http://starship.python.net/crew/atuining/cx_Freeze/).
# Uses the textfile 'comp.txt' and starts a sequence of remotecalls.
#
# Format of 'comp.txt':
# script1.py, destination-folder1
# script2.py, destination-folder2
# script3.py, destination-folder3
# (...)
# Lines with a'#' at the beginning will be ignored
# Runs with 2.4 and higher
'''
The cx_freeze options:

options:
  --version             show program's version number and exit
  -h, --help            show this help message and exit
  -O                    optimize generated bytecode as per PYTHONOPTIMIZE; use
                        -OO in order to remove doc strings
  -c, --compress        compress byte code in zip files
  --base-name=NAME, --base-binary=NAME
                        file on which to base the target file; if the name of
                        the file is not an absolute file name, the
                        subdirectory bases (rooted in the directory in which
                        the freezer is found) will be searched for a file
                        matching the name
  --init-script=NAME    script which will be executed upon startup; if the
                        name of the file is not an absolute file name, the
                        subdirectory initscripts (rooted in the directory in
                        which the freezer is found) will be searched for a
                        file matching the name
  --target-dir=DIR, --install-dir=DIR
                        the directory in which to place the target file and
                        any dependent files
  --target-name=NAME    the name of the file to create instead of the base
                        name of the script and the extension of the base
                        binary
  --shared-lib-name=NAME
                        the name of the shared library (DLL) implementing the
                        Python runtime which is required for a frozen binary
                        to work
  --no-copy-deps, --keep-path
                        do not copy the dependent files (extensions, shared
                        libraries, etc.) to the target directory; this also
                        modifies the default init script to ConsoleKeepPath.py
                        and means that the target executable requires a Python
                        installation to execute properly
  --default-path=DIRS   list of paths separated by the standard path separator
                        for the platform which will be used to initialize
                        sys.path prior to running the module finder
  --include-path=DIRS   list of paths separated by the standard path separator
                        for the platform which will be used to modify sys.path
                        prior to running the module finder
  --replace-paths=DIRECTIVES
                        replace all the paths in modules found in the given
                        paths with the given replacement string; multiple
                        values are separated by the standard path separator
                        and each value is of the form path=replacement_string;
                        path can be * which means all paths not already
                        specified
  --include-modules=NAMES
                        comma separated list of modules to include
  --exclude-modules=NAMES
                        comma separated list of modules to exclude
  --ext-list-file=NAME  name of file in which to place the list of dependent
                        files which were copied into the target directory
  -z SPEC, --zip-include=SPEC
                        name of file to add to the zip file or a specification
                        of the form name=arcname which will specify the
                        archive name to use; multiple --zip-include arguments
                        can be used
'''
##########################################################################################################################
import string,subprocess

freezedir = '/home/PythonScripte/cx_Freeze-3.0.2/FreezePython'       # folder of 'FreezePython'
targetdir = " -OO --target-dir='/home/PythonScripte/Executables/'"   # destination-folder for the executables

d = open('/home/PythonScripte/comp.txt','r')                         # the folder of the textfile 'comp.txt'
f = d.readlines()

for fileName in f:
    if fileName[0]<>'#':
        z = string.find(fileName,'/')
        folder = fileName[z:-2]
        fileName = fileName[:z-1]
        proz = subprocess.Popen(freezedir + targetdir + fileName[:-3] + " '" + folder + fileName + "'",shell=True)
Gruss, Seven
Zuletzt geändert von snakeseven am Donnerstag 31. August 2006, 11:41, insgesamt 6-mal geändert.
Python 47
User
Beiträge: 574
Registriert: Samstag 17. September 2005, 21:04

Bestimmt nützlich wenn man cx Freeze benutzt!
Aber ich nutze ja py2exe! :lol:
mfg

Thomas :-)
snakeseven
User
Beiträge: 408
Registriert: Freitag 7. Oktober 2005, 14:37
Wohnort: Berlin
Kontaktdaten:

Python Master 47 hat geschrieben:Bestimmt nützlich wenn man cx Freeze benutzt!
Aber ich nutze ja py2exe! :lol:
Hier die Py2exe Version:

Code: Alles auswählen

"""
makeExe.py
- Simple Python script to automate the creation
  of Python executables using py2exe.
(c) 2004 Premshree Pillai (24/01/04)
http://www.qiksearch.com/
"""
# dieses Script aus dem Verzeichnis starten, wo die zu compilierenden Sourcen gespeichert sind

import sys,re,string

fileName = ''

def makepy():
    global fileName

    fp = open(fileName)
    fp.close()
    package = re.split(":",fileName)
    package = re.split("/",package[len(package) - 1])
    package = re.split(".py",package[len(package) - 1])
    package = package[0]

    setupName = "setup.py"
    fp = open(setupName,"w")
    temp = """from distutils.core import setup\nimport py2exe\n\nsetup(name = "%s",windows = ["%s"])""" % (package,fileName)
    fp.write(temp)
    fp.close()

    sys.argv.append("py2exe")
    execfile(setupName)

    d = open("C:/Ablage/Python/PythonScripte/comp.txt","r")    #diesen Pfad an die eigenen Umgebung anpassen
    f = d.readlines()

    for fileName in f:
        if fileName[0]<>'#':
            p = string.find(fileName,"\n")
            if p > 0:
                fileName = fileName[:p]
            makepy()
Wichtig: py2exe schreibt die .EXEs in den Ordner 'dist'. Dort wird auch eine ZIP-Datei mit den nötigen Bibliotheken angelegt. Beim Compilieren wird die alte ZIP durch die neue ersetzt. Das kann dazu führen, daß einige der vorangegangenen EXEs nicht mehr laufen.
TIPP 1: Die zuletzt compilierte EXE sollte im Import-Block alle Module enthalten, die auch von den anderen Scripten benötigt werden. Oder man muß doch in mehreren Schritten compilieren und den Inhalt des 'dist'-Verzeichnisses 'retten' bevor man weiter macht.
TIPP 2: Manchmal findet py2exe eine Bilbiothek nicht, meckert aber auch nicht und die resultierende EXE läuft dann natürlich nicht. Anhand der Fehlermeldung kann man dann aber sehen, was fehlt. In diesem Fall die benötigte Bibliothek einfach der Library-ZIP im 'dist'-Ordner hinzufügen.

Viel Erfolg !
Seven
Zuletzt geändert von snakeseven am Samstag 8. April 2006, 09:08, insgesamt 1-mal geändert.
Python 47
User
Beiträge: 574
Registriert: Samstag 17. September 2005, 21:04

Das ist doch super!
Danke, ich werds jetzt bestimmt öfters nutzen!Einfach zeitsparender!

:wink:

Das wäre vielleicht sogar was fürs Wiki!
mfg

Thomas :-)
snakeseven
User
Beiträge: 408
Registriert: Freitag 7. Oktober 2005, 14:37
Wohnort: Berlin
Kontaktdaten:

Python Master 47 hat geschrieben:Das wäre vielleicht sogar was fürs Wiki!
Ich werde auf jeden Fall die cx_freeze Version etwas verfeinern, daß man in comp.txt auch den Ziel-Ordner für jedes Executable angeben kann (z.B. Modul_1.py/dist2). Die py2exe Version ist ja nur ein abgewandeltes 'makepy'. Ob ich das einfach so ins Wiki stellen darf, weiss ich ehrlich gesagt nicht !?

Gruss, Seven
Leonidas
Python-Forum Veteran
Beiträge: 16025
Registriert: Freitag 20. Juni 2003, 16:30
Kontaktdaten:

snakeseven hat geschrieben:Ob ich das einfach so ins Wiki stellen darf, weiss ich ehrlich gesagt nicht !?
Was macht es für einen Unterschied, ob du's ins Wiki stellt oder ins Forum?

Wenn du es aus dem Python Cookbook hast, sollte es kein Problem sein, sowas ins Wiki zu stellen.. oder in ein Mercurial-Repository ;)
My god, it's full of CARs! | Leonidasvoice vs (former) Modvoice
snakeseven
User
Beiträge: 408
Registriert: Freitag 7. Oktober 2005, 14:37
Wohnort: Berlin
Kontaktdaten:

Leonidas hat geschrieben:Was macht es für einen Unterschied, ob du's ins Wiki stellt oder ins Forum?
Hast du völlig recht. Habe aber noch englische comments eingefügt und die Options von cx_freeze.
Gruss, Seven
Antworten