Python Pmw и cx_Freeze?

Не мога да направя изпълним файл от моята програма на python, която използва Pmw (мега джаджи на Python). Използвам cx_Freeze (чрез gui backend "Gui2Exe"). Търсейки в сайта на Pmw, открих, че това е причинено от това как библиотеката Pmw проверява за модули, когато се изпълнява, и не работи, когато използвате py2exe или подобни програми, тъй като библиотеките са в zip файл. Повече информация можете да намерите тук: http://pmw.sourceforge.net/doc/dynamicloader.html Така те дават решение на тази страница, под „Замразяване на Pmw“, като предоставят скрипт, който генерира единичен самостоятелен Pmw модул, който можете лесно да замразите. Този скрипт обаче използва остарял код и няма да работи с Python 2.6+. Опитах се да го модифицирам без успех.

РЕДАКТИРАНЕ: Бих искал да спомена, че просто заместването на „regex“ с „re“ няма да работи.

#!/usr/bin/env python

# Helper script when freezing Pmw applications.  It concatenates all
# Pmw megawidget files into a single file, 'Pmw.py', in the current
# directory.  The script must be called with one argument, being the
# path to the 'lib' directory of the required version of Pmw.
# To freeze a Pmw application, you will also need to copy the
# following files to the application directory before freezing:
#
#    PmwBlt.py PmwColor.py

import os
import regsub
import string
import sys

# The order of these files is significant.  Files which reference
# other files must appear later.  Files may be deleted if they are not
# used.
files = [
    'Dialog', 'TimeFuncs', 'Balloon', 'ButtonBox', 'EntryField',
    'Group', 'LabeledWidget', 'MainMenuBar', 'MenuBar', 'MessageBar',
    'MessageDialog', 'NoteBook', 'OptionMenu', 'PanedWidget', 'PromptDialog',
    'RadioSelect', 'ScrolledCanvas', 'ScrolledField', 'ScrolledFrame',
    'ScrolledListBox', 'ScrolledText', 'HistoryText', 'SelectionDialog',
    'TextDialog', 'TimeCounter', 'AboutDialog', 'ComboBox', 'ComboBoxDialog',
    'Counter', 'CounterDialog',
]

# Set this to 0 if you do not use any of the Pmw.Color functions:
needColor = 1

# Set this to 0 if you do not use any of the Pmw.Blt functions:
needBlt = 1

def expandLinks(path):
    if not os.path.isabs(path):
    path = os.path.join(os.getcwd(), path)
    while 1:
    if not os.path.islink(path):
        break
    dir = os.path.dirname(path)
    path = os.path.join(dir, os.readlink(path))

    return path

def mungeFile(file):
    # Read the file and modify it so that it can be bundled with the
    # other Pmw files.
    file = 'Pmw' + file + '.py'
    text = open(os.path.join(srcdir, file)).read()
    text = regsub.gsub('import Pmw\>', '', text)
    text = regsub.gsub('INITOPT = Pmw.INITOPT', '', text)
    text = regsub.gsub('\<Pmw\.', '', text)
    text = '\n' + ('#' * 70) + '\n' + '### File: ' + file + '\n' + text
    return text

# Work out which version is being bundled.
file = sys.argv[0]
file = os.path.normpath(file)
file = expandLinks(file)

dir = os.path.dirname(file)
dir = expandLinks(dir)
dir = os.path.dirname(dir)
dir = expandLinks(dir)
dir = os.path.basename(dir)

version = string.replace(dir[4:], '_', '.')

# Code to import the Color module.
colorCode = """
import PmwColor
Color = PmwColor
del PmwColor
"""

# Code to import the Blt module.
bltCode = """
import PmwBlt
Blt = PmwBlt
del PmwBlt
"""

# Code used when not linking with PmwBlt.py.
ignoreBltCode = """
_bltImported = 1
_bltbusyOK = 0
"""

