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