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 | """ |
showard | 1c8c221 | 2008-04-03 20:33:58 +0000 | [diff] [blame] | 17 | objects = gather_unique_dicts(objects) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 18 | 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 | |
| 30 | def 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 | |
| 56 | local_vars = threading.local() |
| 57 | |
| 58 | def 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 | |
| 66 | def get_user(): |
| 67 | 'Get the currently logged-in user as a afe.models.User object.' |
| 68 | return local_vars.user |
| 69 | |
| 70 | |
showard | 8fd5824 | 2008-03-10 21:29:07 +0000 | [diff] [blame] | 71 | class InconsistencyException(Exception): |
| 72 | 'Raised when a list of objects does not have a consistent value' |
| 73 | |
| 74 | |
| 75 | def 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 | |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 84 | def 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 |
showard | 8fd5824 | 2008-03-10 21:29:07 +0000 | [diff] [blame] | 87 | 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)}) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 104 | |
| 105 | is_server = (test_type == models.Test.Types.SERVER) |
showard | 8fd5824 | 2008-03-10 21:29:07 +0000 | [diff] [blame] | 106 | is_synchronous = (synch_type == models.Test.SynchType.SYNCHRONOUS) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 107 | if label: |
| 108 | label = models.Label.smart_get(label) |
| 109 | |
showard | 8fd5824 | 2008-03-10 21:29:07 +0000 | [diff] [blame] | 110 | return is_server, is_synchronous, test_objects, label |
showard | 1385b16 | 2008-03-13 15:59:40 +0000 | [diff] [blame] | 111 | |
| 112 | |
| 113 | def 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 |