blob: 8cf92ca2fe1747360c724bd2b68b4b10ca8245c0 [file] [log] [blame]
Patrick Williamsf1e5d692016-03-30 15:21:19 -05001#!/usr/bin/env python
2
3# devtool stress tester
4#
5# Written by: Paul Eggleton <paul.eggleton@linux.intel.com>
6#
7# Copyright 2015 Intel Corporation
8#
9# This program is free software; you can redistribute it and/or modify
10# it under the terms of the GNU General Public License version 2 as
11# published by the Free Software Foundation.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License along
19# with this program; if not, write to the Free Software Foundation, Inc.,
20# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21#
22
23import sys
24import os
25import os.path
26import subprocess
27import re
28import argparse
29import logging
30import tempfile
31import shutil
32import signal
33import fnmatch
34
35scripts_lib_path = os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'lib'))
36sys.path.insert(0, scripts_lib_path)
37import scriptutils
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050038import argparse_oe
Patrick Williamsf1e5d692016-03-30 15:21:19 -050039logger = scriptutils.logger_create('devtool-stress')
40
41def select_recipes(args):
42 import bb.tinfoil
43 tinfoil = bb.tinfoil.Tinfoil()
44 tinfoil.prepare(False)
45
46 pkg_pn = tinfoil.cooker.recipecache.pkg_pn
47 (latest_versions, preferred_versions) = bb.providers.findProviders(tinfoil.config_data, tinfoil.cooker.recipecache, pkg_pn)
48
49 skip_classes = args.skip_classes.split(',')
50
51 recipelist = []
52 for pn in sorted(pkg_pn):
53 pref = preferred_versions[pn]
54 inherits = [os.path.splitext(os.path.basename(f))[0] for f in tinfoil.cooker.recipecache.inherits[pref[1]]]
55 for cls in skip_classes:
56 if cls in inherits:
57 break
58 else:
59 recipelist.append(pn)
60
61 tinfoil.shutdown()
62
63 resume_from = args.resume_from
64 if resume_from:
65 if not resume_from in recipelist:
66 print('%s is not a testable recipe' % resume_from)
67 return 1
68 if args.only:
69 only = args.only.split(',')
70 for onlyitem in only:
71 for pn in recipelist:
72 if fnmatch.fnmatch(pn, onlyitem):
73 break
74 else:
75 print('%s does not match any testable recipe' % onlyitem)
76 return 1
77 else:
78 only = None
79 if args.skip:
80 skip = args.skip.split(',')
81 else:
82 skip = []
83
84 recipes = []
85 for pn in recipelist:
86 if resume_from:
87 if pn == resume_from:
88 resume_from = None
89 else:
90 continue
91
92 if args.only:
93 for item in only:
94 if fnmatch.fnmatch(pn, item):
95 break
96 else:
97 continue
98
99 skipit = False
100 for item in skip:
101 if fnmatch.fnmatch(pn, item):
102 skipit = True
103 if skipit:
104 continue
105
106 recipes.append(pn)
107
108 return recipes
109
110
111def stress_extract(args):
112 import bb.process
113
114 recipes = select_recipes(args)
115
116 failures = 0
117 tmpdir = tempfile.mkdtemp()
118 os.setpgrp()
119 try:
120 for pn in recipes:
121 sys.stdout.write('Testing %s ' % (pn + ' ').ljust(40, '.'))
122 sys.stdout.flush()
123 failed = False
124
125 srctree = os.path.join(tmpdir, pn)
126 try:
127 bb.process.run('devtool extract %s %s' % (pn, srctree))
128 except bb.process.CmdError as exc:
129 failed = True
130 with open('stress_%s_extract.log' % pn, 'w') as f:
131 f.write(str(exc))
132
133 if os.path.exists(srctree):
134 shutil.rmtree(srctree)
135
136 if failed:
137 print('failed')
138 failures += 1
139 else:
140 print('ok')
141 except KeyboardInterrupt:
142 # We want any child processes killed. This is crude, but effective.
143 os.killpg(0, signal.SIGTERM)
144
145 if failures:
146 return 1
147 else:
148 return 0
149
150
151def stress_modify(args):
152 import bb.process
153
154 recipes = select_recipes(args)
155
156 failures = 0
157 tmpdir = tempfile.mkdtemp()
158 os.setpgrp()
159 try:
160 for pn in recipes:
161 sys.stdout.write('Testing %s ' % (pn + ' ').ljust(40, '.'))
162 sys.stdout.flush()
163 failed = False
164 reset = True
165
166 srctree = os.path.join(tmpdir, pn)
167 try:
168 bb.process.run('devtool modify -x %s %s' % (pn, srctree))
169 except bb.process.CmdError as exc:
170 with open('stress_%s_modify.log' % pn, 'w') as f:
171 f.write(str(exc))
172 failed = 'modify'
173 reset = False
174
175 if not failed:
176 try:
177 bb.process.run('bitbake -c install %s' % pn)
178 except bb.process.CmdError as exc:
179 with open('stress_%s_install.log' % pn, 'w') as f:
180 f.write(str(exc))
181 failed = 'build'
182 if reset:
183 try:
184 bb.process.run('devtool reset %s' % pn)
185 except bb.process.CmdError as exc:
186 print('devtool reset failed: %s' % str(exc))
187 break
188
189 if os.path.exists(srctree):
190 shutil.rmtree(srctree)
191
192 if failed:
193 print('failed (%s)' % failed)
194 failures += 1
195 else:
196 print('ok')
197 except KeyboardInterrupt:
198 # We want any child processes killed. This is crude, but effective.
199 os.killpg(0, signal.SIGTERM)
200
201 if failures:
202 return 1
203 else:
204 return 0
205
206
207def main():
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500208 parser = argparse_oe.ArgumentParser(description="devtool stress tester",
209 epilog="Use %(prog)s <subcommand> --help to get help on a specific command")
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500210 parser.add_argument('-d', '--debug', help='Enable debug output', action='store_true')
211 parser.add_argument('-r', '--resume-from', help='Resume from specified recipe', metavar='PN')
212 parser.add_argument('-o', '--only', help='Only test specified recipes (comma-separated without spaces, wildcards allowed)', metavar='PNLIST')
213 parser.add_argument('-s', '--skip', help='Skip specified recipes (comma-separated without spaces, wildcards allowed)', metavar='PNLIST')
214 parser.add_argument('-c', '--skip-classes', help='Skip recipes inheriting specified classes (comma-separated) - default %(default)s', metavar='CLASSLIST', default='native,nativesdk,cross,cross-canadian,image,populate_sdk,meta,packagegroup')
215 subparsers = parser.add_subparsers(title='subcommands', metavar='<subcommand>')
216
217 parser_modify = subparsers.add_parser('modify',
218 help='Run "devtool modify" followed by a build with bitbake on matching recipes',
219 description='Runs "devtool modify" followed by a build with bitbake on matching recipes')
220 parser_modify.set_defaults(func=stress_modify)
221
222 parser_extract = subparsers.add_parser('extract',
223 help='Run "devtool extract" on matching recipes',
224 description='Runs "devtool extract" on matching recipes')
225 parser_extract.set_defaults(func=stress_extract)
226
227 args = parser.parse_args()
228
229 if args.debug:
230 logger.setLevel(logging.DEBUG)
231
232 import scriptpath
233 bitbakepath = scriptpath.add_bitbake_lib_path()
234 if not bitbakepath:
235 logger.error("Unable to find bitbake by searching parent directory of this script or PATH")
236 return 1
237 logger.debug('Found bitbake path: %s' % bitbakepath)
238
239 ret = args.func(args)
240
241if __name__ == "__main__":
242 main()