Python

使用受支持的构建系统(如Meson,CMake或Autotools)的Python应用程序可以使用标准方法构建。 但是,许多Python应用程序使用自定义安装脚本,或者希望通过Setuptools和``pip`` 安装。

对于这些情况,flatpak-builder 提供了 simple 构建系统。和自动化构建过程不同, simple 接受一个 build`-commands 的字符数组,将按顺序执行。

For example, the following YAML makes building the popular requests module rather straightforward:

name: requests
buildsystem: simple
build-commands:
  - pip3 install --prefix=/app --no-deps .
sources:
  - type: archive
    url: https://files.pythonhosted.org/packages/source/r/requests/requests-2.18.4.tar.gz
    sha256: 9c443e7324ba5b85070c4a818ade28bfabedf16ea10206da1132edaa6dda237e

这里,build-commands 是一个数组,包含了构建和安装模块的命令。可以看到,在这种情况下 pip 被用来安装模块。--prefix=/app 选项很重要,因为原本 pip 会尝试将模块安装到 /usr/ 目录下,又由于 /usr/ 在沙箱中是挂载为只读的,安装会失败。

注意 --no-deps 仅仅用于实例——一旦依赖的模块有它自己的依赖,构建就会失败。如果存在许多依赖,最好是用下一章节的方法来安装。

Building multiple Python dependencies

尽管上述的例子被安装了,它实际上也不能正常运行。正式因为它有大量的依赖没有安装:

  • certifi

  • chardet

  • idna

  • urllib3

Four dependencies aren’t very many, and could be installed using the simple method described above. However, anything more complex than this would quickly become tedious.

在这种情况下,flatpak-pip-generator 可以用来产生必要的清单JSON。这是一个Python脚本,可以获取包名和使用``pip``来确定依赖,以及依赖包的压缩包URL和hashes值。

使用 flatpak-pip-generator 是简单的:

$ python3 flatpak-pip-generator requests

Or if you have a requirements.txt file you can generate modules for every listed package:

$ python3 flatpak-pip-generator --requirements-file=requirements.txt

This will output a file called python3-requests.json, containing the necessary manifest JSON, which can then be included in your application’s manifest file. Even if your manifest uses YAML, you can still include JSON like this:

modules:
  - python3-requests.json
  # (other modules go here)