Skip to content

Instantly share code, notes, and snippets.

@iki
Created October 11, 2012 09:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iki/3871309 to your computer and use it in GitHub Desktop.
Save iki/3871309 to your computer and use it in GitHub Desktop.
building deb packages for python projects - just snippet to see how to
make_deb () { # dscfile|srcdir [destdir] [[backportable:] build_dependencies..] [signkey_id] [buildpackage_options..]
local S D BD K SA SX SI ST T W X AA A
S="$1"; D="${2:-.}"; BD="$3"; K="$4"; [ $# -ge 4 ] && shift 4 || shift $#
[ -d "$S" ] && SA='debian source control' && SX='temporary' && SI="$S/debian/control" && ST="$S/debian/changelog"
[ ! -d "$S" ] && SA='debian source archive' && SX='extracted' && SI="$S" && ST=
check_file existing: "$SA" "$SI" || return $?
[ -f "$ST" ] || ST="$SI"
[ -n "$K" ] || K="`xo gpg_get_key_id "$REP_OWN" "$REP_EML" "$REP_INF"`" || return $?
x require_commands dpkg-dev:dpkg-source dpkg-dev:dpkg-buildpackage fakeroot:fakeroot-sysv || return $?
try install_debian_source_builddeps "$S" "$BD" || return $?
X=
W="$PWD"
is_abspath "$S" && SF="$S" || SF="$W/$S"
T="`mktemp -d`" || return $?
if [ -d "$S" ]; then
I "copying debian source files $S"
x cp -a "$SF" "$T/src" || X=$?
# scripts like rules or transform_* need to be executable
[ ! -d "$T/src/debian" ] || x chmod +x "$T/src/debian"/* || return $?
else
I "extracting $SA $S"
cd "$T" && x dpkg-source -x "$SF" src || X=$?
fi
[ -z "$X" ] \
&& I "building debian binary archive" \
&& cd "$T/src" \
&& try dpkg-buildpackage -rfakeroot "-k$K" -b "$@" \
&& cd "$W" \
&& AA="`xo files "$T" '' '' deb`" \
&& for A in $AA; do x mv -f --target-directory="$D" "$A" && x touch -r "$ST" "$D/${A##*/}" \
&& echo "$D/${A##*/}" || X=$?; done \
|| X=$?
cd "$W" || [ -n "$X" ] || X=$?
I "removing $SX source files"
x rm -rf "$T" || [ -n "$X" ] || X=$?
return $X
}
require_python_debian_buildtools () {
[ -n "$_STS_PDB" ] && return $_STS_PDB
_STS_PDB=1
x require_platform debian-based || return $?
x require_debian_packages python-all-dev python-support python-setuptools debhelper apt-file \
|| return $?
if ! x check_python_packages stdeb; then
# if check_platform ubuntu \>= 8.10 || check_platform debian \>= 5.0; then
if x require_debian_packages backportable: 'debhelper>=7 python-support>=0.8.4'; then
try install_python_packages stdeb || return $?
else
try install_python_package_version stdeb \< 0.4 || return $?
# stdeb >= 0.4 requires debhelper>=7 and python-support>=0.8.4
# (ie. isn't suitable for etch/hardy without backports)
fi
fi
_STS_PDB=0
}
py_make_deb () { # project [destdir] [debian_revision] [package] [version]
# [maintainer] [signkey_id] [stdeb_config_key=value] ..
local N D R P V M K CD CF PF PM PC PL BD VS VD O
N="$1"; D="${2:-.}"; R="${3:-1}"; P="$4"; V="$5"; M="$6"; K="$7"; [ $# -ge 7 ] && shift 7 || shift $#
__ "project=$N" || return $?
check_file existing: setup setup.py || return $?
check_dir writeable: target "$D" || return $?
x require_python_debian_buildtools || return $?
[ -n "$P" ] || P="`py_debname "$N"`"
CD="$PWD/etc"
CF="$CD/stdeb.conf"
# reuse pre existing config
[ -f "$PWD/stdeb.conf" ] && CF="$PWD/stdeb.conf"
[ -f "$CD/$N-stdeb.conf" ] && CF="$CD/$N-stdeb.conf"
[ -f "$PWD/$N.egg-info/stdeb.conf" ] && CF="$PWD/$N.egg-info/stdeb.conf"
PF="$CD/$N-debian.patch"
PM="$CD/$N-debian.makepatch"
BD="`mktemp -d`" || return $?
# BD="build/debian" # slow on network shares && vbox shared folders prone to errors => use tmp instead
# check_dir writeable: remove: build "$BD" || return $?
[ -n "$M" ] || M="$REP_OWN <$REP_EML>"
[ -n "$K" ] || K="`xo gpg_get_key_id "$REP_OWN" "$REP_EML" "$REP_INF"`" || return $?
if [ -f "$PM" ]; then
# source make_debian_source_patch() from makepatch script
x . "$PM" || return $?
try inside_tmp make_debian_source_patch "$N" "$CD" "$PF" "$P" || return $?
# generates a level 1 patch file $3 for project $1 package $4 from data in source dir $2
# expects to not have to do any cleanup (e.g. is called inside a temp dir)
fi
[ -f "$PF" ] && PC="Stdeb-Patch-File=${PF##$PWD/}" && PL="Stdeb-Patch-Level=1" || PC=
# use a relative pathname to the patch when possible
try setconf "$CF" "$N" "Source=$P" "Package=$P" "Maintainer=$M" "Debian-Version=$R" ${PC:+"$PC" "$PL"} "$@" || return $?
O=
VS="`python_package_version stdeb`" || return $?
if x compare_versions "$VS" \>= 0.4; then
VD="`xo debian_package_version debhelper`" || return $?
I "using stdeb $VS and debhelper $VD"
if x compare_versions "$VD" \>= 7.3.7; then
O="${O:+$O }--force-buildsystem=True"
if x compare_versions "$VD" \>= 7.4.3; then
O="${O:+$O }--workaround-548392=False"
else
O="${O:+$O }--workaround-548392=True"
fi
else
O="${O:+$O }--force-buildsystem=False"
O="${O:+$O }--workaround-548392=False"
fi
else
I "using stdeb $VS"
fi
try python setup.py --command-packages=stdeb.command \
sdist_dsc -d "$BD" -x "$CF" $O || return $?
# old way:
# python -c 'import stdeb, os.path; _S = "setup.py"; __name__ = "__main__"; \
# __file__ = os.path.abspath(_S); execfile(_S)' \
# sdist_dsc -d "$BD" -x "$CF" $O || return $?
O="`lastfile "$BD" "$(dsc_pattern "$P" "$V" "$R")"`" || return $?
try get: make_deb "$O" "$D" '' "$K" || return $?
x rm -rf "$BD" || return $?
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment