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
|
# Copyright (C) 2010-2012 Linaro Limited
#
# Author: Zygmunt Krynicki <zygmunt.krynicki@linaro.org>
#
# This file is part of lava-core
#
# lava-core is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License version 3
# as published by the Free Software Foundation
#
# lava-core is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with lava-core. If not, see <http://www.gnu.org/licenses/>.
from __future__ import absolute_import, print_function
"""
lava.core.main
==============
Implementation of the `lava-dev` shell command.
See the note in LavaDispatcher docstring about `lava-dev` vs `lava`
"""
import argparse
import logging
import os
import pdb
import sys
from lava.core.commands import Dispatcher
from lava.core.errors import CommandError
from lava.core.utils import Scratch
from lava.core.history import history
class AboutLavaAction(argparse.Action):
"""
Argparse action that implements the `lava-dev --about` command
"""
message = """
About LAVA
LAVA, which stands for Linaro Automated Validation Architecture, is a
project started by Linaro tasked with build a collection of freely
available tools and equipment for test automation. LAVA is composed of many
extensible pieces that together help to plan, schedule, execute, track, and
run tests on virtual and physical devices, then finally to store and
visualize the results.
To learn more about LAVA please visit https://launchpad.net/lava-project/
About Linaro
Linaro was established in June 2010 by founding members ARM, Freescale,
IBM, Samsung, ST-Ericsson and Texas instruments (TI). Members provide
engineering resources and funding. Linaro's goals are to deliver value to
its members through enabling their engineering teams to focus on
differentiation and product delivery, and to reduce time to market for
OEM/ODMs delivering open source based products using ARM technology.
To learn more about Linaro please visit http://www.linaro.org/about/
Software development and hacking:
Source code is freely available and is managed by the Bazaar version
control system and the Launchpad (http://launchpad.net/) collaboration
platform. Note that while LAVA is located in more than one project you
should eventually be able to look at the lava-core project for most of
the code. The process of merging small lava projects into lava-core is
not complete yet, as of May 2012.
To get a copy of LAVA install bazaar and run the following command:
$ bzr branch lp:lava-core
We welcome all contributors, see HACKING to get started
Development is discussed on the linaro-validation@lists.linaro.org mailing
list, you can subscribe by visiting http://lists.linaro.org/
Support:
Report bugs at https://bugs.launchpad.net/lava-project/+filebug
Ask questions in your native language at
https://answers.launchpad.net/lava-project/+addquestion.
You can also ask for real-time support in the #linaro-lava channel on the
freenode IRC network. Keep in mind that although geographically distributed
we (LAVA developers) may be away at a given time. Note that developer time
is precious so please be considerate.
Copyright and license:
Copyright (c) Linaro Limited 2010-2012
LAVA is free software, licensed under GLPv3, LGPLv3 and AGLPv3.
Check the license header in a specific file to know more.
"""
message = message.lstrip()
def __init__(self,
option_strings, dest, default=None, required=False,
help=None):
super(AboutLavaAction, self).__init__(
option_strings=option_strings, dest=dest, default=default, nargs=0,
help=help)
def __call__(self, parser, namespace, values, option_string=None):
parser.exit(message=self.message)
class LavaDispatcher(Dispatcher):
"""
Dispatcher implementing the `lava-dev` shell command (note that
`lava-dev` shall be renamed to `lava` once lava-core replaces
current group of lava-* projects.
This dispatcher imports plugins from the pkg_resources namespace
`lava-dev.commands`. Additional plugins can be registered as either
:class:`lava.command.Command` or :class:`lava.command.CommandGroup`
sub-classes.
"""
name = "lava-dev"
help = None
description = """
This executable allows you to work with LAVA. LAVA commands form a tree, where
you typically invoke sub or sub-sub commands to do actual things. Please
inspect the list below for details.
"""
epilog = """
For information about LAVA and Linaro run: lava-dev --about"""
def __init__(self):
# Call this early so that we don't get logging.basicConfig
# being called by accident. Otherwise we'd have to
# purge all loggers from the root logger and that sucks
self.setup_logging()
# Initialize the base dispatcher
super(LavaDispatcher, self).__init__()
# And import the non-flat namespace commands
self.import_commands('lava_dev.commands')
self.args = None
self._history_needs_saving = True
def construct_parser(self):
"""
Construct a parser for this dispatcher.
This is only used if the parser is not provided by the parent
dispatcher instance.
"""
# Construct a basic parser
parser = super(LavaDispatcher, self).construct_parser()
# Add the --about command
parser.register("action", "about_lava", AboutLavaAction)
parser.add_argument(
"--about",
action="about_lava",
default=argparse.SUPPRESS,
help="Provide detailed information about LAVA and Linaro")
# Add the --verbose flag
parser.add_argument(
"-v", "--verbose",
default=False,
action="store_true",
help="be more verbose (displays all INFO messages)")
group = parser.add_argument_group(title="history management")
group.add_argument(
"-H", "--history",
type=argparse.FileType(mode="wb"),
metavar="TARBALL",
help="Save session history, as tarball, to TARBALL")
group = parser.add_argument_group(
title="logging and software development",
description="""\
Log messages at the WARNING level or higher are sent to stderr. DEBUG
messages (when enabled by -D or -T) go to ~/.cache/lava/logs/debug.log.
""")
# Add the --debug flag
group.add_argument(
"-D", "--debug",
action="store_true",
default=False,
help="enable DEBUG messages on the root logger")
# Add the --trace flag
group.add_argument(
"-T", "--trace",
metavar="LOGGER",
action="append",
default=[],
help=("enable DEBUG messages on the specified logger "
"(can be used multiple times)"))
# Add the --pdb flag
group.add_argument(
"-P", "--pdb",
action="store_true",
default=False,
help="jump into pdb (python debugger) when a command crashes")
# Add the --debug-command-errors flag
group.add_argument(
"-C", "--debug-command-errors",
action="store_true",
default=False,
help="crash on CommandError, useful with --pdb")
# Add the --debug-interrupt flag
group.add_argument(
"-I", "--debug-interrupt",
action="store_true",
default=False,
help="crash on SIGINT/KeyboardInterrupt, useful with --pdb")
# Return the improved parser
return parser
def setup_logging(self):
"""
Setup logging for the root dispatcher
"""
self._setup_error_handler()
self._setup_warning_handler()
def dispatch(self, raw_args=None):
"""
Enhanced version of Dispatcher.dispatch() that logs command duration
and handles all exceptions (swallowing them)
"""
try:
return super(LavaDispatcher, self).dispatch(raw_args, True)
except SystemExit:
pass
except BaseException as exc:
if isinstance(exc, KeyboardInterrupt):
self.say(None, "Interrupted!")
if self.args is None or not self.args.debug_interrupt:
return 1
elif isinstance(exc, CommandError):
if self.args is None or not self.args.debug_command_errors:
return 1
else:
self.history.error(
"Runaway exception while invoking command: {exc}",
exc=str(exc))
if self.args and self.args.pdb:
self.logger.info("Starting pdb...")
pdb.post_mortem()
else:
self.say(None, "This command has crashed")
self.say(None, "The problem was: {0}", exc)
self.say(None, "See {0} for full traceback",
self.error_log_pathname),
return 1
finally:
# Save history as tarball
if self._history_needs_saving and self.args and self.args.history:
self.history.debug("Saving history to {name}",
name=self.args.history.name)
with Scratch() as scratch:
self.save_history_now(scratch)
def save_history_now(self, scratch):
self._history_needs_saving = False
if self.args is not None and self.args.history:
history.top.save_as_tarball(scratch, self.args.history)
def _setup_error_handler(self):
"""
Add a handler that logs all ERRORs to a dedicated file
"""
# TODO: support XDA dirs
pathname = os.path.expanduser("~/.cache/lava/logs/error.log")
self.error_log_pathname = pathname
if not os.path.exists(os.path.dirname(pathname)):
os.makedirs(os.path.dirname(pathname))
handler = logging.FileHandler(pathname, delay=True)
handler.setLevel(logging.ERROR)
handler.setFormatter(
logging.Formatter("%(levelname)s %(name)s: %(message)s"))
logging.getLogger().addHandler(handler)
def _setup_debug_handler(self):
"""
Add a handler that logs everything to a dedicated file
"""
# TODO: support XDA dirs
pathname = os.path.expanduser("~/.cache/lava/logs/debug.log")
self.say(None, "Note: debugging messages are saved to {0}", pathname)
if not os.path.exists(os.path.dirname(pathname)):
os.makedirs(os.path.dirname(pathname))
handler = logging.FileHandler(pathname)
handler.setLevel(logging.DEBUG)
handler.setFormatter(
logging.Formatter((
"%(asctime)s "
"[pid:%(process)s, thread:%(threadName)s, "
"reltime:%(relativeCreated)dms] "
"%(levelname)s %(name)s: %(message)s")))
logging.getLogger().addHandler(handler)
def _setup_warning_handler(self):
"""
Add a handler that logs warnings, and other important stuff to stderr
"""
handler = logging.StreamHandler(sys.stderr)
handler.setLevel(logging.WARN)
handler.setFormatter(
logging.Formatter("%(levelname)s: %(name)s: %(message)s"))
logging.getLogger().addHandler(handler)
def _setup_info_handler(self):
class OnlyInfoFilter(logging.Filterer):
def filter(self, record):
if record.levelno == logging.INFO:
return 1
return 0
class SayHandler(logging.Handler):
def emit(inner_self, record):
self.say(None, "{0}", record.getMessage())
handler = SayHandler()
handler.setLevel(logging.INFO)
handler.setFormatter(logging.Formatter("%(message)s"))
handler.addFilter(OnlyInfoFilter())
logging.getLogger().addHandler(handler)
def _adjust_logging_level(self, args):
# XXX: store arguments as we want to look at some more esoteric options
# in dispatch() later on, this feels ugly, perhaps the dispatcher
# should just know the arguments
self.args = args
# Enable verbose message handler
if args.verbose:
logging.getLogger().setLevel(logging.INFO)
self._setup_info_handler()
# Enable debug.log if needed
if args.debug or args.trace:
self._setup_debug_handler()
# Enable application-wide debugging
if args.debug:
logging.getLogger().setLevel(logging.DEBUG)
# Enable trace loggers
for name in args.trace:
logging.getLogger(name).setLevel(logging.DEBUG)
|