1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 """Routines for submitting bugreport through www.diffpy.org.
16 """
17
18
19 __id__ = "$Id: bugreport.py 6462 2011-04-02 00:41:29Z yshang $"
20
21 import urlparse
22 import HTMLParser
23 from diffpy.srrietveld.exceptions import SrrIOError, SrrValueError
24
25
26
27 ROOT_URL = "http://www.diffpy.org/"
28 FORM_URL = urlparse.urljoin(ROOT_URL, "bugreport/srrietveld/")
29 FORM_REALM = "diffpy"
30 FORM_USER = "diffuser"
31 FORM_ENCPW = "LPR3rU9s"
32
33
34
35
37 """Fill in and submit bugreport form at FORM_URL.
38 The post url is obtained by parsing the first HTML form.
39
40 formfields -- dictionary containing the keys. When optional
41 fields are not specified, use defaults as listed:
42
43 "reporter" optional, ["anonymous"]
44 "summary" required
45 "description" required
46 "component" optional, ["srrietveld"]
47 "version" optional, [current version of SrRietveld]
48 "traceback" optional, [""]
49
50 All values get stripped from leading and trailing spaces.
51 Any other keys in formfields are ignored.
52
53 No return value.
54 Raise KeyError when formfields does not have required keys.
55 Raise IOError on failed posting.
56 """
57 import urllib
58 import urllib2
59 import cookielib
60 from diffpy.srrietveld import __version__
61
62 formdata_user = {
63 "reporter" : formfields.get("reporter", "").strip() or "anonymous",
64 "summary" : formfields["summary"],
65 "description" : formfields["description"],
66 "component" : formfields.get("component", "srrietveld"),
67 "version" : formfields.get("version", __version__),
68 "traceback" : formfields.get("traceback", ""),
69 }
70
71 for k in formdata_user:
72 formdata_user[k] = formdata_user[k].strip()
73 if formdata_user["description"]:
74 formdata_user["description"] += "\n"
75 if formdata_user["traceback"]:
76 formdata_user["traceback"] = "\n" + formdata_user["traceback"] + "\n"
77
78 handler = urllib2.HTTPBasicAuthHandler()
79 handler.add_password(FORM_REALM, ROOT_URL,
80 FORM_USER, FORM_ENCPW.encode('rot13'))
81 cookier = urllib2.HTTPCookieProcessor(cookielib.LWPCookieJar())
82 opener = urllib2.build_opener(handler, cookier)
83 formcontent = opener.open(FORM_URL).read()
84
85 try:
86 formattr, formdata = getFormData(formcontent)
87
88
89 except ValueError, err:
90 emsg = "Invalid webform - %s" % err
91 raise SrrIOError(emsg)
92
93 formdata.update(formdata_user)
94 post_url = urlparse.urljoin(FORM_URL, formattr['action'])
95 post_headers = {'User-agent' : 'SrRietveld (compatible; MSIE 5.5; WindowsNT)'}
96 post_content = urllib.urlencode(formdata)
97 post_request = urllib2.Request(post_url, post_content, post_headers)
98 post_handle = opener.open(post_request)
99
100 return
101
102
117
118
119
120
121
171
172
173
174
175
176
177
178 if __name__ == "__main__":
179 import time
180 submitBugReport({
181 "summary" : "test, do not post",
182 "description" : "posted from submitBugReport on " + time.ctime(),
183 })
184