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 |
mbligh | ec5546d | 2008-06-16 16:51:28 +0000 | [diff] [blame] | 9 | from frontend.afe import models, model_logic |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 10 | |
showard | a62866b | 2008-07-28 21:27:41 +0000 | [diff] [blame] | 11 | NULL_DATETIME = datetime.datetime.max |
| 12 | NULL_DATE = datetime.date.max |
| 13 | |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 14 | def prepare_for_serialization(objects): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 15 | """ |
| 16 | Prepare Python objects to be returned via RPC. |
| 17 | """ |
| 18 | if (isinstance(objects, list) and len(objects) and |
| 19 | isinstance(objects[0], dict) and 'id' in objects[0]): |
| 20 | objects = gather_unique_dicts(objects) |
| 21 | return _prepare_data(objects) |
showard | b8d3424 | 2008-04-25 18:11:16 +0000 | [diff] [blame] | 22 | |
| 23 | |
| 24 | def _prepare_data(data): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 25 | """ |
| 26 | Recursively process data structures, performing necessary type |
| 27 | conversions to values in data to allow for RPC serialization: |
| 28 | -convert datetimes to strings |
showard | 2b9a88b | 2008-06-13 20:55:03 +0000 | [diff] [blame] | 29 | -convert tuples and sets to lists |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 30 | """ |
| 31 | if isinstance(data, dict): |
| 32 | new_data = {} |
| 33 | for key, value in data.iteritems(): |
| 34 | new_data[key] = _prepare_data(value) |
| 35 | return new_data |
showard | 2b9a88b | 2008-06-13 20:55:03 +0000 | [diff] [blame] | 36 | elif (isinstance(data, list) or isinstance(data, tuple) or |
| 37 | isinstance(data, set)): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 38 | return [_prepare_data(item) for item in data] |
showard | 9865997 | 2008-07-17 17:00:07 +0000 | [diff] [blame] | 39 | elif isinstance(data, datetime.date): |
showard | a62866b | 2008-07-28 21:27:41 +0000 | [diff] [blame] | 40 | if data is NULL_DATETIME or data is NULL_DATE: |
| 41 | return None |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 42 | return str(data) |
| 43 | else: |
| 44 | return data |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 45 | |
| 46 | |
showard | b0dfb9f | 2008-06-06 18:08:02 +0000 | [diff] [blame] | 47 | def gather_unique_dicts(dict_iterable): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 48 | """\ |
| 49 | Pick out unique objects (by ID) from an iterable of object dicts. |
| 50 | """ |
| 51 | id_set = set() |
| 52 | result = [] |
| 53 | for obj in dict_iterable: |
| 54 | if obj['id'] not in id_set: |
| 55 | id_set.add(obj['id']) |
| 56 | result.append(obj) |
| 57 | return result |
showard | b0dfb9f | 2008-06-06 18:08:02 +0000 | [diff] [blame] | 58 | |
| 59 | |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 60 | def extra_job_filters(not_yet_run=False, running=False, finished=False): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 61 | """\ |
| 62 | Generate a SQL WHERE clause for job status filtering, and return it in |
| 63 | a dict of keyword args to pass to query.extra(). No more than one of |
| 64 | the parameters should be passed as True. |
| 65 | """ |
| 66 | assert not ((not_yet_run and running) or |
| 67 | (not_yet_run and finished) or |
| 68 | (running and finished)), ('Cannot specify more than one ' |
| 69 | 'filter to this function') |
| 70 | if not_yet_run: |
| 71 | where = ['id NOT IN (SELECT job_id FROM host_queue_entries ' |
| 72 | 'WHERE active OR complete)'] |
| 73 | elif running: |
| 74 | where = ['(id IN (SELECT job_id FROM host_queue_entries ' |
| 75 | 'WHERE active OR complete)) AND ' |
| 76 | '(id IN (SELECT job_id FROM host_queue_entries ' |
| 77 | 'WHERE not complete OR active))'] |
| 78 | elif finished: |
| 79 | where = ['id NOT IN (SELECT job_id FROM host_queue_entries ' |
| 80 | 'WHERE not complete OR active)'] |
| 81 | else: |
| 82 | return None |
| 83 | return {'where': where} |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 84 | |
| 85 | |
showard | 8e3aa5e | 2008-04-08 19:42:32 +0000 | [diff] [blame] | 86 | def extra_host_filters(multiple_labels=[]): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 87 | """\ |
| 88 | Generate SQL WHERE clauses for matching hosts in an intersection of |
| 89 | labels. |
| 90 | """ |
| 91 | extra_args = {} |
| 92 | where_str = ('hosts.id in (select host_id from hosts_labels ' |
| 93 | 'where label_id=%s)') |
| 94 | extra_args['where'] = [where_str] * len(multiple_labels) |
| 95 | extra_args['params'] = [models.Label.smart_get(label).id |
| 96 | for label in multiple_labels] |
| 97 | return extra_args |
showard | 8e3aa5e | 2008-04-08 19:42:32 +0000 | [diff] [blame] | 98 | |
| 99 | |
showard | 8fd5824 | 2008-03-10 21:29:07 +0000 | [diff] [blame] | 100 | class InconsistencyException(Exception): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 101 | 'Raised when a list of objects does not have a consistent value' |
showard | 8fd5824 | 2008-03-10 21:29:07 +0000 | [diff] [blame] | 102 | |
| 103 | |
| 104 | def get_consistent_value(objects, field): |
mbligh | c5ddfd1 | 2008-08-04 17:15:00 +0000 | [diff] [blame] | 105 | if not objects: |
| 106 | # well a list of nothing is consistent |
| 107 | return None |
| 108 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 109 | value = getattr(objects[0], field) |
| 110 | for obj in objects: |
| 111 | this_value = getattr(obj, field) |
| 112 | if this_value != value: |
| 113 | raise InconsistencyException(objects[0], obj) |
| 114 | return value |
showard | 8fd5824 | 2008-03-10 21:29:07 +0000 | [diff] [blame] | 115 | |
| 116 | |
showard | 2b9a88b | 2008-06-13 20:55:03 +0000 | [diff] [blame] | 117 | def prepare_generate_control_file(tests, kernel, label, profilers): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 118 | test_objects = [models.Test.smart_get(test) for test in tests] |
showard | 2b9a88b | 2008-06-13 20:55:03 +0000 | [diff] [blame] | 119 | profiler_objects = [models.Profiler.smart_get(profiler) |
| 120 | for profiler in profilers] |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 121 | # ensure tests are all the same type |
| 122 | try: |
| 123 | test_type = get_consistent_value(test_objects, 'test_type') |
| 124 | except InconsistencyException, exc: |
| 125 | test1, test2 = exc.args |
mbligh | ec5546d | 2008-06-16 16:51:28 +0000 | [diff] [blame] | 126 | raise model_logic.ValidationError( |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 127 | {'tests' : 'You cannot run both server- and client-side ' |
| 128 | 'tests together (tests %s and %s differ' % ( |
| 129 | test1.name, test2.name)}) |
showard | 8fd5824 | 2008-03-10 21:29:07 +0000 | [diff] [blame] | 130 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 131 | try: |
| 132 | synch_type = get_consistent_value(test_objects, 'synch_type') |
| 133 | except InconsistencyException, exc: |
| 134 | test1, test2 = exc.args |
mbligh | ec5546d | 2008-06-16 16:51:28 +0000 | [diff] [blame] | 135 | raise model_logic.ValidationError( |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 136 | {'tests' : 'You cannot run both synchronous and ' |
| 137 | 'asynchronous tests together (tests %s and %s differ)' % ( |
| 138 | test1.name, test2.name)}) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 139 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 140 | is_server = (test_type == models.Test.Types.SERVER) |
| 141 | is_synchronous = (synch_type == models.Test.SynchType.SYNCHRONOUS) |
| 142 | if label: |
| 143 | label = models.Label.smart_get(label) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 144 | |
showard | 2b9a88b | 2008-06-13 20:55:03 +0000 | [diff] [blame] | 145 | return is_server, is_synchronous, test_objects, profiler_objects, label |