/
qt.py
125 lines (102 loc) · 2.76 KB
/
qt.py
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
#!/usr/bin/env python
# encoding: utf-8
import cookielib
import urllib2
import urllib
from pyquery import PyQuery as pq
import re
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import sys
import commands
def setup_cookie():
cj = cookielib.CookieJar();
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj));
urllib2.install_opener(opener);
#login bugzilla
def login_bugzilla():
username = 'xxxxxx'
password = 'xxxxxx'
url = r'https://bugzilla.eng.vmware.com/index.cgi'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.99 Safari/537.36'
}
data = urllib.urlencode({"Bugzilla_login":username,"Bugzilla_password":password, "GoAheadAndLogIn":"Log In"})
req = urllib2.Request(url, data, headers)
response = urllib2.urlopen(req)
the_page = response.read()
#determin if we log in succeed
if re.findall(r'Log out lming',the_page) != []:
print 'login succeed!'
else:
print 'login failed~'
sys.exit()
response.close()
#parse the searched page and return the buglist table in html
def parse_bugzilla():
#target_url='https://bugzilla.eng.vmware.com/buglist.cgi?cmdtype=runnamed&namedcmd=triaging&buglistsort=id,asc'
base_url = 'https://bugzilla.eng.vmware.com/'
#res = urllib2.urlopen(target_url)
#d = pq(res.read())
#table = d('table').html()
d = pq(filename = 'data.html')
table = d('table').html()
print table
commands.getstatusoutput('cat ')
print table
return table
def gen_email_body(table):
header = '''
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
}
</style>
</head>
<body>
<table id="buglistSorter"
class="bz_buglist tablesorter" cellspacing="0"
cellpadding="4" width="100%">
'''
tail = '''
</table>
</body>
</html>
'''
email_body = header + table + tail
return email_body
#send html format email
def send_email(email_body):
me = 'lming@mingrhel.com'
to = 'lming@vmware.com'
#cc = 'ljhuang@vmware.com'
cc = 'lming@vmware.com'
msg = MIMEMultipart('alternative')
msg['Subject'] = "Daily Triage Bugs"
msg['From'] = me
msg['To'] = to
msg['CC'] = cc
part = MIMEText(email_body, 'html')
msg.attach(part)
s = smtplib.SMTP('localhost')
s.sendmail(me, to, msg.as_string())
s.quit()
def main():
# setup_cookie()
#
# try:
# login_bugzilla()
# except Exception, e:
# print 'fail to login bugzilla'
# print e
# sys.exit()
table = parse_bugzilla()
#email_body = gen_email_body(table)
#send_email(email_body)
print 'ok'
if __name__=='__main__':
main()