Mercurial > hg > config
comparison python/require.py @ 157:f145d7b1fbf7
fix
| author | Jeff Hammel <jhammel@mozilla.com> |
|---|---|
| date | Wed, 27 Jul 2011 09:58:57 -0700 |
| parents | a9c3a32d8385 |
| children | 09c748a71b1b |
comparison
equal
deleted
inserted
replaced
| 156:a9c3a32d8385 | 157:f145d7b1fbf7 |
|---|---|
| 1 def require(url): | 1 def require(url): |
| 2 """ | 2 """ |
| 3 import a module from the web | 3 import a module from the web |
| 4 url should be like scheme://host.name/path/to/module.py | 4 url should be like scheme://host.name/path/to/module.py |
| 5 """ | 5 """ |
| 6 import imp | |
| 6 import os | 7 import os |
| 7 import tempfile | 8 import tempfile |
| 8 import urllib2 | 9 import urllib2 |
| 9 contents = urllib2.urlopen(url).read() | 10 contents = urllib2.urlopen(url).read() |
| 10 filename = url.rsplit('/', 1)[-1] | 11 filename = url.rsplit('/', 1)[-1] |
| 11 module = filename.rsplit('.', 1)[-1] | 12 module = filename.rsplit('.', 1)[-1] |
| 12 dest = os.path.join(tempfile, filename) | 13 dest = os.path.join(tempfile.gettempdir(), filename) |
| 13 f = file(dest, 'w') | 14 f = file(dest, 'w') |
| 14 f.write(contents) | 15 f.write(contents) |
| 15 f.close() | 16 f.close() |
| 16 return imp.load_source(module, dest) | 17 return imp.load_source(module, dest) |
| 17 | 18 |
