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
|
# vim:fileencoding=utf-8
# (c) 2011-2012 Michał Górny <mgorny@gentoo.org>
# Released under the terms of the 2-clause BSD license.
from __future__ import print_function
import os, os.path, shlex
import gobject
from traceback import format_exc
from optparse import OptionParser
from . import PV
from .dbus_handler import DBusHandler
from .library import load_library
from .repository import NewEbuildRepository
from .repository.pms_eclass import get_common_eclass_files
from .output import get_output_modules, TestResult
from .pm import get_package_managers
class PMSTestSuiteCLI(object):
"""
A class encapsulating the CLI routines used by the PMS Test Suite.
>>> cli = PMSTestSuiteCLI()
@ivar optparser: the option parser
@type optparser: optparse.OptionParser
@ivar library: the test library
@type library: L{TestLibrary}
@ivar pm: the active package manager
@type pm: L{PackageManager}
@ivar repository: the active ebuild repository
@type repository: L{EbuildRepository}
"""
_defpm = 'portage'
def __init__(self):
"""
Initialize the class. Set the option parser up.
"""
opt = OptionParser(version = PV)
def add_pmopts(opt, optstr, optval, parser):
try:
p = parser.values.pm[-1]
except IndexError:
p = self._defpm
if not hasattr(parser.values, 'pmopts'):
setattr(parser.values, 'pmopts', {})
parser.values.pmopts[p] = optval
opt.add_option('-C', '--create-repo-only', dest='create_repo_only',
help='Create ebuild repository only',
action='store_true', default=False)
opt.add_option('-l', '--library', dest='library_name',
help='Test library to use (default: standard)',
default='standard')
opt.add_option('-L', '--limit-packages', dest='limit_pkgs', type='int',
help='Limit the number of packages merged at once')
opt.add_option('-M', '--no-manifests', dest='no_manifests',
help='Disable Manifest generation/updates',
action='store_true', default=False)
opt.add_option('-o', '--output-module', dest='outputmod',
help='Output module to use',
default='cli')
opt.add_option('-O', '--output-file', dest='outputfile',
help='File to write output to (may not be used)')
opt.add_option('-p', '--package-manager', dest='pm',
help='Package manager to use (can be specified multiple times)',
action='append', default=[])
opt.add_option('-P', '--pm-options', callback=add_pmopts,
help='Additional options to pass to the Package Manager (shell-quoted,'
' apply to the parser specified before it)',
action='callback', type='string')
opt.add_option('-r', '--repository-name', dest='repo_name',
help='Name of the repository to store ebuilds in')
opt.add_option('-R', '--repository-path', dest='repo_path',
help='Path to the repository to store ebuilds in')
opt.add_option('-t', '--tests', dest='tests',
help='Limit the tests to be run (may be specified multiple times)',
action='append', default=[])
opt.add_option('-T', '--thorough', dest='thorough',
help='Run tests thoroughly (i.e. in all possible variants)',
action='store_true', default=False)
opt.add_option('-U', '--unsupported-eapis', dest='undefined',
help='Run tests in undefined-behavior EAPIs as well',
action='store_true', default=False)
opt.add_option('-v', '--verbose', dest='verbose',
help='Report test results verbosely (even if they succeed)',
action='store_true', default=False)
self.optparser = opt
def _start(self, prog, *args):
"""
Initialize the program. Parse command-line args. Instantiate classes
encapsulating the Package Manager, test library and repository.
@param prog: the program name (C{argv[0]})
@type prog: string
@param args: command line arguments (C{argv[1:]})
@type args: strings
"""
opt = self.optparser
opt.prog = os.path.basename(prog)
(opts, args) = opt.parse_args(list(args))
if not opts.pm:
opts.pm = [self._defpm]
if not hasattr(opts, 'pmopts'):
setattr(opts, 'pmopts', {})
if opts.repo_path and opts.repo_name:
opt.error('--repository-path and --repository-name are mutually exclusive')
if not opts.create_repo_only:
for x in get_output_modules():
if x.name == opts.outputmod:
self.output = x(opts.outputfile)
break
else:
opt.error('Output module not available: %s' % opts.outputmod)
pmset = set(opts.pm)
pms = []
for x in get_package_managers():
if x.name in pmset:
try:
pm = x.inst()
except Exception as e:
opt.error(e)
pm.pm_options = shlex.split(opts.pmopts[x.name]) \
if x.name in opts.pmopts else []
pms.append(pm)
pmset.remove(x.name)
if pmset:
opt.error('PM(s) not supported: %s' % ', '.join(pmset))
self.pms = sorted(pms, key = lambda x: opts.pm.index(x.name))
os.umask(0o22)
try:
if opts.repo_path:
self.repository = NewEbuildRepository(opts.repo_path, 'pms-test-suite')
for pm in self.pms:
pm.append_repository(self.repository)
else:
if not opts.repo_name:
opts.repo_name = 'pms-test-suite'
for pm in self.pms:
# XXX: all PMs should match it...
self.repository = pm.get_repository(opts.repo_name)
break
except (EnvironmentError, KeyError, ValueError) as e:
opt.error('Repository open failed: %s' % e)
dbus_uid = pm.config.userpriv_uid if pm.config.userpriv_enabled \
else None
self.dbus_hdlr = DBusHandler(uid = dbus_uid)
try:
try:
self.test_library = load_library(opts.library_name,
thorough = opts.thorough, undefined = opts.undefined,
dbus_hdlr = self.dbus_hdlr)
except (ImportError, TypeError) as e:
opt.error('Test library load failed: %s' % e)
for pm in self.pms:
pm.package_limit = opts.limit_pkgs
self.create_repo_only = opts.create_repo_only
self.update_manifests = not opts.no_manifests
self.verbose = opts.verbose
limit_tests = set()
for t in opts.tests:
limit_tests.update(t.split())
if limit_tests:
self.test_library.limit_tests(limit_tests)
except Exception:
dbus_hdlr.terminate()
def _print_stage(self, text):
print('-> [%s] %s...' % (self.pm.name, text))
def tests_done(self):
try:
self.pm.reload_config()
self._print_stage('Checking test results')
results = {}
for t in self.test_library:
tr = TestResult(t, self.pm)
if tr:
outc = '.'
elif tr.exception:
outc = 'E'
raise tr.exception
else:
outc = 'F'
print(outc, end='')
results[t] = tr
t.clean(self.pm)
self.results[self.pm] = results
print('')
if self.pm.has_pending_actions:
self._print_stage('Unmerging test ebuilds')
self.pm.commit(self.prepare)
else:
self.prepare()
except Exception as e:
self.exception = format_exc()
self.loop.quit()
def all_done(self):
ret = self.output(self.results, verbose = self.verbose)
self.ret = 0 if ret else 1
self.loop.quit()
def start_pm(self):
self._print_stage('Running PM')
for t in self.test_library:
t.start(self.pm)
self.pm.commit(self.tests_done)
def pre_unmerge_done(self):
try:
self.pm.reload_config()
for t in self.test_library:
t.clean(self.pm)
if self.pm.has_pending_actions:
print('Failed to unmerge the following test ebuilds:')
print(' '.join(self.pm.pkg_queue))
print('Refusing to proceed.')
self.loop.quit()
return
self.start_pm()
except Exception as e:
self.exception = format_exc()
self.loop.quit()
def prepare(self, first = False):
try:
try:
self.pm = next(self.pm_iter)
except StopIteration:
self.all_done()
else:
if not first:
self.pm.reload_config()
for t in self.test_library:
t.clean(self.pm)
if self.pm.has_pending_actions:
print('-> Unmerging already-merged test ebuilds...')
self.pm.commit(self.pre_unmerge_done)
else:
self.start_pm()
except Exception as e:
self.exception = format_exc()
self.loop.quit()
def generate_and_start(self):
try:
print('-> Generating ebuilds...')
files = {}
for t in self.test_library:
files.update(t.get_output_files())
if len(self.test_library) == 0:
print('No tests found (?!), refusing to proceed.')
return 1
files.update(get_common_eclass_files())
files.update(self.test_library.get_common_files())
self.repository.write_files(files)
if self.update_manifests:
needs_manifests = False
for pm in self.pms:
needs_manifests |= pm.requires_manifests
try:
self.repository.remanifest(files, pm)
except NotImplementedError:
pass
else:
break
else:
if needs_manifests:
print('No PM was able to do the Manifests, failing.')
return 1
if self.create_repo_only:
return 0
self.pm_iter = iter(self.pms)
self.results = {}
self.prepare(first = True)
except Exception as e:
self.exception = format_exc()
self.loop.quit()
return False
def main(self, argv):
self._start(*argv)
self.exception = None
try:
gobject.idle_add(self.generate_and_start)
self.ret = 1
self.loop = gobject.MainLoop()
self.loop.run()
finally:
# Ensure to terminate the spawned D-Bus.
self.dbus_hdlr.terminate()
if self.exception is not None:
return self.exception
return self.ret
|