annotate example.py @ 56:6ebd2d10fc03 default tip

stub embedding fetch
author Jeff Hammel <jhammel@mozilla.com>
date Fri, 02 Dec 2011 17:41:12 -0800
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
56
6ebd2d10fc03 stub embedding fetch
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1 #!/usr/bin/env python
6ebd2d10fc03 stub embedding fetch
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
2 """
6ebd2d10fc03 stub embedding fetch
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
3 http://k0s.org/geekcode tmp/code file
6ebd2d10fc03 stub embedding fetch
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
4 #https://github.com/mozilla/mozbase/tarball/master#mozprofile tmp/mozprofile tar
6ebd2d10fc03 stub embedding fetch
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
5 #http://k0s.org/mozilla/hg/fetch#fetch.py tmp/fetch.py hg
6ebd2d10fc03 stub embedding fetch
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
6 git://github.com/mozautomation/mozmill.git#jsbridge tmp/jsbridge git
6ebd2d10fc03 stub embedding fetch
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
7 """
6ebd2d10fc03 stub embedding fetch
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
8
6ebd2d10fc03 stub embedding fetch
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
9 # example embedding fetch
6ebd2d10fc03 stub embedding fetch
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
10
6ebd2d10fc03 stub embedding fetch
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
11 def require(url):
6ebd2d10fc03 stub embedding fetch
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
12 """
6ebd2d10fc03 stub embedding fetch
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
13 import a module from the web
6ebd2d10fc03 stub embedding fetch
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
14 url should be like scheme://host.name/path/to/module.py
6ebd2d10fc03 stub embedding fetch
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
15 """
6ebd2d10fc03 stub embedding fetch
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
16 import imp
6ebd2d10fc03 stub embedding fetch
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
17 import os
6ebd2d10fc03 stub embedding fetch
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
18 import tempfile
6ebd2d10fc03 stub embedding fetch
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
19 import urllib2
6ebd2d10fc03 stub embedding fetch
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
20 contents = urllib2.urlopen(url).read()
6ebd2d10fc03 stub embedding fetch
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
21 filename = url.rsplit('/', 1)[-1]
6ebd2d10fc03 stub embedding fetch
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
22 module = filename.rsplit('.', 1)[-1]
6ebd2d10fc03 stub embedding fetch
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
23 dest = tempfile.mkstemp(suffix='.py', prefix=module)
6ebd2d10fc03 stub embedding fetch
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
24 f = file(dest, 'w')
6ebd2d10fc03 stub embedding fetch
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
25 f.write(contents)
6ebd2d10fc03 stub embedding fetch
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
26 f.close()
6ebd2d10fc03 stub embedding fetch
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
27 return imp.load_source(module, dest)