How To: Python Egg as a REDHAWK Shared Library

Introduction

This post is a step-by-step guide to creating a REDHAWK Shared Library from a Python Egg. Shared Libraries are package dependencies of REDHAWK Components that enable environment-agnostic deployment to general purpose processors (GPPs). When developing a Python Component, it is common to have a Python package dependency (often installed by a package manager like pip) that you need to deploy as a Shared Library. The REDHAWK IDE currently only supports C++ for Shared Libraries, so this guide will walk you through the steps required modify the Shared Library project to implement a Python Egg as a REDHAWK Shared Library!

In this guide, I am using the redis-py 2.10.6 Python package to demonstrate the workflow. This example, including the code and screenshots, was implemented with REDHAWK 2.0.6.

Create the Shared Library Project

Open the REDHAWK IDE, and select the menu option File | New | Project…. In the New Project dialog box, select REDHAWK Shared Library Project.

Click Next, and in the New Shared Library Project dialog box, enter the name of the project.

Click Finish.

Modify the Project Settings

In the REDHAWK IDE, open the Shared Library Project you just created by double-clicking on the .spd.xml file. In the Overview tab, set the Version field of the General Information section to the version of the Python package.

In the Implementations tab, set the ID field of the Implementation section to python.

In the Implementations tab, set the Output Dir field of the Code Generation Details section to python.

In the Implementations tab, set the File field of the Code section to python/lib/pkg_name (replace pkg_name with the Python package name you are using).

Note: This path must match the folder name that is unpacked from the .egg file (see the next section). Most commonly, it is the Python package name used by pip.

Navigate to the .spd.xml tab, and modify the XML code between the implementation tags to replace these three lines of code:




with these three lines of code:




It should look something like this:

Save the project.

Extract the Python Egg

Open a terminal window and cd into the root of your Shared Library project. Install the Python Egg of the package using pip:

$ pip install redis

Run the following commands to create and cd into the lib folder:

$ mkdir ./python/lib
$ cd ./python/lib

Copy the .egg file into the lib folder you just created and unpack it:

$ cp /usr/lib/python2.7/site-packages/redis-2.10.6-py2.7.egg .
$ unzip redis-2.10.6-py2.7.egg

Note: The path prefix shown above /usr/lib/python2.7/site-packages/ will depend on your Python version and Linux distribution.

Generate and Modify the Code

In the REDHAWK IDE, open the Shared Library Project you created by double-clicking on the .spd.xml file. Select the menu option Project | Generate Component. In the Regenerate Files dialog box, make sure all checkboxes are selected.

Click OK. It will take a minute to do the code generation, and you will see more files added to the project explorer when it has finished. One of those generated files is configure.ac. The contents of configure.ac should be replaced with the code below, and then you should make the following modifications:

  1. On line 1, replace redis-py-lib with the name of your Shared Library project.
  2. On line 1, replace 2.10.6 with the version of the Python package.
AC_INIT(redis-py-lib, 2.10.6)
AM_INIT_AUTOMAKE([nostdinc foreign])
AC_CONFIG_MACRO_DIR([m4])

AC_PROG_INSTALL

RH_SOFTPKG_PREFIX([redis-py-lib],[python])
AM_PATH_PYTHON([2.4])

AC_CONFIG_FILES(Makefile)
AC_OUTPUT

Another file that is a part of the output products is Makefile.am. The contents of Makefile.am should be replaced with the code below, and then you should make the following modifications:

  1. On line 5, replace redis-py-lib with the name of your Shared Library project.
  2. On lines 19-27, replace redis with the Python package name that was unpacked from the .egg file.
  3. Analyze the extracted contents of the .egg file, and make sure all the .py files were added in lines 20-27.
ACLOCAL_AMFLAGS = -I m4 -I${OSSIEHOME}/share/aclocal/ossie
AUTOMAKE_OPTIONS = subdir-objects

xmldir = $(prefix)
dist_xml_DATA = ../redis-py-lib.spd.xml

distclean-local:
rm -rf autom4te.cache
rm -rf m4
rm -f aclocal.m4
rm -f config.*
rm -f configure
rm -f depcomp
rm -f install-sh
rm -f ltmain.sh
rm -f Makefile.in
rm -f missing

pythoneggdir = $(prefix)/python/lib/redis/
pythonegg_DATA = lib/redis/__init__.py
pythonegg_DATA += lib/redis/_compat.py
pythonegg_DATA += lib/redis/client.py
pythonegg_DATA += lib/redis/connection.py
pythonegg_DATA += lib/redis/exceptions.py
pythonegg_DATA += lib/redis/lock.py
pythonegg_DATA += lib/redis/sentinel.py
pythonegg_DATA += lib/redis/utils.py

Select the menu option Project | Build All. Your shared library is now ready to export to the Target SDR!

Using the Shared Library

To use your new Python Shared Library, you need to add it as a dependency within the REDHAWK Component that uses it. Open your REDHAWK Component project, and navigate to the Dependencies section of the Implementations tab.

Click Add to the right of the Dependency box. In the Dependency Wizard dialog box, select Shared Library (SoftPkg) Reference for Kind, other for Type, and select your Python Shared Library project in the Shared Library (SoftPkg) Reference box.

Click Finish. The Dependency box should now look like this:

Your Shared Library is now linked to the Component, and it will deploy to a GPP automatically whenever the Component is deployed!

Reminder: Make sure you export the Shared Library to the Target SDR before trying to deploy your Component.

Version Control (Optional)

You may notice that the code generation and build steps create quite a few files. Some of these are temporary files used in the build process, and some are C++ files from the code generator that aren’t even referenced by the project! To ignore the temporary files and the unused C++ files in your git repository, put the code below in a .gitignore file in the root of your repository. Alternatively, you can manually inspect the list below to tell you what isn’t important to keep.

python/autom4te.cache/
python/include/
python/lib/EGG-INFO/
python/lib/*.egg
python/m4/
python/src/
python/configure
python/depcomp
python/install-sh
python/libtool
python/Makefile
python/Makefile.am.ide
python/Makefile.in
python/missing
python/aclocal.m4
python/config.log
python/config.status
python/*.pc
python/*.pc.in
python/*.la
python/.libs/
*.pyc

As always, let us know how we can help you with all your Redhawk and SDR needs!

Recent Posts

Ready for an exciting change?

Work with US!