7

我正在使用 Babel 开发一个 Flask 应用程序。多亏了Distutils/Setuptools 集成,所有 compile/extract/... 函数的参数都存储在i18n 文件中setup.cfg,编译 i18n 文件就像

./setup.py compile_catalog

伟大的。现在我希望在运行时自动完成

./setup.py install

换句话说make,就是让install目标依赖于compile_catalog目标。

上下文

我们仅将翻译 ( .po) 文件存储在代码存储库中。.gitignore排除.mo.pot跟踪文件。

当开发人员拉取代码的新版本时,他运行

pip install -r requirements.txt

更新依赖项并在开发模式下安装项目。然后,使用上面的命令行,他编译翻译二进制 ( .mo) 文件。

是否有一种简单且推荐的方式来修改setup.py以一步完成这两项操作?还是我试图滥用setuptools

使用这样的脚本可以用于开发目的:

#!/bin/sh
./setup.py compile_catalog
pip install -r requirements.txt

但我想要一个在使用通常的setup.py安装说明安装软件包时也能工作的解决方案,比如从 PyPi 安装。

我应该明白吗setuptools不应该像这样使用,分发软件的人在创建档案时手动或使用自定义脚本编译他们的翻译文件,而不是依赖于setup.py在安装时编译它们?

我在互联网上没有找到很多解决这个问题的帖子。我发现的那些涉及pybabel从 中的函数运行命令行界面setup.py,这听起来很可惜,因为它错过了 setuptools 集成的要点。

4

1 回答 1

5

我认为你的要求是完全有效的,我很惊讶似乎没有关于如何实现这一点的官方指南。

我现在从事的项目也使用了多种语言,这就是我所做的:

  • setup.cfg中,输入适当的条目,以便compile_catalog可以在没有选项的情况下运行。

  • setup.py中,子类安装命令来自setuptools

设置.py:

from setuptools import setup
from setuptools.command.install import install

class InstallWithCompile(install):
    def run(self):
        from babel.messages.frontend import compile_catalog
        compiler = compile_catalog(self.distribution)
        option_dict = self.distribution.get_option_dict('compile_catalog')
        compiler.domain = [option_dict['domain'][1]]
        compiler.directory = option_dict['directory'][1]
        compiler.run()
        super().run()

然后,在调用 setup() 时,使用名称“install”注册我们的 InstallWithCompile 命令,并确保 *.mo 文件将包含在包中:

setup(
    ...
    cmdclass={
        'install': InstallWithCompile,
    },
    ...
    package_data={'': ['locale/*/*/*.mo', 'locale/*/*/*.po']},
)

由于在设置过程中使用了 babel,因此您应该将其添加为设置依赖项:

setup_requires=[
    'babel',
],

请注意,两者中都出现了一个包(此处setup_requiresinstall_requiresbabel ),python setup.py install由于.setuptoolspip install

于 2016-12-13T11:37:35.440 回答