mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 1 | """\ |
| 2 | Utility functions for rpc_interface.py. We keep them in a separate file so that |
| 3 | only RPC interface functions go into that file. |
| 4 | """ |
| 5 | |
| 6 | __author__ = 'showard@google.com (Steve Howard)' |
| 7 | |
| 8 | import datetime, xmlrpclib, threading |
| 9 | from frontend.afe import models |
| 10 | |
| 11 | def 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 | |
| 29 | def 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 | |
| 55 | local_vars = threading.local() |
| 56 | |
| 57 | def 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 | |
| 65 | def get_user(): |
| 66 | 'Get the currently logged-in user as a afe.models.User object.' |
| 67 | return local_vars.user |
| 68 | |
| 69 | |
showard | 8fd5824 | 2008-03-10 21:29:07 +0000 | [diff] [blame] | 70 | class InconsistencyException(Exception): |
| 71 | 'Raised when a list of objects does not have a consistent value' |
| 72 | |
| 73 | |
| 74 | def 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 | |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 83 | def 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 |
showard | 8fd5824 | 2008-03-10 21:29:07 +0000 | [diff] [blame] | 86 | 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)}) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 103 | |
| 104 | is_server = (test_type == models.Test.Types.SERVER) |
showard | 8fd5824 | 2008-03-10 21:29:07 +0000 | [diff] [blame] | 105 | is_synchronous = (synch_type == models.Test.SynchType.SYNCHRONOUS) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 106 | if label: |
| 107 | label = models.Label.smart_get(label) |
| 108 | |
showard | 8fd5824 | 2008-03-10 21:29:07 +0000 | [diff] [blame] | 109 | return is_server, is_synchronous, test_objects, label |