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.