forked from openSUSE/openSUSE-release-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
osc-staging.py
599 lines (500 loc) · 24.9 KB
/
osc-staging.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
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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
import os
import os.path
import subprocess
import sys
import tempfile
import warnings
import yaml
import colorama
from colorama import Fore
from colorama import ansi
from osc import cmdln
from osc import conf
from osc import core
from osc import oscerr
from osclib.accept_command import AcceptCommand
from osclib.adi_command import AdiCommand
from osclib.check_command import CheckCommand
from osclib.check_duplicate_binaries_command import CheckDuplicateBinariesCommand
from osclib.cleanup_rings import CleanupRings
from osclib.conf import Config
from osclib.freeze_command import FreezeCommand
from osclib.ignore_command import IgnoreCommand
from osclib.unignore_command import UnignoreCommand
from osclib.list_command import ListCommand
from osclib.obslock import OBSLock
from osclib.select_command import SelectCommand
from osclib.stagingapi import StagingAPI
from osclib.cache import Cache
from osclib.unselect_command import UnselectCommand
from osclib.repair_command import RepairCommand
from osclib.rebuild_command import RebuildCommand
from osclib.request_splitter import RequestSplitter
from osclib.sentry import sentry_init
from osclib.supersede_command import SupersedeCommand
from osclib.prio_command import PrioCommand
try:
import __builtin__
input = getattr(__builtin__, 'raw_input')
except (ImportError, AttributeError):
pass
def _print_version(self):
from osclib.common import VERSION
print(VERSION)
quit(0)
def _full_project_name(self, project):
"""Deduce the full project name."""
if project.startswith(('openSUSE', 'SUSE')):
return project
if 'Factory' in project or 'openSUSE' in project:
return 'openSUSE:%s' % project
if 'SLE' in project:
return 'SUSE:%s' % project
# If we can't guess, raise a Warning
warnings.warn('%s project not recognized.' % project)
return project
def lock_needed(cmd, opts):
return not(
cmd in ('check', 'check_duplicate_binaries', 'check_local_links', 'frozenage', 'rebuild', 'unlock', 'setprio') or
(cmd == 'list' and not opts.supersede)
)
def clean_args(args):
out = []
for arg in args:
if arg == 'and':
continue
if ' ' not in arg:
arg = arg.rstrip(',')
if ',' in arg:
out.extend(arg.split(','))
continue
out.append(arg)
return out
@cmdln.option('--move', action='store_true',
help='force the selection to become a move')
@cmdln.option('--by-develproject', action='store_true',
help='split the requests by devel project')
@cmdln.option('--split', action='store_true',
help='split the requests into individual groups')
@cmdln.option('--supersede', action='store_true',
help='replace staged requests when superseded')
@cmdln.option('--filter-from', metavar='STAGING',
help='filter request list to only those from a specific staging')
@cmdln.option('-p', '--project', dest='project', metavar='PROJECT',
help='indicate the project on which to operate, default is openSUSE:Factory')
@cmdln.option('--force', action='store_true',
help='force action, overruling internal checks (CAUTION)')
@cmdln.option('-v', '--version', action='store_true',
help='print the plugin version')
@cmdln.option('--no-freeze', dest='no_freeze', action='store_true',
help='force the select command ignoring the time from the last freeze')
@cmdln.option('--cleanup', action='store_true', help='cleanup after completing operation')
@cmdln.option('--no-cleanup', dest='no_cleanup', action='store_true',
help='do not cleanup remaining packages in staging projects after accept')
@cmdln.option('--no-bootstrap', dest='bootstrap', action='store_false', default=True,
help='do not update bootstrap-copy when freezing')
@cmdln.option('--wipe-cache', dest='wipe_cache', action='store_true', default=False,
help='wipe GET request cache before executing')
@cmdln.option('-m', '--message', help='message used by ignore command')
@cmdln.option('--filter-by', action='append', help='xpath by which to filter requests')
@cmdln.option('--group-by', action='append', help='xpath by which to group requests')
@cmdln.option('-i', '--interactive', action='store_true', help='interactively modify selection proposal')
@cmdln.option('-n', '--non-interactive', action='store_true', help='do not ask anything, use default answers')
@cmdln.option('--merge', action='store_true', help='propose merge where applicable and store details to allow future merges')
@cmdln.option('--try-strategies', action='store_true', default=False, help='apply strategies and keep any with desireable outcome')
@cmdln.option('--strategy', help='apply a specific strategy')
@cmdln.option('--no-color', action='store_true', help='strip colors from output (or add staging.color = 0 to the .oscrc general section')
@cmdln.option('--save', action='store_true', help='save the result to the pseudometa package')
def do_staging(self, subcmd, opts, *args):
"""${cmd_name}: Commands to work with staging projects
${cmd_option_list}
"accept" will accept all requests in the given stagings. Without argument,
it accepts all acceptable stagings.
"adi" will list already staged requests, stage new requests, and supersede
requests where applicable. New adi stagings will be created for new
packages based on the grouping options used. The default grouping is by
source project. When adi stagings are empty, they are deleted.
"check" will check if all packages are links without changes
"check_local_links" lists local links that don't match multispec package
"check_duplicate_binaries" list binaries provided by multiple packages
"cleanup_rings" will try to cleanup rings content and print
out problems
"freeze" will freeze the sources of the project's links while not
affecting the source packages
"frozenage" will show when the respective staging project was last frozen
"ignore" will ignore a request from "list" and "adi" commands until unignored
"unignore" will remove from requests from ignore list
If the --cleanup flag is included then all ignored requests that were
changed from state new or review more than 3 days ago will be removed.
"list" will list/supersede requests for ring packages or all if no rings.
"lock" acquire a hold on the project in order to execute multiple commands
and prevent others from interrupting. An example:
lock -m "checkin round"
list --supersede
adi
accept A B C D E
unlock
Each command will update the lock to keep it up-to-date.
"repair" will attempt to repair the state of a request that has been
corrupted.
Use the --cleanup flag to include all untracked requests.
"select" will add requests to the project
Stagings are expected to be either in short-hand or the full project
name. For example letter or named stagings can be specified simply as
A, B, Gcc6, etc, while adi stagings can be specified as adi:1, adi:2,
etc. Currently, adi stagings are not supported in proposal mode.
Requests may either be the target package or the request ID.
When using --filter-by or --group-by the xpath will be applied to the
request node as returned by OBS. Use the following on a current request
to see the XML structure.
osc api /request/1337
A number of additional values will supplement the normal request node.
- ./action/target/@devel_project: the devel project for the package
- ./action/target/@devel_project_super: super devel project if relevant
- ./action/target/@ring: the ring to which the package belongs
- ./@aged: either True or False based on splitter-request-age-threshold
- ./@ignored: either False or the provided message
Some useful examples:
--filter-by './action/target[starts-with(@package, "yast-")]'
--filter-by './action/target/[@devel_project="YaST:Head"]'
--filter-by './action/target[starts-with(@ring, "1")]'
--filter-by '@id!="1234567"'
--filter-by 'contains(description, "#Portus")'
--group-by='./action/target/@devel_project'
--group-by='./action/target/@ring'
Multiple filter-by or group-by options may be used at the same time.
Note that when using proposal mode, multiple stagings to consider may be
provided in addition to a list of requests by which to filter. A more
complex example:
select --group-by='./action/target/@devel_project' A B C 123 456 789
This will separate the requests 123, 456, 789 by devel project and only
consider stagings A, B, or C, if available, for placement.
No arguments is also a valid choice and will propose all non-ignored
requests into the first available staging. Note that bootstrapped
stagings are only used when either required or no other stagings are
available.
Another useful example is placing all open requests into a specific
letter staging with:
select A
Built in strategies may be specified as well. For example:
select --strategy devel
select --strategy quick
select --strategy special
select --strategy super
The default is none and custom is used with any filter-by or group-by
arguments are provided.
To merge applicable requests into an existing staging.
select --merge A
To automatically try all available strategies.
select --try-strategies
These concepts can be combined and interactive mode allows the proposal
to be modified before it is executed.
Moving requests can be accomplished using the --move flag. For example,
to move already staged pac1 and pac2 to staging B use the following.
select --move B pac1 pac2
The staging in which the requests are staged will automatically be
determined and the requests will be removed from that staging and placed
in the specified staging.
Related to this, the --filter-from option may be used in conjunction
with --move to only move requests already staged in a specific staging.
This can be useful if a staging master is responsible for a specific set
of packages and wants to move them into a different staging when they
were already placed in a mixed staging. For example, if one had a file
with a list of packages the following would move any of them found in
staging A to staging B.
select --move --filter-from A B $(< package.list)
"unselect" will remove from the project - pushing them back to the backlog
If a message is included the requests will be ignored first.
Use the --cleanup flag to include all obsolete requests.
"unlock" will remove the staging lock in case it gets stuck or a manual hold
If a command lock gets stuck while a hold is placed on a project the
unlock command will need to be run twice since there are two layers of
locks.
"rebuild" will rebuild broken packages in the given stagings or all
The rebuild command will only trigger builds for packages with less than
3 failures since the last success or if the build log indicates a stall.
If the force option is included the rebuild checks will be ignored and
all packages failing to build will be triggered.
"setprio" will set priority of requests withing stagings
If no stagings are specified all stagings will be used.
The default priority is important, but the possible values are:
"critical", "important", "moderate" or "low".
"supersede" will supersede requests were applicable.
A request list can be used to limit what is superseded.
Usage:
osc staging accept [--force] [--no-cleanup] [STAGING...]
osc staging adi [--move] [--by-develproject] [--split] [REQUEST...]
osc staging check [STAGING...]
osc staging check_duplicate_binaries
osc staging check_local_links
osc staging cleanup_rings
osc staging freeze [--no-bootstrap] STAGING...
osc staging frozenage [STAGING...]
osc staging ignore [-m MESSAGE] REQUEST...
osc staging unignore [--cleanup] [REQUEST...|all]
osc staging list [--supersede]
osc staging lock [-m MESSAGE]
osc staging select [--no-freeze] [--move [--filter-from STAGING]]
STAGING REQUEST...
osc staging select [--no-freeze] [--interactive|--non-interactive]
[--filter-by...] [--group-by...]
[--merge] [--try-strategies] [--strategy]
[STAGING...] [REQUEST...]
osc staging unselect [--cleanup] [-m MESSAGE] [REQUEST...]
osc staging unlock
osc staging rebuild [--force] [STAGING...]
osc staging repair [--cleanup] [REQUEST...]
osc staging setprio [STAGING...] [priority]
osc staging supersede [REQUEST...]
"""
if opts.version:
self._print_version()
# verify the argument counts match the commands
if len(args) == 0:
raise oscerr.WrongArgs('No command given, see "osc help staging"!')
cmd = args[0]
if cmd in (
'accept',
'adi',
'check',
'config',
'frozenage',
'unignore',
'select',
'unselect',
'rebuild',
'repair',
'supersede',
):
min_args, max_args = 0, None
elif cmd in (
'freeze',
'setprio',
'ignore',
):
min_args, max_args = 1, None
elif cmd in (
'check_duplicate_binaries',
'check_local_links',
'cleanup_rings',
'list',
'lock',
'unlock',
):
min_args, max_args = 0, 0
else:
raise oscerr.WrongArgs('Unknown command: %s' % cmd)
args = clean_args(args)
if len(args) - 1 < min_args:
raise oscerr.WrongArgs('Too few arguments.')
if max_args is not None and len(args) - 1 > max_args:
raise oscerr.WrongArgs('Too many arguments.')
# Allow for determining project from osc store.
if not opts.project:
if core.is_project_dir('.'):
opts.project = core.store_read_project('.')
else:
opts.project = 'Factory'
# Cache the remote config fetch.
Cache.init()
# Init the OBS access and configuration
opts.project = self._full_project_name(opts.project)
opts.apiurl = self.get_api_url()
opts.verbose = False
Config(opts.apiurl, opts.project)
colorama.init(autoreset=True,
strip=(opts.no_color or not bool(int(conf.config.get('staging.color', True)))))
# Allow colors to be changed.
for name in dir(Fore):
if not name.startswith('_'):
# .oscrc requires keys to be lower-case.
value = conf.config.get('staging.color.' + name.lower())
if value:
setattr(Fore, name, ansi.code_to_chars(value))
sentry_init(opts.apiurl, {'osc_plugin': subcmd})
if opts.wipe_cache:
Cache.delete_all()
api = StagingAPI(opts.apiurl, opts.project)
needed = lock_needed(cmd, opts)
with OBSLock(opts.apiurl, opts.project, reason=cmd, needed=needed) as lock:
# call the respective command and parse args by need
if cmd == 'check':
if len(args) == 1:
CheckCommand(api).perform(None)
else:
for prj in args[1:]:
CheckCommand(api).perform(prj)
print()
elif cmd == 'check_duplicate_binaries':
CheckDuplicateBinariesCommand(api).perform(opts.save)
elif cmd == 'check_local_links':
AcceptCommand(api).check_local_links()
elif cmd == 'freeze':
for prj in args[1:]:
prj = api.prj_from_short(prj)
print(Fore.YELLOW + prj)
FreezeCommand(api).perform(prj, copy_bootstrap=opts.bootstrap)
elif cmd == 'frozenage':
projects = api.get_staging_projects_short() if len(args) == 1 else args[1:]
for prj in projects:
prj = api.prj_from_letter(prj)
print('{} last frozen {}{:.1f} days ago'.format(
Fore.YELLOW + prj + Fore.RESET,
Fore.GREEN if api.prj_frozen_enough(prj) else Fore.RED,
api.days_since_last_freeze(prj)))
elif cmd == 'accept':
cmd = AcceptCommand(api)
cmd.accept_all(args[1:], opts.force, not opts.no_cleanup)
elif cmd == 'unselect':
UnselectCommand(api).perform(args[1:], opts.cleanup, opts.message)
elif cmd == 'select':
# Include list of all stagings in short-hand and by full name.
existing_stagings = api.get_staging_projects_short(None)
existing_stagings += api.get_staging_projects()
stagings = []
requests = []
for arg in args[1:]:
# Since requests may be given by either request ID or package
# name and stagings may include multi-letter special stagings
# there is no easy way to distinguish between stagings and
# requests in arguments. Therefore, check if argument is in the
# list of short-hand and full project name stagings, otherwise
# consider it a request. This also allows for special stagings
# with the same name as package, but the staging will be assumed
# first time around. The current practice seems to be to start a
# special staging with a capital letter which makes them unique.
# lastly adi stagings are consistently prefix with adi: which
# also makes it consistent to distinguish them from request IDs.
if arg in existing_stagings and arg not in stagings:
stagings.append(api.extract_staging_short(arg))
elif arg not in requests:
requests.append(arg)
if len(stagings) != 1 or len(requests) == 0 or opts.filter_by or opts.group_by:
if opts.move or opts.filter_from:
print('--move and --filter-from must be used with explicit staging and request list')
return
open_requests = api.get_open_requests({'withhistory': 1})
if len(open_requests) == 0:
print('No open requests to consider')
return
splitter = RequestSplitter(api, open_requests, in_ring=True)
considerable = splitter.stagings_load(stagings)
if considerable == 0:
print('No considerable stagings on which to act')
return
if opts.merge:
splitter.merge()
if opts.try_strategies:
splitter.strategies_try()
if len(requests) > 0:
splitter.strategy_do('requests', requests=requests)
if opts.strategy:
splitter.strategy_do(opts.strategy)
elif opts.filter_by or opts.group_by:
kwargs = {}
if opts.filter_by:
kwargs['filters'] = opts.filter_by
if opts.group_by:
kwargs['groups'] = opts.group_by
splitter.strategy_do('custom', **kwargs)
else:
if opts.merge:
# Merge any none strategies before final none strategy.
splitter.merge(strategy_none=True)
splitter.strategy_do('none')
splitter.strategy_do_non_bootstrapped('none')
proposal = splitter.proposal
if len(proposal) == 0:
print('Empty proposal')
return
if opts.interactive:
with tempfile.NamedTemporaryFile(mode='w', suffix='.yml') as temp:
temp.write(yaml.safe_dump(splitter.proposal, default_flow_style=False) + '\n\n')
if len(splitter.requests):
temp.write('# remaining requests:\n')
for request in splitter.requests:
temp.write('# {}: {}\n'.format(
request.get('id'), request.find('action/target').get('package')))
temp.write('\n')
temp.write('# move requests between stagings or comment/remove them\n')
temp.write('# change the target staging for a group\n')
temp.write('# remove the group, requests, staging, or strategy to skip\n')
temp.write('# stagings\n')
if opts.merge:
temp.write('# - mergeable: {}\n'
.format(', '.join(sorted(splitter.stagings_mergeable +
splitter.stagings_mergeable_none))))
temp.write('# - considered: {}\n'
.format(', '.join(sorted(splitter.stagings_considerable))))
temp.write('# - remaining: {}\n'
.format(', '.join(sorted(splitter.stagings_available))))
temp.flush()
editor = os.getenv('EDITOR')
if not editor:
editor = 'xdg-open'
return_code = subprocess.call(editor.split(' ') + [temp.name])
proposal = yaml.safe_load(open(temp.name).read())
# Filter invalidated groups from proposal.
keys = ['group', 'requests', 'staging', 'strategy']
for group, info in sorted(proposal.items()):
for key in keys:
if not info.get(key):
del proposal[group]
break
print(yaml.safe_dump(proposal, default_flow_style=False))
print('Accept proposal? [y/n] (y): ', end='')
if opts.non_interactive:
print('y')
else:
response = input().lower()
if response != '' and response != 'y':
print('Quit')
return
for group, info in sorted(proposal.items()):
print('Staging {} in {}'.format(group, info['staging']))
# SelectCommand expects strings.
request_ids = map(str, info['requests'].keys())
target_project = api.prj_from_short(info['staging'])
# TODO: Find better place for splitter info
# if 'merge' not in info:
# Assume that the original splitter_info is desireable
# and that this staging is simply manual followup.
# api.set_splitter_info_in_prj_pseudometa(target_project, info['group'], info['strategy'])
SelectCommand(api, target_project) \
.perform(request_ids, no_freeze=opts.no_freeze)
else:
target_project = api.prj_from_short(stagings[0])
filter_from = api.prj_from_short(opts.filter_from) if opts.filter_from else None
SelectCommand(api, target_project) \
.perform(requests, opts.move,
filter_from, opts.no_freeze)
elif cmd == 'cleanup_rings':
CleanupRings(api).perform()
elif cmd == 'ignore':
IgnoreCommand(api).perform(args[1:], opts.message)
elif cmd == 'unignore':
UnignoreCommand(api).perform(args[1:], opts.cleanup)
elif cmd == 'list':
ListCommand(api).perform(supersede=opts.supersede)
elif cmd == 'lock':
lock.hold(opts.message)
elif cmd == 'adi':
AdiCommand(api).perform(args[1:], move=opts.move, by_dp=opts.by_develproject, split=opts.split)
elif cmd == 'rebuild':
RebuildCommand(api).perform(args[1:], opts.force)
elif cmd == 'repair':
RepairCommand(api).perform(args[1:], opts.cleanup)
elif cmd == 'setprio':
stagings = []
priority = None
priorities = ['critical', 'important', 'moderate', 'low']
for arg in args[1:]:
if arg in priorities:
priority = arg
else:
stagings.append(arg)
PrioCommand(api).perform(stagings, priority)
elif cmd == 'supersede':
SupersedeCommand(api).perform(args[1:])
elif cmd == 'unlock':
lock.release(force=True)