TAB completion for fabric tasks [on OS X]

Found an excellent bash script on foobar.lu that enables tab completion on the fabric tasks defined in fabfile.py script (in case you don’t know what Fabric is, try Yuji Tomita’s article to get started with it). I just had to modify a little bit to make it work on OS X. I actually wanted to post it in their comments section, but it seems the comments are blocked on that site. So here it goes:

Install bash-completion with Homebrew

brew install bash-completion

You’ll need to add some lines in your ~/.bash_profile file to finish the installation. This will create an etc/bash_completion file under /usr/local directory. Now open the ~/.bash_profile file and add the following lines at the end:

function _fab_complete() {
    local cur
    if [ -f "fabfile.py" ]; then
        cur="${COMP_WORDS[COMP_CWORD]}"
        COMPREPLY=( $(compgen -W "$(fab -F short -l)" -- ${cur}) )
        return 0
    else
        # no fabfile.py found. Don't do anything.        
        return 1
    fi
}
if [ -f /usr/local/etc/bash_completion ] && ! shopt -oq posix; then
    . /usr/local/etc/bash_completion
    complete -o nospace -F _fab_complete fab
fi

Reload the file with source ~/.bash_profile command, navigate to your project directory where your Fabric script is located and try it out yourself.

Making Chrome Canary the default browser [OS X]

This post is for Apple OS X users. This is a nice trick for setting Google Chrome Canary as the default browser in your OS X. You can’t do this within Canary itself. Since this is a nightly build edition of Chrome browser, the developers have intentionally disabled this feature. So to have Canary as your default browser:

  1. Launch Safari
  2. Click on Preferences
  3. Under the tab General, choose Google Chrome Canary as the value for Default web browser
That should do the trick!
Update: Since this post is getting quite a lot of attention I figured I should update this with a cautionary tale. You should know the Canary is an unstable and untested build of Google Chrome. When the browser gets updated behind the scene, you may be introduced to a few bugs. One such bug deleted all the links in the Bookmark bar! And since my bookmarks are synced across browsers and devices using Xmarks, I lost the bookmarks in all my browsers in all my machines! I launched Firefox and watched the links disappear before my eyes and cried out in horror! Fortunately Xmarks keeps backups of your links in the cloud, so I was able to download the backup files and restore them. But I stopped using Canary since then.
Morale of the story: use with caution!

Getting started with pip/virtualenv

If you find yourself in a situation where you have to work with multiple Django projects on the same system, where each one requires a specific version of Django(and some other libraries), or even specific version of Python(2.5/2.6..), the combination of virtualenv and pip will make your life easier.

virtualenv is tool to create isolated Python environments.

pip is a Python package installer, much like easy_install. It’s complementary with virtualenv; you’ll use pip to install, upgrade, or uninstall packages in isolated virtualenvs.

In this post, I’ll show a simple workflow that will give you the basics on how to use these tools to manage multiple Django projects.

Installation

First, get these tools installed on your machine. If you have easy_install set up, you can install pip by just issuing the following command

sudo easy_intsall pip

Now install virtualenv, using pip itself

sudo pip install virtualenv

Creating virtualenvs

Now that you have them installed, let’s try creating some virtualenvs. It’s good to have a specific location on your machine where you would have all the virtualenvs. I personally like to have them under ~/.virtualenvs directory.

Create the directory

mkdir -p ~/.virtualenvs

Navigate into it

cd ~/.virtualenvs

Create a virtualenv

virtualenv -p python2.5 --no-site-packages projectA

The ‘-p’ option specifies which version of the Python interpreter you want to use for the virtualenv(In case you have multiple in your system). If not specified, the default interpreter will be used.

The ‘–no-site-packages’ option means do NOT inherit any packages from /usr/lib/python2.5/site-packages (or wherever your global site-packages directory is). Use this if you don’t want depend on the global packages and want more isolation.

Finally ‘projectA’ is the name of the virtualenv, named after the project itself(not a rule, just a good convention).

You’ll see the directory projectA created once the command prompt returns. This is the virtualenv, feel free to explore it.

Activating/Deactivating the virtualenv

Once pip have finished downloading and installing the packages, hopefully without any glitches, activate the virtualenv

source ~/virtualenvs/projectA/bin/activate

You should see “(projectA)” in your command prompt.

To deactivate it type ‘deactivate’ in your prompt and hit enter. The “(projectA)” should disappear.

So now you have an isolated and functional environment for your Django project. You should go ahead and create a new virtualenv with more recent versions of Python/Django as your homework.

