blob: 8c3366d7269d86b758859d0087f449da248fdf0c [file] [log] [blame]
mblighe8819cd2008-02-15 16:48:40 +00001"""\
2Utility functions for rpc_interface.py. We keep them in a separate file so that
3only RPC interface functions go into that file.
4"""
5
6__author__ = 'showard@google.com (Steve Howard)'
7
8import datetime, xmlrpclib, threading
9from frontend.afe import models
10
11def prepare_for_serialization(objects):
12 """\
13 Do necessary type conversions to values in data to allow for RPC
14 serialization.
15 -convert datetimes to strings
16 """
showard1c8c2212008-04-03 20:33:58 +000017 objects = gather_unique_dicts(objects)
mblighe8819cd2008-02-15 16:48:40 +000018 new_objects = []
19 for data in objects:
20 new_data = {}
21 for key, value in data.iteritems():
22 if isinstance(value, datetime.datetime):
23 new_data[key] = str(value)
24 else:
25 new_data[key] = value
26 new_objects.append(new_data)
27 return new_objects
28
29
30def extra_job_filters(not_yet_run=False, running=False, finished=False):
31 """\
32 Generate a SQL WHERE clause for job status filtering, and return it in
33 a dict of keyword args to pass to query.extra(). No more than one of
34 the parameters should be passed as True.
35 """
36 assert not ((not_yet_run and running) or
37 (not_yet_run and finished) or
38 (running and finished)), ('Cannot specify more than one '
39 'filter to this function')
40 if not_yet_run:
41 where = ['id NOT IN (SELECT job_id FROM host_queue_entries '
42 'WHERE active OR complete)']
43 elif running:
44 where = ['(id IN (SELECT job_id FROM host_queue_entries '
45 'WHERE active OR complete)) AND '
46 '(id IN (SELECT job_id FROM host_queue_entries '
47 'WHERE not complete OR active))']
48 elif finished:
49 where = ['id NOT IN (SELECT job_id FROM host_queue_entries '
50 'WHERE not complete OR active)']
51 else:
52 return None
53 return {'where': where}
54
55
56local_vars = threading.local()
57
58def set_user(user):
59 """\
60 Sets the current request's logged-in user. user should be a
61 afe.models.User object.
62 """
63 local_vars.user = user
64
65
66def get_user():
67 'Get the currently logged-in user as a afe.models.User object.'
68 return local_vars.user
69
70
showard8fd58242008-03-10 21:29:07 +000071class InconsistencyException(Exception):
72 'Raised when a list of objects does not have a consistent value'
73
74
75def get_consistent_value(objects, field):
76 value = getattr(objects[0], field)
77 for obj in objects:
78 this_value = getattr(obj, field)
79 if this_value != value:
80 raise InconsistencyException(objects[0], obj)
81 return value
82
83
mblighe8819cd2008-02-15 16:48:40 +000084def prepare_generate_control_file(tests, kernel, label):
85 test_objects = [models.Test.smart_get(test) for test in tests]
86 # ensure tests are all the same type
showard8fd58242008-03-10 21:29:07 +000087 try:
88 test_type = get_consistent_value(test_objects, 'test_type')
89 except InconsistencyException, exc:
90 test1, test2 = exc.args
91 raise models.ValidationError(
92 {'tests' : 'You cannot run both server- and client-side '
93 'tests together (tests %s and %s differ' % (
94 test1.name, test2.name)})
95
96 try:
97 synch_type = get_consistent_value(test_objects, 'synch_type')
98 except InconsistencyException, exc:
99 test1, test2 = exc.args
100 raise models.ValidationError(
101 {'tests' : 'You cannot run both synchronous and '
102 'asynchronous tests together (tests %s and %s differ)' % (
103 test1.name, test2.name)})
mblighe8819cd2008-02-15 16:48:40 +0000104
105 is_server = (test_type == models.Test.Types.SERVER)
showard8fd58242008-03-10 21:29:07 +0000106 is_synchronous = (synch_type == models.Test.SynchType.SYNCHRONOUS)
mblighe8819cd2008-02-15 16:48:40 +0000107 if label:
108 label = models.Label.smart_get(label)
109
showard8fd58242008-03-10 21:29:07 +0000110 return is_server, is_synchronous, test_objects, label
showard1385b162008-03-13 15:59:40 +0000111
112
113def gather_unique_dicts(dict_iterable):
114 """\
115 Pick out unique objects (by ID) from an iterable of object dicts.
116 """
117 id_set = set()
118 result = []
119 for obj in dict_iterable:
120 if obj['id'] not in id_set:
121 id_set.add(obj['id'])
122 result.append(obj)
123 return result