Sunday, June 05, 2011

 

Version 2.0.0 of Wordpress get-post plugin

Well, after a couple of years I dusted it off and rewrote a lot of the guts. It's about a hundred times shinier. Download at http://wordpress.org/extend/plugins/get-post

Source at https://github.com/jtatum/get-post

Friday, May 13, 2011

 

How to test a proxy

echo test | nc -X connect -x proxy_host:port target_host target_port

Tuesday, May 10, 2011

 

Belated announcement: First public release of PyATOM

The PyATOM team is proud to announce the initial release of PyATOM.

About PyATOM:

Short for Automated Testing on Mac, PyATOM is the first Python library to fully enable GUI testing of Macintosh applications via the Apple Accessibility API. This library was created out of desperation. Existing tools such as using appscript to send messages to accessibility objects are painful to write and slow to use. PyATOM has direct access to the API. It's fast and easy to use to write tests.

Changes in this release:

It's the first public release, so none yet!

Special thanks:
The VMware Fusion automation team
Nagappan Alagappan and the LDTP team

Download source:
https://github.com/pyatom/pyatom

Documentation references:

Documentation is still a work in progress. Read the README on the
Github page for an introduction to PyATOM.

Report bugs - https://github.com/pyatom/pyatom/issues

To subscribe to PyATOM mailing lists, visit http://lists.pyatom.com/

IRC Channel - #pyatom on irc.freenode.net

Friday, January 28, 2011

 

Python exception madness - err, Python namespace madness

Riddle me this: What the heck?

a.py:
import b

class MyException(Exception):
   pass

if __name__ == '__main__':
   try:
      b.evil()
   except MyException:
      print 'Caught my exception'
   except Exception as e:
      print 'Caught something else: ' + repr(type(e))
b.py:
from a import MyException

def evil():
   print 'Raising my exception'
   raise MyException('My exception')
Output:
$ python a.py
Raising my exception
Caught something else:
<class 'a.myexception'>

What?! Looks like an oddity of the Python namespace. Note the class being listed as 'a.myexception' - it's as though the imported exception class isn't being recognized as being equal to the class defined just a few lines above. If you put the exception class in c.py, it works.

I'm guessing this is documented behavior of the package import mechanism... but it was certainly unexpected behavior to me.

Edit:

My good friend Rick suggested this was because of circular imports, but I suspected it was because a was both __main__ and a. So I did this:
run.py:
import a

a.test()
a.py:
import b

class MyException(Exception):
   pass

def test():
   try:
      b.evil()
   except MyException:
      print 'Caught my exception'
   except Exception as e:
      print 'Caught something else: ' + repr(type(e))
b.py:
import a

def evil():
   print 'Raising my exception'
   raise a.MyException('My exception')
output:
$ python run.py 
Raising my exception
Caught my exception
To distill a lesson from all this, it's: don't put exception classes in any Python scripts run directly... Or maybe don't import a Python script that's run directly from another Python script.

Rereading all this, I think in the first case that a might actually have been read in twice - once as __name__=='__main__' and once as __name__=='a'. In that case, it seems that __main__.MyException is not a.MyException - which leads to the exception handler failing.

If this is one of the reasons people recommend against circular imports, I'm inclined to agree - I spent way too long troubleshooting a very vague issue that seemed like an interpreter bug at first.

Thursday, October 14, 2010

 

James' guide to sshing through an http proxy

May you never need this guide.

Preparing sshd on the server:

Why port 443 (https)? https can't be truly proxied without subterfuge. Between the SSL protocol itself and the presumed good behavior of the CAs, your company or school proxy can't establish trusted https connections on your behalf. To overcome this, http proxies implement a command called CONNECT which establishes a pass-through connection between the client and the specified host. The operating theory is that the vast majority of http proxies are going to allow CONNECT to arbitrary hosts over port 443, or the web would be broken for most of their users. The proxy can't interfere with this traffic and hopefully they don't look at it too closely either, because ssh is distinguishable from ssl traffic.

Client side (for testing):
  • There is a program out there called corkscrew but it isn't needed on an OS with a relatively thick GNU stack, like Linux or OS X. It needs to have nc, which is corkscrew on crack.
  • Try this: ssh -o ProxyCommand="nc -X connect -x <proxy host>:<proxy port> %h %p" -p 443 <user>@<ssh host>
If that works, awesome! Toss the ProxyCommand line in ~/.ssh/config. The Internets have some resources on doing this dynamically based on whether you're behind a proxy or not.

Friday, August 13, 2010

 

Python package pattern

I first saw this pattern in pyObjC, although I am sure it's implemented elsewhere. The basic idea is, if I have package a:

a/
  __init__.py
  clsa.py
  clsb.py

To get at the module in clsa.py, I'll import a.clsa. But what if you want to present a different interface for your package?

Imports are like anything else. They wind up in the dictionary of the containing namespace. Import sys, then dir() - sys appears, as a module object. The same happens in modules themselves. Imports, like global variables, appear in the namespace of the module and so are accessible via the module object.

As an example of this, try:

>>> import os
>>> dir(os)
>>> os.sys
>>> print os.sys.version

The os module imports sys, so we can actually access sys via os.sys. Python's importer is smart enough to give us two different references to the same object, rather than actually importing another copy of sys (if you already have sys imported).

So what does this mean? Well, if you wanted to give the example package above a flatter view, you could edit __init__.py:
from clsa import someclass
import clsa
import clsb

With the above __init__.py, you'll have a package that works as follows:
>>> import a
>>> a.clsa.somefunc() # would traverse the clsa import in __init__.py
>>> instance = a.someclass() # this uses the someclass import from clsa directly

In larger packages, this allows you to expose interesting classes to the user while still keeping everything organized in separate modules.

Labels: ,


Thursday, April 15, 2010

 

Ubuntu Lucid and KVM

Holy cow, KVM is fast! In addition to the steps on the Wiki, install virt-manager for a GUI (and manually connect to Localhost rather than using that little pre-populated localhost connection). Here's the builder command line I used:

This requires a bit of free space in /tmp to setup the chroot and do a debootstrap. Then, it creates the virtual disk in the current working directory. I can see why they don't recommend this for end users, as the tools are definitely rough around the edges but the speed is awesome. The best part about this setup is that it would be fairly easy to point the vm-builder to something like the current dev build of ubuntu and have it auto install something from a PPA or perform some other tasks to kick off some kind of automation. The whole thing is really automation friendly.

This page is powered by Blogger. Isn't yours?