blob: 27c2bc8f71cd4e8f46c59fcdb33db57d84aeaf44 [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 """
17 new_objects = []
18 for data in objects:
19 new_data = {}
20 for key, value in data.iteritems():
21 if isinstance(value, datetime.datetime):
22 new_data[key] = str(value)
23 else:
24 new_data[key] = value
25 new_objects.append(new_data)
26 return new_objects
27
28
29def extra_job_filters(not_yet_run=False, running=False, finished=False):
30 """\
31 Generate a SQL WHERE clause for job status filtering, and return it in
32 a dict of keyword args to pass to query.extra(). No more than one of
33 the parameters should be passed as True.
34 """
35 assert not ((not_yet_run and running) or
36 (not_yet_run and finished) or
37 (running and finished)), ('Cannot specify more than one '
38 'filter to this function')
39 if not_yet_run:
40 where = ['id NOT IN (SELECT job_id FROM host_queue_entries '
41 'WHERE active OR complete)']
42 elif running:
43 where = ['(id IN (SELECT job_id FROM host_queue_entries '
44 'WHERE active OR complete)) AND '
45 '(id IN (SELECT job_id FROM host_queue_entries '
46 'WHERE not complete OR active))']
47 elif finished:
48 where = ['id NOT IN (SELECT job_id FROM host_queue_entries '
49 'WHERE not complete OR active)']
50 else:
51 return None
52 return {'where': where}
53
54
55local_vars = threading.local()
56
57def set_user(user):
58 """\
59 Sets the current request's logged-in user. user should be a
60 afe.models.User object.
61 """
62 local_vars.user = user
63
64
65def get_user():
66 'Get the currently logged-in user as a afe.models.User object.'
67 return local_vars.user
68
69
showard8fd58242008-03-10 21:29:07 +000070class InconsistencyException(Exception):
71 'Raised when a list of objects does not have a consistent value'
72
73
74def get_consistent_value(objects, field):
75 value = getattr(objects[0], field)
76 for obj in objects:
77 this_value = getattr(obj, field)
78 if this_value != value:
79 raise InconsistencyException(objects[0], obj)
80 return value
81
82
mblighe8819cd2008-02-15 16:48:40 +000083def prepare_generate_control_file(tests, kernel, label):
84 test_objects = [models.Test.smart_get(test) for test in tests]
85 # ensure tests are all the same type
showard8fd58242008-03-10 21:29:07 +000086 try:
87 test_type = get_consistent_value(test_objects, 'test_type')
88 except InconsistencyException, exc:
89 test1, test2 = exc.args
90 raise models.ValidationError(
91 {'tests' : 'You cannot run both server- and client-side '
92 'tests together (tests %s and %s differ' % (
93 test1.name, test2.name)})
94
95 try:
96 synch_type = get_consistent_value(test_objects, 'synch_type')
97 except InconsistencyException, exc:
98 test1, test2 = exc.args
99 raise models.ValidationError(
100 {'tests' : 'You cannot run both synchronous and '
101 'asynchronous tests together (tests %s and %s differ)' % (
102 test1.name, test2.name)})
mblighe8819cd2008-02-15 16:48:40 +0000103
104 is_server = (test_type == models.Test.Types.SERVER)
showard8fd58242008-03-10 21:29:07 +0000105 is_synchronous = (synch_type == models.Test.SynchType.SYNCHRONOUS)
mblighe8819cd2008-02-15 16:48:40 +0000106 if label:
107 label = models.Label.smart_get(label)
108
showard8fd58242008-03-10 21:29:07 +0000109 return is_server, is_synchronous, test_objects, label
showard1385b162008-03-13 15:59:40 +0000110
111
112def gather_unique_dicts(dict_iterable):
113 """\
114 Pick out unique objects (by ID) from an iterable of object dicts.
115 """
116 id_set = set()
117 result = []
118 for obj in dict_iterable:
119 if obj['id'] not in id_set:
120 id_set.add(obj['id'])
121 result.append(obj)
122 return result