forked from openSUSE/openSUSE-release-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
requestfinder.py
executable file
·233 lines (190 loc) · 8.37 KB
/
requestfinder.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
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
#!/usr/bin/python3
from configparser import ConfigParser
from xdg.BaseDirectory import load_first_config
from lxml import etree as ET
import sys
import cmdln
import os
import osc.core
import ToolBase
class Requestfinder(ToolBase.ToolBase):
def __init__(self):
ToolBase.ToolBase.__init__(self)
def fill_package_meta(self, project):
self.package_metas = dict()
url = osc.core.makeurl(self.apiurl, ['search', 'package'], "match=[@project='%s']" % project)
root = ET.fromstring(self.cached_GET(url))
for p in root.findall('package'):
name = p.attrib['name']
self.package_metas[name] = p
def find_requests(self, settings):
xquery = settings['query']
if settings['devel']:
self.fill_package_meta('openSUSE:Factory')
url = osc.core.makeurl(self.apiurl, ('search', 'request'), {"match": xquery})
root = ET.parse(osc.core.http_GET(url)).getroot()
self.requests = []
for request in root.findall('request'):
req = osc.core.Request()
req.read(request)
if settings['devel']:
p = req.actions[0].tgt_package
pm = self.package_metas[p] if p in self.package_metas else None
devel = pm.find('devel') if pm else None
if devel is None or devel.get('project') in settings['devel']:
self.requests.append(req)
else:
self.requests.append(req)
return self.requests
class CommandLineInterface(ToolBase.CommandLineInterface):
def __init__(self, *args, **kwargs):
ToolBase.CommandLineInterface.__init__(self, args, kwargs)
self.cp = ConfigParser()
d = load_first_config('opensuse-release-tools')
if d:
self.cp.read(os.path.join(d, 'requestfinder.conf'))
def get_optparser(self):
parser = ToolBase.CommandLineInterface.get_optparser(self)
return parser
def setup_tool(self):
tool = Requestfinder()
return tool
def _load_settings(self, settings, name):
section = 'settings {}'.format(name)
for option in settings.keys():
if self.cp.has_option(section, option):
settings[option] = self.cp.get(section, option).replace('\n', ' ')
def print_actions(self, r):
for a in r.actions:
if a.type == 'submit':
print(' '.join(('#', r.reqid, a.type, a.src_project, a.src_package, a.tgt_project)))
else:
if hasattr(a, 'tgt_package'):
print(' '. join(('#', r.reqid, a.type, a.tgt_project, a.tgt_package)))
else:
print(' '. join(('#', r.reqid, a.type, a.tgt_project)))
@cmdln.option('--exclude-project', metavar='PROJECT', action='append', help='exclude review by specific project')
@cmdln.option('--exclude-user', metavar='USER', action='append', help='exclude review by specific user')
@cmdln.option('--query', metavar='filterstr', help='filter string')
@cmdln.option('--action', metavar='action', help='action (accept/decline)')
@cmdln.option('--settings', metavar='settings', help='settings to load from config file')
@cmdln.option('-m', '--message', metavar="message", help="message")
@cmdln.option('--devel', dest='devel', metavar='PROJECT', action='append',
help='only packages with specified devel project')
def do_review(self, subcmd, opts):
"""${cmd_name}: print commands for reviews
${cmd_usage}
${cmd_option_list}
"""
settings = {
'action': 'accept',
'message': 'ok',
'query': None,
'exclude-project': None,
'exclude-user': None,
'exclude-group': None,
'devel': None,
}
if opts.settings:
self._load_settings(settings, opts.settings)
if opts.action:
settings['action'] = opts.action
settings['message'] = opts.action
if opts.message:
settings['message'] = opts.message
if opts.devel:
settings['devel'] = opts.devel
if opts.query:
settings['query'] = opts.query
if not settings['query']:
raise Exception('please specify query')
rqs = self.tool.find_requests(settings)
for r in rqs:
self.print_actions(r)
for review in r.reviews:
if review.state != 'new':
continue
if review.by_project:
skip = False
if settings['exclude-project']:
for p in settings['exclude-project'].split(' '):
if review.by_project.startswith(p):
skip = True
break
if not skip:
if review.by_package:
print("osc review %s -m '%s' -P %s -p %s %s" %
(settings['action'], settings['message'],
review.by_project, review.by_package, r.reqid))
else:
print("osc review %s -m '%s' -P %s %s" %
(settings['action'], settings['message'], review.by_project, r.reqid))
elif review.by_group:
skip = False
if settings['exclude-group']:
groups = settings['exclude-group'].split(' ')
for g in groups:
if review.by_group == g:
skip = True
break
if not skip:
print("osc review %s -m '%s' -G %s %s" %
(settings['action'], settings['message'], review.by_group, r.reqid))
elif review.by_user:
skip = False
if settings['exclude-user']:
users = settings['exclude-user'].split(' ')
for u in users:
if review.by_user == u:
skip = True
break
if not skip:
print("osc review %s -m '%s' -U %s %s" %
(settings['action'], settings['message'], review.by_user, r.reqid))
@cmdln.option('--query', metavar='filterstr', help='filter string')
@cmdln.option('--action', metavar='action', help='action (accept/decline)')
@cmdln.option('--settings', metavar='settings', help='settings to load from config file')
@cmdln.option('-m', '--message', metavar="message", help="message")
@cmdln.option('--devel', dest='devel', metavar='PROJECT', action='append', help='only packages with specified devel project')
def do_request(self, subcmd, opts):
"""${cmd_name}: print commands for requests
${cmd_usage}
${cmd_option_list}
"""
settings = {
'action': 'reopen',
'message': 'reopen',
'query': None,
'devel': None,
}
if opts.settings:
self._load_settings(settings, opts.settings)
if opts.action:
settings['action'] = opts.action
settings['message'] = opts.action
if opts.message:
settings['message'] = opts.message
if opts.devel:
settings['devel'] = opts.devel
rqs = self.tool.find_requests(settings)
for r in rqs:
self.print_actions(r)
print("osc rq {} -m '{}' {}".format(settings['action'], settings['message'], r.reqid))
def help_examples(self):
return """$ cat > ~/.config/opensuse-release-tools/requestfinder.conf << EOF
[settings foo]
query = (review[@by_project='example' and @state='new']
and state/@name='review'
and action/source/@project='openSUSE:Factory'
and action/target/@project='openSUSE:Leap:15.0')
exclude-user = repo-checker
exclude-project = openSUSE:Leap:15.0:Staging
message = override
action = accept
EOF
$ ${name} review --settings foo | tee doit.sh
./doit.sh
"""
if __name__ == "__main__":
app = CommandLineInterface()
sys.exit(app.main())