如何使用setuptools打包包含gsettings模式的Python应用程序?

我正在尝试使用setuptools打包一个依赖于gsettings来存储和检索用户偏好的Python应用程序。然而,我以前没有使用过该工具,不确定如何处理setup.py脚本,以指示它安装并编译模式。
2个回答

  1. 将您的模式添加到 setup.py 中:

    setup(...,
          data_files=[('/usr/share/glib-2.0/schemas', ['filename.schema.xml'])]
         )
    
  2. setup.py 中添加一个系统调用以运行:

    glib-compile-schemas /usr/share/glib-2.0/schemas
    
如下所述,ntc2评论说当使用自定义安装路径时,例如使用--user,这将失败。
  • 一个可能的解决方案是使用相对路径share/glib-2.0/schemas,这也意味着使用sys.prefix变量重建glib-compile-schemas命令的输入文件夹路径。

1这将无法适用于--user安装。 - ntc2
@ntc2 一个可能的解决方案是使用相对路径 share/glib-2.0/schemas,这也意味着使用 sys.prefix 变量来重建 glib-compile-schemas 命令的输入文件夹路径。 - user.dz

您可以在setup.py中的自定义install_data子类中运行glib-compile-schemas。例如,这是我们在{{link1:fluxgui的setup.py}}中的做法:
from distutils.core import setup
from distutils.log import info
import distutils.command.install_data, os.path

# On Ubuntu 18.04 both '/usr/local/share/glib-2.0/schemas' (global
# install) and '~/.local/share/glib-2.0/schemas' (local '--user'
# install) are on the default search path for glib schemas. The global
# search paths are in '$XDG_DATA_DIRS'.
gschema_dir_suffix = 'share/glib-2.0/schemas'

data_files = [<other data files>,
    (gschema_dir_suffix, ['apps.fluxgui.gschema.xml'])]

class install_data(distutils.command.install_data.install_data):
    def run(self):
        # Python 3 'super' call.
        super().run()

        # Compile '*.gschema.xml' to update or create 'gschemas.compiled'.
        info("compiling gsettings schemas")
        # Use 'self.install_dir' to build the path, so that it works
        # for both global and local '--user' installs.
        gschema_dir = os.path.join(self.install_dir, gschema_dir_suffix)
        self.spawn(["glib-compile-schemas", gschema_dir])

setup(<other setup args>,
    cmdclass = {'install_data': install_data})