Thursday, December 31, 2009

Using django-admin.py within cygwin

Sometimes I still work in windows environment. I always have cygwin installed in windows environment since I use a lot of *nix tools and scripts.
Currently, I am working a lot with python and django, very nice scripting language and powerful web framework. I used native windows python installation, instead of installing it within cygwin.
The problem occur when I tried to invoke django-admin.py script in cygwin's bash shell. It won't run. It is due to windows version of python expecting windows path of the django-admin.py script as the argument.
Here is the exact situation. Windows python expecting, something like this:

python c:\Python26\Scripts\django-admin.py


but since I run that command within cygwin, it will become like the following:

python /cygdrive/c/Python26/Scripts/django-admin.py


and throws some error messages.

So, I searched a bit and found out that cygwin has a tool that converts *nix path to windows path. The name of the tool is cygpath.

And here is what I did to solve the problem.
I created the following file:
/usr/local/bin/django-admin.py

which contains:
python.exe "$(cygpath -aw "/cygdrive/c/Python26/Scripts/django-admin.py")"

Of course you need to make /usr/local/bin/django-admin.py executable

chmod +x /usr/local/bin/django-admin.py


and you can run django-admin.py from anywhere.


Check out the following links for more information:
http://www.cygwin.com/cygwin-ug-net/using-effectively.html
http://www.cygwin.com/cygwin-ug-net/using-utils.html#cygpath

Wednesday, December 16, 2009

multipart sftp download and resume using lftp

Previously I wrote about how to resume scp download using rsync. It is pretty nice. After working a lot with scp and sftp, I was looking for a way to perform multipart download using sftp. It is pretty easy to resume sftp download, since there is already 'reget' command. Then I read some article about lftp. I thought that it was just another ftp client. I was wrong. Taken from the website itself, 'LFTP is sophisticated ftp/http client.' Truly sophisticated. Check more information about lftp from wiki.
One of the coolest feature is the ability to resume and multi-part sftp download. The command itself is pretty simple. Here is how one can do it:

$ lftp -e 'pget -c -n 5 /path/to/file' sftp://username@server


short explanation:
-e: lftp option to execute command
pget: is the command for partial download
-c: pget option to resume
-n: pget option for number of parts

Enjoy!

Wednesday, July 29, 2009

XML Document processing comparison between java and python

I had a project that requires xml document processing. At first, I was thinking to do it using Java, but since the project was not so big, I decided to use Python. I really have to say that it was a good decision. In my opinion, xml doc processing feels more natural in Python compare to Java.

Let's go straight to the code comparison.
There are many steps involved in java before actually getting the xml doc elements that match with xpath expression defined.

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true); // never forget this!
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse("books.xml");

XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
XPathExpression expr = xpath.compile("//book[author='Neal Stephenson']/title/text()");

Object result = expr.evaluate(doc, XPathConstants.NODESET);

NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println(nodes.item(i).getNodeValue());
}


I took the example from IBM website, here. You should check out the link if you are interested using xpath in java.

Now, in Python, using lxml library.

doc = etree.parse("books.xml")
nodes = doc.xpath("//book[author='Neal Stephenson']/title/text()")
print(nodes)


I mostly use java for my projects, pretty much happy with it, even though sometimes I wish that java has some syntactic sugars like in C#.
So, if you have the option to choose which programming language for your xml projects, give Python a try. I am sure you will like it.

Saturday, July 18, 2009

Set super+l shortcut key for locking screen in gnome

The default shortcut keys to lock screen in gnome is <ctrl><alt>l (it is small caps 'L'). I do not like that shortcut and prefer to use the combination of "super key" and "l", which actually defined as <Mod4>l. The keybinding GUI setting somehow does not accept any combination of super key with other keys. So, to be able to do that, you need to use gconf-editor.
Perform the following:

run gconf-editor,
option 1, from CLI:
$ gconf-editor


option 2, from GUI:
press <Alt>F2 to get the 'run application' dialog and type in gconf-editor and click 'run'

In gconf-editor window, go to the following key location, by expanding the keys tree:
/apps/gnome_settings_daemon/keybindings


then, double click 'screensaver' key and change the value to: <Mod4>l

and you are done. Test it by pressing super key and l.

There is also feature to search the key name and value, by pressing <Ctrl>f in gconf-editor. Actually, I found the correct location of the 'screensaver' key by searching it.

Friday, July 10, 2009

backup and restore MBR

Partition Table backup

Perform the following command to backup the partition table:
sfdisk -d /dev/hdx > /part/to/pt_info.sfdisk



Partition Table restore

Perform the following command to restore the partition table:
sfdisk -d /dev/hdx < /part/to/pt_info.sfdisk



MBR backup

Perform the following command to backup the MBR:
dd if=/dev/hdx of=/path/to/image count=1 bs=512



MBR restore

Perform the following command to restore the MBR:
dd if=/path/to/image of=/dev/hdx count=1 bs=446

Notice that you need to include the option "count=1 bs=446", otherwise your partition table will be overwritten.

Wednesday, July 8, 2009

Install chromium browser with flash plugin in ubuntu

Install chromium browser with flash plugin in ubuntu

Add the following repository in your apt's sources.list
deb http://ppa.launchpad.net/chromium-daily/ppa/ubuntu jaunty main


Add the key for it
$ sudo apt-key adv --recv-keys --keyserver keyserver.ubuntu.com 4E5E17B5


Perform the following
$ sudo aptitude update
$ sudo aptitude install chromium-browser


Chromium browser should be installed and ready to be used. If you already installed flashplugin you can add the plugin to chromium-browser. I noticed the browser takes a lot of resource after adding flashplugin, so I no longer user. To set flashplugin perform the following.
$ cd /usr/lib/chromium-browser/plugins
$ sudo ln -s /usr/lib/flashplugin-installer/libflashplayer.so