使用 py2exe 捆绑 GTK 资源

发布于 12-11 21:03 字数 1301 浏览 0 评论 0原文

我正在使用 all-in 中的 Python 2.6 和 PyGTK 2.22.6 -Windows XP 上的一个安装程序,尝试构建单个文件可执行文件(通过 py2exe) 对于我的应用程序。

我的问题是,当我将应用程序作为脚本运行时(即未内置到 .exe 文件中,只是作为 .py 文件的松散集合),它使用看起来原生的 Windows 主题,但是当我运行构建的 exe 时,我看到默认的 GTK 主题。

我知道这个问题可以通过将一堆文件复制到 py2exe 创建的 dist 目录中来解决,但我读到的所有内容都涉及手动复制数据,而我希望这是一个自动部分构建过程。此外,有关该主题的所有内容(包括常见问题解答) 已过时 - PyGTK 现在将其文件保存在 C:\Python2x\Lib\site-packages\gtk-2.0\runtime\... 中,只需复制libetc 目录不能解决问题。

我的问题是:

  1. 我希望能够以编程方式在 setup.py 中查找 GTK 运行时数据,而不是硬编码路径。我该如何做到这一点?

  2. 我需要包含的最少资源是什么?

更新:我可能已经通过反复试验几乎回答了#2。为了使“wimp”(即 MS Windows)主题正常工作,我需要来自以下位置的文件:

runtime\lib\gtk-2.0\2.10.0\engines\libwimp.dll
runtime\etc\gtk-2.0\gtkrc
runtime\share\icons\*
runtime\share\themes\MS-Windows

...不带 runtime 前缀,但具有相同的目录结构,直接位于 dist py2exe 生成的目录。但是,鉴于 gtk.gtk_version(2,22,0)2.10.0 从何而来?

I'm using Python 2.6 and PyGTK 2.22.6 from the all-in-one installer on Windows XP, trying to build a single-file executable (via py2exe) for my app.

My problem is that when I run my app as a script (ie. not built into an .exe file, just as a loose collection of .py files), it uses the native-looking Windows theme, but when I run the built exe I see the default GTK theme.

I know that this problem can be fixed by copying a bunch of files into the dist directory created by py2exe, but everything I've read involves manually copying the data, whereas I want this to be an automatic part of the build process. Furthermore, everything on the topic (including the FAQ) is out of date - PyGTK now keeps its files in C:\Python2x\Lib\site-packages\gtk-2.0\runtime\..., and just copying the lib and etc directories doesn't fix the problem.

My questions are:

  1. I'd like to be able to programmatically find the GTK runtime data in setup.py rather than hard coding paths. How do I do this?

  2. What are the minimal resources I need to include?

Update: I may have almost answered #2 by trial-and-error. For the "wimp" (ie. MS Windows) theme to work, I need the files from:

runtime\lib\gtk-2.0\2.10.0\engines\libwimp.dll
runtime\etc\gtk-2.0\gtkrc
runtime\share\icons\*
runtime\share\themes\MS-Windows

...without the runtime prefix, but otherwise with the same directory structure, sitting directly in the dist directory produced by py2exe. But where does the 2.10.0 come from, given that gtk.gtk_version is (2,22,0)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北渚2024-12-18 21:03:59

在这里回答我自己的问题,但如果有人知道更好,也请随意回答。其中一些看起来非常脆弱(例如路径中的版本号),因此如果您知道更好的方法,请评论或编辑。

1. 查找文件

首先,我使用此代码实际查找 GTK 运行时的根目录。不过,这对于安装运行时的方式非常具体,并且可以通过对常见位置进行大量检查来改进:

#gtk file inclusion
import gtk
# The runtime dir is in the same directory as the module:
GTK_RUNTIME_DIR = os.path.join(
    os.path.split(os.path.dirname(gtk.__file__))[0], "runtime")

assert os.path.exists(GTK_RUNTIME_DIR), "Cannot find GTK runtime data"

2. 要包含哪些文件

这取决于 (a) 关注点的大小,以及 (b) ) 应用程序部署的上下文。我的意思是,您是否将其部署到任何人都可以拥有任意区域设置的整个世界,或者仅用于不需要翻译的股票字符串的公司内部使用?

如果您想要 Windows 主题,则需要包括:

GTK_THEME_DEFAULT = os.path.join("share", "themes", "Default")
GTK_THEME_WINDOWS = os.path.join("share", "themes", "MS-Windows")
GTK_GTKRC_DIR = os.path.join("etc", "gtk-2.0")
GTK_GTKRC = "gtkrc"
GTK_WIMP_DIR = os.path.join("lib", "gtk-2.0", "2.10.0", "engines")
GTK_WIMP_DLL = "libwimp.dll"

如果您想要 Tango 图标:

GTK_ICONS = os.path.join("share", "icons")

还有本地化数据(我省略了,但您可能不想):

GTK_LOCALE_DATA = os.path.join("share", "locale")

3. 将其拼凑在一起

首先,这是一个运行的函数给定点的文件系统树并生成适合 data_files 选项。

