Calling Shotgun API v3.0.8 from IronPython

loocas | dotNET,Python,software,technical | Wednesday, February 1st, 2012

IronPython and Shotgun

As you might know there has been a significant change in the latest Shotgun API that’s somehow transparent to the CPython users, but presents a rather significant roadblock for IronPython users (including our duberPython bridge, that is based on the IronPython engine).

First, let’s discuss what’s changed in the API so dramatically that it breaks IronPython compatibility. It’s the introduction of a JSON formatting that requires a few specific CPython libraries that are not available in IronPython. The effect it has on CPython users is a faster data transfer to/from Shotgun, but other than that, the API looks to be unchanged from a user point of view. You still keep calling the same methods and you’re getting back the same objects. From IronPython point of view, you’ll hit a roadblock as there are a few modifications you’ll have to make to the Shotgun modules in order to make them run in IPy without issues.

With huge thanks to the Shotgun Software support, I was able to work around the few limitations and start using the latest API without issues inside IronPython. Here’s what you have to do, in order to make this happen:

Now if you try to instantiate the Shotgun class, you’ll get tons of errors, so, allow me to save you a few minutes (hours in my case, to be honest) searching and working around them. :)

  • The first thing you have to overcome the “IDNA” encoding, which isn’t supported directly in IronPython. This isn’t as clean and easy as it could be, though the workaround is rather trivial, but bear in mind you might break something potentially doing this. I haven’t come across any issues yet, so, I’d consider this modification safe. Find the iri2uri.py module in the httplib2 folder, open it and change line 71 from authority = authority.encode(‘idna’) to authority = authority.encode(‘utf-8’). This effectively changes the codec under which you’ll be dealing with the unicode strings. Again, it’s not a clean modification and you can potentially run into issues doing this, but in my experience, all seems to have been working fine so far.
  • The next problem will be SSL authentication, which, again, is tripping IronPython’s SSL module. To fix this, open the shotgun.py module and on line 73 change NO_SSL_VALIDATION = False to NO_SSL_VALIDATION = True.
  • Then IronPython will still be complaining about a module called zlib, which is a compiled CPython module that cannot be used inside IronPython. Luckily there is a C# variant based on the same code as zlib. Download it from Jeff Hardy’s bitBucket site and place it somewhere where IronPython can source it (i.e. within your PATH). I put it in a recommended folder called DLLs directly in the IronPython folder. This means that the IPy interpreter will source the DLL automatically, but bear in mind you’ll have to add a reference to this library manually in your scripts when run outside the shell.
  • The easiest way is to add these two lines in your imports into the shotgun.py module:

    import clr
    clr.AddReference("IronPython.Zlib")

This way you’ll make sure the zlib module required by some of the shotgun modules is going to be readily available.

That’s it! Let’s test it. :) Oh, one more thing, I’ve found these Python variant couples to be perfectly compatible and working in production: IronPython 2.6.2 and CPython 2.6 and for clr4 folks IronPython 2.7.1 and Python 2.7.2.

IronPython 2.7.1:
IronPython 2.7.1

duberPython (utilizing IronPython engine 2.6.2):
duberPython

I hope this helps you out with your IronPython and Shotgun scripting and I’m certainly looking forward to the new versions of the Shotgun API. :)

A huge thank you to the Shotgun Software support team!

1 Comment »

  1. Thanks for this. It will be helpful when we upgrade.
    Best,
    Patrick

    Comment by Patrick Wolf — March 28, 2012 @ 02:55

RSS feed for comments on this post. TrackBack URI

Leave a comment

Powered by WordPress | Theme by Roy Tanck