~launchpad-pqm/launchpad/devel

10637.3.1 by Guilherme Salgado
Use the default python version instead of a hard-coded version
1
#!/usr/bin/python -S
8687.15.4 by Karl Fogel
Add the copyright header block to more files; tweak format in a few files.
2
#
3
# Copyright 2009 Canonical Ltd.  This software is licensed under the
4
# GNU Affero General Public License version 3 (see the file LICENSE).
5723.7.1 by Barry Warsaw
Clean up this script, but also add a few useful command line options, such as
5
9641.1.4 by Gary Poster
more updates for _pythonpath. Still need to update all subprocess calls to python to use -S; still need to update z3c.recipe.filetemplate to provide sys.modules from -S run.
6
import _pythonpath
7
5723.7.2 by Barry Warsaw
Sort the imports
8
import base64
9
import httplib
3049.1.1 by Dafydd Harries
add paste utility
10
import os
5723.7.1 by Barry Warsaw
Clean up this script, but also add a few useful command line options, such as
11
import pwd
3049.1.1 by Dafydd Harries
add paste utility
12
import sys
13
import urllib
5723.7.1 by Barry Warsaw
Clean up this script, but also add a few useful command line options, such as
14
import webbrowser
15
from optparse import OptionParser
4761.2.1 by Elliot Murphy
Modified paste script to work with the new pastebin.canonical.com.
16
from urlparse import urljoin
10921.1.1 by Gary Poster
fix an import
17
from cookielib import Cookie, CookieJar
9105.1.2 by Bjorn Tillenius
Move the import.
18
from mechanize import HTTPRobotRulesProcessor
6275.2.1 by Bjorn Tillenius
make paste talk openid.
19
20
from zope.testbrowser.browser import Browser
21
22
5723.7.1 by Barry Warsaw
Clean up this script, but also add a few useful command line options, such as
23
# Should we be able to override any of these?
6275.2.2 by Bjorn Tillenius
change the auth file and improve the error message.
24
AUTH_FILE = '~/.lp_auth_cookie'
9105.1.1 by Bjorn Tillenius
Use pastebin.ubuntu.com by default.
25
PRIVATE_PASTE_HOST = 'pastebin.canonical.com'
26
PUBLIC_PASTE_HOST = 'pastebin.ubuntu.com'
5723.7.1 by Barry Warsaw
Clean up this script, but also add a few useful command line options, such as
27
PASTE_PATH = ''
6275.2.2 by Bjorn Tillenius
change the auth file and improve the error message.
28
LP_AUTH_INSTRUCTIONS = """
29
%s doesn't contain a valid LP authentication cookie.
30
31
Please update this file, with the 'lp' cookie value your browser sends
32
when visiting https://launchpad.net (while being logged in). It should
33
look something like this:
34
35
    sd33JsfeJop3esf6joi8sldfjJoIj3dssD6isfsdweJDe6i9JIKEYK
36
""" % AUTH_FILE
5723.7.1 by Barry Warsaw
Clean up this script, but also add a few useful command line options, such as
37
38
39
def parse_arguments():
40
    parser = OptionParser(usage='%prog [options] [title] < stdin')
41
    parser.add_option('-b', '--browser',
42
                      default=False, action='store_true',
43
                      help='Open web browser to the pastebin.')
9105.1.1 by Bjorn Tillenius
Use pastebin.ubuntu.com by default.
44
    parser.add_option('-p', '--private',
45
                      default=False, action='store_true',
46
                      help='Use a private pastebin (pastebin.canonical.com).')
5723.7.1 by Barry Warsaw
Clean up this script, but also add a few useful command line options, such as
47
    parser.add_option('-s', '--syntax',
48
                      default='text', type='string',
49
                      help='The syntax of the pastebin.')
50
    parser.add_option('-f', '--file',
51
                      type='string',
52
                      help='File to pastebin instead of stdin.')
53
    options, arguments = parser.parse_args()
54
    if len(arguments) == 0:
55
        parser.title = None
56
    elif len(arguments) == 1:
57
        parser.title = arguments[0]
58
    else:
59
        parser.error('Too many arguments')
60
        # Does not return
61
    parser.options = options
62
    parser.arguments = arguments
63
    return parser
64
3049.1.1 by Dafydd Harries
add paste utility
65
6275.2.1 by Bjorn Tillenius
make paste talk openid.
66
def get_lp_auth_cookie(path):
67
    """Read the authentication file, and return a Cookie object."""
5723.7.1 by Barry Warsaw
Clean up this script, but also add a few useful command line options, such as
68
    path = os.path.expanduser(path)
6275.2.2 by Bjorn Tillenius
change the auth file and improve the error message.
69
    if not os.path.exists(path):
70
        return None
5723.7.4 by Barry Warsaw
Make this Python 2.4 compatable (i.e. no with-statement).
71
    f = open(path)
