Tuesday, June 30, 2009

How to easily fork python code into separate processes via a decorator

Do you have a bit of python code that is taking a long time, and you don't want to wait for it to finish?

The blogger here made an excellent way of forking pieces of your code into separate processes using a decorator.

http://buffis.com/2008/01/09/easy-forking-of-processes-in-python-using-generators/

It doesn't get easier than this.

Friday, June 19, 2009

How to get the firefox java plugin working in Ubuntu 8.10 64bit

The default java plugin for Firefox on Ubuntu 8.10 is not fully functional on 64 bit machines.  It works for some applets, but not all.

This blogger took the time to figure out how to replace it with Sun's plugin. Thank you for sharing!

http://tp0x45.blogspot.com/2009/03/openjdk-issue-in-ubuntu-810-64-bit.html

Friday, April 17, 2009

Use Python for Microsoft Windows Login and Group Policy Scripts

Background

Microsoft's Active Directory provides a way to distribute msi files and execute scripts on login, logout, shutdown etc. This makes it very easy to distribute a script to be run on login for a group of machines registered to Active Directory.

The type of scripts that can be run are .bat files, JScript or VBScript files via Windows Script Host (WSH. It's executables are wscript.exe (windows version) and cscript.exe (command-line version)), or windows executables (exe files). While those options are not too bad, I much prefer python for programming so here we are going to outline how to use python for these scripts.

Tim Golden has written some excellent wrapper code for python to make programming for Active Directory, WMI, shell work (like creating shortcuts) amoung other windows-specific things a pleasure.

Instructions
  • Get the Python MSI from Active State. We could have used the msi from python.org but we would have had installed the windows specific stuff that Mark Hammond wrote (pywin32 Windows Python Extensions) separately. And since I want to distribute this with Actirve Directory I would have had to convert the pywin32 package to an MSI as it is distributed as an exe. I tried compiling the source distribution to an msi with distutils (i.e.,
    python setup.py bdist_msi
    ) but the build failed because I did not have Visual Studio 2003 installed (I was building python 2.5). I do not want to buy or install Visual Studio (especially the old 2003 version) so I was happy that Active State packaged it all up in an msi already and distributed it for free! Thank you Active State!
  • If you would like to install Tim Golden's wrapper code via Active Directory as well (highly recommended) it is easy since he provided the setup.py distutils scripts for each package. Just download the zip files you want from his site and then for each one run
    python setup.py bdist_msi
    and an msi will be created for you. Thank you Tim!
  • You can distribute any python code that you want via Active Directory this way. Just make an msi of your code by writing a setup.py script and then running
    python setup.py bdist_msi
    . Distutils makes it easy!
  • Then, distribute those msi files with Active Directory Group Policy. I'll leave that to an exercise for the reader, but here is a good starting point. For some reason, I had to reboot a couple of times on some workstations for them to get the msi files installed.
  • Now your setup work is done and your workstations (or servers) have python installed along with pywin32 and optionally some nice wrapper code. From here you can write python scripts for those hosts that will be able to interact with active directory, WMI, COM, etc.
  • Now write your python login scripts.
  • To setup your login scripts to run at login you have a choice. First, you could compile your python code into exe files. You can do that without a lot of effort using py2exe (a distutils extension). This route adds a separate step (conversion to exe file) and every time you make a new exe it copies the whole python interpreter and other code with it so the exe files are large. This is a good option if you don't want to install python on the hosts though.
  • Windows Script Host (WSH) integration is another good option (I won't go into calling the python intepreter on your scripts via a bat file, but that is also an option). This way cscript.exe or wscript.exe runs the python intepreter which runs your script. Here's what you need to do to set this up.
    • First, run C:\Python25\Lib\site-packages\win32comext\axscript\client\pyscript.py (adjust for your installed version of python) on each host. This registers python as a language that Windows Script Host can run (called pyscript). Once this is done your python files (saved with the .pys extension can be used as Windows login scripts! You could put this command in a MSI file and distribute it with python so that when your login script runs it will already have been done.

    • There is one registry entry that needs to be added I have found in experimenting and below is some jscript code that does it (put it in a file in with a .js extension that you can publish it via a Group Policy in the Computer section of the policy to run on startup of the machine (best not done on logon to make sure it get's done before your python scripts run))


      var objShell = WScript.CreateObject("WScript.Shell");
      objShell.RegWrite("HKEY_CLASSES_ROOT\\pysFile\\shell\\open\\command\\", "\"C:\\WINDOWS\\system32\\wscript.exe\" \"%1\"", "REG_SZ");



    • Lastly setup your new shiny .pys script to run at login user the User side of the Active Directory Group Policy Object. Here is a site that shows you how.

    • This site has a lot of good information as well, but I have found that you don't need to do all the steps it outlines because the msi files above do most of it. The steps above worked for me.

Friday, January 30, 2009

edendb - A thin, flexible and fast python DBAPI wrapper for those who like SQL but don't like to type

If you are like me, you have looked for ways to make connecting to database from your python code easier and less painful.

For a long time I looked into the myriad ORMs available for python and they all seemed to be overkill for me. I don't mind using SQL. In fact I like it, but when putting it into python code and using the DBAPI I found my productivity was declining. I needed something to make executing sql commands faster and easier from python code. I needed something abstracted away from the DBAPI, but that also allowed me to execute straight sql if needed and easily handle all the possible values that could be returned.

So, I wrote a python library to do it.

Meet edendb!

Check it out and let me know what you think.

Thursday, January 29, 2009

Google Talk on Ubuntu Linux

Chat, Voip calls, system tray, Excellent!

http://jamesselvakumar.wordpress.com/2008/11/17/good-news-for-google-talk-users-on-linux/

Thursday, January 22, 2009

How to configure mutt to use Gmail as smtp and imap server

The following helped me the most when configuring this.

http://shreevatsa.wordpress.com/2007/07/31/using-gmail-with-mutt-the-minimal-way/

The only thing I changed was

set postponed =~/Mail/Drafts

so that I could write messages offline and then send them when I get back to an internet connection.

I can't wait to try this out on my Asus Eeepc 2G Surf

Tuesday, January 20, 2009

Creating an application to insert events into Google Calendar

Here is how you can create a quick command-line application in python that will post events into Google Calendar.
  1. Get a Google Calendar account (go here to get one).
  2. Install the python gdata client which you can get from here.
  3. Find out the url to use to post to your calendars. To do that just go to this url and then view the source of the xml document that it returns to you. Each of your google calendars will be listed with the url that you need to use for each one. Remove the 'http://www.google.com' part of the url when you use it in your code.
  4. Write up some code to post with. Here is an example.
import gdata.calendar.service
import gdata.calendar
import atom

def add_to_gcal(item):
cal_client = gdata.calendar.service.CalendarService()
cal_client.email = 'youremail@gmail.com'
cal_client.password = 'yourpassword'
cal_client.ProgrammaticLogin()

event = gdata.calendar.CalendarEventEntry()
event.content = atom.Content(text=item)
event.quick_add = gdata.calendar.QuickAdd(value='true');

new_event = cal_client.InsertEvent(event, 'url from step 3 above')

add_to_gcal('Dentist Appt today at 4pm')
print "Your event was created"
There you have it.