PyGobject(一百一十一)使用Pyinstaller打包成APP和EXE
在上一節PyGobject(一百一十)程式碼整合及GtkSource安裝使用中,對程式碼進行了整合,在自己電腦上或者別人安裝好同樣環境的電腦上才可以執行,如何將程式碼打包成App或者exe等可獨立執行的程式,在沒有裝過Python和PyGobject上的電腦上執行,就顯得比較重要,本文就是基於這個原因而誕生的。
首先我試過py2app,這個打包一般的應用比較好用,可惜的是目前還不支援PyGobject的打包,遂放棄了。不知py2exe能不能行。
接著嘗試使用Pyinstaller,官網上介紹說支援PyGobject。接下來詳細介紹~
Mac Pyinstaller安裝與使用
安裝
安裝比較簡單
pip3 install pyinstaller
使用
cd /Applications/Project/Python/project/PYGUI/pygtk3
pyinstaller --onedir -y -w gtk-demo.py
當然,沒有那麼簡單,打包過程中會有很多錯誤,gtk-demo.app打不開,開啟dist/gtk-demo目錄下的gtk-demo,報錯
/Applications/Project/Python/project/PYGUI/pygtk3/dist/gtk-demo/gi/module.py:178: Warning: cannot register existing type ‘gchar’
**
GLib-GObject:ERROR:gvaluetypes.c:455:_g_value_types_init: assertion failed: (type == G_TYPE_CHAR)
Abort trap: 6
我們還需要做一些工作,見下
修改pyinstaller hooks
現在直接使用Pyinstaller打包我的程式碼還有點問題,因為我有用到GtkSource,但是它沒有新增到Pyinstaller的hooks中
- 新增hooks/hook-gi.repository.GtkSource.py檔案
/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/PyInstaller/hooks/hook-gi.repository.GtkSource.py
# -----------------------------------------------------------------------------
# Copyright (c) 2005-2016, PyInstaller Development Team.
#
# Distributed under the terms of the GNU General Public License with exception
# for distributing bootloader.
#
# The full license is in the file COPYING.txt, distributed with this software.
# -----------------------------------------------------------------------------
"""
Import hook for PyGObject https://wiki.gnome.org/PyGObject
"""
from PyInstaller.utils.hooks import get_gi_typelibs, collect_glib_share_files
binaries, datas, hiddenimports = get_gi_typelibs('GtkSource', '3.0')
datas += collect_glib_share_files('gtksourceview-3.0')
- 新增hooks/pre_safe_import_module/hook-gi.repository.GtkSource.py 檔案
/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/PyInstaller/hooks/pre_safe_import_module/hook-gi.repository.GtkSource.py
#-----------------------------------------------------------------------------
# Copyright (c) 2005-2016, PyInstaller Development Team.
#
# Distributed under the terms of the GNU General Public License with exception
# for distributing bootloader.
#
# The full license is in the file COPYING.txt, distributed with this software.
#-----------------------------------------------------------------------------
def pre_safe_import_module(api):
# PyGObject modules loaded through the gi repository are marked as
# MissingModules by modulegraph so we convert them to
# RuntimeModules so their hooks are loaded and run.
api.add_runtime_module(api.module_name)
- 修改hooks/hook-gi.repository.cairo.py
新增下面一句
hiddenimports += ["cairo"]
- 修改hooks/hook-gi.repository.Gtk.py
新增datas += collect_glib_share_files(‘mime’)
datas += collect_glib_share_files('fontconfig')
datas += collect_glib_share_files('icons')
datas += collect_glib_share_files('themes')
datas += collect_glib_translations('gtk30')
datas += collect_glib_share_files('mime')
新增gir路徑
打包的時候有報錯
Unable to find gir directory: /share/gir-1.0.
解決辦法:
將$USER/gtk/inst/share/gir-1.0 複製到/share 目錄下
新增icon,theme,gtksourceview-3.0
Pyinstaller打包貌似對軟連線的支援不太好,將
/usr/share/icon;/usr/share/themes;/usr/share/gtksourceview-3.0
中的軟連線全部刪掉,將$USER/gtk/inst/share目錄下的相關內容拷貝到/usr/share對應的檔案下
再次打包
pyinstaller --onedir -y -w gtk-demo.py
生成的gtk-demo.app仍然打不開,開啟dist/gtk-demo目錄下的gtk-demo,報錯
GLib.Error: g-file-error-quark:
開啟檔案“/Applications/Project/Python/project/PYGUI/pygtk3/dist/gtk-demo/demos/Data/demo.gresource”失敗:open() 失敗:No such file or directory
看起來是檔案沒有打包進應用程式
執行上面的命令會生成一個gtk-demo.spec檔案,
開始它是這樣的
# -*- mode: python -*-
block_cipher = None
a = Analysis(['gtk-demo.py'],
pathex=['/Applications/Project/Python/project/PYGUI/pygtk3'],
binaries=None,
datas=None,
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
exclude_binaries=True,
name='gtk-demo',
debug=False,
strip=False,
upx=True,
console=False )
coll = COLLECT(exe,
a.binaries,
a.zipfiles,
a.datas,
strip=False,
upx=True,
name='gtk-demo')
app = BUNDLE(coll,
name='gtk-demo.app',
icon=None,
bundle_identifier=None)
現在我們需要對它做一點修改
# -*- mode: python -*-
block_cipher = None
APP_NAME='gtk-demo'
added_files = [( 'demos', '' ),]
binaries_files=[]
a = Analysis(['gtk-demo.py'],
pathex=['/Applications/Project/Python/project/PYGUI/pygtk3'],
binaries=binaries_files,
datas=added_files,
hiddenimports=["pygtkcompat"],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
exclude_binaries=True,
name='gtk-demo',
debug=False,
strip=False,
upx=True,
console=False )
coll = COLLECT(exe,
a.binaries,
a.zipfiles,
a.datas,
strip=False,
upx=True,
name='gtk-demo')
app = BUNDLE(coll,
name='gtk-demo.app',
icon=None,
bundle_identifier=None)
主要是講demos資料夾打包進應用程式
gtk-demo.py也要做一點改變
if getattr(sys, 'frozen', False):
# we are running in a bundle
DEMOROOTDIR = sys._MEIPASS
else:
# we are running in a normal Python environment
DEMOROOTDIR = os.path.abspath(os.path.dirname(__file__))
這個時候就需要使用這個配置檔案來打包應用程式了,
注意:打包命令不一樣了
pyinstaller --onedir -y gtk-demo.spec
打包完成後,開啟gtk-demo.app
結果是這樣的,如下圖,很模糊
解決辦法,spec檔案中新增
‘NSHighResolutionCapable’: ‘True’,具體見下
新增圖示和版本資訊,支援Retina屏
圖示字尾為icns,可以到這裡下載
修改gtk-demo.spec檔案
app中內容改成如下
app = BUNDLE(coll,
name='gtk-demo.app',
icon='gtk-demo.icns',
bundle_identifier=None,
info_plist={
'CFBundleName': APP_NAME,
'CFBundleDisplayName': APP_NAME,
'CFBundleGetInfoString': "Making gtk-demo",
'CFBundleIdentifier': "tk.xiaosanyu.gtk-demo",
'CFBundleVersion': "0.1.0",
'CFBundleShortVersionString': "0.1.0",
'NSHumanReadableCopyright': "Copyright © 2016, Xiaosanyu, All Rights Reserved",
'NSHighResolutionCapable': 'True',
})
再次執行上面的命令,開啟新生成的app
熟悉的畫面出現了,淚奔~~~
Windows Pyinstaller安裝與使用
安裝Python3.4
安裝pygboject
執行程式
- 選擇Python3.4安裝路徑
- 勾選要安裝的庫
我這個demo,我另外勾選了以下四個
如果你的專案需要其它的庫,如Gstreamer,可自行安裝
- 下一步,勾選gir,如果需要glade,可勾選
點選完成,完成安裝。
安裝pyinstaller
pip3 install pyinstaller
同MAC上一樣,需要對pyinstaller做一些修改
修改hooks
- 新增hooks/hook-gi.repository.GtkSource.py檔案
C:\Python34_32bit\Lib\site-packages\PyInstaller\hooks\hook-gi.repository.GtkSource.py
from PyInstaller.utils.hooks import get_gi_typelibs, collect_glib_share_files
binaries, datas, hiddenimports = get_gi_typelibs('GtkSource', '3.0')
datas += collect_glib_share_files('gtksourceview-3.0')
- 新增hooks/pre_safe_import_module/hook-gi.repository.GtkSource.py 檔案
C:\Python34_32bit\Lib\site-packages\PyInstaller\hooks\
pre_safe_import_module\hook-gi.repository.GtkSource.py
def pre_safe_import_module(api):
# PyGObject modules loaded through the gi repository are marked as
# MissingModules by modulegraph so we convert them to
# RuntimeModules so their hooks are loaded and run.
api.add_runtime_module(api.module_name)
- 新增hooks/hook-gi.repository.PangoFT2.py檔案
C:\Python34_32bit\Lib\site-packages\PyInstaller\hooks\hook-gi.repository.PangoFT2.py
from PyInstaller.utils.hooks import get_gi_typelibs, collect_glib_share_files
binaries, datas, hiddenimports = get_gi_typelibs('PangoFT2', '1.0')
新增hooks/pre_safe_import_module/hook-gi.repository.PangoFT2.py檔案
C:\Python34_32bit\Lib\site-packages\PyInstaller\hooks\
pre_safe_import_module\hook-gi.repository.PangoFT2.py
內容同
hooks/pre_safe_import_module/hook-gi.repository.GtkSource.py新增hooks/hook-gi.repository.fontconfig.py檔案
C:\Python34_32bit\Lib\site-packages\PyInstaller\hooks\hook-gi.repository.fontconfig.py
from PyInstaller.utils.hooks import get_gi_typelibs, collect_glib_share_files
binaries, datas, hiddenimports = get_gi_typelibs('fontconfig', '2.0')
新增hooks/pre_safe_import_module/hook-gi.repository.fontconfig.py檔案
C:\Python34_32bit\Lib\site-packages\PyInstaller\hooks\
pre_safe_import_module\hook-gi.repository.fontconfig.py
內容同
hooks/pre_safe_import_module/hook-gi.repository.GtkSource.py新增hooks/hook-gi.repository.freetype2.py檔案
C:\Python34_32bit\Lib\site-packages\PyInstaller\hooks\hook-gi.repository.freetype2.py
from PyInstaller.utils.hooks import get_gi_typelibs, collect_glib_share_files
binaries, datas, hiddenimports = get_gi_typelibs('freetype2', '2.0')
新增hooks/pre_safe_import_module/hook-gi.repository.freetype2.py檔案
C:\Python34_32bit\Lib\site-packages\PyInstaller\hooks\
pre_safe_import_module\hook-gi.repository.freetype2.py
內容同
hooks/pre_safe_import_module/hook-gi.repository.GtkSource.py修改hooks/hook-gi.repository.cairo.py
新增下面一句
hiddenimports += ["cairo"]
- 修改hooks/hook-gi.repository.GdkPixbuf.py
由於windows下沒有gdk-pixbuf-query-loaders命令,導致GdkPixbuf hook失敗,在這個檔案最後新增
from PyInstaller.utils.hooks import get_gi_typelibs
binaries, datas, hiddenimports = get_gi_typelibs('GdkPixbuf', '2.0')
datas += collect_glib_translations('gdk-pixbuf')
新增gir路徑
將C:\Python34_32bit\Lib\site-packages\gnome\share\gir-1.0目錄複製到C:\Python34_32bit\share\gir-1.0
開始打包
把在MAC環境下的gtk-demo.spec檔案拷貝過來,改一下
pathex=[‘c:\pygtk3’],
# -*- mode: python -*-
block_cipher = None
APP_NAME='gtk-demo'
added_files = [( 'demos', '' ),]
binaries_files=[]
a = Analysis(['gtk-demo.py'],
pathex=['c:\\pygtk3'],
binaries=binaries_files,
datas=added_files,
hiddenimports=["pygtkcompat"],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
exclude_binaries=True,
name='gtk-demo',
debug=False,
strip=False,
upx=True,
console=False )
coll = COLLECT(exe,
a.binaries,
a.zipfiles,
a.datas,
strip=False,
upx=True,
name='gtk-demo')
app = BUNDLE(coll,
name='gtk-demo.app',
icon='gtk-demo.icns',
bundle_identifier=None,
info_plist={
'CFBundleName': APP_NAME,
'CFBundleDisplayName': APP_NAME,
'CFBundleGetInfoString': "Making gtk-demo",
'CFBundleIdentifier': "tk.xiaosanyu.gtk-demo",
'CFBundleVersion': "0.1.0",
'CFBundleShortVersionString': "0.1.0",
'NSHumanReadableCopyright': "Copyright © 2016, Xiaosanyu, All Rights Reserved",
'NSHighResolutionCapable': 'True',
})
開始執行
cd c:\pygtk3
C:\Python34_32bit\Scripts\pyinstaller.exe -y --onedir gtk-demo.spec
開啟報錯
解決辦法,新增圖示後就好了
新增圖示
圖示字尾為ico,可以到這裡下載
修改spec. EXE配置項中新增icon圖示名
exe = EXE(pyz,
a.scripts,
exclude_binaries=True,
name='gtk-demo',
debug=False,
strip=False,
upx=True,
console=False,
icon="gtk-demo.ico" )
執行後開啟dist/gtk-demo/gtk-demo.exe
新增版本資訊
Windows下的pygobject及打包到目前為止都比Mac上簡單N倍,特別是pygobject的的安裝,簡直簡單到沒朋友。但是接下來的這個問題就為難到我了,那就是給exe檔案新增版本資訊。
參閱pyinstaller官方文件,發下有兩個命令用來獲取和新增版本資訊,那就是
- 獲取exe檔案版本資訊
C:\Python34_32bit\Scripts\pyi-grab_version.exe
- 設定exe檔案版本
C:\Python34_32bit\Scripts\pyi-set_version.exe
然而這兩個命令不支援Python3.x,博主(a87b01c14)經過一番努力,將這兩個命令做了一些修改,現在分享給大家
主要是修改了
C:\Python34_32bit\Lib\site-packages\PyInstaller\utils\win32\versioninfo.py這個檔案
# -*- coding: utf-8 -*-
#-----------------------------------------------------------------------------
# Copyright (c) 2013-2016, PyInstaller Development Team.
#
# Distributed under the terms of the GNU General Public License with exception
# for distributing bootloader.
#
# The full license is in the file COPYING.txt, distributed with this software.
#-----------------------------------------------------------------------------
import codecs
import struct
import pywintypes
import win32api
import pefile
import sys
# TODO implement read/write version information with pefile library.
# PE version info doc: http://msdn.microsoft.com/en-us/library/ms646981.aspx
def pefile_read_version(filename):
"""
Return structure like:
{
# Translation independent information.
# VS_FIXEDFILEINFO - Contains version information about a file. This information is language and code page independent.
u'FileVersion': (1, 2, 3, 4),
u'ProductVersion': (9, 10, 11, 12),
# PE files might contain several translations of version information.
# VS_VERSIONINFO - Depicts the organization of data in a file-version resource. It is the root structure that contains all other file-version information structures.
u'translations': {
'lang_id1' : {
u'Comments': u'日本語, Unicode 対応.',
u'CompanyName': u'your company.',
u'FileDescription': u'your file desc.',
u'FileVersion': u'1, 2, 3, 4',
u'InternalName': u'your internal name.',
u'LegalCopyright': u'your legal copyright.',
u'LegalTrademarks': u'your legal trademarks.',
u'OriginalFilename': u'your original filename.',
u'PrivateBuild': u'5, 6, 7, 8',
u'ProductName': u'your product name',
u'ProductVersion': u'9, 10, 11, 12',
u'SpecialBuild': u'13, 14, 15, 16',
},
'lang_id2' : {
...
}
}
}
Version info can contain multiple languages.
"""
# TODO
vers = {
'FileVersion': (0, 0, 0, 0),
'ProductVersion': (0, 0, 0, 0),
'translations': {
'lang_id1': {
'Comments': '',
'CompanyName': '',
'FileDescription': '',
'FileVersion': '',
'InternalName': '',
'LegalCopyright': '',
'LegalTrademarks': '',
'OriginalFilename': '',
'PrivateBuild': '',
'ProductName': '',
'ProductVersion': '',
'SpecialBuild': '',
}
}
}
pe = pefile.PE(filename)
#ffi = pe.VS_FIXEDFILEINFO
#vers['FileVersion'] = (ffi.FileVersionMS >> 16, ffi.FileVersionMS & 0xFFFF, ffi.FileVersionLS >> 16, ffi.FileVersionLS & 0xFFFF)
#vers['ProductVersion'] = (ffi.ProductVersionMS >> 16, ffi.ProductVersionMS & 0xFFFF, ffi.ProductVersionLS >> 16, ffi.ProductVersionLS & 0xFFFF)
#print(pe.VS_FIXEDFILEINFO.FileVersionMS)
# TODO Only first available language is used for now.
#vers = pe.FileInfo[0].StringTable[0].entries
from pprint import pprint
pprint(pe.VS_FIXEDFILEINFO)
print(dir(pe.VS_FIXEDFILEINFO))
print(repr(pe.VS_FIXEDFILEINFO))
print(pe.dump_info())
return vers
# Ensures no code from the executable is executed.
LOAD_LIBRARY_AS_DATAFILE = 2
STRINGTYPE = type(u'')
def getRaw(o):
return o.encode('UTF-16LE')
def decode(pathnm):
h = win32api.LoadLibraryEx(pathnm, 0, LOAD_LIBRARY_AS_DATAFILE)
if not nm :
print(pathnm+" don't have the version information")
win32api.FreeLibrary(h)
return
else:
nm=nm[0]
data = win32api.LoadResource(h, pefile.RESOURCE_TYPE['RT_VERSION'], nm)
vs = VSVersionInfo()
j = vs.fromRaw(data)
win32api.FreeLibrary(h)
return vs
class VSVersionInfo:
"""
WORD wLength; // length of the VS_VERSION_INFO structure
WORD wValueLength; // length of the Value member
WORD wType; // 1 means text, 0 means binary
WCHAR szKey[]; // Contains the Unicode string "VS_VERSION_INFO".
WORD Padding1[];
VS_FIXEDFILEINFO Value;
WORD Padding2[];
WORD Children[]; // zero or more StringFileInfo or VarFileInfo
// structures (or both) that are children of the
// current version structure.
"""
def __init__(self, ffi=None, kids=None):
self.ffi = ffi
self.kids = kids or []
def fromRaw(self, data):
i, (sublen, vallen, wType, nm) = parseCommon(data)
#vallen is length of the ffi, typ is 0, nm is 'VS_VERSION_INFO'.
i = int((i + 3) / 4) * 4
# Now a VS_FIXEDFILEINFO
self.ffi = FixedFileInfo()
j = self.ffi.fromRaw(data, i)
i = j
while i < sublen:
j = i
i, (csublen, cvallen, ctyp, nm) = parseCommon(data, i)
if nm.strip() == 'StringFileInfo':
sfi = StringFileInfo()
k = sfi.fromRaw(csublen, cvallen, nm, data, i, j+csublen)
self.kids.append(sfi)
i = k
else:
vfi = VarFileInfo()
k = vfi.fromRaw(csublen, cvallen, nm, data, i, j+csublen)
self.kids.append(vfi)
i = k
i = j + csublen
i = int((i + 3) / 4) * 4
return i
def toRaw(self):
nm = 'VS_VERSION_INFO'
rawffi = self.ffi.toRaw()
vallen = len(rawffi)
typ = 0
sublen = 6 + 2*len(nm) + 2
pad = b''
if sublen % 4:
pad = b'\000\000'
sublen = sublen + len(pad) + vallen
pad2 = b''
if sublen % 4:
pad2 = b'\000\000'
tmp=b''
for kid in self.kids:
tmp+=kid.toRaw()
sublen = sublen + len(pad2) + len(tmp)
result=(struct.pack('hhh', sublen, vallen, typ)
+ getRaw(nm) + b'\000\000' + pad + rawffi + pad2 + tmp)
return result
def __str__(self, indent=u''):
indent = indent + u' '
tmp = [kid.__str__(indent+u' ')
for kid in self.kids]
tmp = u', \n'.join(tmp)
return (u"""# UTF-8
#
# For more details about fixed file info 'ffi' see:
# http://msdn.microsoft.com/en-us/library/ms646997.aspx
VSVersionInfo(
%sffi=%s,
%skids=[
%s
%s]
)
""" % (indent, self.ffi.__str__(indent), indent, tmp, indent))
def parseCommon(data, start=0):
i = start + 6
(wLength, wValueLength, wType) = struct.unpack('3h', data[start:i])
i, text = parseUString(data, i, i+wLength)
return i, (wLength, wValueLength, wType, text)
def parseUString(data, start, limit):
i = start
while i < limit:
if data[i:i+2] == b'\000\000':
break
i += 2
if sys.version_info < (3, 0):
text = unicode(data[start:i], 'UTF-16LE')
else:
text = data[start:i].decode("UTF-16LE", "ignore")
i += 2
return i, text
class FixedFileInfo:
"""
DWORD dwSignature; //Contains the value 0xFEEFO4BD
DWORD dwStrucVersion; // binary version number of this structure.
// The high-order word of this member contains
// the major version number, and the low-order
// word contains the minor version number.
DWORD dwFileVersionMS; // most significant 32 bits of the file's binary
// version number
DWORD dwFileVersionLS; //
DWORD dwProductVersionMS; // most significant 32 bits of the binary version
// number of the product with which this file was
// distributed
DWORD dwProductVersionLS; //
DWORD dwFileFlagsMask; // bitmask that specifies the valid bits in
// dwFileFlags. A bit is valid only if it was
// defined when the file was created.
DWORD dwFileFlags; // VS_FF_DEBUG, VS_FF_PATCHED etc.
DWORD dwFileOS; // VOS_NT, VOS_WINDOWS32 etc.
DWORD dwFileType; // VFT_APP etc.
DWORD dwFileSubtype; // 0 unless VFT_DRV or VFT_FONT or VFT_VXD
DWORD dwFileDateMS;
DWORD dwFileDateLS;
"""
def __init__(self, filevers=(0, 0, 0, 0), prodvers=(0, 0, 0, 0),
mask=0x3f, flags=0x0, OS=0x40004, fileType=0x1,
subtype=0x0, date=(0, 0)):
self.sig = 0xfeef04bd
self.strucVersion = 0x10000
self.fileVersionMS = (filevers[0] << 16) | (filevers[1] & 0xffff)
self.fileVersionLS = (filevers[2] << 16) | (filevers[3] & 0xffff)
self.productVersionMS = (prodvers[0] << 16) | (prodvers[1] & 0xffff)
self.productVersionLS = (prodvers[2] << 16) | (prodvers[3] & 0xffff)
self.fileFlagsMask = mask
self.fileFlags = flags
self.fileOS = OS
self.fileType = fileType
self.fileSubtype = subtype
self.fileDateMS = date[0]
self.fileDateLS = date[1]
def fromRaw(self, data, i):
(self.sig,
self.strucVersion,
self.fileVersionMS,
self.fileVersionLS,
self.productVersionMS,
self.productVersionLS,
self.fileFlagsMask,
self.fileFlags,
self.fileOS,
self.fileType,
self.fileSubtype,
self.fileDateMS,
self.fileDateLS) = struct.unpack('13l', data[i:i+52])
return i+52
def toRaw(self):
return struct.pack('L12l', self.sig,
self.strucVersion,
self.fileVersionMS,
self.fileVersionLS,
self.productVersionMS,
self.productVersionLS,
self.fileFlagsMask,
self.fileFlags,
self.fileOS,
self.fileType,
self.fileSubtype,
self.fileDateMS,
self.fileDateLS)
def __str__(self, indent=u''):
fv = (self.fileVersionMS >> 16, self.fileVersionMS & 0xffff,
self.fileVersionLS >> 16, self.fileVersionLS & 0xFFFF)
pv = (self.productVersionMS >> 16, self.productVersionMS & 0xffff,
self.productVersionLS >> 16, self.productVersionLS & 0xFFFF)
fd = (self.fileDateMS, self.fileDateLS)
tmp = [u'FixedFileInfo(',
u'# filevers and prodvers should be always a tuple with four items: (1, 2, 3, 4)',
u'# Set not needed items to zero 0.',
u'filevers=%s,' % str(fv),
u'prodvers=%s,' % str(pv),
u"# Contains a bitmask that specifies the valid bits 'flags'r",
u'mask=%s,' % hex(self.fileFlagsMask),
u'# Contains a bitmask that specifies the Boolean attributes of the file.',
u'flags=%s,' % hex(self.fileFlags),
u'# The operating system for which this file was designed.',
u'# 0x4 - NT and there is no need to change it.',
u'OS=%s,' % hex(self.fileOS),
u'# The general type of file.',
u'# 0x1 - the file is an application.',
u'fileType=%s,' % hex(self.fileType),
u'# The function of the file.',
u'# 0x0 - the function is not defined for this fileType',
u'subtype=%s,' % hex(self.fileSubtype),
u'# Creation date and time stamp.',
u'date=%s' % str(fd),
u')'
]
return (u'\n'+indent+u' ').join(tmp)
class StringFileInfo(object):
"""
WORD wLength; // length of the version resource
WORD wValueLength; // length of the Value member in the current
// VS_VERSION_INFO structure
WORD wType; // 1 means text, 0 means binary
WCHAR szKey[]; // Contains the Unicode string 'StringFileInfo'.
WORD Padding[];
StringTable Children[]; // list of zero or more String structures
"""
def __init__(self, kids=None):
self.name = u'StringFileInfo'
self.kids = kids or []
def fromRaw(self, sublen, vallen, name, data, i, limit):
self.name = name
while i < limit:
st = StringTable()
j = st.fromRaw(data, i, limit)
self.kids.append(st)
i = j
return i
def toRaw(self):
vallen = 0
typ = 1
sublen = 6 + 2*len(self.name) + 2
pad = b''
if sublen % 4:
pad = b'\000\000'
tmp=b''
for kid in self.kids:
tmp+=kid.toRaw()
sublen = sublen + len(pad) + len(tmp)
if tmp[-2:] == '\000\000':
sublen = sublen - 2
result=(struct.pack('hhh', sublen, vallen, typ)
+ getRaw(self.name) + b'\000\000' + pad + tmp)
return result
def __str__(self, indent=u''):
newindent = indent + u' '
tmp = [kid.__str__(newindent)
for kid in self.kids]
tmp = u', \n'.join(tmp)
return (u'%sStringFileInfo(\n%s[\n%s\n%s])'
% (indent, newindent, tmp, newindent))
class StringTable:
"""
WORD wLength;
WORD wValueLength;
WORD wType;
WCHAR szKey[];
String Children[]; // list of zero or more String structures.
"""
def __init__(self, name=None, kids=None):
self.name = name or u''
self.kids = kids or []
def fromRaw(self, data, i, limit):
i, (cpsublen, cpwValueLength, cpwType, self.name) = parseCodePage(data, i, limit) # should be code page junk
#i = ((i + 3) / 4) * 4
while i < limit:
ss = StringStruct()
j = ss.fromRaw(data, i, limit)
i = j
self.kids.append(ss)
i = int((i + 3) / 4) * 4
return i
def toRaw(self):
vallen = 0
typ = 1
sublen = 6 + 2*len(self.name) + 2
tmp = b''
for kid in self.kids:
raw = kid.toRaw()
if len(raw) % 4:
raw = raw + b'\000\000'
tmp+=raw
sublen += len(tmp)
if tmp[-2:] == '\000\000':
sublen -= 2
result= (struct.pack('hhh', sublen, vallen, typ)
+ getRaw(self.name) + b'\000\000' + tmp)
return result
def __str__(self, indent=u''):
newindent = indent + u' '
tmp = map(str, self.kids)
tmp = (u',\n%s' % newindent).join(tmp)
return (u"%sStringTable(\n%su'%s',\n%s[%s])"
% (indent, newindent, self.name, newindent, tmp))
class StringStruct:
"""
WORD wLength;
WORD wValueLength;
WORD wType;
WCHAR szKey[];
WORD Padding[];
String Value[];
"""
def __init__(self, name=None, val=None):
self.name = name or u''
self.val = val or u''
def fromRaw(self, data, i, limit):
i, (sublen, vallen, typ, self.name) = parseCommon(data, i)
limit = i + sublen
i = int((i + 3) / 4) * 4
i, self.val = parseUString(data, i, limit)
return i
def toRaw(self):
if type(self.name) is STRINGTYPE:
# Convert unicode object to byte string.
raw_name = self.name.encode('UTF-16LE')
if type(self.val) is STRINGTYPE:
# Convert unicode object to byte string.
raw_val = self.val.encode('UTF-16LE')
# TODO document the size of vallen and sublen.
vallen = len(raw_val) + 2
typ = 1
sublen = 6 + len(raw_name) + 2
pad = b''
if sublen % 4:
pad = b'\000\000'
sublen = sublen + len(pad) + vallen
abcd = (struct.pack('hhh', sublen, vallen, typ)
+ raw_name + b'\000\000' + pad
+ raw_val + b'\000\000')
return abcd
def __str__(self, indent=''):
return u"StringStruct(u'%s', u'%s')" % (self.name, self.val)
def parseCodePage(data, i, limit):
i, (sublen, wValueLength, wType, nm) = parseCommon(data, i)
return i, (sublen, wValueLength, wType, nm)
class VarFileInfo:
"""
WORD wLength; // length of the version resource
WORD wValueLength; // length of the Value member in the current
// VS_VERSION_INFO structure
WORD wType; // 1 means text, 0 means binary
WCHAR szKey[]; // Contains the Unicode string 'VarFileInfo'.
WORD Padding[];
Var Children[]; // list of zero or more Var structures
"""
def __init__(self, kids=None):
self.kids = kids or []
def fromRaw(self, sublen, vallen, name, data, i, limit):
self.sublen = sublen
self.vallen = vallen
self.name = name
i = int((i + 3) / 4) * 4
while i < limit:
vs = VarStruct()
j = vs.fromRaw(data, i, limit)
self.kids.append(vs)
i = j
return i
def toRaw(self):
self.vallen = 0
self.wType = 1
self.name = 'VarFileInfo'
sublen = 6 + 2*len(self.name) + 2
pad = b''
if sublen % 4:
pad = b'\000\000'
tmp=b''
for kid in self.kids:
tmp+=kid.toRaw()
self.sublen = sublen + len(pad) + len(tmp)
result= (struct.pack('hhh', self.sublen, self.vallen, self.wType)
+ getRaw(self.name) + b'\000\000' + pad + tmp)
return result
def __str__(self, indent=''):
tmp = map(str, self.kids)
return "%sVarFileInfo([%s])" % (indent, ', '.join(tmp))
class VarStruct:
"""
WORD wLength; // length of the version resource
WORD wValueLength; // length of the Value member in the current
// VS_VERSION_INFO structure
WORD wType; // 1 means text, 0 means binary
WCHAR szKey[]; // Contains the Unicode string 'Translation'
// or a user-defined key string value
WORD Padding[]; //
WORD Value[]; // list of one or more values that are language
// and code-page identifiers
"""
def __init__(self, name=None, kids=None):
self.name = name or u''
self.kids = kids or []
def fromRaw(self, data, i, limit):
i, (self.sublen, self.wValueLength, self.wType, self.name) = parseCommon(data, i)
i = int((i + 3) / 4) * 4
for j in range(int(self.wValueLength/2)):
kid = struct.unpack('h', data[i:i+2])[0]
self.kids.append(kid)
i += 2
return i
def toRaw(self):
self.wValueLength = len(self.kids) * 2
self.wType = 0
sublen = 6 + 2*len(self.name) + 2
pad = b''
if sublen % 4:
pad =b'\000\000'
self.sublen = sublen + len(pad) + self.wValueLength
tmp=b''
for kid in self.kids:
tmp+=struct.pack('h', kid)
return (struct.pack('hhh', self.sublen, self.wValueLength, self.wType)
+ getRaw(self.name) + b'\000\000' + pad + tmp)
def __str__(self, indent=u''):
return u"VarStruct(u'%s', %r)" % (self.name, self.kids)
def SetVersion(exenm, versionfile):
if isinstance(versionfile, VSVersionInfo):
vs = versionfile
else:
fp = codecs.open(versionfile, 'rU', 'utf-8')
txt=fp.read()
fp.close()
vs = eval(txt)
data=vs.toRaw()
hdst = win32api.BeginUpdateResource(exenm, 0)
win32api.UpdateResource(hdst, pefile.RESOURCE_TYPE['RT_VERSION'], 1, data)
win32api.EndUpdateResource (hdst, 0)
還要修改一個檔案
C:\Python34_32bit\Lib\site-packages\PyInstaller\utils\cliutil
s\grab_version.py
try:
vs = PyInstaller.utils.win32.versioninfo.decode(args.exe