/
bzattach
executable file
·160 lines (115 loc) · 4.36 KB
/
bzattach
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/env python
import httplib
import getpass
import os
import urlparse
import warnings
import argparse
from pyquery import PyQuery
with warnings.catch_warnings():
warnings.simplefilter('ignore')
import twill.commands as twill
import multipart
ROOT = os.path.expanduser('~/.bugzilla-submitter')
COOKIES = os.path.join(ROOT, 'bugzilla.cookies')
BUGZILLA = 'https://bugzilla.mozilla.org/'
ATTACH = BUGZILLA + 'attachment.cgi?bugid=%s&action=enter'
ACTION = 'attachment.cgi'
TYPES = {
'text': 'text/plain',
'html': 'text/html',
'xml': 'application/xml',
'gif': 'image/gif',
'jpg': 'image/jpeg',
'png': 'image/png',
'svg': 'image/svg+xml',
'binary': 'application/octet-stream',
'xul': 'application/vnd.mozilla.xul+xml',
}
def logged_in():
pq = PyQuery(twill.browser.get_html())
return bool(pq('#header a[href^=relogin]'))
class Attacher(object):
def __init__(self, bug, file, description, patch=False, reviewer=None,
comment=None, mimetype=None, username=None):
self.bug = bug
self.file = file
self.description = description
self.patch = patch
self.reviewer = reviewer
self.comment = comment
self.mimetype = mimetype
self.username = username
if not os.path.exists(ROOT):
os.mkdir(ROOT)
if os.path.exists(COOKIES):
twill.load_cookies(COOKIES)
else:
self.login()
def login(self, url=BUGZILLA):
print 'Logging in...'
if url:
twill.go(url)
username = self.username or raw_input('Username: ')
twill.fv('login', 'Bugzilla_login', username)
twill.fv('login', 'Bugzilla_password', getpass.getpass())
twill.fv('login', 'Bugzilla_remember', '1')
twill.submit()
twill.save_cookies(COOKIES)
if not logged_in():
print 'Login failed, trying again...'
self.username = None
self.login()
def attach(self):
twill.go(ATTACH % self.bug)
if not logged_in():
self.login(None)
fields = {'description': self.description}
if self.patch:
fields['ispatch'] = '1'
if self.comment:
fields['comment'] = self.comment
if self.reviewer:
fields['flag_type-4'] = '?'
fields['requestee_type-4'] = self.reviewer
if self.mimetype:
fields['contenttypeselection'] = self.mimetype
fields['contenttypemethod'] = 'list'
# Get hidden inputs.
pq = PyQuery(twill.browser.get_html())
hidden = pq('form[action="%s"] input[type=hidden]' % ACTION)
fields.update((i.name, i.value) for i in hidden)
files = {'data': (self.file, open(self.file).read())}
return self.post(fields, files)
def post(self, fields=None, files=None):
# Get cookies.
cookie = '; '.join('%s=%s' % (c.name, c.value)
for c in twill.browser.cj)
content_type, body = multipart.encode_multipart_formdata(fields, files)
headers = {'Content-type': content_type,
'Content-length': str(len(body)),
'Cookie': cookie,
}
url = urlparse.urlparse(BUGZILLA + ACTION)
h = httplib.HTTPSConnection(url.hostname)
h.request('POST', url.path, body, headers)
response = h.getresponse()
print response.status, response.reason
def main():
parser = argparse.ArgumentParser(description='Submit Bugzilla attachments')
parser.add_argument('bug', type=int, metavar='BUG', help='Bug number')
parser.add_argument('file', metavar='FILE', help='File to upload')
parser.add_argument('--description', help='Attachment description',
required=True)
parser.add_argument('--patch', action='store_true',
help='Is this a patch?')
parser.add_argument('--reviewer', help='Bugzilla name of someone to r?')
parser.add_argument('--comment', help='Comment for the attachment')
parser.add_argument('--mimetype', choices=TYPES, help="File's mimetype")
parser.add_argument('--username', help='Bugzilla username')
args = parser.parse_args()
if args.mimetype:
args.mimetype = TYPES[args.mimetype]
Attacher(**dict(args._get_kwargs())).attach()
if __name__ == '__main__':
main()