Converting Python source code to Debian packages

2nd of October 2012

PyGraz Logo

Michael Prokop & Lukas Prokop

Packaging in Python

Python-specific

Developer's tasks

  1. write a setup script
  2. (optional) write a setup configuration file [0, PEP 0390]
  3. create a source distribution
  4. (optional) create one or more built (binary) distributions

[1, docs.python.org/distutils/introduction]

A simple setup script

from distutils.core import setup
setup(name='foo',
      version='1.0',
      py_modules=['foo'])

[2, docs.python.org/distutils/introduction#a-simple-example]

Application "sample"

Building the python package

python setup.py sdist    # create a source distribution
python setup.py test     # run unittest suite
python setup.py install  # installation at system

What about setup.py?

Defines the current directory as root of a python distribution. Contains metadata and a description for relation of files to be distributed.

Metadata

MANIFEST.in file

distribute, distutils, distutils2, pip, PyPI

distribute
A fork of setuptools. Some kind of deprecated in favor of distutils.
distutils
State of the art.
distutils2
The future of python packaging.
pip
manages and installs packages
PyPI
Official repository. CPAN (perl), RubyGems (ruby), CTAN (TeX)

Current state of packaging

State of Packaging
Current state of packaging via guide.python-distribute.org

Distutils is the standard tool used for packaging. It works rather well for simple needs, but is limited and not trivial to extend.

—Éric Araujo (distutils maintainer) [3, stackoverflow.com]

Installing packages from PyPI?

easy_install <module>  # apt-get install python-setuptools

Deprecated.

pip install <module>  # apt-get install python-pip

Use it! (As of 2012-10-05)

Problems

The truth

bereite drei [vorträge] vor, und je nachdem welches tool diese sekunde dann grad nicht obsolet ist, nimmst du den passenden

— weasel on IRC

Wat?

If a package requires the setuptools package, it is our recommendation that you install the Distribute package, which provides a more up to date version of setuptools than does the original Setuptools package.

http://guide.python-distribute.org/introduction.html

python-setuptools is provided via 'distribute', starting with Debian/wheezy distribute is part of python-setuptools package; the python-setuptools package is built from distribute sources

Packaging tools (as of 2012-10-02)

dh_python2 - calculates Python dependencies, adds maintainer scripts to byte compile files, etc.

% man dh_python2 dh_python3

Debian's tech-ctte

Debian Packaging - debian/rules

cat > debian/rules << EOF
#!/usr/bin/make -f
# Uncomment this to turn on verbose mode.
# export DH_VERBOSE=1

%:
        dh $@ --with python2
EOF

Debian Packaging - debian/control

% cat > debian/control << EOF
Source: python-foobar
Section: python
Priority: extra
Maintainer: Your Name <mail@example.org>
Build-Depends: debhelper (>= 7),
               python-all
Standards-Version: 3.9.4

Package: python-foobar
Architecture: all
Depends: [...]
         ${misc:Depends},
         ${python:Depends}
Description: short description
 longer package description...
EOF

PS: use python-all-dev in Build-Depends when building extensions

Debian Packaging - the rest?

As usual (AKA as in other Debian packages)

Packaging for Perl and Ruby

% gem2deb chef

% dh-make-perl --source-format "3.0 (quilt)" --dh 8 --cpan XML::Entities

Why can't we have nice things too?

py2dsc exists but sadly is too unreliable (because you can't rely on setup.py), BUT:

  • PEP 376 - Database of Installed Python Distributions
  • PEP 386 - Changing the version comparison module in Distutils
  • PEP 390 - Static metadata for Distutils
  • PEP 3147 - PYC Repository Directories
  • pypi2deb

    tool for automatic conversion of PyPI repository to (unofficial) Debian repository

    https://gitorious.org/pypi2deb/pypi2deb

    QA for your package

    % lintian -IE $package_*.changes
    % lintian-info -t $PROBLEM
    

    + jenkins-debian-glue

    Resources

  • Debian Packaging Tutorial
  • Debian Developer's Reference
  • Debian New Maintainers' Guide
  • Debian Python Policy
  • Style Guide for Packaging Python Libraries
  • Transition to dh_python2
  • man dh_python2
  • Debian Mentors FAQ (get your package into Debian)
  • Existing python-* Debian packages (with care)
  • Credits/Thanks