-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathbase.py
More file actions
426 lines (363 loc) · 15.2 KB
/
base.py
File metadata and controls
426 lines (363 loc) · 15.2 KB
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
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
# Python API for the Nitrate test case management system.
# Copyright (c) 2012 Red Hat, Inc. All rights reserved.
# Author: Petr Splichal <psplicha@redhat.com>
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"""
Nitrate class and internal utilities
Search support
~~~~~~~~~~~~~~
Multiple Nitrate classes provide the static method 'search' which takes
the search query in the Django QuerySet format which gives an easy
access to the foreign keys and basic search operators. For example:
Product.search(name="Red Hat Enterprise Linux 6")
TestPlan.search(name__contains="python")
TestRun.search(manager__email='login@example.com'):
TestCase.search(script__startswith='/CoreOS/python')
For the complete list of available operators see Django documentation:
https://docs.djangoproject.com/en/dev/ref/models/querysets/#field-lookups
"""
import six
import datetime
import nitrate.config as config
import nitrate.utils as utils
import nitrate.xmlrpc_driver as xmlrpc_driver
import nitrate.teiid as teiid
from nitrate.config import log, Config
from nitrate.xmlrpc_driver import NitrateError
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Internal Utilities
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def _getter(field):
"""
Simple getter factory function.
For given field generate getter function which calls self._fetch(), to
initialize instance data if necessary, and returns self._field.
"""
def getter(self):
# Initialize the attribute unless already done
if getattr(self, "_" + field) is NitrateNone:
self._fetch()
# Return self._field
return getattr(self, "_" + field)
return getter
def _setter(field):
"""
Simple setter factory function.
For given field return setter function which calls self._fetch(), to
initialize instance data if necessary, updates the self._field and
remembers modifed state if the value is changed.
"""
def setter(self, value):
# Initialize the attribute unless already done
if getattr(self, "_" + field) is NitrateNone:
self._fetch()
# Update only if changed
if getattr(self, "_" + field) != value:
setattr(self, "_" + field, value)
log.info(u"Updating {0}'s {1} to '{2}'".format(
self.identifier, field, value))
# Remember modified state if caching
if config.get_cache_level() != config.CACHE_NONE:
self._modified = True
# Save the changes immediately otherwise
else:
self._update()
return setter
def _idify(id):
"""
Pack/unpack multiple ids into/from a single id
List of ids is converted into a single id. Single id is converted
into list of original ids. For example:
_idify([1, 2]) ---> 1000000002
_idify(1000000002) ---> [1, 2]
This is used for indexing by fake internal id.
"""
if isinstance(id, list):
result = 0
for value in id:
result = result * config._MAX_ID + value
return result
elif isinstance(id, int):
result = []
while id > 0:
remainder = id % config._MAX_ID
id = id // config._MAX_ID
result.append(int(remainder))
result.reverse()
return result
else:
raise NitrateError("Invalid id for idifying: '{0}'".format(id))
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# NitrateNone Class
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class NitrateNone(object):
""" Used for distinguishing uninitialized values from regular 'None' """
pass
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Nitrate Class
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class Nitrate(object):
"""
General Nitrate Object.
Takes care of initiating the connection to the Nitrate server and
parses user configuration.
"""
# Unique object identifier. If not None ---> object is initialized
# (all unknown attributes are set to special value NitrateNone)
_id = None
# Timestamp when the object data were fetched from the server.
# If not None, all object attributes are filled with real data.
_fetched = None
# Default expiration for immutable objects is 1 month
_expiration = datetime.timedelta(days=30)
# List of all object attributes (used for init & expiration)
_attributes = []
_connection = None
_teiid_instance = None
_requests = 0
_multicall_proxy = None
_identifier_width = 0
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Nitrate Properties
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
id = property(_getter("id"), doc="Object identifier.")
@property
def identifier(self):
""" Consistent identifier string """
# Use id if known
if self._id not in [None, NitrateNone]:
id = self._id
# When unknown use 'ID#UNKNOWN' or 'ID#UNKNOWN (name)'
else:
name = getattr(self, "_name", None)
if name not in [None, NitrateNone]:
id = "UNKNOWN ({0})".format(name)
else:
id = "UNKNOWN"
return "{0}#{1}".format(
self._prefix, str(id).rjust(self._identifier_width, "0"))
@property
def _server(self):
""" Connection to the server """
# Connect to the server unless already connected
if Nitrate._connection is None:
log.debug(u"Contacting server {0}".format(
Config().nitrate.url))
# Plain authentication if username & password given
try:
Nitrate._connection = xmlrpc_driver.NitrateXmlrpc(
Config().nitrate.username,
Config().nitrate.password,
Config().nitrate.url).server
# Kerberos otherwise
except AttributeError:
Nitrate._connection = xmlrpc_driver.NitrateKerbXmlrpc(
Config().nitrate.url).server
# Return existing connection
Nitrate._requests += 1
return Nitrate._connection
@property
def _teiid(self):
""" Connection to the Teiid instance """
# Create the instance unless already exist
if Nitrate._teiid_instance is None:
Nitrate._teiid_instance = teiid.Teiid()
# Return the instance
return Nitrate._teiid_instance
@classmethod
def _cache_lookup(cls, id, **kwargs):
""" Look up cached objects, return found instance and search key """
# ID check
if isinstance(id, int) or isinstance(id, six.string_types):
return cls._cache[id], id
# Check injet (initial object dictionary) for id
if isinstance(id, dict):
return cls._cache[id['id']], id["id"]
raise KeyError
@classmethod
def _is_cached(cls, id):
"""
Check whether objects are cached (initialized & fetched)
Accepts object id, list of ids, object or a list of objects.
Makes sure that the object is in the memory and has attached
all attributes. For ids, cache index is checked for presence.
"""
# Check fetch timestamp if object given
if isinstance(id, Nitrate):
return id._fetched is not None
# Check for presence in cache, make sure the object is fetched
if isinstance(id, int) or isinstance(id, six.string_types):
return id in cls._cache and cls._cache[id]._fetched is not None
# Run recursively for each given id/object if list given
if isinstance(id, list) or isinstance(id, set):
return all(cls._is_cached(i) for i in id)
# Something went wrong
return False
@property
def _is_expired(self):
""" Check if cached object has expired """
return self._fetched is None or (
datetime.datetime.now() - self._fetched) > self._expiration
def _is_initialized(self, id_or_inject, **kwargs):
"""
Check whether the object is initialized, handle names & injects
Takes object id or inject (initial object dict), detects which
of them was given, checks whether the object has already been
initialized and returns tuple: (id, name, inject, initialized).
"""
id = name = inject = None
# Initial object dict
if isinstance(id_or_inject, dict):
inject = id_or_inject
# Object identified by name
elif isinstance(id_or_inject, six.string_types):
name = id_or_inject
# Regular object id
else:
id = id_or_inject
# Initialized objects have the self._id attribute set
if self._id is None:
return id, name, inject, False
# If inject given, fetch data from it (unless already fetched)
if inject is not None and not self._fetched:
self._fetch(inject, **kwargs)
return id, name, inject, True
@property
def _multicall(self):
"""
Enqueue xmlrpc calls if MultiCall enabled otherwise send directly
If MultiCall mode enabled, put xmlrpc calls to the queue, otherwise
send them directly to server.
"""
if Nitrate._multicall_proxy is not None:
return self._multicall_proxy
else:
return self._server
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Nitrate Special
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def __new__(cls, id=None, *args, **kwargs):
""" Create a new object, handle caching if enabled """
# No caching when turned of or class does not support it
if (config.get_cache_level() < config.CACHE_OBJECTS or
getattr(cls, "_cache", None) is None):
return super(Nitrate, cls).__new__(cls)
# Make sure that cache has been initialized
Cache()
# Look up cached object by id (or other arguments in kwargs)
try:
# If found, we get instance and key by which it was found
instance, key = cls._cache_lookup(id, **kwargs)
if isinstance(key, int):
log.cache("Using cached {0} ID#{1}".format(cls.__name__, key))
else:
log.cache("Using cached {0} '{1}'".format(cls.__name__, key))
return instance
# Object not cached yet, create a new one and cache it
except KeyError:
new = super(Nitrate, cls).__new__(cls)
if isinstance(id, int):
log.cache("Caching {0} ID#{1}".format(cls.__name__, id))
cls._cache[id] = new
elif isinstance(id, six.string_types) or "name" in kwargs:
log.cache("Caching {0} '{1}'".format(
cls.__name__, (id or kwargs.get("name"))))
return new
def __init__(self, id=None, prefix="ID"):
""" Initialize the object id, prefix and internal attributes """
# Set up the prefix
self._prefix = prefix
# Initialize internal attributes and reset the fetch timestamp
self._init()
# Check and set the object id
if id is None:
self._id = NitrateNone
elif isinstance(id, int):
self._id = id
else:
try:
self._id = int(id)
except ValueError:
raise NitrateError("Invalid {0} id: '{1}'".format(
self.__class__.__name__, id))
def __str__(self):
""" Provide ascii string representation """
if six.PY2:
return utils.ascii(unicode(self))
else:
return self.__unicode__()
def __unicode__(self):
""" Short summary about the connection """
return u"Nitrate server: {0}\nTotal requests handled: {1}".format(
Config().nitrate.url, self._requests)
def __eq__(self, other):
""" Objects are compared based on their id """
# Special handling for comparison with None
if other is None:
return False
# We can only compare objects of the same type
if self.__class__ != other.__class__:
raise NitrateError("Cannot compare '{0}' with '{1}'".format(
self.__class__.__name__, other.__class__.__name__))
return self.id == other.id
def __ne__(self, other):
""" Objects are compared based on their id """
return not(self == other)
def __hash__(self):
""" Use object id as the default hash """
return self.id
def __repr__(self):
""" Object(id) or Object('name') representation """
# Use the object id by default, name (if available) otherwise
if self._id is not NitrateNone:
id = self._id
elif getattr(self, "_name", NitrateNone) is not NitrateNone:
id = "'{0}'".format(self._name)
else:
id = "<unknown>"
return "{0}({1})".format(self.__class__.__name__, id)
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Nitrate Methods
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def _init(self):
""" Set all object attributes to NitrateNone, reset fetch timestamp """
# Each class is expected to have a list of attributes defined
for attribute in self._attributes:
setattr(self, "_" + attribute, NitrateNone)
# And reset the fetch timestamp
self._fetched = None
def _fetch(self, inject=None):
""" Fetch object data from the server """
# This is to be implemented by respective class.
# Here we just save the timestamp when data were fetched.
self._fetched = datetime.datetime.now()
# Store the initial object dict for possible future use
self._inject = inject
def _index(self, *keys):
""" Index self into the class cache if caching enabled """
# Skip indexing completely when caching off
if config.get_cache_level() < config.CACHE_OBJECTS:
return
# Index by ID
if self._id is not NitrateNone:
self.__class__._cache[self._id] = self
# Index each given key
for key in keys:
self.__class__._cache[key] = self
# We need to import cache only here because of cyclic import
from nitrate.cache import Cache