# Code to define the functions normally supplied by the dynamic loader.
extraCode = """

### Loader functions:

_VERSION = '%s'

def setversion(version):
    if version != _VERSION:
        raise ValueError, 'Dynamic versioning not available'

def setalphaversions(*alpha_versions):
    if alpha_versions != ():
    raise ValueError, 'Dynamic versioning not available'

def version(alpha = 0):
    if alpha:
        return ()
    else:
        return _VERSION

def installedversions(alpha = 0):
    if alpha:
        return ()
    else:
        return (_VERSION,)

"""

if '-noblt' in sys.argv:
    sys.argv.remove('-noblt')
    needBlt = 0

if '-nocolor' in sys.argv:
    sys.argv.remove('-nocolor')
    needColor = 0

if len(sys.argv) != 2:
    print 'usage: bundlepmw.py [-noblt] [-nocolor] /path/to/Pmw/Pmw_X_X_X/lib'
    sys.exit()

srcdir = sys.argv[1]

if os.path.exists('Pmw.py'):
    print 'Pmw.py already exists. Remove it and try again.'
    sys.exit()

outfile = open('Pmw.py', 'w')

if needColor:
    outfile.write(colorCode)

if needBlt:
    outfile.write(bltCode)

outfile.write(extraCode % version)

# Specially handle PmwBase.py file:
text = mungeFile('Base')
text = regsub.gsub('import PmwLogicalFont', '', text)
text = regsub.gsub('PmwLogicalFont._font_initialise', '_font_initialise', text)
outfile.write(text)
if not needBlt:
    outfile.write(ignoreBltCode)

files.append('LogicalFont')
for file in files:
    text = mungeFile(file)
    outfile.write(text)

print ''
print '   Pmw.py has been created.'

if needColor or needBlt:
    print '   Before running freeze, also copy the following file(s):'
    if needBlt:
    print '   ' + os.path.join(srcdir, 'PmwBlt.py')
    if needColor:
    print '   ' + os.path.join(srcdir, 'PmwColor.py')

person Brock123    schedule 21.07.2011    source източник


Отговори (1)


За съжаление оригиналният файл има някои проблеми с табулатори и интервали (просто вижте вдлъбнатината while в expandlinks).

Поправих тези проблеми с отстъпа, промених regsub.gsub с re.sub и премахнах импортирането на string, използвайки методите за тип низ.

След това скриптът работи перфектно:

   Pmw.py has been created.
   Before running freeze, also copy the following file(s):
   C:\Users\joaquin\Desktop\Pmw.1.3.2\src\Pmw\Pmw_1_3\lib\PmwBlt.py
   C:\Users\joaquin\Desktop\Pmw.1.3.2\src\Pmw\Pmw_1_3\lib\PmwColor.py

Ето ви коригирания скрипт:

#!/usr/bin/env python

# Helper script when freezing Pmw applications.  It concatenates all
# Pmw megawidget files into a single file, 'Pmw.py', in the current
# directory.  The script must be called with one argument, being the
# path to the 'lib' directory of the required version of Pmw.
# To freeze a Pmw application, you will also need to copy the
# following files to the application directory before freezing:
#
#    PmwBlt.py PmwColor.py

import os
import re
import sys

# The order of these files is significant.  Files which reference
# other files must appear later.  Files may be deleted if they are not
# used.
files = [
    'Dialog', 'TimeFuncs', 'Balloon', 'ButtonBox', 'EntryField',
    'Group', 'LabeledWidget', 'MainMenuBar', 'MenuBar', 'MessageBar',
    'MessageDialog', 'NoteBook', 'OptionMenu', 'PanedWidget', 'PromptDialog',
    'RadioSelect', 'ScrolledCanvas', 'ScrolledField', 'ScrolledFrame',
    'ScrolledListBox', 'ScrolledText', 'HistoryText', 'SelectionDialog',
    'TextDialog', 'TimeCounter', 'AboutDialog', 'ComboBox', 'ComboBoxDialog',
    'Counter', 'CounterDialog',
]

# Set this to 0 if you do not use any of the Pmw.Color functions:
needColor = 1

# Set this to 0 if you do not use any of the Pmw.Blt functions:
needBlt = 1

