Thursday, December 10, 2009

Extract ISO Files to Local Directory

If you would like to extract ISO files into a directory on your local machine, please do the following (should work on all *NIX machines, but I run Ubuntu).

mkdir /home/raja/extract
sudo mount -o loop -t iso9660 "location of ur iso" /home/raja/extract

This should extract your ISO into /home/raja/extract.

For the tech-savvy readers, "loop" option will make the source file read as a block device. You would typically need to give a location where it can be mounted (say, /dev/loop2) but if you dont give any, its picks up the first free loop file.
ISO9660 is the standard for CD files. Wikipedia has a nice entry about this.

Sunday, December 6, 2009

Eclipse Pydev for Django with autoreload

In an attempt to use Eclipse for Django, I was looking around to find how I can maximize productivity. IDE provides a few good things like auto completion and debug but if you were to work around it even with an IDE, then its probably not worth it.

Found "How to debug django webapp with autoreload" . That was a smart hack to hook into the autoreload mechanism to set up pydevd.settrace. Its a real pain without that to put in a

    import pydevd

in all places that you want to debug. With an IDE, you should be able to setup a breakpoint, hit the code , let it stop where you set the breakpoint and move on.

The post above indicated how things probably worked for Django pre 1.0 and dint work for me with the 1.1 codebase, Here is the modified code for that worked with Django 1.1

command = None
if len(sys.argv) != 1:
    command = sys.argv[1]
if settings.DEBUG and (command == 'runserver' or command == 'testserver'):
        import pydevd
        inEclipse = True
    except ImportError:
        inEclipse = False

    if inEclipse:
        from django.utils import autoreload
        m = autoreload.main
        def main(main_func):
            import os
            if os.environ.get("RUN_MAIN") == 'true':
                def pydevdDecorator(func):
                    def wrap(*args, **kws):
                        pydevd.settrace(suspend = False)
                        return func()
                    return wrap
                main_func = pydevdDecorator(main_func)
            return m(main_func)
        autoreload.main = main

Placing the above code in after importing the "settings" module seems to allow debugging with autoreload option on.

I also added some logic to check if Im running in Eclipse or not. I dont use Eclipse when Im not using my laptop -- vim is my friend. Its not a robust check, but works to check if we are in Eclipse or not. The idea is to not go into the auto-debug block if you are not running in Eclipse (for e.g. when you are running with vim and a command line).

Sunday, November 8, 2009

Using WNCK for capturing Gnome window events

For those new to WNCK (that includes me as well), it stands for Window Navigation Construction Kit and is used for capturing all window based events in GNOME. You could use python's pygtk to do some window management logic. For e.g. Find below some snippets about capturing when a window got switched. The below snippet will print the name of the application whenever any of the GNOME Windows switches (from Alt-Tab or clicking on window on the taskbar)
   import wnck
   import gtk
   scr = wnck.screen_get_default()
   while gtk.events_pending():    # This is required to capture all existing events
   def window_switch_handler(screen,window):
      # Get Current window, then the application and the name from it
      cur_win = screen.get_active_window().get_application().get_name()
      print "Currently showing %s" %cur_win

   scr.connect("active-window-changed", window_switch_handler)
After the last line "gtk.main()", the python interpreter will hang waiting for events to be serviced. Now if you switch any of the windows, you would see the name of the application displayed on the console. Im working on a similar setup for KDE (using PyQT). I will post a similar setup after I try it at work tomorrow (I dont have a KDE Setup at home). As for Windows, it turns out to be much more difficult. The PyAA seems to have stuff for WM_ACTIVATE and WM_DEACTIVATE but its compiled against Python 2.4 and noone has a later version available (unless its built). Im going to try 2 things. * Compile PyAA with Python 2.5 or Python 2.6 * Use AutoIT for automating windows tasks. Its non-python and could be an issue when I compile all three interfaces together, but atleast it would provide some way to do it. I dont know of any such automation for Mac OSX, but thats partly due to my lack of knowledge on that OS.

Saturday, October 31, 2009

Bypass the ON UPDATE trigger in mysql