I hope you’ve got the basics properly. Check out the online docs to learn more advanced usages of these tools.

In the next post, I’ll talk about virtualenvwrapper, a tool that makes managing multiple virtualenvs a little easier.

Modifying PDF files with PHP

Last week, a friend of mine asked me to help him with a programming problem that he had been wrestling with for some time. The problem sounds simple:

  1. Take a PDF file
  2. Write something at the footer of each page of that file

And this had to be done with PHP.

Although there are several libraries available in PHP for dealing with PDF files, none seem to have capabilities to modify the contents of an existing PDF file. Their manuals/tutorials are full of examples on how to create PDF on the fly. After spending few fruitless hours trying to get the much recommended PDFLib installed in my Mac and have it work with MAMP, I painfully realized this library is for commercial use only. The free version leaves a horrible watermark of their site address on the generated PDF documents.

My search for a solution took me to FPDF, an open-source library for PDF file generation in PHP. In their FAQ section, I found the link to an extension of the library, named FPDI. This one was seemingly capable of ‘manipulating’ PDF files in an ad hoc fashion. It extracts the contents of each page in the file, uses it as a template, lets you put texts/shapes on the template and then outputs the modified file. Excited, I got into coding and after an hour of labor, finally succeeded to achieve my goal! Thank God for creating open source!

Enough talk, now lets get our hand dirty!

First we need to have following libraries downloaded and unzipped. They are just packages of PHP scripts that you just require/include in your own script. No need to deal with .dll/.so extensions.

  1. FPDF
  2. FPDI
  3. FPDF_TPL

Keep them in the same directory of your script, or in the include path. The following code snippet gives a basic idea of how to get started with it:

require_once('fpdf/fpdf.php');
require_once('fpdi/fpdi.php');

$pdf =& new FPDI();
$pdf->AddPage();

//Set the source PDF file
$pagecount = $pdf->setSourceFile("my_existing_pdf.pdf");

//Import the first page of the file
$tpl = $pdf->importPage($i);
//Use this page as template
$pdf->useTemplate($tpl);

#Print Hello World at the bottom of the page

//Go to 1.5 cm from bottom
$pdf->SetY(-15);
//Select Arial italic 8
$pdf->SetFont('Arial','I',8);
//Print centered cell with a text in it
$pdf->Cell(0, 10, "Hello World", 0, 0, 'C');

$pdf->Output("my_modified_pdf.pdf", "F");

The above code takes a PDF file “my_existing_pdf.pdf”, and creates a copy of it “my_modified_pdf.pdf” with “Hello World” printed at the centre bottom of the first page.

That’s it! To achieve my goal, which I outlined at the start of this post, I extended the FPDI class, and overrode the Footer() method to print a customized footer in each page.

I only wish that the PHP online manual did NOT have an entire section dedicated to PDFLib, a non-free and commercial library, and rather point to free ones such FPDF or TCPDF. It could have saved me hours.

Installing Go on Ubuntu

When Google announced their newly developed system programming language Go, I decided to give it a try on my Ubuntu machine, the Mac would have to wait. The installation was supposed to be a walk in the park, but it wasn’t. I realized I didn’t have some other essential tools installed, and encountered one problem after another, frantically googling for solutions. So in this post I’ll detail the step by step process to install and run Go on your Ubuntu machine.

Step 1: Setting Up Environment Variables

Prior to installation you need to have these environment variables set up:

$GOROOT – The root of the Go tree. Typically this is $HOME/go but it can be any directory.

$GOOS – Name of the target operating system. Since this is Ubuntu the value would be ‘linux’

$GOARCH – Name of the target compilation architecture.  If you’re on a 32 bit x86 machine this would be ‘386’. Check out this section in Go online docs for other options.

$GOBIN – The location where binaries will be installed. You can set this to $HOME/bin or whatever path of your choice. Then you have to add this to your $PATH so that newly built Go-specific command such as the compiler can be found during the build.

Fire up the terminal and type the following command

sudo vi /etc/profile

This’ll open up the system-wide bash profile file in vi editor. You need to add the following lines at the end of the file.

export GOROOT=$HOME/go
export GOOS=linux
export GOARCH=386
export GOBIN=$HOME/bin

Now add $GOBIN to your $PATH. Open .bashrc in your $HOME directory

sudo vi $HOME/.bashrc

and add the line

export PATH=${PATH}:$GOBIN

Restart the machine for these changes to take effect.
Reload the files for the changes to take effect(thanks peter vahlu)

source /etc/profile
source ~/.bashrc

Step 2: Install Mercurial and clone Go repository

