chiark / gitweb /
unreleased-packages: add d-i
[bin.git] / ubuntu-paste
1 #!/usr/bin/python
2
3 import httplib
4 import os
5 import sys
6 import urllib
7 from urlparse import urljoin
8
9 paste_host = 'paste.ubuntu.com'
10 paste_path = ''
11
12 def http_post_form_with_auth(host, path, form):
13     form_data = urllib.urlencode(form).strip()
14     connection = httplib.HTTPConnection(host)
15     connection.request('POST', path, form_data, {
16         'Host': host,
17         'Content-type': 'application/x-www-form-urlencoded',
18         'Content-length': str(len(form_data)),
19         })
20     return connection.getresponse()
21
22 if __name__ == '__main__':
23     poster = os.environ.get("USER")
24     syntax = 'text'
25     if len(sys.argv) > 1:
26         title = sys.argv[1]
27     else:
28         title = "The loser %s didn't even add a title" % poster
29     form = (('poster', poster), ('title', title), ('syntax', syntax), ('content', sys.stdin.read()))
30     response = http_post_form_with_auth(paste_host, paste_path, form)
31     location = response.getheader('Location')
32     if location:
33         print urljoin('http://' + paste_host, location)
34     else:
35         print 'Unexpected response:\n%s' % response.getheaders()