Creating a Sample Python Application¶
Use this procedure to create a sample Python application as a foundation for learning how to create a binary package with eLxr.
About This Task¶
The application detailed in this procedure demonstrates the Fibonacci series, which takes the number of terms as a command-line parameter. You will create the minimum source files necessary for building the application and identifying how you plan to share it. For a Python-based application, this includes the following files. These files are specific to the application itself, and required to build and test the application prior to packaging it.
Two source files, including a main.py file to define application functionality and an __init__.py file for directories to be importable packages.
A setup.py file that defines build requirements for Debian tools.
A Makefile to provide guidance for building the final binary and clean up the working directory
A LICENSE file
This example shows how to work with applications that are not yet part of your eLxr 12 image to help you understand the requirements for adding custom application packages. You can use this procedure for any stand-alone application that you want to develop and test apart from your eLxr image.
Note
A set of source files for the Python fibonacci application is available at eLxr User Documentation: Samples repository. Simply create your folder structure and copy these files into your working directory to save yourself some time.
If you already have an application that you want to use, go to Modifying Package Template Files for Python Applications to get your application ready for packaging.
Before You Begin¶
You must have a Linux development host or eLxr 12 image.
You must have the required development packages on your Linux development host or eLxr 12 image. For details, see Development Tools Overview and Requirements
Procedure¶
Create a working directory for your application project.
$ mkdir -p ~/elxr-devpy/fibonacci-python-1.0 $ cd ~/elxr-devpy/fibonacci-python-1.0
Inside the fibonacci-python-1.0 directory, create an app subdirectory to organize your source code.
$ mkdir app
Set up the main.py file in the fibonacci-python-1.0/app directory.
$ vi app/main.py # Copyright 2024 Wind River Systems, Inc. def fibonacci(n): if n <= 0: return [] elif n == 1: return [0] elif n == 2: return [0, 1] series = [0, 1] for _ in range(2, n): series.append(series[-1] + series[-2]) return series def main(): try: n = int(input("Enter the number of terms for the Fibonacci series: ")) series = fibonacci(n) print("Fibonacci series:") print(series) except ValueError: print("Invalid input. Please enter an integer.") if __name__ == "__main__": main()Set up the __init__.py file in the fibonacci-python-1.0/app directory. Debian uses standard Python import rules to verify module layout. As a result, the __init__.py file is required for directories to be importable packages.
$ vim app/__init__.py #This file can be empty also.
Create the Makefile in the fibonacci-python-1.0 directory.
$ vi Makefile
Update the file to include the following contents:
# # This is public domain software # # Makefile for Python package archive VERSION := 1.0 PKG_NAME := fibonacci-python SRC_DIR := app SRC := $(SRC_DIR)/main.py ARCHIVE := $(PKG_NAME)-$(VERSION).tar.gz .PHONY: all archive install clean all: archive archive: $(ARCHIVE) $(ARCHIVE): $(SRC) setup.py Makefile @echo "Creating source archive $@" tar --transform "s,^,$(PKG_NAME)-$(VERSION)/," -czvf $@ $^ cp $@ ../ rm -rf $(ARCHIVE)Create the LICENSE file in the fibonacci-python-1.0 directory.
$ vi LICENSE
Update the file to include the following:
The Fibonacci application is public domain software. For the purposes of this example we keep these statements in this file but you should include the full text of the license here instead.
Create the setup.py file in fibonacci-python-1.0 directory.
$ vi setup.py
Update the file to include the following:
# Tells Debian tools how to build your python package from setuptools import setup, find_packages setup( name='fibonacci-python', version='1.0', packages=find_packages(where='app'), package_dir={'': 'app'}, entry_points={ 'console_scripts': [ 'fibonacci-python=app.main:main' # calls main() from fibonacci/main.py ] }, python_requires='>=3.11', )Create an archive of the application files. This is required by the debmake command to ensure a source archive is available. The Makefile specifies the creation of the fibonacci-python-1.0.tar.gz tarball, and copies it to the package main directory, which is parent to the working directory.
$ make archive
Once done, the folder structure should look like this:
$ tree ├── fibonacci-python-1.0 │ ├── app │ │ ├── __init__.py │ │ └── main.py │ ├── Makefile │ └── setup.py └── fibonacci-python-1.0.tar.gz
Results¶
Now that you have a working application prepared for packaging, you will need to run the debmake command to generate and update the package template files. For details, see Modifying Package Template Files for Python Applications.