Recently at work, I came across a problem where I had to bypass the ON UPDATE trigger for a particular table. I dint want this to happen as I was making manual updates on the backend and dint want the change to get reflected on the UI. To prevent this, you could add the column in the UPDATE clause (even though it doesnt have to change). Something like
   CREATE TABLE  test (
         id integer not null auto_increment,
         name varchar(20) not null,
         upd_date datetime default current_timestamp on update current_timestamp

If I just do a
      UPDATE test set name = 'raja' where id = 2;
that would update the upd_date column as well. IF I want to avoid the upd_date column from getting updated, you could do a
      UPDATE test set name = 'raja', upd_date = upd_date where id = 2;

Sunday, October 18, 2009

Adding code snippets to Blogger

As mentioned earlier, I was using Wordpress for my blogging attempts, but since I never went past 2 or 3 posts in total, its not something worth mentioning. But what wordpress had was easy support for code snippets.

Since this is going to be a technical blog, I needed ways to display code properly and in Wordpress, it was pretty simple using the "sourcecode" custom tag.

The equivalent of this is SyntaxHighlighter. View this post for a description as to how to add code snippets for blogger.

Add Search Engine - Firefox and IE7

Artifacts (Issues, comments, discussions) drive most of what we do at my day job. During an iteration meeting today, I found that most of us know the artifact id for getting to a page but the route to get to that page is atleast 2 clicks away (2 clicks before you get to the page to enter artifact id and one more to get to the actual page). I was wondering if it would be possible to use the "search engine" helpers that are available in Firefox 2 and IE 7. That would just make us enter the artifact id in the "Search engine" box and it takes us directly to that page (ofcourse after you login once to the application).

Luckily the OpenSearch Specification helps us here. Just Create an xml that adheres to the Opensearch specification and loading that page in both Firefox(2 and above) and IE(Version 7 and above) adds those to the list of search engines managed by your browser. There is a good tutorial at mozilla developer center. Ill cover the basics here. If you are interested, I can even make an application that lets you fill in the details and will post the xml file that you can use for adding your own search engine. Please add a comment if you are interested.

Without further ado, here is the minimal template for the xml file
      Some Short Name that appears in the Search Engines box
      Long Description
Call this file, say, plugin.xml in your web root directory. For a simple Apache server install, place this in /var/www (or whatever your DocumentRoot is). An example of the template modified for Google Search Engine is shown below

      My Google
      Google search implemented as firefox addon

Save that as google-plugin.xml. The thing to note above is the {searchTerms} in the URL Tag above. This will be replaced with whatever is entered in the searchbox. So if you entered "test", the url will be and will take you to that page.Using this as a template, you could modify any REST-ful GET URLs that will retrieve your page. Heck, it would be nice if all webapps have this behaviour as we are all used to entering things in the Search URLs rather than a few clicks.

Deploying this in Browsers:

Firefox (Only 2 and above):
  • Create a HTML File with this tag in the HEAD section of the file
        <link rel="search" type="application/opensearchdescription+xml" title="Some identifiable text" href="/plugin.xml" />
  • In the above example, replace the title with what you want to see in the Search Engine box and href with path to your Opensearch XML File. In my case, its in the DocumentRoot, so the /plugin.xml.
  • Load the html file in your system.
  • Once the page is loaded, you should see a "Add <title>" in the Search engines list.
  • Click on the Button and you should have that plugin added right into your browser.
  • Make that your default section by going to "Manage Search Engines" or just select it to enter text in there which will take you to google
  • If you need to delete this,go to "Manage search engine" and Remove it

Internet Explorer 7:
  • Load the plugin.html directly in your browser
  • In the Search List, click on the button that says "Add Search Providers" and click on the one that you just added
  • Once its done, the plugin should be added in your search list
  • Make that your default or select it to enter search terms
  • As in Firefox, to delete it, go to "Manage engines" and delete it

Note: I wasnt able to get this working on a Firefox 1.0 browser with the above steps. But it looks like the file is located in your system at <Home Directory>/.mozilla/firefox/<encrypted profile dir>/searchplugins directory. I need to check if just copying the plugin.xml into this directory and a subsequent restart will have the same effect. I will confirm this.There are other interesting things you can do with this. You can add an image to the searchbar by adding a "Image" tag with the contents of the image base64 encoded. Also for Mozilla based browsers, there is a Suggestion URL that can be used to retrieve the list of possible suggestions as you start typing. This should be a cool one to do and I will post a tutorial for that.

nxclient setup tricks

NoMachine is a uber cool alternative to VNC or any of the remote access solutions. Whats more, its very secure as it runs on top of ssh and doesnt need any special ports to be managed. You can use your regular ssh access to get into your box and its pretty fast too.
However, there are specific issues when installing the nxserver and nxclient. Ive attempted to mention a few of the problems and possible steps for getting an error free install and happy computing :)
1. Download the nxserver, nxclient and nxnode from here (Assuming the server is on Linux). Install the rpms or deb packages on the Server.
2. Download the nxclient and install it on your client machine.
3. Generate a public-private key combination on the client machine.
raja@binarytech:~$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/raja/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/raja/.ssh/id_rsa.
Your public key has been saved in /home/raja/.ssh/
The key fingerprint is:
After this, you would have (containing the public key) and id_rsa (containing the private key).
4. Now we need to check how we can use NXClient to connect to the server. Copy the public key (the from earlier step) into ~nx/.ssh/authorized_keys2 on the server machine. The typical directory for ~nx would be /usr/NX/home/nx.
5. Try and connect to the server from the client. This should succeed without asking for passwords
6. Login into the server as the user you want to connect. In this case, I want to login as "raja"
7. Enter your password.
8. You can run any command once you are in the shell. Play with it before you quit out of the shell.
9. Now we are good with connecting to the server as "nx" from the shell. Try creating a client configuration using the "NX Client for Linux".
10. Mostly this should work and connect you to the server. In cases it doesnt and throw messages like "Authentication User: nx
Using auth method: public key
Authentication failed"
Chances are that the key generated as part of the server may be stale. Go to the server machine and run a keygen
-bash-3.1$ sudo /usr/NX/bin/nxserver --keygen
NX> 704 Starting: server-keygen operation at: Tue Oct 07 12:36:25 2008.
NX> 704 Generating new ssh-keys. Please wait.
NX> 704 Keys generated correctly. Backing up files.
NX> 704 Back up of keys made. Updating files.
NX> 704 Keys updated. NX clients should now use key:
NX> 704 /usr/NX/share/keys/default.id_dsa.key
NX> 704 to get connected to this NX server.
11. Copy the key from /usr/NX/share/keys/default.id_dsa.key into your NX Client wizard and try connecting. This should work perfectly.
Happy NX Machine!!
If you have issues with connecting, drop me a post and I can try to fix it.

