Installing Go on Ubuntu

Posted in How-To, Linux, Programming, Sys Admin, Ubuntu with tags , , , , on November 13, 2009 by rubayeet

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

Posted in Django, How-To, Programming, Python with tags , on October 31, 2009 by rubayeet

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()

Posted in How-To, Programming, Python with tags , , , , on October 23, 2009 by rubayeet

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.

Back in Gordon Freeman’s Suit!

Posted in Personal, Random Ruminations with tags , on September 30, 2009 by rubayeet

gordon

I am playing PC game again!

It’s Half Life 2 Episode 1!

I know you’re rolling your eyes thinking ‘How 2004 are you? That’s an old game!”.  I know, but I haven’t been around gaming  for a long time. I think I somewhat grew bored of it as I got involved other responsibilities in my personal life.

When I recently got my hands on this game, I couldn’t resist the temptation to play the sequel to one of the best PC games of all time. I booted my PC, which I have been neglecting since the new Macbook Pro came in, installed the game from the DVD, fired it up and started another journey through Half-life’s universe.

There are some cultural changes I have noticed so far. Instead of the legendary crowbar, the game starts with the gravity gun as the first weapon at hand. The biggest change of all, is that Gordon Freeman isn’t alone this time, he has a friend! It’s Alyx Vance!  The ass-kicking chick from Half-life 2. The developers at Valve have designed a sophisticated buddy system, so Alyx is going to fight alongside Gordon throughout the whole game. AI programming in games has come a long way from Daikatana, where moronic activities of your AI controlled buddy(like coming in the line of fire, or firing at you!) made me want to chop that buddy into pieces!

ep1_desktop5_1600x

I’m enjoying the game experience so far. And I’m quite glad that Alyx is with me. I’ve always felt a sense of desperation and fear, as I went through Half Life’s cinematic thrilling environment. That’s why it’s really good that I have Alyx by my side!

Update Finished playing Episode 1 few nights back. It was shorter than I expected but satisfying. Just started to play Episode Two!