Created
July 22, 2014 09:20
-
-
Save labeneator/7435f6833be1c662c747 to your computer and use it in GitHub Desktop.
How to generate and build a .deb package for python virtualenv projects
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# Variables | |
PKG_NAME=SomeCompany-python-api | |
PKG_VERSION=0.1 | |
[email protected] | |
export DEBFULLNAME="Laban Mwangi" | |
PKG_NAME_VERSION=${PKG_NAME}-${PKG_VERSION} | |
PKG_SCRIPT_NAME=$(echo $PKG_NAME | sed -e "s/-/_/g") | |
# Should we cache the pypi packages used to build to keep build times low? | |
USE_DEVPI_FOR_PYPI_CACHING=1 | |
# Dir name needs to be in the format packagename-version | |
mkdir $PKG_NAME_VERSION | |
cd $PKG_NAME_VERSION | |
# Initialise a git repo and switch to a development branch | |
# git init . | |
git checkout -b "debianisation" | |
# Create a fake tar archive to fool dh_make | |
touch ${PKG_NAME_VERSION}.tar.gz | |
# Generate the debian subdir | |
dh_make --single -n --createorig -e $AUTHOR_EMAIL -f ${PKG_NAME_VERSION}.tar.gz | |
# Remove the fake archive | |
rm ${PKG_NAME_VERSION}.tar.gz | |
# You should now have a buildable debian source package | |
# Try build it with dpkg-buildpackage -uc -us | |
#h2. Virtualenv packaging for python | |
# Your code lives here | |
mkdir bin lib | |
# Gitignore... | |
echo >.gitignore """debian/${PKG_NAME}* | |
develop-eggs | |
downloads | |
eggs | |
log | |
parts | |
tmp | |
.DS_Store | |
env | |
build | |
debian/files | |
*.pyc | |
""" | |
# Create a setup script to package your python scripts | |
cat >setup.py <<EOF | |
from setuptools import setup, find_packages | |
setup( | |
name = "$PKG_NAME", | |
version = "${PKG_VERSION}", | |
packages = ["$PKG_NAME"], | |
# Search for packages listed above under the lib dir. | |
package_dir = {'':'lib'}, | |
scripts = ['bin/$PKG_SCRIPT_NAME.py'], | |
entry_points = { | |
'console_scripts': ['$PKG_SCRIPT_NAME = $PKG_SCRIPT_NAME:main'] | |
}, | |
package_data = { | |
# If any package contains *.txt or *.rst files, include them: | |
'': ['*.rst', '*.sh'], | |
}, | |
# metadata for upload to PyPI | |
author = "${DEBFULLNAME}", | |
author_email = "${AUTHOR_EMAIL}", | |
description = "SomeCompany API recast in python", | |
license = "Proprietary", | |
keywords = "API package", | |
) | |
EOF | |
# Generate a rules file that will generate your virtualenv | |
echo >debian/rules '''#!/usr/bin/make -f | |
# This is a makefile. Indentations are tab separated! | |
# Uncomment this to turn on verbose mode. | |
# export DH_VERBOSE=1 | |
%: | |
\tdh $@ --with python-virtualenv | |
# Uncomment the --pypi-url section if you are using a local pypi caching server | |
# See http://doc.devpi.net/latest/ | |
override_dh_virtualenv: | |
\tdh_virtualenv --setuptools # --pypi-url http://localhost:3141/root/pypi/ | |
''' | |
# You might really want to use devpi for caching your pypi packages. | |
# This will make your builds sane. See http://doc.devpi.net/latest/ | |
[ "$USE_DEVPI_FOR_PYPI_CACHING" -eq 1 ] && sed -in 's/# --pypi-url/ --pypi-url/' debian/rules | |
# Edit the control file to vend sane metadata about your package | |
# You may also want to add your dependencies here. | |
# For example if your python package requires the uwsgi package, | |
# add it to the Depends section of your package. | |
# vim debian/control | |
# Generate a basic requirements file. This is my skeleton | |
cat >requirements.txt <<EOF | |
ipython==2.1.0 | |
ipdb==0.8 | |
pytz==2014.2 | |
python-dateutil==2.2 | |
flake8==2.2 | |
EOF | |
# Create a skeleton code entry point. | |
cat >bin/${PKG_SCRIPT_NAME}.py <<EOF | |
#!/usr/bin/env python | |
# from $PKG_SCRIPT_NAME.module import app | |
def main(): | |
# Do something here with your code | |
print "Hello world" | |
if __name__ == '__main__': | |
main() | |
EOF | |
# Build the package | |
cat >buildit.sh <<EOF | |
#!/bin/sh | |
# If we are to use the pypi caching server. Make sure that it's up. | |
grep "^[^#].*--pypi-url" debian/rules && nc -v -z localhost 3141 || echo "You need to start up a devpi server. Run 'sudo pip install -v -U devpi-server && devpi-server --start &' " | |
# Run flake8. Exclude the setup script | |
echo "Flake8 running. Fix the errors below" | |
find . -iname "*.py" |grep -v setup.py | xargs flake8 --max-complexity=10 | |
# Make sure that you commit your pristine git environment prior to building it | |
# Git log would fail on a pristine environment | |
git log || { git add bin/ buildit.sh debian/ requirements.txt setup.py .gitignore && git commit -m "Initial import"; } | |
echo 'Creating a tag' | |
git-buildpackage --git-export-dir=/var/tmp/build-area/$PKG_NAME -S -uc -us --git-tag --git-ignore-branch | |
echo 'Building the tagged revision' | |
git-buildpackage --git-export-dir=/var/tmp/build-area/$PKG_NAME -uc -us --git-ignore-branch | |
EOF |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment