sdbe – Setup Debian Build Environment for a package – so can apply your own fixes/patch work

This script setup a build environment for a debian package. It will extract a debian package and all of the contents (data and control tars) into the appropriate build folders. DATA goes to ./build/newpkg/ where as CONTROL goes to ./build/newpkg/DEBIAN. For more info read the script and comments below.

Goes with this article: Manual Debian Patch work

The script:

#!/bin/bash
###
### FILENAME: /usr/local/bin/sdbe (you can save it anywhere you like though)
###
### PREREQ: Uses "ar" and "apt-get download" and "apt-cache show"
### PREREQ FOR BUILDING: "dpkg" and "fakeroot" (if your not root)
###
#############
###  USAGE: #
#############
###
### ./sdbe <package name>
###
#####################
### WHAT THIS DOES: #
#####################
###
### This sets up build environment of current packages that exist in any debian repository
### it will make a folder called PKGNAME-sbe in current folder
### with the downloaded debian package (it will download it with apt-get download),
### the original data and control files (extracted from the deb),
### a new build folder (where you can apply patch work to your debian package)
### and instructions on how to compile the new package
###
### BENEFITS: now you can go to your build folder, do all of the needed fixes and then build the new debian package (the instructions are all there too, including build-me scripts to help the process be faster)
###
#############
### EXAMPLE #
#############
### If I run this script from /root
### # cd /root
### # ./sdbe apache2
### It will create
### /root/apache2-sdbe
### With the following files and folders in there:
### 1) apache2.deb <-- you will have access to the original debian package
### 2) orig-deb-contents (contains data.tar.gz and control.tar.gz and debian-binary)
### 3) build
### --- There will be a folder called apache2-new (this is where you will apply any source code fixes, this is where the developer/programmer will spend their time at)
### --- Inside the apache2-new folder is DEBIAN folder where the control files go for debian package installs and uninstalls
### --- From the build folder you will run dpkg -b to make the apache2-new.deb file
### --- The build folder will have a text file explaining how to build a new debian package
### --- contents of the build folders apache2-new:
### --- /root/apache2-sdbe/build/apache2-new/DEBIAN: CONTROL files
### --- /root/apache2-sdbe/build/apache2-new/etc: from DATA File
### --- /root/apache2-sdbe/build/apache2-new/usr: from DATA File 
### --- /root/apache2-sdbe/build/apache2-new/bin: from DATA File
### --- the last 3 folders will extract out to the system during install either to the root of the system / or to the specified prefix install folder such as /usr/local/apache2/
### NOTE: in reality the apache2 package doesnt have these folders/files, here is how the contents will look like for apache2
### ACTUAL CONTENTS AFTER RUNNING: # ./sdbe apache2
### # cd apache2-sdbe
### # find
### OUTPUT OF FIND:
### ./orig-deb-contents
### ./orig-deb-contents/control.tar.gz
### ./orig-deb-contents/debian-binary
### ./orig-deb-contents/data.tar.xz
### ./apache2_2.2.22-13+deb7u1_i386.deb
### ./build
### ./build/apache2-new
### ./build/apache2-new/usr
### ./build/apache2-new/usr/share
### ./build/apache2-new/usr/share/doc
### ./build/apache2-new/usr/share/doc/apache2
### ./build/apache2-new/usr/share/bug
### ./build/apache2-new/usr/share/bug/apache2
### ./build/apache2-new/usr/share/bug/apache2/script
### ./build/apache2-new/usr/share/bug/apache2/control
### ./build/apache2-new/DEBIAN
### ./build/apache2-new/DEBIAN/postinst
### ./build/apache2-new/DEBIAN/md5sums
### ./build/apache2-new/DEBIAN/control
### ./build/README-build-here.txt
### If you were to patch it and build it the final package will be:
### ./build/apache2-new.deb
###
########################
### HERE IS THE SCRIPT #
########################
###
# --- get package name ----
PKGNAME=$1
# --- make sure a valid package and only 1 parameter is passed (the package name)
if [[ $# -eq 1 ]]; then
 echo "Checking Package name for existance in debian repositories: $PKGNAME"
 apt-cache show $PKGNAME > /dev/null 2> /dev/null || { echo "ERROR: Package $PKGNAME does not exit"; echo "Try to rerun after 'apt-get update'"; exit 1; }
 echo "Success Package $PKGNAME exists"
else
 echo "Setup Debian Build Environment for any repo package (into current folder)"
 echo "Usage: $0 packagename"
 exit 1
fi
# ---create the folders---
STARTD=`pwd`
ROOTD=$STARTD/$PKGNAME-sdbe
ARXD=$ROOTD/orig-deb-contents
BUILDD=$ROOTD/build
DATAD=$BUILDD/$PKGNAME-new
CONTROLD=$DATAD/DEBIAN
mkdir -p $ROOTD 2> /dev/null
mkdir -p $ARXD 2> /dev/null
mkdir -p $BUILDD 2> /dev/null
mkdir -p $DATAD 2> /dev/null
mkdir -p $CONTROLD 2> /dev/null
# ---Start the process---
echo "==== DNE START on $PKGNAME ===="
echo "- Starting here `pwd` and putting everything inside $ROOTD"
echo "==== Creating these 5 locations/folders ===="
echo '1 + ROOT FOLDER: '$ROOTD'     <- the main folder created inside the current working folder (here goes the original debian package, the extracted contents and the new build folder - containing the patch work)'
echo '2 + EXTRACTED ORIGINAL:'$ARXF'    <- the extracted contents of the original deb package, you will have the data.tar.gz and the control.tar.gz and the debian-binary file as well'
echo '3 + BUILD FOLDER FOR NEW PKG: '$BUILDD'    <- "cd" into this folder to build new package after patch work like this "dpkg -l $PKGNAME-new", final "$PKGNAME-new.deb" file will be here'
echo '4 + DATA ROOT FOR NEW PKG: '$DATAD'    <- Work in this folder to patch the source code - this is the root of the package, its alike to the root of a system or the prefix install dir (by default files&folders here, will end up on the root / of the system after the package installs, unless a different prefix install dir is used - contents inside the DEBIAN folder, seen in here, contains control files used by dpkg. The DEBIAN folder does not get extracted anywhere final on the filesystem, just its scripts are run during various dpkg operations)'
echo '5 + DEBIAN CONTROL DIR FOR NEW PKG: '$CONTROLD'    <- The DEBIAN folder (more info above) control files go here (used by dpkg to run scripts before and after uninstall and installs)'
echo "==== Downloading The Original Package ===="
cd $ROOTD
apt-get download $PKGNAME
echo "- Package Name: `ls -1 $ROOTD | grep deb`"
echo "==== Extracting The Original Package ===="
echo "- Extracting to $ARXD"
cp $PKGNAME* $ARXD
cd $ARXD
ar vx *
rm $PKGNAME*
cd $ROOTD
echo "==== Constructing the new Build folder ===="
echo "- Extracting data files to $DATAD"
tar xf $ARXD/data* -C $DATAD
echo "- Extracting control files to $CONTROLD"
tar xf $ARXD/control* -C $CONTROLD
echo "==== Complete - Closing Message ===="
CLOSEMSG="$BUILDD/README-build-here.txt"
echo "... below message can also be found at $CLOSEMSG"
# --- build instructions output to screen and to file ---
(echo "Work on the source code here: $DATA"
echo "To build package:"
echo "# cd $BUILDD"
echo "# dpkg -b $PKGNAME-new"
echo "If not Root: # fakeroot dpkg -b $PKGNAME-new"
echo "There will be 2 scripts that can be run from this folder to do build the package:"
echo "# build-me.sh"
echo "# build-me-fakeroot.sh") | tee $CLOSEMSG
# --- making final scripts - the build-me scripts ---
echo "cd $BUILDD" > $BUILDD/build-me.sh
echo "dpkg -b $PKGNAME-new" >> $BUILDD/build-me.sh
chmod +x $BUILDD/build-me.sh
echo "cd $BUILDD" > $BUILDD/build-me-fakeroot.sh
echo "fakeroot dpkg -b $PKGNAME-new" >> $BUILDD/build-me-fakeroot.sh
chmod +x $BUILDD/build-me-fakeroot.sh

 

Leave a Reply

Your email address will not be published. Required fields are marked *