I ♥ Python

Oh what a wonderful language it is!

Whenever I start up my Windows desktop, it is programmed to wake up my Linux server where I keep all my music and movies. I often make the mistake of just opening up Winamp and expecting it to play whatever I choose from the media library. The reason this does not work, fundamentally, is because Windows is shite (on several counts in this case); The only way to automate tasks at startup is through the autoexec.bat batch file which means that if it tries to reconnect to a mapped drive at that time, it obviously fails as the server takes just over 40 seconds to boot. So I do not do this, instead I have to connect by opening the drive.

Having to remember to do this every time has been pissing me off. Today I’d finally had enough and decided to find a way to make the autoexec.bat file check if the drive is available, if not wait 60 seconds and set up the mapping. After looking into how to simply wait 60 seconds in Batch, I shuddered and heard a voice (which was actually George) say “If you’re gonna script, do it in Python!”. Excellent advice as the code to do it in python is import time, time.sleep(60). I had another problem though, again due to Windows’ inadequacies;

Unlike *nix, Windows does not have comprehensive command line tools. It is supposed to sometimes but its attempts are feeble! Try running net use <drive> <location> <user> on a letter already assigned and you will be in a world of pain! Even if you don’t it still seems to completely b0rk the gui created link to the drive and so still screws everything up. Excellent work! Luckily pyWin32 with its array of API extensions can handle this flawlessly.

One final problem here though is that using this script meant having my username and password for my server in plain text on a Windows machine. Not a good idea! Oh well, a distutil extension called py2exe sorts that out. It isn’t perfect as it turned a 500byte script into a 3.5Mb .exe file (its includes a python interpreter and deppendancies so it can run on systems without a python install) but still, a small price to pay to have a 2 line autoexec.bat file which wakes up the server, waits 60 seconds and connects the drive.

I ♥ Python!

About Matt

Cloud Systems Engineer at Reed Elsevier; cloud computing advocate, rock climber, swing dancer, amateur photographer, professional idiot....
Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *