02 August 2009

pexpect = EPIC WIN

I needed to read a bunch of data from a database only accessible via a menu-driven Unix shell program. Expect is designed for just this problem, but I understand that it invovles using Tcl, and Tk horrifies me so much that I wanted nothing to do with Tcl whatsoever.

Then I discovered pexpect, which is like expect but uses Python, which was the most win I've ever seen crammed into just 1.3 kloc of source code. Pexpect lets you script programs that expect human input. You tell it what to look for; it tells you what it read; you tell it how to respond. It even works on python 2.3, which was great, because the host with the shell program only had python 2.3.

One pitfall was that either pexpect or python 2.3 tended to leak file descriptors.
If I spawned the database client too many times from my pexpect script, Python would eventually fail with complaints about running out of file handles. This happened even when I called close() on the handles. The solution was to wrap the pexpect script in another python script that in turn called os.system wrote the results to an append-only file. This was a gross hack, but I got the data I needed without wearing out my fingers. Highly recommended.

No comments:

Post a Comment

About Me

blog at barillari dot org Older posts at http://barillari.org/blog