def expandLinks(path):
    if not os.path.isabs(path):
        path = os.path.join(os.getcwd(), path)
    while 1:
        if not os.path.islink(path):
            break
        dir = os.path.dirname(path)
        path = os.path.join(dir, os.readlink(path))

    return path

def mungeFile(file):
    # Read the file and modify it so that it can be bundled with the
    # other Pmw files.
    file = 'Pmw' + file + '.py'
    text = open(os.path.join(srcdir, file)).read()
    text = re.sub('import Pmw\>', '', text)
    text = re.sub('INITOPT = Pmw.INITOPT', '', text)
    text = re.sub('\<Pmw\.', '', text)
    text = '\n' + ('#' * 70) + '\n' + '### File: ' + file + '\n' + text
    return text

# Work out which version is being bundled.
file = sys.argv[0]
file = os.path.normpath(file)
file = expandLinks(file)

dir = os.path.dirname(file)
dir = expandLinks(dir)
dir = os.path.dirname(dir)
dir = expandLinks(dir)
dir = os.path.basename(dir)

version = dir[4:].replace('_', '.')

# Code to import the Color module.
colorCode = """
import PmwColor
Color = PmwColor
del PmwColor
"""
# Code to import the Blt module.
bltCode = """
import PmwBlt
Blt = PmwBlt
del PmwBlt
"""
# Code used when not linking with PmwBlt.py.
ignoreBltCode = """
_bltImported = 1
_bltbusyOK = 0
"""
# Code to define the functions normally supplied by the dynamic loader.
extraCode = """

### Loader functions:

_VERSION = '%s'

def setversion(version):
    if version != _VERSION:
        raise ValueError, 'Dynamic versioning not available'

def setalphaversions(*alpha_versions):
    if alpha_versions != ():
        raise ValueError, 'Dynamic versioning not available'

def version(alpha = 0):
    if alpha:
        return ()
    else:
        return _VERSION

def installedversions(alpha = 0):
    if alpha:
        return ()
    else:
        return (_VERSION,)

"""
if '-noblt' in sys.argv:
    sys.argv.remove('-noblt')
    needBlt = 0

if '-nocolor' in sys.argv:
    sys.argv.remove('-nocolor')
    needColor = 0

if len(sys.argv) != 2:
    print 'usage: bundlepmw.py [-noblt] [-nocolor] /path/to/Pmw/Pmw_X_X_X/lib'
    sys.exit()

srcdir = sys.argv[1]

if os.path.exists('Pmw.py'):
    print 'Pmw.py already exists. Remove it and try again.'
    sys.exit()

outfile = open('Pmw.py', 'w')

if needColor:
    outfile.write(colorCode)

if needBlt:
    outfile.write(bltCode)

outfile.write(extraCode % version)

# Specially handle PmwBase.py file:
text = mungeFile('Base')
text = re.sub('import PmwLogicalFont', '', text)
text = re.sub('PmwLogicalFont._font_initialise', '_font_initialise', text)
outfile.write(text)
if not needBlt:
    outfile.write(ignoreBltCode)

files.append('LogicalFont')
for file in files:
    text = mungeFile(file)
    outfile.write(text)

print ''
print '   Pmw.py has been created.'

if needColor or needBlt:
    print '   Before running freeze, also copy the following file(s):'
    if needBlt:
        print '   ' + os.path.join(srcdir, 'PmwBlt.py')
    if needColor:
        print '   ' + os.path.join(srcdir, 'PmwColor.py')
person joaquin    schedule 01.10.2011
comment
Страхотен. Сега ще пробвам. Предлагам да се свържете с разработчиците на Pmw, за да добавите промените. Е, успяхте, затова казвам, че ако нямате време, мога да го направя вместо вас. - person Brock123; 16.10.2011
comment
@Brock123, не е необходимо. Проверих сайта и въпреки че изглежда abandonware, сътрудник, gruber, актуализира скрипта преди месеци и го постави в cvs sourceforge хранилище - person joaquin; 16.10.2011
comment
може ли да ми го обясните, момчета, аз не го разбирам, аз съм начинаещ - person Cool Cloud; 12.07.2020