|
1 |
| -Python |
2 |
| -====== |
| 1 | +Building python modules |
| 2 | +----------------------- |
| 3 | + |
| 4 | +Python applications that use supported buildsystems such as meson, |
| 5 | +cmake, or autotools can be built in the same way as regular c |
| 6 | +applications. However, many python apps and dependencies use custom |
| 7 | +install scripts or are expected to be installed through setuptools and |
| 8 | +pip. |
| 9 | + |
| 10 | +For these cases, ``flatpak-builder`` provides the ``simple`` |
| 11 | +buildsystem. Rather than trying to automate the process like the other |
| 12 | +buildsystems, ``simple`` takes a ``build-commands`` array of strings and |
| 13 | +only executes the commands given there. |
| 14 | + |
| 15 | +For example, this makes building the popular requests module rather |
| 16 | +straightforward: |
| 17 | + |
| 18 | +:: |
| 19 | + |
| 20 | + { |
| 21 | + "name": "requests", |
| 22 | + "buildsystem": "simple", |
| 23 | + "build-commands": [ |
| 24 | + "pip3 install --prefix=/app --no-deps ." |
| 25 | + ], |
| 26 | + "sources": [ |
| 27 | + { |
| 28 | + "type": "archive", |
| 29 | + "url": "https://files.pythonhosted.org/packages/source/r/requests/requests-2.18.4.tar.gz", |
| 30 | + "sha256": "9c443e7324ba5b85070c4a818ade28bfabedf16ea10206da1132edaa6dda237e" |
| 31 | + } |
| 32 | + ] |
| 33 | + } |
| 34 | + |
| 35 | +``name`` is the name of the module, in practice this also means the name |
| 36 | +folder in which your module will be built. |
| 37 | + |
| 38 | +``build-commands`` is an array with the commands you need to build and |
| 39 | +install your module. In this case we are running pip to do it, but the |
| 40 | +parameters are important. ``--prefix=/app`` is necessary, because |
| 41 | +otherwise pip would try to install it under ``/usr/`` and (because |
| 42 | +``/usr/`` is mounted read-only inside the sandbox) it would fail. |
| 43 | + |
| 44 | +``--no-deps`` is also relevant, flatpak-builder downloads all |
| 45 | +``sources`` before it starts building, and doesn't allow network access |
| 46 | +during once a build starts. This means that any attempts to download any |
| 47 | +further dependencies past that point would fail. It is used here for |
| 48 | +illustrative purposes, but it can be detrimental because it will make |
| 49 | +pip quiet about real errors. If you must install multiple dependencies, |
| 50 | +it is better to do it in one go using the method in the next section. |
| 51 | + |
| 52 | +Building multiple python dependencies |
| 53 | +------------------------------------- |
| 54 | + |
| 55 | +You'll notice that, even though it installs fine, it doesn't actually |
| 56 | +work. This is because requests has a number of dependencies that we've |
| 57 | +neglected to install: |
| 58 | + |
| 59 | +- certifi |
| 60 | +- chardet |
| 61 | +- idna |
| 62 | +- urllib3 |
| 63 | + |
| 64 | +Four dependencies aren't too much work, we could install all these using |
| 65 | +the same method and it would work just fine. However, anything more |
| 66 | +complex than this would quickly become tedious. |
| 67 | + |
| 68 | +For these cases, we can use |
| 69 | +`flatpak-pip-generator <https://github.com/flatpak/flatpak-builder-tools/tree/master/pip>`_, |
| 70 | +this is a python script that takes a package name and uses pip to track |
| 71 | +its dependencies, tarball urls and hashes. |
| 72 | + |
| 73 | +Using it is as simple as: |
| 74 | + |
| 75 | +:: |
| 76 | + |
| 77 | + $ python3 flatpak-pip-generator requests |
| 78 | + |
| 79 | +This will output a file called ``python3-requests.json`` containing json |
| 80 | +data that can be directly included among your manifest's modules. |
0 commit comments