~launchpad-pqm/launchpad/devel

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/python

import base64
import httplib
import os
import sys
import urllib
import urllib2

from MimeWriter import MimeWriter
from StringIO import StringIO

paste_host = 'devpad.canonical.com'
paste_path = '/~andrew/paste/'
auth_file = os.path.expanduser('~/.canonical_paste_auth')

def read_auth_file(path):
    f = file(path)
    username = f.readline().strip()
    password = f.readline().strip()
    f.close()
    return (username, password)

def make_basic_auth_header(username, password):
    auth = '%s:%s' % (username, password)
    return 'Basic %s' % base64.encodestring(auth).strip()

def https_post_form_with_auth(host, path, form, auth):
    form_data = urllib.urlencode(form).strip()
    connection = httplib.HTTPSConnection(host)
    connection.request('POST', path, form_data, {
        'Host': host,
        'Authorization': auth,
        'Content-type': 'application/x-www-form-urlencoded',
        'Content-length': str(len(form_data)),
        })
    return connection.getresponse()

if __name__ == '__main__':
    if len(sys.argv) > 1:
        title = sys.argv[1]
    else:
        title = "The loser %s didn't even add a title" % os.environ.get("USER")
    form = (('title', title), ('content', sys.stdin.read()))
    username, password = read_auth_file(auth_file)
    auth = make_basic_auth_header(username, password)
    response = https_post_form_with_auth(paste_host, paste_path, form, auth)
    refresh = response.getheader('refresh')

    if refresh:
        print refresh.split('=')[1]