/usr/share/pyshared/qm/test/context.py is in qmtest 2.4.1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | ########################################################################
#
# File: context.py
# Author: Mark Mitchell
# Date: 11/06/2001
#
# Contents:
# QMTest Context class
#
# Copyright (c) 2001, 2002, 2003 by CodeSourcery, LLC. All rights reserved.
#
########################################################################
########################################################################
# Imports
########################################################################
import qm
import qm.common
import re
import sys
import types
########################################################################
# Classes
########################################################################
class ContextException(qm.common.QMException):
"""A 'ContextException' indicates an invalid context variable."""
def __init__(self, key, msg = "missing context variable"):
"""Construct a new 'ContextException'.
'key' -- A string giving the context key for which no valid
value was available.
'msg' -- A diagnostic identifier explaining the problem. The
message string may contain a fill-in for the key."""
msg = qm.error(msg, key = key)
qm.common.QMException.__init__(self, msg)
self.key = key
class ContextWrapper:
"""Do-nothing class to preserve pickle compatability.
A class called 'ContextWrapper' used to be used in instead of a
'Context' class in some cases, and we used to put contexts into
'Result's. Because of how pickles work, this means that the only way
to unpickle these old 'Result's is to have a do-nothing placeholder
class that can be instantiated and then thrown away."""
pass
class Context(types.DictType):
"""Test-time and local configuration for tests.
A 'Context' object contains all of the information a test needs to
execute, beyond what is stored as part of the test specification
itself. Information in the context can include,
* Local (per-user, etc.) configuration, such as where to find the
tested program.
* Environmental information, such as which machine the test is
running on.
* One-time configuration, including test arguments specified on
the command line.
A 'Context' object is effectively a mapping object whose keys must
be labels and values must be strings."""
TARGET_CONTEXT_PROPERTY = "qmtest.target"
"""The context variable giving the name of the current target."""
DB_PATH_CONTEXT_PROPERTY = "qmtest.dbpath"
"""The context variable giving the path to the database.
The value of this context variable will be a string giving the
path to the database directory. For example, if QMTest is invoked
as 'qmtest -D /path/to/db run', the value of this variable would
be '/path/to/db'. The value may be an absolute or a relative
path."""
ID_CONTEXT_PROPERTY = "qmtest.id"
"""The context variable giving the name of the running test or resource.
This value of this context variable will be the string giving the
name of the of the test or resource that is presently executing."""
TMPDIR_CONTEXT_PROPERTY = "qmtest.tmpdir"
"""A context property whose value is a string giving the path to a
temporary directory. This directory will be used only by the
'Runnable' in whose context this property occurs during the
execution of that 'Runnable'. No other object will use the same
temporary directory at the same time. There is no guarantee that
the temporary directory is empty, however; it may contain files
left behind by the execution of other 'Runnable' objects."""
__safe_for_unpickling__ = 1
"""Required to unpickle new-style classes under Python 2.2."""
def __init__(self, context = None):
"""Construct a new context.
'context' -- If not 'None', the existing 'Context' being
wrapped by this new context."""
super(Context, self).__init__()
self.__context = context
# Stuff everything in the RC configuration into the context.
options = qm.rc.GetOptions()
for option in options:
value = qm.rc.Get(option, None)
assert value is not None
self[option] = value
def GetDerivedValue(self, klass, variable, default = None):
"""Return the value for 'variable' in scope 'klass'.
Scopes are nested with '.', and inner variables hide
outer variables of the same name. Thus, looking up the
value of 'a.b.c.var' will return 1 if the context
contains
a.b.c.var=1
but 2 if it contains
a.b.d.var=1
a.b.var=2
a.var=3.
'klass' -- The variable's scope.
'variable' -- The variable name.
'default' -- Default value."""
while True:
if klass:
k = klass + '.' + variable
else:
k = variable
if self.has_key(k):
return self[k]
if not klass:
return default
if '.' not in klass:
klass = ''
else:
klass = klass[0:klass.rfind('.')]
def GetBoolean(self, key, default = None):
"""Return the boolean value associated with 'key'.
'key' -- A string.
'default' -- A default boolean value.
returns -- The value associated with 'key' in the context,
interpreted as a boolean.
If there is no value associated with 'key' and default is not
'None', then the boolean value associated with default is
used. If there is no value associated with 'key' and default
is 'None', an exception is raised.
The value associated with 'key' must be a string. If not, an
exception is raised. If the value is a string, but does not
correspond to a boolean value, an exception is raised."""
valstr = self.get(key)
if valstr is None:
if default is None:
raise ContextException(key)
else:
return default
try:
return qm.common.parse_boolean(valstr)
except ValueError:
raise ContextException(key, "invalid boolean context var")
def GetStringList(self, key, default = None):
"""Return the list of strings associated with 'key'.
'key' -- A string.
'default' -- A default list.
If there is no value associated with 'key' and default is not
'None', then the boolean value associated with default is
used. If there is no value associated with 'key' and default
is 'None', an exception is raised.
The value associated with 'key' must be a string. If not, an
exception is raised. If the value is a string, but does not
correspond to a string list, an exception is raised.
"""
valstr = self.get(key)
if valstr is None:
if default is None:
raise ContextException(key)
else:
return default
try:
return qm.common.parse_string_list(valstr)
except ValueError:
raise ContextException(key, "invalid string list context var")
def GetTemporaryDirectory(self):
"""Return the path to the a temporary directory.
returns -- The path to the a temporary directory. The
'Runnable' object may make free use of this temporary
directory; no other 'Runnable's will use the same directory at
the same time."""
return self[self.TMPDIR_CONTEXT_PROPERTY]
def Read(self, file_name):
"""Read the context file 'file_name'.
'file_name' -- The name of the context file.
Reads the context file and adds the context properties in the
file to 'self'."""
if file_name == "-":
# Read from standard input.
file = sys.stdin
else:
# Read from a named file.
try:
file = open(file_name, "r")
except:
raise qm.cmdline.CommandError, \
qm.error("could not read file", path=file_name)
# Read the assignments.
assignments = qm.common.read_assignments(file)
# Add them to the context.
for (name, value) in assignments.items():
try:
# Insert it into the context.
self[name] = value
except ValueError, msg:
# The format of the context key is invalid, but
# raise a 'CommandError' instead.
raise qm.cmdline.CommandError, msg
# Methods to simulate a map object.
def __contains__(self, key):
if super(Context, self).__contains__(key):
return 1
if self.__context is not None:
return self.__context.__contains__(key)
return 0
def get(self, key, default = None):
if key in self:
return self[key]
return default
def has_key(self, key):
return key in self
def __getitem__(self, key):
try:
return super(Context, self).__getitem__(key)
except KeyError:
if self.__context is None:
raise ContextException(key)
try:
return self.__context[key]
except KeyError:
raise ContextException(key)
def items(self):
if self.__context is None:
return super(Context, self).items()
else:
# Have to be careful, because self.__context and self may
# contain different values for the same keys, and the values
# defined in self should override the values defined in
# self.__context.
unified_dict = dict(self.__context.items())
unified_dict.update(self)
return unified_dict.items()
# Helper methods.
def GetAddedProperties(self):
"""Return the properties added to this context by resources.
returns -- A map from strings to values indicating properties
that were added to this context by resources."""
if self.__context is None:
return {}
added = self.__context.GetAddedProperties()
added.update(self)
return added
|