-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbugzilla-fbsd
executable file
·231 lines (191 loc) · 6.68 KB
/
bugzilla-fbsd
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
#!/usr/bin/env python
import subprocess
import sys
import requests
import argparse
import re
import os
import logging
import logging.handlers
logger = logging.getLogger(__name__)
DRY_RUN=False
BUGZILLA_CMD=["/usr/local/bin/bugzilla", "--bugzilla", "https://bugs.freebsd.org/bugzilla/xmlrpc.cgi" ]
"""
/usr/local/bin/bugzilla --bugzilla \
https://bugs.freebsd.org/bugzilla/xmlrpc.cgi --nosslverify attach \
--file "$PORTFILE" --desc "a fix" "$BUG_ID"
"""
import tempfile
import shutil
def upload_shar_from_patchfile(patch_file_name, port_path, bugid, patch_strip_count):
(portdir, portname) = port_path.split('/')
tmpdir = None
if patch_strip_count is None:
"""
Do a quick heuristic to determine if we're a git patchfile and if
so if we have the leading a/b parts we need to strip off by passing
-p1 to patch(1)
"""
has_git_diff = False
has_git_leading_ab = False
pfile = open(patch_file_name, "r")
for line in pfile:
if not has_git_diff and re.match("^diff --git ", line):
has_git_diff = True
if not has_git_leading_ab and re.match("^\+\+\+ b\/", line):
has_git_leading_ab = True
if has_git_diff and has_git_leading_ab:
logger.info("looks like a git style patch, using -p1 to patch program.")
patch_strip_count = 1
else:
patch_strip_count = 0
try:
tmpdir = tempfile.mkdtemp()
portsdir = tmpdir + '/ports'
os.mkdir(portsdir)
args = [ "patch", "-d", portsdir, "-p%d" % patch_strip_count, "-i", patch_file_name ]
check_call(args, run_during_dry_run=True)
#shar `find oneko` > oneko.shar
sharoutput = tmpdir + '/' + portname + '.shar'
cmd = "cd '%s' && shar `find '%s'` > '%s'" % (
portsdir + '/' + portdir,
portname,
sharoutput)
print "Executing: %s" % (cmd)
check_call(cmd, shell=True, run_during_dry_run=True)
print sharoutput
ADD_FILE_OPTS = [
"attach",
"--file", sharoutput,
"--desc", os.path.basename(sharoutput),
bugid,
]
check_call(BUGZILLA_CMD + ADD_FILE_OPTS)
finally:
if tmpdir is not None and not DRY_RUN:
shutil.rmtree(tmpdir)
def upload_shar_from_fabid(fabid, port_path, bugid):
patchnam = None
r = requests.get('https://reviews.freebsd.org/' + fabid + '?download=true')
(patchfd, patchnam) = tempfile.mkstemp()
try:
#patchnam = patchfd.name
patchfilehandle = open(patchnam, "w")
patchfilehandle.write(r.text)
patchfilehandle.flush()
upload_shar_from_patchfile(patch_file_name = patchnam, port_path=port_path, bugid=bugid)
finally:
if patchnam is not None:
os.remove(patchnam)
def get_faburl(fabid):
return "https://reviews.freebsd.org/" + fabid
"""
Do sanity checking on a fabricator id, including an http request to see if it exists.
"""
def check_fabid(fabid):
# match is start of string...
if not re.match("^D[0-9]+$", fabid):
print "option --fabid %s doesn't match expected regex /^D[0-9]+/"
sys.exit(1)
faburl = get_faburl(fabid)
print "Making sure fabric request exists..."
r = requests.get(faburl)
if r.status_code != 200:
print "Got non 200 status code from %s: code: %d" % (faburl, r.status_code)
sys.exit(1)
def check_call(arg, **kwargs):
logger.debug("Running: %s %s" % (arg, kwargs))
dry_run = DRY_RUN
if kwargs.get("run_during_dry_run", False):
del kwargs["run_during_dry_run"]
dry_run = False
if not dry_run:
subprocess.check_call(arg, **kwargs)
def check_output(arg, **kwargs):
logger.debug("Running: %s %s" % (arg, kwargs))
if not DRY_RUN:
return subprocess.check_output(arg, **kwargs)
else:
return ""
def init_main_args(args):
LOGFILE=None #args.logfile
logger = logging.getLogger(__name__)
if __name__ == "__main__":
logging_args = {}
logging.basicConfig(**logging_args)
if LOGFILE:
logging_args["filename"] = LOGFILE
logger.addHandler(logging.handlers.WatchedFileHandler(LOGFILE))
if args.verbose:
logger.setLevel(logging.DEBUG)
else:
logger.setLevel(logging.INFO)
if args.dry_run:
global DRY_RUN
DRY_RUN = True
if args.nosslverify:
global BUGZILLA_CMD
BUGZILLA_CMD.append("--nosslverify")
logger.debug("init_main_args done...")
"""
Create a new bugzilla for a port based on either a patchfile or a phabric id.
"""
def bz_newport(args):
logger.debug("enter bz_newport...")
init_main_args(args)
"""
print "args: %s" % args
print "DRY_RUN: %s" % DRY_RUN
"""
fabid = None
if args.fabid:
fabid = args.fabid
check_fabid(fabid)
NEW_PORT_OPTS = ["new", "-p", "Ports Tree", "-v", "Latest", "-c", "Individual Port(s)" ]
port_long_desc = "new port for %s is available"
if fabid:
port_long_desc += "in phabricator at %s" % (args.name, get_faburl(fabid))
ADD_OPTS= [
"-s", "new port: %s" % args.name,
"--oneline",
"-l", port_long_desc
]
if fabid:
ADD_OPTS.extend("--url", get_faburl(fabid))
output = check_output(BUGZILLA_CMD + NEW_PORT_OPTS + ADD_OPTS)
if DRY_RUN:
bugid = "1000"
else:
bugid = re.sub(r'^#+', '', output.split()[0])
if fabid:
upload_shar_from_fabid(fabid = args.fabid, port_path = args.name, bugid = bugid)
else:
upload_shar_from_patchfile(patch_file_name = args.patchfile, port_path = args.name, bugid = bugid, patch_strip_count = args.patch_strip_count)
print "New bug created at: https://bugs.freebsd.org/%s" % (bugid)
def bz_login(args):
init_main_args(args)
check_call(BUGZILLA_CMD + [ "login" ])
def main():
PROGNAME="bugzilla-fbsd"
parser = argparse.ArgumentParser()
parser.add_argument('--verbose', action='store_true', help='output more of what is going on.')
parser.add_argument('--nosslverify', action='store_true', help='turn off ssl checking, FreeBSD.org has broken ssl.')
parser.add_argument('--dry-run', '-n', action='store_true', help='Dry run, do not upload anything to bugzilla.')
subparsers = parser.add_subparsers()
"""Login takes no args, just a verb."""
parser_login = subparsers.add_parser('login')
parser_login.set_defaults(func=bz_login)
"""Create a new port args."""
parser_newport = subparsers.add_parser('newport', description='Upload a new port based on fabricator diff or patchfile.')
parser_newport.add_argument('--name', required=True, help='name of port')
parser_newport.add_argument('--patch-strip-count', default=None, help='Strip this many components off when peeling apart patch. git users should use 1')
group = parser_newport.add_mutually_exclusive_group(required=True)
group.add_argument('--fabid', default=None, help='fabricator id')
group.add_argument('--patchfile', default=None, help='path to patchfile')
parser_newport.set_defaults(func=bz_newport)
args = parser.parse_args()
args.func(args)
print "done..."
sys.exit(0)
if __name__ == "__main__":
main()