view talosnames/require.py @ 27:ce9374c19169

try to fix a keyerror
author Jeff Hammel <jhammel@mozilla.com>
date Fri, 13 Jul 2012 15:47:05 -0700
parents 1b18b2746e69
children cc0edf61ab21
line wrap: on
line source

import imp
import os
import tempfile
import urllib2

def require(url):
    """
    import a module from the web
    url should be like scheme://host.name/path/to/module.py
    """
    contents = urllib2.urlopen(url).read()
    filename = url.rsplit('/', 1)[-1]
    module = filename.rsplit('.', 1)[-1]
    dest = tempfile.mktemp(suffix='.py', prefix=module)
    f = file(dest, 'w')
    f.write(contents)
    f.close()
    return imp.load_source(module, dest)

# TODO: make an equivalent method for a tarball