-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithub_commenter.py
110 lines (89 loc) · 3.06 KB
/
github_commenter.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
#!/usr/bin/env python3
from __future__ import with_statement
import sys
import os
import logging
import json
import post_pics_to_pr
_moduleLogger = logging.getLogger(__name__)
def _parse_options(args):
import optparse
parser = optparse.OptionParser("usage: %prog [options] TOKEN PICS PR INFO")
# Setup parser
parser.add_option(
"-t", "--token",
dest="token",
metavar="TOKEN",
help="Github Access token needed to perform write operations"
)
parser.add_option(
"-d", "--pic-dir",
dest="picDir",
metavar="PICS",
help="Absolute path to the directory containing the pictures you want to upload"
)
parser.add_option(
"-p", "--pull-req",
dest="pr",
metavar="PR",
help="Github pull request number where you want to comment"
)
parser.add_option(
"-i", "--info",
dest="info",
metavar="INFO",
help="Information about the org, repo, and pull request you are trying to use, e.g. 'LabVIEW-DCAF/IntegrationTesting/PR-13"
)
debugGroup = optparse.OptionGroup(parser, "Debug")
debugGroup.add_option(
"-v", "--verbose",
action="count", dest="verbosity", default=0,
help="Turn on verbose output. (Useful if you really care to see what the tool is doing at all times.)"
)
debugGroup.add_option(
"-q", "--quiet",
action="count", dest="quietness", default=0,
help="Don't print anything to the module logger."
)
debugGroup.add_option(
"--test",
action="store_true", dest="test", default=False,
help="Run doctests then quit."
)
parser.add_option_group(debugGroup)
(options, args) = parser.parse_args(args)
# We want to default to WARNING
# Quiet should make us only show CRITICAL
# Verbosity gives us granularity to control past that
verbosity = 2 + (2 * options.quietness) - options.verbosity
loggingLevel = {
0: logging.DEBUG,
1: logging.INFO,
2: logging.WARNING,
3: logging.ERROR,
4: logging.CRITICAL,
}.get(verbosity, None)
if loggingLevel is None:
parser.error("Unsupported verbosity: %r" % verbosity)
# Perform validation on results
if options.test:
return options, loggingLevel
if args:
parser.error("Positional arguments are not supported: %r" % (args, ))
if options.picDir is None or not os.path.exists(options.picDir):
parser.error("Picture directory does not exist: %r" % options.picDir)
return options, loggingLevel
def main(args):
options, loggingLevel = _parse_options(args)
logFormat = '(%(asctime)s) %(levelname)-5s %(name)s.%(funcName)s: %(message)s'
logging.basicConfig(level=loggingLevel, format=logFormat)
if options.test:
import doctest
print(doctest.testmod())
return
else:
post_pics_to_pr.post_pics_to_pr(options.token, options.picDir, options.info, options.pr)
return 0
if __name__ == "__main__":
retCode = main(sys.argv[1:])
sys.exit(retCode)