Saturday, October 17, 2009

Time Tracking System in Ruby on Unfuddle

Last year, when I wanted to create a timetracking system for my team, I thought of picking one from the Opensource world but couldnt find one that will fit my needs. I could have customized one of the existing ones, but they were either commercial or too primitive. I took that as an excuse to do an app in Rails and called it YATSA (Yet Another TimeSheet Application). It has the following concepts
  • People
  • Department
  • Activities
People belong to a Department and there could be many activities in a department. When someone logs in to enter time, they are presented with the departments to which they have access and on selecting a department, the activities associated with that department are chosen. The person can then enter start/end times and a description. You could then run reports for yourself or your team (if you are an Department Admin) and export the reports to Excel as well.

The Features document is available here. The project itself is uploaded to Unfuddle. Unfuddle does not have public access yet, but if you need the code, let me know, I can help create an account on the system for you to access it (The free version only supports 2 users, and Im one of them, Im planning to use the other version as the "public" user). There have been requests from a commercial firm asking to add Projects to this list so we can associate People to Projects.  I will be adding those into this system. I will also add instructions on how to set this up, so if anyone is interested, they could use this. There is no license for it right now, but Ill set it up under Apache License 2.0.

Sunday, October 11, 2009

HMAC SHA Signatures using Python for Amazon webservices

Ive recently been working on the Amazon Product Advertising APIs to get some information from Amazon and display it to my users in a different format.

Since Django is such a wonderful framework, I decided to do this using app using Django. The Amazon Product Advertising documentation has examples for Java, C# and PHP, but not much help for Python. There isnt anything unique to Python as the calls are all either REST based or SOAP based, but there was one part that was tricky -- The HMAC SHA Signatures required for REST requests.

For starters, HMAC is abbreviation for keyed-Hash Message Authentication Code. For more information about HMAC, look up the RFC or the wikipedia entry. There is enough information in the documentation about how to make a REST request but none whatsoever about creating the HMAC based signature.

So, after some debugging and browsing through the forums, here is a snippet that will show how to create a HMAC-SHA based signature that you can use with AWS.

In the below code, its assumed that you followed this doc upto Step 7 since there is nothing thats specific to any programming language.

