blob: 0bd1acc293fb959a7d987ce6efca3e9fd3900aeb [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
70def prepare_generate_control_file(tests, kernel, label):
71 test_objects = [models.Test.smart_get(test) for test in tests]
72 # ensure tests are all the same type
73 test_type = test_objects[0].test_type
74 for test in test_objects[1:]:
75 if test.test_type != test_type:
76 raise models.ValidationError(
77 {'tests' : 'You cannot run both server- and '
78 'client-side tests together (tests %s '
79 'and %s differ' % (test_objects[0].name,
80 test.name)})
81
82 is_server = (test_type == models.Test.Types.SERVER)
83 if is_server:
84 if kernel or label:
85 error = 'This field is not supported for server jobs'
86 error_dict = {}
87 if kernel:
88 error_dict['kernel'] = error
89 if label:
90 error_dict['label'] = error
91 raise models.ValidationError(error_dict)
92
93 if label:
94 label = models.Label.smart_get(label)
95
96 return is_server, test_objects, label