Google uses Mercurial to store Go source code, so you have to install it and fetch the repository.

Since 1.0, Mercurial has been installable by easy-install. So you need to get the python setuptools, header files and other essential  tools installed  first.

sudo apt-get install python-setuptools python-dev build-essential

Now install Mecurial

sudo easy_install -U mercurial

This part was quite confusing. Mercurial was throwing up some errors when I tried to clone Go’s repo.

*** failed to import extension hgext.hbisect: No module named hbisect

The extension bisect is a built-in command since version 1.0 and so should not be used. If you get this error, open the configuration file(/etc/mercurial/hgrc.d/hgext) and remove/comment out the line hbisect=

Make sure the $GOROOT directory does not exist or is empty. Then check out the repository:

hg clone -r release https://go.googlecode.com/hg/ $GOROOT

Step 3: Build Go from source

The Go tool chain is written in C. To build it, you need to have GCC, the standard C libraries, the parser generator Bison, make and the text editor ed installed.

sudo apt-get install bison gcc libc6-dev ed make

Next, build Go from source

cd $GOROOT/src

./all.bash

If all.bash runs without trouble, it’ll finish by printing

--- cd ../test

N known bugs; 0 unexpected bugs

Where N is the number of bugs, changes from release to release.

You now have GO installed on your Ubuntu. Happy coding!

Further Reading

  1. The installation page at Go website.
  2. Article on installing Mercurial on Ubuntu and dealing with its tantrums.

Django: How to make a variable available in all templates

Sometimes when building a web application with Django, you have a common piece of information that is available at all the templates. Foe example, you may have a dynamically built tree menu appearing in multiple templates.  It’s possible to achieve so by adding the data to the context of each template. But that goes against Django’s policy on code reuse. The right way to do that, is to use template context processors and ReuestContext objects.

The sections on Django official documentation are either too short or under misleading headings. So I’m trying to write this in a paint-by-numbers sort of way.

First, the template

Let’s suppose you have the following silly piece of HTML in each of your templates

Hello World! My name is {{name}}

Next, create a custom context processor

Under the directory of your Django application create a new file. Let’s name it ‘custom_context_processors.py’. This is your custom context processor, that will list a number of methods, each of which will have a HttpRequest object as parameter. Suppose you want to have a variable named ‘domain’ available at all template. Let’s add a method to the processor by that name:

define name(request):
   return {'name': 'Django Guru'}

Install the Context Processor

Open settings.py. Add the following line in the TEMPLATE_CONTEXT_PROCESSORS

'myapp.custom_context_processors.domain,'

Add the RequestContext

In your views.py, import the RequestContext module

from django.templates import RequestContext

Now when you render the template using render_to_response() method, a RequestContext object has to be added as the optional context_instance argument. The RequestContext object takes a HttpRequest object as a parameter

def my_view(request):
   #view code
   return redner_to_response('my_template.html', {'foo:bar'}, \
                              context_instance=RequestContext(request))

The variable {{name}} will now be available in my_template.html, without explicitly adding it in the template context. If you need to make it available in some_other_template.html, all you need to do is to pass the RequestContext object as the third parameter to render_to_reponse().

Python equivalent of PHP’s ip2long()

I’ve been spoiled by PHP! It sometimes makes your job too easy with its large collection of library functions. Python, on the other hand, has a lot of powerful tools for doing low-level stuff. To get something done, which would take a single function call in PHP, you may have to meld together a number of those tools and build a new one.

I was looking for a Python equivalent of PHP’s ip2long() function. which converts an IPv4 address from dotted decimal notation(for example 208.69.34.231) to a 32 bit integer(3494191847). After spending few minutes with Google, I realized that Python has no such function in it’s core or standard library. To achieve this, I had to make use of Python’s socket interface and struct library. Here’s what I did:

from socket import inet_aton
from sturct import unpack

def ip2long(ip_addr):
...ip_packed = inet_aton(ip_addr)
...ip = unpack("!L", ip_packed)[0]
...return ip

Note: Dots(.) in the above code represents indentation. WordPress is eating up the whitespace mysteriously!

The first line inside the ip2long() method, the inet_aton() function, converts the dotted-quad IP address to a 32-bit packed binary format, which is string of four characters in length. To make it an integer, you have to unpack it with the unpack() method, which takes the format as the first argument(which is “!L” in this case, for big-endian unsigned integer) and the packed string as the second. It returns a tuple with  the 32-bit integer as the first element.

To make a long2ip() function you can just reverse engineer the above process with struct.pack and and socket.inet_ntoa.