72
    try:
6275.2.1 by Bjorn Tillenius
make paste talk openid.
73
        cookie_value = f.readline().strip()
5723.7.4 by Barry Warsaw
Make this Python 2.4 compatable (i.e. no with-statement).
74
    finally:
75
        f.close()
6275.2.1 by Bjorn Tillenius
make paste talk openid.
76
    return Cookie(
77
        version=0, name='lp', value=cookie_value,
78
        port=None, port_specified=False,
79
        domain='login.launchpad.net', domain_specified=True,
80
        domain_initial_dot=False, path='', path_specified=None,
81
        secure=True, expires=None, discard=True,
82
        comment=None, comment_url=None, rest=None, rfc2109=False)
83
84
85
def authenticate(browser):
86
    """Go through the OpenID process and authenticate."""
87
    # First click on the page where it says we have to log in.
88
    browser.getControl('Continue').click()
8322.2.1 by Brad Crittenden
Fixed paste to conform to the moving target. Again.
89
    return True
3049.1.1 by Dafydd Harries
add paste utility
90
5723.7.1 by Barry Warsaw
Clean up this script, but also add a few useful command line options, such as
91
92
def main():
93
    parser = parse_arguments()
94
    try:
95
        poster = os.environ['USER']
96
    except KeyError:
97
        poster = pwd.getpwuid(os.getuid()).pw_name
98
99
    if parser.title is None:
4761.2.3 by Elliot Murphy
Added back title field in anticipation of updates to pastebin.
100
        title = "The loser %s didn't even add a title" % poster
5723.7.1 by Barry Warsaw
Clean up this script, but also add a few useful command line options, such as
101
    else:
102
        title = parser.title
103
104
    if parser.options.file:
5723.7.4 by Barry Warsaw
Make this Python 2.4 compatable (i.e. no with-statement).
105
        f = open(parser.options.file)
106
        try:
5723.7.1 by Barry Warsaw
Clean up this script, but also add a few useful command line options, such as
107
            content = f.read()
5723.7.4 by Barry Warsaw
Make this Python 2.4 compatable (i.e. no with-statement).
108
        finally:
109
            f.close()
5723.7.1 by Barry Warsaw
Clean up this script, but also add a few useful command line options, such as
110
    else:
111
        content = sys.stdin.read()
112
113
    form = (
114
        ('poster', poster),
6275.2.1 by Bjorn Tillenius
make paste talk openid.
115
        ('syntax', [parser.options.syntax]),
5723.7.1 by Barry Warsaw
Clean up this script, but also add a few useful command line options, such as
116
        ('content', content),
117
        )
118
6275.2.1 by Bjorn Tillenius
make paste talk openid.
119
    browser = Browser()
9105.1.1 by Bjorn Tillenius
Use pastebin.ubuntu.com by default.
120
    paste_host = PUBLIC_PASTE_HOST
121
    if parser.options.private:
122
        paste_host = PRIVATE_PASTE_HOST
123
        # Figure out the authentication.
124
        lp_cookie = get_lp_auth_cookie(AUTH_FILE)
125
        if lp_cookie is None:
6275.2.2 by Bjorn Tillenius
change the auth file and improve the error message.
126
            print LP_AUTH_INSTRUCTIONS
6275.2.1 by Bjorn Tillenius
make paste talk openid.
127
            return
9105.1.1 by Bjorn Tillenius
Use pastebin.ubuntu.com by default.
128
        cookiejar = CookieJar()
129
        cookiejar.set_cookie(lp_cookie)
130
        browser.mech_browser.set_cookiejar(cookiejar)
131
132
    # Remove the the check for robots.txt, since the one on
133
    # pastebin.ubuntu.com doesn't allow us to open the page. We're not
134
    # really a robot.
135
    browser.mech_browser.handlers = [
136
        handler for handler in browser.mech_browser.handlers
137
        if not isinstance(handler, HTTPRobotRulesProcessor)]
138
    browser.open(urljoin('https://' + paste_host, PASTE_PATH))
139
140
    if parser.options.private:
141
        # We need to authenticate before pasting.
142
        oid_form = browser.getForm(id='openid_message')
143
        if oid_form is not None:
144
            authenticated = authenticate(browser)
145
            if not authenticated:
146
                print LP_AUTH_INSTRUCTIONS
147
                return
6275.2.1 by Bjorn Tillenius
make paste talk openid.
148
    for name, value in form:
149
        browser.getControl(name=name).value = value
150
    browser.getControl('Paste!').click()
151
    print browser.url
152
    if parser.options.browser:
153
        webbrowser.open(browser.url)
5723.7.1 by Barry Warsaw
Clean up this script, but also add a few useful command line options, such as
154
155
156
if __name__ == '__main__':
157
    main()