forked from sympy/sympy-bot-old
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsympy-bot
executable file
·565 lines (500 loc) · 23.6 KB
/
sympy-bot
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
#! /usr/bin/env python
import sys
import os
import ConfigParser
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter, ArgumentTypeError
from tempfile import mkdtemp
from utils.cmd import (cmd, get_interpreter_version_info, get_platform_version,
get_sphinx_version)
from utils.github import (github_add_comment_to_pull_request,
github_authenticate, github_get_pull_request, github_get_user_info,
github_list_pull_requests)
from utils.reviews import reviews_sympy_org_upload
from utils.testrunner import run_tests, get_hashes, merge_branch, fetch_branch
from utils.url_templates import URLs
default_testcommand = "setup.py test"
default_build_docs_command = "make clean; make html-errors"
default_interpreter = ["python"]
default_interpreter3 = ["python3"]
default_protocol = "https"
def main():
global default_interpreter
global default_interpreter3
# Subclass argparser so help message is displayed when no args passed
class BotArgumentParser(ArgumentParser):
def error(self, message):
if message == "too few arguments":
self.print_help(sys.stderr)
else:
self.print_usage(sys.stderr)
self.exit(2, '%s: error: %s\n' % (self.prog, message))
parser = BotArgumentParser(
epilog="For the full help on the review and list commands, run "
"'sympy-bot command -h'.",
formatter_class=ArgumentDefaultsHelpFormatter)
subparsers = parser.add_subparsers(title="command", dest="command")
parser_review = subparsers.add_parser("review",
description="Reviews specified pull requests.",
help="Reviews pull requests",
formatter_class=ArgumentDefaultsHelpFormatter)
parser_review.add_argument("n", nargs="+",
help="Numbers of pull requests to review. You can also specify 'all' "
"or 'mergable' pull requests.")
parser_review.add_argument("--profile", type=str, default="default",
help="Configuration file profile to use, see README for information "
"about setting up profiles")
parser_review.add_argument("-n", "--no-upload", action="store_true",
help="Do not upload the review to the server")
parser_review.add_argument("-2", "--python2", nargs="?", const=True,
default=None, help="Run the tests with the interpreters specified "
"by the `interpreter` option, this is the default behavior, but it "
"must be called explicitly to run these interpreters when Python 3 "
"tests are run or the docs are built")
parser_review.add_argument("-3", "--python3", nargs="?", const=True,
default=False, help="Run the tests with the interpreters specified "
"by the `interpreter3` configuration file option, which is `python3` "
"by default")
parser_review.add_argument("-D", "--build-docs", nargs="?", const=True,
default=False, help="Test the building of the Sphinx HTML "
"documentation")
parser_review.add_argument("--no-comment", dest="comment",
action="store_false", help="Upload review but do not submit summary "
"comment to pull request on GitHub")
parser_review.add_argument("-i", "--interpreter", action="append",
type=str, default=default_interpreter, help="Python interpreter used "
"to run tests")
parser_review.add_argument("--interpreter3", action="append", type=str,
default=default_interpreter3, help="Python 3 interpreter used to run "
"tests")
parser_review.add_argument("-t", "--testcommand", type=str,
default=default_testcommand, metavar="COMMAND", help="Command, run as "
"an argument of `python`, used to execute tests, allowing the use of "
"sympy-bot on a subset of the tests, to run a command that is not an "
"argument of `python`, add '-V;' to the beginning of the option, for "
"example '-V; mycommand'")
parser_review.add_argument("--build-docs-command", type=str,
default=default_build_docs_command, metavar="COMMAND", help="Command run to "
"build the Sphinx docs")
parser_review.add_argument("--build-docs-dir", type=str, default="doc",
metavar="DIR", help="Directory in which to build the Sphinx docs")
parser_review.add_argument("-r", "--reference", type=str, help="Path to "
"sympy repository, passed to 'git clone <sympy repo>', setting this "
"speeds up sympy-bot and decreases network traffic")
parser_review.add_argument("-m", "--master-commit", type=str,
default="origin/master", metavar="COMMIT", help="Commit to use as "
"master for merging, use 'HEAD' to not merge")
parser_review.add_argument("-p", "--protocol", type=str,
default=default_protocol, choices=["https", "git"], help="Protocol "
"for communicating with GitHub")
parser_review.add_argument("-s", "--server", type=str,
default="http://reviews.sympy.org", help="Server to upload results")
parser_review.add_argument("-R", "--repository", type=str, default="sympy/sympy",
help="GitHub repository used, allowing sympy-bot to be used with "
"other projects")
parser_review.add_argument("--copy-py3k-sympy", nargs="?", const=True,
default=False, help="Copy the py3k-sympy directory from the reference "
"directory. This requires -r.")
parser_list = subparsers.add_parser("list",
description="Lists available pull requests",
help="Lists available pull requests",
formatter_class=ArgumentDefaultsHelpFormatter)
parser_list.add_argument("-n", "--numbers", action="store_true",
help="List only the numbers of open pull requests, suitable for "
"piping into 'xargs -L 1 sympy-bot review'")
parser_list.add_argument("--profile", type=str, default="default",
help="Configuration file profile to use, see README for information "
"about setting up profiles")
parser_list.add_argument("-R", "--repository", type=str, default="sympy/sympy",
help="GitHub repository used, allowing sympy-bot to be used with "
"other projects")
boolargs = {"build_docs", "no_comment", "comment", "no_upload", "python2", "python3", "copy_py3k_sympy",}
# Initial parse to print help
options = parser.parse_args()
# Load configuration and set defaults from it
config = load_config_file(options.profile)
# Parse args that should be booleans, but come as strings from ConfigParser
for i in boolargs:
if i in config and isinstance(config[i], str):
val = config[i].strip()
if val.lower() in {"true", "y", "yes"}:
config[i] = True
elif val.lower() in {"false", "n", "no"}:
config[i] = False
else:
raise ConfigParser.Error("Unable to parse boolean configuration value for %s: %s" % (i, val))
# If interpreter/interpreter3 is set in config file, use them as the default
parser.set_defaults(interpreter=None)
parser.set_defaults(interpreter3=None)
if "interpreter" in config:
interp = config["interpreter"]
if interp.strip().lower() == "none":
config["interpreter"] = []
else:
config["interpreter"] = [i.strip() for i in interp.split(",")]
if "interpreter3" in config:
interp = config.pop("interpreter3")
if interp.strip().lower() == "none":
config["interpreter3"] = []
else:
config["interpreter3"] = [i.strip() for i in interp.split(",")]
parser.set_defaults(**config)
# Parse args
options = parser.parse_args()
if options.copy_py3k_sympy and not options.reference:
parser.error("--copy-py3k-sympy requires --reference")
gh_user, gh_repo = options.repository.split("/")
urls = URLs(user=gh_user, repo=gh_repo)
if options.command == "list":
github_list_pull_requests(urls, numbers_only=options.numbers)
elif options.command == "review":
# Set bool args
for i in boolargs:
val = getattr(options, i, None)
if isinstance(val, str):
val = val.strip().lower()
if val in {"true", "y", "yes"}:
setattr(options, i, True)
elif val in {"false", "n", "no"}:
setattr(options, i, False)
else:
raise ArgumentTypeError("Unable to parse boolean configuration value for %s: %s" % (i, val))
# No interpreter/interpreter3 set
if options.interpreter is None:
options.interpreter = default_interpreter
if options.interpreter3 is None:
options.interpreter3 = default_interpreter3
# Nothing specified, then use default
if options.python2 is None and not options.python3 and not options.build_docs:
interpreter = options.interpreter
else:
interpreter = []
# Command line `-2`/config file `python2`
if options.python2:
interpreter += options.interpreter
# Command line `-3`/config file `python3`
if options.python3:
interpreter += options.interpreter3
options.interpreter = interpreter
if options.n == "mergable":
print "> Reviewing all *mergeable* pull requests"
print
nonmergeable, mergeable = github_list_pull_requests(urls, numbers_only=True)
options.n = mergeable
elif options.n == "all":
print "> Reviewing *all* pull requests"
print
nonmergeable, mergeable = github_list_pull_requests(urls, numbers_only=True)
options.n = nonmergeable + mergeable
else:
# list of pull request numbers, convert it:
options.n = map(int, options.n)
if not options.no_upload and options.comment:
username, password = github_authenticate(urls, config.get("user"), config.get("password"))
else:
username = password = None
try:
dispatch_reviews(options, urls, username=username, password=password)
except KeyboardInterrupt:
print "\n> Quitting on signal SIGINT."
sys.exit(1)
def load_config_file(profile):
conf_file = os.path.normpath('~/.sympy/sympy-bot.conf')
conf_file = os.path.expanduser(conf_file)
parser = ConfigParser.SafeConfigParser()
if os.path.exists(conf_file):
print "> Using config file %s" % conf_file
with open(conf_file) as f:
try:
parser.readfp(f)
except IOError as e:
print "> WARNING: Unable to open config file:", e
except ConfigParser.Error as e:
print "> WARNING: Unable to parse config file:", e
else:
print "> Loaded configuration file"
if parser.has_section("default"):
default_items = dict(parser.items("default", raw=True))
else:
default_items = {}
if profile.lower() == "default":
items = default_items
elif parser.has_section(profile):
items = dict(parser.items(profile, vars=default_items))
else:
raise ConfigParser.Error("Configuration file does not contain profile: %s" % profile)
return items
return {}
def dispatch_reviews(config, urls, **kwargs):
username = kwargs.get("username", None)
password = kwargs.get("password", None)
pr_numbers = config.n
interpreters = config.interpreter
python3 = {i: get_interpreter_version_info(i)[0] == '3' for i in interpreters}
tmpdir = mkdtemp(prefix="sympy-bot-tmp")
repo_path = os.path.join(tmpdir, "sympy")
print "> Working directory: %s" % tmpdir
print "> Cloning %s master" % config.repository
if config.reference:
reference = os.path.abspath(os.path.expanduser(os.path.expandvars(config.reference)))
cmd("git clone --reference %s git://github.com/%s.git" % (reference, config.repository),
cwd=tmpdir)
if config.python3 and config.copy_py3k_sympy:
print "> Copying py3k-sympy directory"
try:
cmd("cp -R %s/py3k-sympy %s/sympy/" % (reference, tmpdir))
except CmdException:
print "> Could not copy the py3k-sympy directory from %s. It probably doesn't exist." % reference
print " Run ./bin/2to3 in that directory to generate this directory, which will make this go much faster."
else:
cmd("git clone git://github.com/%s.git" % config.repository,
cwd=tmpdir)
# Generate all reviews
log_dir_base = os.path.join(tmpdir, "out")
os.mkdir(log_dir_base)
reviews = {}
master_hashes = {}
branch_hashes = {}
users = {}
branches = {}
for n in pr_numbers:
if len(pr_numbers) == 1:
log_dir = log_dir_base
else:
log_dir = os.path.join(log_dir_base, "pr-%s" % n)
os.mkdir(log_dir)
# Write pull request info
pull = github_get_pull_request(urls, n)
assert pull["number"] == n
print "> Reviewing pull request #%d" % n
repo_url = pull["head"]["repo"]["html_url"]
repo_url = repo_url.replace(default_protocol, config.protocol)
branch = pull["head"]["ref"]
user = pull["head"]["user"].get("login")
user_info = github_get_user_info(urls, user)
author = "\"%s\" <%s>" % (user_info.get("name", "unknown"),
user_info.get("email", ""))
fetchinfo = fetch_branch(repo_url, branch, repo_path, n)
if fetchinfo:
mergeinfo = {"result": fetchinfo, "log": ""}
# This shouldn't be used anyway
hashinfo = {"master_hash": "", "branch_hash": ""}
else:
# XXX: This has to go before merge_branch, or else it will return
# the merged commit SHA1 instead of the branch SHA1.
hashinfo = get_hashes(repo_path, config.master_commit, n)
if not hashinfo:
print "> There was an error. Report not uploaded."
sys.exit(1)
mergeinfo = merge_branch(repo_path, config.master_commit)
branch_hashes[n] = hashinfo['branch_hash']
master_hashes[n] = hashinfo['master_hash']
users[n] = user
branches[n] = branch
print "> Pull request info:"
print unicode("> Author: %s" % author).encode('utf8')
print "> Repository: %s" % repo_url
print "> Branch: %s" % branch
del pull
del hashinfo
pull_review = {}
run2to3 = True
# Iterate over interpreters
for log_num, i in enumerate(interpreters):
# Run tests
print "> Testing interpreter %s" % i
command = "%s %s" % (i, config.testcommand)
if mergeinfo["result"] in {'fetch', 'conflicts'}:
result = mergeinfo
else:
result = run_tests(repo_url, branch, repo_path, command,
python3[i], config.master_commit, run2to3=run2to3)
if python3[i]:
run2to3 = False
if result["result"] == "error":
print "> There was an error. Report not uploaded."
sys.exit(1)
print "> Done."
# Log results
log_file = os.path.join(log_dir, "interpreter-%s" % log_num)
with open(log_file, "w") as log:
log.write(result["log"])
print "> Results logged to %s" % log_file
# Upload results
if not config.no_upload:
print "> Uploading test results"
url_base = config.server
data = {
"num" : n,
"result" : result["result"],
"interpreter": i,
"log": result["log"],
"testcommand": config.testcommand,
}
report_url = reviews_sympy_org_upload(data, url_base)
print "> Uploaded report for '%s' at: %s" % (i, report_url)
else:
report_url = "(report was not uploaded)"
pull_review[i] = {
"result" : result["result"],
"url" : report_url,
}
del result
print
if config.build_docs:
# Run tests
print "> Building Sphinx docs"
if get_sphinx_version() is None:
print "> WARNING: Cannot find sphinx, disabling building HTML docs"
if interpreters == []:
print "> No tests to run, exiting"
exit()
else:
docs_repo_path = os.path.join(tmpdir, "sympy", config.build_docs_dir)
result = run_tests(repo_url, branch, docs_repo_path,
config.build_docs_command, False, config.master_commit)
if result["result"] == "error":
print "There was an error. Report not uploaded."
sys.exit(1)
print "> Done."
# Log results
log_file = os.path.join(log_dir, "docs")
with open(log_file, "w") as log:
log.write(result["log"])
print "> Results logged to %s" % log_file
# Upload results
if not config.no_upload:
print "> Uploading test results"
url_base = config.server
data = {
"num" : n,
"result" : result["result"],
"interpreter": "None",
"log": result["log"],
"testcommand": config.build_docs_command,
}
report_url = reviews_sympy_org_upload(data, url_base)
print "> Uploaded report for building docs at: %s" % report_url
else:
report_url = "(report was not uploaded)"
pull_review["build_docs"] = {
"result" : result["result"],
"url" : report_url,
}
del result
print
reviews[n] = pull_review
print "> View logs for PR %d in: %s" % (n, log_dir_base)
# Summarize and comment
for n, pull_review in reviews.iteritems():
report_url = {i: result["url"] for i, result in pull_review.iteritems()}
report_status = {i: result["result"] for i, result in pull_review.iteritems()}
master_hash = master_hashes[n]
branch_hash = branch_hashes[n]
# Generate summary
review = formulate_review(report_status, report_url, master_hash,
branch_hash, config.interpreter, config.testcommand,
config.build_docs, config.build_docs_command, users[n],
branches[n], config.master_commit)
print "> Review:"
print
print review
print
# Comment to GitHub
if not config.no_upload and config.comment:
print "> Uploading the review to the GitHub pull request ..."
github_add_comment_to_pull_request(urls, username, password, n,
review)
print "> Done."
print "> Check the results: https://github.com/%s/pull/%d" % (config.repository, n)
def formulate_review(report_status, report_url, master_hash, branch_hash,
interpreter, testcommand, build_docs, build_docs_command,
user, branch_name, master_commit):
if user:
atuser = "@"+user+": "
branch_name = user + '/' + branch_name
else:
atuser = ""
if master_commit == 'origin/master':
master_name = 'master'
else:
master_name = "**" + master_commit + "**"
formatdict = {'branch_hash': branch_hash, 'master_hash': master_hash,
'atuser': atuser, 'master_name': master_name, 'branch_name':
branch_name, 'master_name': master_name}
if any([status == "conflicts" for status in report_status.itervalues()]):
summary = """:exclamation: There were merge conflicts (could not \
merge {branch_name} ({branch_hash}) into {master_name} ({master_hash})); could \
not test the branch.
{atuser}Please rebase or merge your branch with master. \
See the report for a list of the merge conflicts."""
elif any([status == "fetch" for status in report_status.itervalues()]):
summary = """:x: Could not fetch the branch {branch_name}.
{atuser}Please make sure that {branch_name} has been pushed to GitHub and run the \
sympy-bot tests again."""
elif any([status == "Failed" for status in report_status.itervalues()]):
summary = """:red_circle: There were test failures (merged \
{branch_name} ({branch_hash}) into {master_name} ({master_hash})).
{atuser}Please fix the test failures."""
elif all([status == "Passed" for status in report_status.itervalues()]):
summary = """:eight_spoked_asterisk: All tests have passed (merged \
{branch_name} ({branch_hash}) into {master_name} ({master_hash}))."""
else:
raise ValueError("Unknown report_status")
report = """**[SymPy Bot][sympy-bot] Summary**: %s\n""" % summary
for n, i in enumerate(interpreter, start=1):
status = report_status[i]
summary = get_summary(status, report_url[i])
if status in {"conflicts", "fetch"}:
# This is irrelevant in this case
# XXX: We can't use defaultdict here, as it has no effect on
# format(**details). Any way we can get the same sort of thing?
details = {
'executable': "",
'python_version': "",
'platform_system': "",
'architecture': "",
'use_cache': "",
'additional_info': "",
}
else:
details = get_platform_version(i)
if testcommand != default_testcommand:
details['additional_info'] += "\n*Test command:* **%s**" % testcommand
if details['use_cache'] != 'yes':
details['additional_info'] += "\n*Cache:* **%s**" % details['use_cache']
details['summary'] = summary
details['status'] = status
details['symbol'] = status_symbols[status]
report += "{symbol} **Python {python_version}**: {summary}{additional_info}\n".format(**details)
if build_docs:
details = get_sphinx_version()
details['status'] = report_status["build_docs"]
details['symbol'] = status_symbols[details['status']]
details['summary'] = get_summary(details['status'], report_url['build_docs'])
if build_docs_command != default_build_docs_command:
details['additional_info'] += "\n*Docs build command:* **%s**" % build_docs_command
report += "{symbol}**Sphinx {sphinx_version}:** {summary}{additional_info}\n".format(**details)
report += '''\n[sympy-bot]: https://github.com/sympy/sympy-bot "The \
SymPy-Bot GitHub page."'''
return report.format(**formatdict)
status_symbols = {
"conflicts": ":exclamation:",
"fetch": ":x:",
"Failed": ":red_circle:",
"Passed": ":eight_spoked_asterisk:",
}
def get_summary(status, report_url):
if status == "conflicts":
summary = "There were [merge conflicts]({report_url}); could not test the branch."
elif status == "fetch":
summary = "Could not [fetch the branch]({report_url})."
elif status == "Failed":
summary = "[fail]({report_url})"
elif status == "Passed":
summary = "[pass]({report_url})"
else:
raise ValueError("Unknown report_status")
return summary.format(report_url=report_url)
if __name__ == "__main__":
main()
sys.exit(0)