Patching BitTornado



In this occasion, we’ll patch the bittornado package. (More info about BitTornado here)

Launchpad bug report
Debian bug report

So, here’s what we’ll do:

  1. Creating the patch from scratch.
  2. Apply upstream’s (debian) patch.

Creating the patch from scratch

First of all, we need to understand the bug report; what’s the problem and what’s the solution.

So, as we can read in the bug report, the problem it’s about deprecated sha module. The solution is to replace it with hashlib module. (More information here)

To start, we need to get BitTornado’s branch. But before that, we need to set a working directory and then we’ll be ready to work:
$ bzr branch lp:ubuntu/bittornado

Then, we need to create a branch starting from the original one:
$ bzr branch bittornado bittornado.fix-420387

And now it’s time to get down to work. What we firstly need, is to create a quilt patch (I hope that you know this patches system, BUT, if someone (if there’s someone over here xD) ask for a quick tutorial we’ll gladly give it to you ^^).

It’s recommended that we take a look at the patches naming under debian/patches directory, thus we can get an idea of what name use for our patch. So, the bug report patch’s name looks pretty good, and we’ll use it (following the numbering):
$ quilt new 32_use_hashlib_for_sha.patch

Then, find the python scripts that use the deprecated sha module and add them to our new patch. This can be achieved with the find and grep commands:
$ find . -path ./.pc -prune -o -name '*.py' | xargs grep 'from sha import'

In the above expression, we search in all the python scripts, skipping the files under .pc (applied patches), for the string “from sha import” and we’ll get an output like this:

./ sha import *
./ sha import *
./ sha import sha
./ sha import sha
./BitTornado/ sha import sha
./BitTornado/ sha import sha
./BitTornado/ sha import sha
./BitTornado/BT1/ sha import sha
./BitTornado/BT1/ sha import sha
./BitTornado/BT1/ sha import sha
./BitTornado/BT1/ sha import sha
./BitTornado/ sha import sha
./ sha import sha

So, we need to extract from the result the files name, and then, add them to the patch, and we can do that with this:
$ find . -path ./.pc -prune -o -name '*.py' | xargs grep 'from sha import' | awk -F ":" '{print $1}' | xargs quilt add

From now on, quilt will be tracking our files for changes, and when we write the changes, it’ll generate the patch.

What we need now, is to edit the files and correct the bug. We have two choices:

  1. Go for the files one by one and correct them
  2. Files have same bug, use an expression for all of them

Well, the easiest way is to use an expression, and to achieve this we’ll use find, grep, awk and sed. I’ve built an expression like this:
$ find . -path ./.pc -prune -o -name '*.py' | xargs grep 'from sha import' | awk -F ":" '{print $1}' | xargs sed -i 's/from sha import sha/from hashlib import sha1 as sha/'

With that, we’ll edit all the bug files, but not the two files that had ‘*’ because they do not fit the sed arguments. So, is not difficult edit them and we will do it manually by changing:

from sha import sha *


from hashlib import sha1 as sha

Now, we’re ready to write the changes and get the patch:
$ quilt refresh

After that, we can visualize in the debian/patches directory our patch. That’s all about the patch, it’s time to edit the changelog:
$ dch -i

Here, we’ll increase the ubuntu revision and change the distribution version to natty (the actual development version), also create and entry specifying our changes. This is my changelog entry (thanks to ari-tczew for correcting it):

* debian/patches/32_use_hashlib_for_sha.patch:
– Updated use of deprecated sha module to hashlib. (LP: #420387)

— Ronny Cardona (Rcart) Mon, 24 Jan 2011 17:27:47 -0600

After save the changes, we need to add the quilt patch that we have created to our local branch, clean up the local branch by removing all applied patches, and then just miss commit the changes.
$ bzr add debian/patches/32_use_hashlib_for_sha.patch
$ quilt pop -a
$ bzr commit

We can leave as the commit message, the changelog entry.

Well, until here we have our patch ready to be applied, now we need to create the package’s source code, build the package and test it to confirm that our patch works correctly.

Create the package source code (for a gpg signed package):
$ bzr bd -- -S

And then, depending on our pbuilder configuration, build the package:
$ cd ../ ; pbuilder-dist natty build *.dsc

The package is finished, tested and the changes are correctly confirmed. So, let’s upload it to Launchpad, and afterward, we will request to be Proposed for Merge
$ bzr push lp:~rcart/ubuntu/natty/bittornado/fix-420387
$ bzr lp-open

In firefox (or whatever web browser that we have) click on “Propose for Merge

And we can now wait for a reviewer member to sponsor our branch.

Apply Upstream bug report patch

This patch differs with the above, in that this uses exception handling and it’s safer.

The process is almost the same, difference is that we’ll not edit the files directly, instead we’ll use patch utility to edit them with an existing patch.

Get the branch, create a branch starting from the original one and enter to it.

Then, copy and save the patch code in a file (ending with .patch extension) to the parent working directory (../). Create a new quilt patch (the name of the patch from scratch is okay), find the bug files, add them to quilt patch, and then run:
$ patch -p1 -i ../patchname.patch

The above will correct the bug files by inserting the Debian patch. Now, write the patch:
quilt refresh

Update changelog file:
$ dch -i

Add the new patch to the local branch:
$ bzr add debian/patches/32_use_hashlib_for_sha.patch

Pop out the applied patches:
quilt pop -a

Commit the changes (use the changelog entry):
$ bzr commit

Now, create the source package, build it with pbuilder, test it and confirm that patch works. If all with building was fine, then upload it to LP:
$ bzr push lp:~rcart/ubuntu/natty/bittornado/fix-420387

Open LP and propose it for merge ;)
$ bzr lp-open

That’s all!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s