You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

345 lines
19 KiB

  1. # Python packages folder
  2. ## Table of contents
  3. 1. [Description](#description)
  4. 2. [Introduction](#introduction)
  5. 3. [Python 2 end-of-life](#python-2-end-of-life)
  6. 4. [Using Python in external/other package feeds](#using-python-in-externalother-package-feeds)
  7. 5. [Build considerations](#build-considerations)
  8. 6. [General folder structure](#general-folder-structure)
  9. 7. [Building a Python package](#building-a-python-package)
  10. 1. [Include python3-package.mk](#include-python3-packagemk)
  11. 2. [Add Package/<PKG_NAME> OpenWrt definitions](#add-packagepkg_name-openwrt-definitions)
  12. 3. [Python package dependencies](#python-package-dependencies)
  13. 4. [Wrapping things up so that they build](#wrapping-things-up-so-that-they-build)
  14. 5. [Customizing things](#customizing-things)
  15. 6. [Host-side Python packages for build](#host-side-python-packages-for-build)
  16. ## Description
  17. This section describes specifics for the Python packages that are present in this repo, and how things are structured.
  18. In terms of license, contributing guide, etc, all of that information is described in the top [README.md](README.md) file, and it applies here as well. This document attempts to cover only technical aspects of Python packages, and maybe some explanations about how things are (and why they are as they are).
  19. ## Introduction
  20. This sub-tree came to exist after a number of contributions (Python packages) were made to this repo, and the [lang](lang) subtree grew to a point where a decision was made to move all Python packages under [lang/python](lang/python).
  21. It contains the Python 3 interpreter and Python packages. Most of the Python packages are downloaded from [pypi.org](https://pypi.org/). Python packages from [pypi.org](https://pypi.org/) are typically preferred when adding new packages.
  22. If more packages (than the ones packaged here) are needed, they can be downloaded via [pip](https://pip.pypa.io). Note that the versions of `pip` & `setuptools` [available in this repo] are the ones that are packaged inside the Python package (yes, Python comes packaged with `pip` & `setuptools`).
  23. ## Python 2 end-of-life
  24. Python 2 [will not be maintained past 2020](https://www.python.org/dev/peps/pep-0373/). All Python 2 packages have been removed from the packages feed (this repo) and archived in the [abandoned packages feed](https://github.com/openwrt/packages-abandoned).
  25. ## Using Python in external/other package feeds
  26. In the feeds.conf (or feeds.conf.default file, whatever is preferred), the packages repo should be present.
  27. Example
  28. ```
  29. src-git packages https://git.openwrt.org/feed/packages.git
  30. src-git luci https://git.openwrt.org/project/luci.git
  31. src-git routing https://git.openwrt.org/feed/routing.git
  32. src-git telephony https://git.openwrt.org/feed/telephony.git
  33. #
  34. #
  35. src-git someotherfeed https://github.com/<github-user>/<some-other-package>
  36. ```
  37. Assuming that there are Python packages in the `<some-other-package>`, they should include `python3-package.mk` like this:
  38. ```
  39. include $(TOPDIR)/feeds/packages/lang/python/python3-package.mk
  40. ```
  41. Same rules apply for `python3-package.mk` as the Python packages in this repo.
  42. **One important consideration:**: if the local name is not `packages`, it's something else, like `openwrt-packages`. And in `feeds.conf[.default]` it's:
  43. ```
  44. src-git openwrt-packages https://git.openwrt.org/feed/packages.git
  45. ```
  46. Then, the inclusions also change:
  47. ```
  48. include $(TOPDIR)/feeds/openwrt-packages/lang/python/python3-package.mk
  49. ```
  50. Each maintainer[s] of external packages feeds is responsible for the local name, and relative inclusion path back to this feed (which is named `packages` by default).
  51. In case there is a need/requirement such that the local package feed is named something else than `packages`, one approach to make the package flexible to change is:
  52. ```
  53. PYTHON3_PACKAGE_MK:=$(wildcard $(TOPDIR)/feeds/*/lang/python/python3-package.mk)
  54. # verify that there is only one single file returned
  55. ifneq (1,$(words $(PYTHON3_PACKAGE_MK)))
  56. ifeq (0,$(words $(PYTHON3_PACKAGE_MK)))
  57. $(error did not find python3-package.mk in any feed)
  58. else
  59. $(error found multiple python3-package.mk files in the feeds)
  60. endif
  61. else
  62. $(info found python3-package.mk at $(PYTHON3_PACKAGE_MK))
  63. endif
  64. include $(PYTHON3_PACKAGE_MK)
  65. ```
  66. This should solve the corner-case where the `python3-package.mk` can be in some other feed, or if the packages feed will be named something else locally.
  67. ## Build considerations
  68. In order to build the Python interpreter, a host Python interpreter needs to be built, in order to process some of the build for the target Python build. The host Python interpreter is also needed so that Python bytecodes are generated, so the host interpreter needs to be the exact version as on the target. And finally, the host Python interpreter also provides pip, so that it may be used to install some Python packages that are required to build other Python packages.
  69. That's why you'll also see a Python build & staging directories.
  70. As you're probably thinking, this sounds [and is] somewhat too much complication [just for packaging], but the status of things is-as-it-is, and it's probably much worse than what's currently visible on the surface [with respect to packaging Python & packages].
  71. As mentioned earlier, Python packages are shipped with bytecodes, and the reason for this is simply performance & size.
  72. The thought/discussion matrix derives a bit like this:
  73. * shipping both Python source-code & bytecodes takes too much space on some devices ; Python source code & byte-code take about similar disk-size
  74. * shipping only Python source code has a big performance penalty [on some lower end systems] ; something like 500 msecs (Python source-only) -> 70 msecs (Python byte-codes) time reduction for a simple "Hello World" script
  75. * shipping only Python byte-codes seems like a good trade-off, and this means that `python3-src` can be provided for people that want the source code
  76. By default, automatic Python byte-code generation is disabled when running a Python script, in order to prevent a disk from accidentally filling up. Since some disks reside in RAM, this also means not filling up the RAM. If someone wants to convert Python source to byte-code then he/she is free to compile it [directly on the device] manually via the Python interpreter & library.
  77. ## General folder structure
  78. The basis of all these packages is:
  79. * [lang/python/python3](lang/python/python3) - The Python 3.x.y interpreter
  80. This is a normal OpenWrt package, which will build the Python interpreter. This also provides `python3-pip` & `python3-setuptools`. Each Python package is actually split into multiple sub-packages [e.g. python3-email, python3-sqlite3, etc]. This can be viewed inside [lang/python/python3/files](lang/python/python3/files).
  81. The reason for this splitting, is purely to offer a way for some people to package Python in as-minimal-as-possible-and-still-runable way, and also to be somewhat maintainable when packaging. A standard Python installation can take ~20-30 MBs of disk, which can be somewhat big for some people, so there is the `python3-base` package which brings that down to ~5 MBs. This seems to be good enough (and interesting) for a number of people.
  82. The Python interpreter is structured like this:
  83. * `python3-base`, which is just the minimal package to startup Python and run basic commands
  84. * `python3` is a meta-package, which installs almost everything (python3-base [plus] Python library [minus] some unit-tests & some windows-y things)
  85. * `python3-light` is `python3` [minus] packages that are in [lang/python/python3/files](lang/python/python3/files) ; the size of this package may be sensible (and interesting) to another group of people
  86. All other Python packages (aside from the intepreter) typically use these files:
  87. * **python3-host.mk** - this file contains paths and build rules for running the Python interpreter on the host-side; they also provide paths to host interprete, host Python lib-dir & so on
  88. * **python3-package.mk**
  89. * includes **python3-host.mk**
  90. * contains all the default build rules for Python packages; these will be detailed below in the [Building a Python package](#Building a Python package) section
  91. **Note** that Python packages don't need to use these files (i.e. `python3-package.mk` & `python3-host.mk`), but they do provide some ease-of-use & reduction of duplicate code. And they do contain some learned-lessons about packaging Python packages, so it's a good idea to use them.
  92. ## Building a Python package
  93. Packaging for Python uses the `VARIANT` mechanism for packaging inside OpenWrt. (#### FIXME: find a link for this later if it exists)
  94. ### Include python3-package.mk
  95. Add this after `include $(INCLUDE_DIR)/package.mk`
  96. ```
  97. include ../python3-package.mk
  98. ```
  99. This will make sure that build rules for Python can be specified and picked up for build.
  100. ### Include pypi.mk (optional)
  101. If the package source code will be downloaded from [pypi.org](https://pypi.org/), including `pypi.mk` can help simplify the package Makefile.
  102. To use `pypi.mk`, add this **before** `include $(INCLUDE_DIR)/package.mk`:
  103. ```
  104. include ../pypi.mk
  105. ```
  106. `pypi.mk` has several `PYPI_*` variables that must/can be set (see below); these should be set before `pypi.mk` is included, i.e. before the `include ../pypi.mk` line.
  107. `pypi.mk` also provides default values for `PKG_SOURCE` and `PKG_SOURCE_URL`, so these variables may be omitted.
  108. One variable is required:
  109. * `PYPI_NAME`: Package name on pypi.org. This should match the PyPI name exactly.
  110. For example (from the `python-yaml` package):
  111. ```
  112. PYPI_NAME:=PyYAML
  113. ```
  114. These variables are optional:
  115. * `PYPI_SOURCE_NAME`: Package name component of the source tarball filename
  116. Default: Same value as `PYPI_NAME`
  117. * `PYPI_SOURCE_EXT`: File extension of the source tarball filename
  118. Default: `tar.gz`
  119. `pypi.mk` constructs the default `PKG_SOURCE` value from these variables (and `PKG_VERSION`):
  120. ```
  121. PKG_SOURCE?=$(PYPI_SOURCE_NAME)-$(PKG_VERSION).$(PYPI_SOURCE_EXT)
  122. ```
  123. The `PYPI_SOURCE_*` variables allow this default `PKG_SOURCE` value to be customized as necessary.
  124. ### Add Package/<PKG_NAME> OpenWrt definitions
  125. This part is similar to default OpenWrt packages.
  126. Example:
  127. ```
  128. define Package/python3-lxml
  129. SECTION:=lang
  130. CATEGORY:=Languages
  131. SUBMENU:=Python
  132. TITLE:=Pythonic XML processing library
  133. URL:=https://lxml.de
  134. DEPENDS:=+python3-light +libxml2 +libxslt +libexslt
  135. VARIANT:=python3
  136. endef
  137. define Package/python3-lxml/description
  138. The lxml XML toolkit is a Pythonic binding
  139. for the C libraries libxml2 and libxslt.
  140. endef
  141. ```
  142. Some considerations here (based on the example above):
  143. * `VARIANT=python3` must be added
  144. * typically the package is named `Package/python3-<something>` ; this convention makes things easier to follow, though it could work without naming things this way
  145. * `TITLE` can be something a bit more verbose/neat ; typically the name is short as seen above
  146. ### Python package dependencies
  147. Aside from other libraries and programs, every Python package will depend on at least one of these three types of packages:
  148. * The Python interpreter: All Python packages should depend on one of these three interpreter packages:
  149. * `python3-light` is the best default for most Python packages.
  150. * `python3-base` should only be used as a dependency if you are certain the bare interpreter is sufficient.
  151. * `python3` is useful if many (more than three) Python standard library packages are needed.
  152. * Python standard library packages: As noted above, many parts of the Python standard library are packaged separate from the Python interpreter. These packages are defined by the files in [lang/python/python3/files](lang/python/python3/files).
  153. To find out which of these separate standard library packages are necessary, after completing a draft Makefile (including the `$(eval ...)` lines described in the next section), run `make` with the `configure` target and `PY3=stdlib V=s` in the command line. For example:
  154. ```
  155. make package/python-lxml/configure PY3=stdlib V=s
  156. ```
  157. If the package has been built previously, include the `clean` target to trigger configure again:
  158. ```
  159. make package/python-lxml/{clean,configure} PY3=stdlib V=s
  160. ```
  161. This will search the package for module imports and generate a list of suggested dependencies. Some of the found imports may be false positives, e.g. in example or test files, so examine the matches for more information.
  162. * Other Python packages: The easiest way to find these dependencies is to look for the `install_requires` keyword inside the package's `setup.py` file (it will be a keyword argument to the `setup()` function). This will be a list of run-time dependencies for the package.
  163. There may already be packages in the packages feed that provide these dependencies. If not, they will need to be packaged for your Python package to function correctly.
  164. Any packages in a `setup_requires` keyword argument are build-time dependencies that may need to be installed on the host (host Python inside of OpenWrt buildroot, not system Python that is part of the outer computer system). To ensure these build-time dependencies are present, see [Host-side Python packages for build](#host-side-python-packages-for-build). (Note that Setuptools is already installed as part of host Python.)
  165. ### Wrapping things up so that they build
  166. If all the above prerequisites have been met, all that's left is:
  167. ```
  168. $(eval $(call Py3Package,python3-lxml))
  169. $(eval $(call BuildPackage,python3-lxml))
  170. ```
  171. The `$(eval $(call Py3Package,python3-lxml))` part will instantiate all the default Python build rules so that the final Python package is packaged into an OpenWrt.
  172. And `$(eval $(call BuildPackage,python3-lxml))` will bind all the rules generated with `$(eval $(call Py3Package,python3-lxml))` into the OpenWrt build system.
  173. These packages will contain byte-codes and binaries (shared libs & other stuff).
  174. If a user wishes to ship source code, adding one more line creates one more package that ship Python source code:
  175. ```
  176. $(eval $(call Py3Package,python3-lxml))
  177. $(eval $(call BuildPackage,python3-lxml))
  178. $(eval $(call BuildPackage,python3-lxml-src))
  179. ```
  180. The name `*-src` must be the Python package name; so for `python3-lxml-src` a equivalent `python3-lxml` name must exist.
  181. ### Customizing things
  182. Some packages need custom build rules (because they do).
  183. The default package build and install processes are defined in `python3-package.mk`.
  184. #### Building
  185. The default build process calls `setup.py install` inside the directory where the Python source package is extracted (`PKG_BUILD_DIR`). This "installs" the Python package to an intermediate location (`PKG_INSTALL_DIR`) where it is used by the default install process.
  186. There are several Makefile variables that can be used to customize this process (all optional):
  187. * `PYTHON3_PKG_SETUP_DIR`: Path where `setup.py` can be found, relative to the package directory (`PKG_BUILD_DIR`).
  188. Default: empty value (`setup.py` is in the package directory)
  189. * `PYTHON3_PKG_SETUP_VARS`: Additional environment variables to set for the call to `setup.py`. Should be in the form of `VARIABLE1=value VARIABLE2=value ...`.
  190. Default: empty value
  191. * `PYTHON3_PKG_SETUP_GLOBAL_ARGS`: Additional command line arguments to pass to `setup.py`, before / in front of the `install` command.
  192. Default: empty value
  193. * `PYTHON3_PKG_SETUP_ARGS`: Additional command line arguments to pass to `setup.py`, after the `install` command.
  194. Default: `--single-version-externally-managed`
  195. Conceptually, these variables are used in this way:
  196. ```
  197. cd $(PKG_BUILD_DIR)/$(PYTHON3_PKG_SETUP_DIR)
  198. $(PYTHON3_PKG_SETUP_VARS) python3 setup.py $(PYTHON3_PKG_SETUP_GLOBAL_ARGS) install $(PYTHON3_PKG_SETUP_ARGS)
  199. ```
  200. The default build process can be completely overridden by defining a custom `Py3Build/Compile` rule in the package Makefile.
  201. #### Installing
  202. The default install process copies some/all of the files from `PKG_INSTALL_DIR`, placed there by the build process, to a location passed to the install rule as the first argument (`$(1)`). The OpenWrt build system will then take those files and create the actual .ipk package archives.
  203. This default process uses 2 build rules:
  204. * `Py3Package/<package>/filespec` which are Python library files relative to `/usr/lib/pythonX.Y` ; by default this is `/usr/lib/python$(PYTHON3_VERSION)/site-packages` (`PYTHON3_PKG_DIR`) ; most Python packages generate files that get installed in this sub-folder
  205. * `Py3Package/<package>/install` is similar to `Package/<package>/install` ; this allows binary (or other files) to be installed on the target
  206. Both the 2 above rules generate a `Package/<package>/install` build rule, which gets picked up by the build system. Both can be used together (they are not mutually exclusive), and provide a good enough flexibility for specifying Python packages.
  207. The `Py3Package/<package>/filespec` rule contains one or more lines of the following format (whitespace added for clarity):
  208. ```
  209. <one of: +-=> | <file/directory path> | <file permissions>
  210. ```
  211. The initial character controls the action that will be taken:
  212. * `+`: Install the given path. If the path is a directory, all files and subdirectories inside are installed.
  213. * If file permissions is specified (optional), then the file or directory (and all files and subdirectories) are assigned the given permissions; if omitted, then the file or directory retains its original permissions.
  214. * `-`: Remove the given path. Useful when most of a directory should be installed except for a few files or subdirectories.
  215. * File permissions is not used / ignored in this case.
  216. * `=`: Assign the given file permissions to the given path. File permissions is required in this case.
  217. As mentioned, the default `Py3Package/<package>/filespec` installs `PYTHON3_PKG_DIR`:
  218. ```
  219. define Py3Package/python3-example/filespec
  220. +|$(PYTHON3_PKG_DIR)
  221. endef
  222. ```
  223. If the package installs a `example_package` directory inside `PYTHON3_PKG_DIR`, and there is an `examples` directory and `test_*.py` files that can be omitted to save space, this can be specified as:
  224. ```
  225. define Py3Package/python3-example/filespec
  226. +|$(PYTHON3_PKG_DIR)
  227. -|$(PYTHON3_PKG_DIR)/example_package/examples
  228. -|$(PYTHON3_PKG_DIR)/example_package/test_*.py
  229. endef
  230. ```
  231. ### Host-side Python packages for build
  232. These can be installed via pip and ideally they should only be installed like this, because it's a bit simpler than running them through the OpenWrt build system. Build variants on the host-side build are more complicated (and nearly impossible to do sanely) in the current OpenWrt build system.
  233. Which is why [for example] if you need python cffi on the host build, it's easier to just add it via:
  234. ```
  235. HOST_PYTHON3_PACKAGE_BUILD_DEPENDS:="cffi==$(PKG_VERSION)"
  236. ```
  237. [cffi is one of those packages that needs a host-side package installed].
  238. This works reasonably well in the current OpenWrt build system, as binaries get built for this package and get installed in the staging-dir `$(STAGING_DIR)/usr/lib/pythonX.Y/site-packages`.