import hmac
import haslib
import base64
import urllib
request = """
Build request as shown in the above link till Step 7
h =, request, hashlib.sha256)
signature = base64.b64encode(h.digest())
request = "%s&Signature=%s" %(request,urllib.quote(sig))
rest_output = urllib.urlopen('' %(request)).read()

The line for urllib.quote is required because Amazon wants us to quote the output that is gotten from HMAC based signature (Step 9 in the above document).

Hope it helps in doing a Python based HMAC-SHA signature generation for AWS.

Monday, October 5, 2009

Pound Sign with Accented A in PHP -- Solved

I had this weird problem in one of the PDFs that was getting generated through ezPDF. The problem was that the price in that page was appearing as a pound character with an accented A before it. My code looked like
$pound_sign = '£';

On most of the pages, this appeared properly as a pound sign followed by value but in some pages, it wasnt. After lot of debugging and googling around, the problem seems to be one of encoding. If there is a pound sign and its interpreted as a UTF-8 character, it is represented by a 2-byte sequence
   0xC2 0xA3

0xC2 is the accented A character and 0xA3 is the one for the pound sign, but if its interpreted as a ISO-8859-1 (Latin) encoding, it is represented as

which is what we want.

So, to solve the problem, something like

   $pound_sign = html_entity_decode("&#xA3;", ENT_COMPAT, 'ISO-8859-1');

worked and the accented A disappeared. This is because Im forcing the encoding to be ISO-8859-1 and the character passed to it in 0xA3.

Hope this helps someone

Sunday, October 4, 2009

Widget For Tracking Blogspot Visitors

Ive been googling for some Blogger Visitor widgets and most of them seem to redirect you to specific sites for getting information after you add a bit of code in the templates. Services like comstat seem to do that.

Having used Wordpress before, I like their idea of seeing each post and how many users visited those pages from the admin pages. That's more intuitive and easy for the user. Infact, the earlier post I made about Monitoring Traffic for Blogger Hosted sites requires that you login to Google Analytics to view that.

Im thinking of writing a plugin that would gather information from Google Analytics (And they have an api) and show it as a widget in the Admin Panel. Im doing this just to learn a bit of how to write widgets for Blogger as well as make it easier for bloggers to view traffic info.

Is there something like this available already ?

Saturday, October 3, 2009

Monitoring Traffic for Blogger Hosted Sites

I started using Blogger yesterday and from earlier experiences, I wanted to know how to monitor traffic for these types of hosted sites. It gives an indication as to what content is being looked upon more often (and its also a booster to post more if you have more people reading ur blog :-))

Blogger doesnt have a way to monitor traffic on its own unlike Wordpress, where the Dashboard will tell you on a post-by-post basis as to the number of people who visited that particular page. Instead we could do this through Google Analytics. Here are the steps

  • Login using your Gmail account to Google Analytics 
  • In the Dashboard section, create a new profile. This screen allows you to enter the domain name of the site where you want traffic monitored. Enter your blog address ( and give a suitable name for it
  • The following page will give you a piece of javascript that you will have to copy-paste and add it to your Blogger HTML template. Within Blogger, go to your Layout page and onto the "Edit HTML" link. In the "Edit Template" textbox, go to the bottom of the page and just before the ending body tag, add this piece of javascript obtained from Google Analytics
  • Thats it. Finish creating the setup on Google Analytics.
  • When you visit the Dashboard for Google Analytics immediately, the "status" column will give you an indication as to whether you entered the javascript correctly or not. This doesnt happen immediately and might take upto 24 hours according to Google. In my case, it happened in about an hour or so. If the "Status" column indicates a warning symbol, then there could be something wrong and this page should say what went wrong. There is also a video available from that page that shows how to add the tracking code
  • Wait for 24 hours and you should see data coming into Google Analytics.

Friday, October 2, 2009

Bug in GoogleStore page

I just created this new blog so I could try and post regularly, Lots of failed attempts because of my laziness, but anyway, the first post (as it usually happens) isnt a dummy post. Its a bug report, yay!!

When accessing the "gear" link from Blogger profile pages, I get a error page from Google Store, nothing to worry there, it happens here and there, but whats surprising is this block of code thats spit on the page.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="err.aspx.cs" Inherits="Ecometry.Web.Impl.err" %>

Would anyone know where to file a bug report for this?