March 16, 2008

Python as a Platform

Something that recently occurred to me is that the only operating system that doesn’t come with Python pre-installed on it is Windows.

While Linux and OS X both view Python as essentially a first-class development platform–i.e., as something that shrink-wrap applications can be built on–Windows does not. Instead, it’s generally expected that a Python-based Windows application be “frozen”: bundled into a self-contained package that includes a copy of the Python interpreter and whatever libraries it uses, which are private to the particular application. While this ensures that the application will function as expected and not run into “dependency hell”, it also results in a relatively large download–distributing a simple “Hello World” program is at least a megabyte in size, and makes extending the program’s functionality more difficult.

During the summer of 2007, I made simple Python-based tool for some friends who played World of Warcraft; for those that played on the Mac, the situation was easy because their computers already had Python on them. For the Windows users, however, I was faced with the dilemma of either “freezing” the program into a fairly large download which would be difficult to test and update, or making my friends download the Python installer from python.org and running my Python script from source. Either solution seemed too cumbersome.

So instead, I opted for a different solution: I’d create a tiny executable that would first check for the existence of a Python installation on the end-user’s system. If Python was already installed, the executable would simply extract a Python script from itself and run it. That script was much like ez_setup.py; along with a few tricks, it essentially ensured that my friends would transparently auto-update to the latest version of the tool whenever they launched the program.

If there was no Python installation on the end-user’s system, however, the executable would simply transparently download the latest Python installer from python.org, silently install it on the end-user’s system without any prompting, and then proceed as outlined above.

So, in other words, a small executable file–only about 50k in size–was essentially responsible for “bootstrapping” my program by downloading and silently installing Python if necessary, and then downloading and running the latest version of my program’s source code. This ultimately meant that the user would be able to run a self-updating Python program from a tiny executable without having to click through any installation prompts or dialog boxes.

I’m glossing over a few details here and there are definitely some holes in my implementation–for instance, if the user uses a web proxy, they’re hosed–but I thought the general idea was interesting.

The source for the executable was compiled using the Nullsoft Scriptable Install System, along with its InetLoad plug-in. The source file is below for those interested.

This is a NSIS script that builds an executable that launches the

WoW addon updater, installing Python beforehand if necessary.

Requires the InetLoad plugin:

http://nsis.sourceforge.net/InetLoad_plug-in

;——————————– ; General Attributes

Name “WoW Addon Updater” OutFile “WowAddonUpdater.exe”

;——————————– ;Interface Settings

!include “MUI.nsh”

!insertmacro MUI_PAGE_INSTFILES !insertmacro MUI_LANGUAGE “English”

;——————————– ;Installer Sections

Function .onInit # Figure out if Python is installed by seeing if there’s any # program associated with opening files with the .py extension. EnumRegKey $1 HKCR .py 0 IfErrors InstallPython 0 # Python is installed, so we want to just run the addon updater # as though this is the executable for launching it. SetSilent silent Return InstallPython: # Python isn’t installed, so we want to act like an installer and # install it. SetSilent normal FunctionEnd

Section “InstallAndRun” # If at all possible, extract the addon updater to WoW’s root dir, just # so it’s always running from the same location. ReadRegStr $0 HKLM “SOFTWARE\Blizzard Entertainment\World of Warcraft”
“InstallPath” IfErrors 0 +2 StrCpy $0 “$TEMP” SetOutPath $0

IfSilent ExecutePyFile
    # Download and install Python.
    InetLoad::load "http://www.python.org/ftp/python/2.5.1/python-2.5.1.msi"
                    "$TEMP\python-2.5.1.msi"
    Pop $0
    StrCmp $0 "OK" +2
        Abort "Downloading of Python installer aborted."
    DetailPrint "Downloading of Python installer returned $0"
    # Install Python 'passively' so that the user doesn't have to click
    # anything; we want the installatio to be as unintrusive as possible.
    ExecWait '"msiexec.exe" /package "$TEMP\python-2.5.1.msi" /passive' $0
    DetailPrint "Python installer returned $0"
ExecutePyFile:
    # Run the addon updater.
    File "WowAddonUpdater.py"
    ExecShell "open" "$OUTDIR\WowAddonUpdater.py"

SectionEnd

© Atul Varma 2017