-
Notifications
You must be signed in to change notification settings - Fork 0
/
automate.py
142 lines (107 loc) · 4.42 KB
/
automate.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env python
import os
import re
import urlparse
import datetime
import optparse
PWD = os.path.abspath('.')
DT = datetime.datetime.now().strftime('%Y%m%dT%H%M%S')
def count_occurences(items):
types = dict.fromkeys(set(items), 0)
for item in items:
types[item] += 1
return types
def main(options, config):
# Run tests
test_output_fname = '%s.txt' % DT
test_output_full_fname = os.path.join('testruns', '%s' % test_output_fname)
test_output_full_fname = os.path.abspath(test_output_full_fname)
test_command = "%s && cd \"%s\" && %s src tests > \"%s\" 2>&1" % \
(config.pythonpath, PWD, config.nosetests_cmd, \
test_output_full_fname)
status = os.system(test_command)
test_output = open(test_output_full_fname).read()
coverage_result = re.findall('TOTAL.*', test_output)[0]
num_tests_ran = re.findall('Ran [0-9]+ tests in .*', test_output)[0]
test_result = [l.strip() for l in test_output.split('\n') if l.strip()][-1]
if options.details:
print test_output
# Run epydoc to generate documentation
epydoc_url = urlparse.urljoin(config.url, 'doc/')
epydoc_log_url = urlparse.urljoin(epydoc_url, 'epydoc-log.html')
epydoc_command = "cd \"%s\" && %s" % (PWD, config.epydoc_cmd)
retval = os.system(epydoc_command)
fname = os.path.join(PWD, 'html', 'epydoc-log.html')
e_data = re.findall('class="log-([we][a-z]+)"', open(fname).read())
e_data = count_occurences(e_data)
e_data = ['%s: %s' % (k,v) for k,v in e_data.iteritems()]
e_data = ', '.join(e_data)
# Run pylint to get code quality information
pylint_output_fname = 'pylint_%s.txt' % DT
pylint_output_full_fname = os.path.abspath('testruns/%s' % pylint_output_fname)
pylint_command = "%s && cd \"%s\" && %s > %s" % \
(config.pythonpath, PWD, config.pylint_cmd, \
pylint_output_full_fname)
status = os.system(pylint_command)
pylint_output = open(pylint_output_full_fname).read()
pylint_result = re.findall('Your code has been rated at(.*)', pylint_output)[0].strip()
pylint_link = urlparse.urljoin(config.url, 'pylint/') + pylint_output_fname
if options.details:
print pylint_output
# generate report
test_and_converage_link = urlparse.urljoin(config.url, 'tests/') + test_output_fname
report = generate_report(test_result, num_tests_ran,
coverage_result, test_and_converage_link,
pylint_result, pylint_link, e_data,
epydoc_log_url, epydoc_url)
# send report mail
dirname = os.path.basename(PWD)
if not options.no_email:
send_report_mail(config, dirname, test_result, report)
else:
print report
def generate_report(test_result, num_tests_ran,
coverage_result, test_and_converage_link,
pylint_result, pylint_link, epydoc_data,
epydoc_log_url, epydoc_url):
text = '''
Test result: %(test_result)s
%(num_tests_ran)s
Coverage:
%(coverage_result)s
details: %(test_and_converage_link)s
Pylint:
%(pylint_result)s
details: %(pylint_link)s
Epydoc:
%(epydoc_data)s
details: %(epydoc_log_url)s
Documentation:
details: %(epydoc_url)s
''' % locals()
return text
def send_report_mail(config, project, test_result, text):
mail_data_fname = '/tmp/%s.txt' % os.getpid()
mailf = open(mail_data_fname, 'w')
print >> mailf, 'SUBJECT: [%s] %s - %s' % (project, test_result, DT)
print >> mailf, 'FROM: %s@ellina.homeip.net' % (project)
print >> mailf
print >> mailf, text
mailf.close()
recipients = ', '.join(config.recipients)
r = os.system('%s -t "%s" < %s; rm -f %s' % \
(config.sendmail, recipients, \
mail_data_fname, mail_data_fname))
return r
if __name__ == '__main__':
parser = optparse.OptionParser()
parser.add_option('-n', '--no-email', metavar='BOOL', \
action='store_true', help='Do not send report email')
parser.add_option('-d', '--details', metavar='BOOL', \
action='store_true', help='Show detailed report')
parser.add_option('-c', '--config', metavar='PATH', \
help='config module', default='automate_config')
options, args = parser.parse_args()
config = __import__(options.config, fromlist=['AutomateConfig'])
config = config.AutomateConfig
main(options, config)