def generate_data_files(prefix, tree, file_filter=None):
    """
    Walk the filesystem starting at "prefix" + "tree", producing a list of files
    suitable for the data_files option to setup(). The prefix will be omitted
    from the path given to setup(). For example, if you have

        C:\Python26\Lib\site-packages\gtk-2.0\runtime\etc\...

    ...and you want your "dist\" dir to contain "etc\..." as a subdirectory,
    invoke the function as

        generate_data_files(
            r"C:\Python26\Lib\site-packages\gtk-2.0\runtime",
            r"etc")

    If, instead, you want it to contain "runtime\etc\..." use:

        generate_data_files(
            r"C:\Python26\Lib\site-packages\gtk-2.0",
            r"runtime\etc")

    Empty directories are omitted.

    file_filter(root, fl) is an optional function called with a containing
    directory and filename of each file. If it returns False, the file is
    omitted from the results.
    """
    data_files = []
    for root, dirs, files in os.walk(os.path.join(prefix, tree)):        
        to_dir = os.path.relpath(root, prefix)

        if file_filter is not None:
            file_iter = (fl for fl in files if file_filter(root, fl))
        else:
            file_iter = files

        data_files.append((to_dir, [os.path.join(root, fl) for fl in file_iter]))

    non_empties = [(to, fro) for (to, fro) in data_files if fro]

    return non_empties

所以现在你可以像这样调用 setup()

setup(
    # Other setup args here...

    data_files = (
                    # Use the function above...
                    generate_data_files(GTK_RUNTIME_DIR, GTK_THEME_DEFAULT) +
                    generate_data_files(GTK_RUNTIME_DIR, GTK_THEME_WINDOWS) +
                    generate_data_files(GTK_RUNTIME_DIR, GTK_ICONS) +

                    # ...or include single files manually
                    [
                        (GTK_GTKRC_DIR, [
                            os.path.join(GTK_RUNTIME_DIR,
                                GTK_GTKRC_DIR,
                                GTK_GTKRC)
                        ]),

                        (GTK_WIMP_DIR, [
                            os.path.join(
                                GTK_RUNTIME_DIR,
                                GTK_WIMP_DIR,
                                GTK_WIMP_DLL)
                        ])
                    ]
                )
)

Answering my own question here, but if anyone knows better feel free to answer too. Some of it seems quite fragile (eg. version numbers in paths), so comment or edit if you know a better way.

1. Finding the files

Firstly, I use this code to actually find the root of the GTK runtime. This is very specific to how you install the runtime, though, and could probably be improved with a number of checks for common locations:

#gtk file inclusion
import gtk
# The runtime dir is in the same directory as the module:
GTK_RUNTIME_DIR = os.path.join(
    os.path.split(os.path.dirname(gtk.__file__))[0], "runtime")

assert os.path.exists(GTK_RUNTIME_DIR), "Cannot find GTK runtime data"

2. What files to include

This depends on (a) how much of a concern size is, and (b) the context of your application's deployment. By that I mean, are you deploying it to the whole wide world where anyone can have an arbitrary locale setting, or is it just for internal corporate use where you don't need translated stock strings?

If you want Windows theming, you'll need to include:

GTK_THEME_DEFAULT = os.path.join("share", "themes", "Default")
GTK_THEME_WINDOWS = os.path.join("share", "themes", "MS-Windows")
GTK_GTKRC_DIR = os.path.join("etc", "gtk-2.0")
GTK_GTKRC = "gtkrc"
GTK_WIMP_DIR = os.path.join("lib", "gtk-2.0", "2.10.0", "engines")
GTK_WIMP_DLL = "libwimp.dll"

If you want the Tango icons:

GTK_ICONS = os.path.join("share", "icons")

There is also localisation data (which I omit, but you might not want to):

GTK_LOCALE_DATA = os.path.join("share", "locale")

3. Piecing it together

Firstly, here's a function that walks the filesystem tree at a given point and produces output suitable for the data_files option.

def generate_data_files(prefix, tree, file_filter=None):
    """
    Walk the filesystem starting at "prefix" + "tree", producing a list of files
    suitable for the data_files option to setup(). The prefix will be omitted
    from the path given to setup(). For example, if you have

        C:\Python26\Lib\site-packages\gtk-2.0\runtime\etc\...

    ...and you want your "dist\" dir to contain "etc\..." as a subdirectory,
    invoke the function as

        generate_data_files(
            r"C:\Python26\Lib\site-packages\gtk-2.0\runtime",
            r"etc")

    If, instead, you want it to contain "runtime\etc\..." use:

        generate_data_files(
            r"C:\Python26\Lib\site-packages\gtk-2.0",
            r"runtime\etc")

    Empty directories are omitted.

    file_filter(root, fl) is an optional function called with a containing
    directory and filename of each file. If it returns False, the file is
    omitted from the results.
    """
    data_files = []
    for root, dirs, files in os.walk(os.path.join(prefix, tree)):        
        to_dir = os.path.relpath(root, prefix)

        if file_filter is not None:
            file_iter = (fl for fl in files if file_filter(root, fl))
        else:
            file_iter = files

        data_files.append((to_dir, [os.path.join(root, fl) for fl in file_iter]))

    non_empties = [(to, fro) for (to, fro) in data_files if fro]

    return non_empties

So now you can call setup() like so:

setup(
    # Other setup args here...

    data_files = (
                    # Use the function above...
                    generate_data_files(GTK_RUNTIME_DIR, GTK_THEME_DEFAULT) +
                    generate_data_files(GTK_RUNTIME_DIR, GTK_THEME_WINDOWS) +
                    generate_data_files(GTK_RUNTIME_DIR, GTK_ICONS) +

                    # ...or include single files manually
                    [
                        (GTK_GTKRC_DIR, [
                            os.path.join(GTK_RUNTIME_DIR,
                                GTK_GTKRC_DIR,
                                GTK_GTKRC)
                        ]),

                        (GTK_WIMP_DIR, [
                            os.path.join(
                                GTK_RUNTIME_DIR,
                                GTK_WIMP_DIR,
                                GTK_WIMP_DLL)
                        ])
                    ]
                )
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文