| OLD | NEW |
| 1 # Copyright 2015 The LUCI Authors. All rights reserved. | 1 # Copyright 2015 The LUCI Authors. All rights reserved. |
| 2 # Use of this source code is governed by the Apache v2.0 license that can be | 2 # Use of this source code is governed by the Apache v2.0 license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 """This module facilitates conversion from dictionaries to ProtoRPC messages. | 5 """This module facilitates conversion from dictionaries to ProtoRPC messages. |
| 6 | 6 |
| 7 Given a dictionary whose keys' names and values' types comport with the | 7 Given a dictionary whose keys' names and values' types comport with the |
| 8 fields defined for a protorpc.messages.Message subclass, this module tries to | 8 fields defined for a protorpc.messages.Message subclass, this module tries to |
| 9 generate a Message instance that corresponds to the provided dictionary. The | 9 generate a Message instance that corresponds to the provided dictionary. The |
| 10 "normal" use case is for ndb.Models which need to be represented as a | 10 "normal" use case is for ndb.Models which need to be represented as a |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 swarming_rpcs.BotEvent, | 95 swarming_rpcs.BotEvent, |
| 96 entity, | 96 entity, |
| 97 dimensions=_string_list_pairs_from_dict(entity.dimensions), | 97 dimensions=_string_list_pairs_from_dict(entity.dimensions), |
| 98 state=json.dumps(entity.state, sort_keys=True, separators=(',', ':')), | 98 state=json.dumps(entity.state, sort_keys=True, separators=(',', ':')), |
| 99 task_id=entity.task_id if entity.task_id else None) | 99 task_id=entity.task_id if entity.task_id else None) |
| 100 | 100 |
| 101 | 101 |
| 102 def task_request_to_rpc(entity): | 102 def task_request_to_rpc(entity): |
| 103 """"Returns a swarming_rpcs.TaskRequest from a task_request.TaskRequest.""" | 103 """"Returns a swarming_rpcs.TaskRequest from a task_request.TaskRequest.""" |
| 104 assert entity.__class__ is task_request.TaskRequest | 104 assert entity.__class__ is task_request.TaskRequest |
| 105 cipd_input = None |
| 106 if entity.properties.cipd_input: |
| 107 client_package = None |
| 108 if entity.properties.cipd_input.client_package: |
| 109 client_package = _ndb_to_rpc( |
| 110 swarming_rpcs.CipdPackage, |
| 111 entity.properties.cipd_input.client_package) |
| 112 cipd_input = _ndb_to_rpc( |
| 113 swarming_rpcs.CipdInput, |
| 114 entity.properties.cipd_input, |
| 115 client_package=client_package, |
| 116 packages=[ |
| 117 _ndb_to_rpc(swarming_rpcs.CipdPackage, p) |
| 118 for p in entity.properties.cipd_input.packages |
| 119 ]) |
| 120 |
| 105 inputs_ref = None | 121 inputs_ref = None |
| 106 if entity.properties.inputs_ref: | 122 if entity.properties.inputs_ref: |
| 107 inputs_ref = _ndb_to_rpc( | 123 inputs_ref = _ndb_to_rpc( |
| 108 swarming_rpcs.FilesRef, entity.properties.inputs_ref) | 124 swarming_rpcs.FilesRef, entity.properties.inputs_ref) |
| 125 |
| 109 props = entity.properties | 126 props = entity.properties |
| 110 cmd = None | 127 cmd = None |
| 111 if props.commands: | 128 if props.commands: |
| 112 cmd = props.commands[0] | 129 cmd = props.commands[0] |
| 113 elif props.command: | 130 elif props.command: |
| 114 cmd = props.command | 131 cmd = props.command |
| 115 properties = _ndb_to_rpc( | 132 properties = _ndb_to_rpc( |
| 116 swarming_rpcs.TaskProperties, | 133 swarming_rpcs.TaskProperties, |
| 117 props, | 134 props, |
| 135 cipd_input=cipd_input, |
| 118 command=cmd, | 136 command=cmd, |
| 119 dimensions=_string_pairs_from_dict(props.dimensions), | 137 dimensions=_string_pairs_from_dict(props.dimensions), |
| 120 env=_string_pairs_from_dict(props.env), | 138 env=_string_pairs_from_dict(props.env), |
| 121 inputs_ref=inputs_ref, | 139 inputs_ref=inputs_ref) |
| 122 packages=[ | |
| 123 _ndb_to_rpc(swarming_rpcs.CipdPackage, p) for p in props.packages | |
| 124 ]) | |
| 125 | 140 |
| 126 return _ndb_to_rpc( | 141 return _ndb_to_rpc( |
| 127 swarming_rpcs.TaskRequest, | 142 swarming_rpcs.TaskRequest, |
| 128 entity, | 143 entity, |
| 129 authenticated=entity.authenticated.to_bytes(), | 144 authenticated=entity.authenticated.to_bytes(), |
| 130 properties=properties) | 145 properties=properties) |
| 131 | 146 |
| 132 | 147 |
| 133 def new_task_request_from_rpc(msg, now): | 148 def new_task_request_from_rpc(msg, now): |
| 134 """"Returns a task_request.TaskRequest from a swarming_rpcs.NewTaskRequest.""" | 149 """"Returns a task_request.TaskRequest from a swarming_rpcs.NewTaskRequest.""" |
| 135 assert msg.__class__ is swarming_rpcs.NewTaskRequest | 150 assert msg.__class__ is swarming_rpcs.NewTaskRequest |
| 136 props = msg.properties | 151 props = msg.properties |
| 137 if not props: | 152 if not props: |
| 138 raise ValueError('properties is required') | 153 raise ValueError('properties is required') |
| 154 |
| 155 cipd_input = None |
| 156 if props.cipd_input: |
| 157 client_package = None |
| 158 if props.cipd_input.client_package: |
| 159 client_package = _rpc_to_ndb( |
| 160 task_request.CipdPackage, props.cipd_input.client_package) |
| 161 cipd_input = _rpc_to_ndb( |
| 162 task_request.CipdInput, |
| 163 props.cipd_input, |
| 164 client_package=client_package, |
| 165 packages=[ |
| 166 _rpc_to_ndb(task_request.CipdPackage, p) |
| 167 for p in props.cipd_input.packages |
| 168 ]) |
| 169 |
| 139 inputs_ref = None | 170 inputs_ref = None |
| 140 if props.inputs_ref: | 171 if props.inputs_ref: |
| 141 inputs_ref = _rpc_to_ndb(task_request.FilesRef, props.inputs_ref) | 172 inputs_ref = _rpc_to_ndb(task_request.FilesRef, props.inputs_ref) |
| 173 |
| 142 properties = _rpc_to_ndb( | 174 properties = _rpc_to_ndb( |
| 143 task_request.TaskProperties, | 175 task_request.TaskProperties, |
| 144 props, | 176 props, |
| 177 cipd_input=cipd_input, |
| 145 # Passing command=None is supported at API level but not at NDB level. | 178 # Passing command=None is supported at API level but not at NDB level. |
| 146 command=props.command or [], | 179 command=props.command or [], |
| 147 # Legacy, ignored. | 180 # Legacy, ignored. |
| 148 commands=None, | 181 commands=None, |
| 149 dimensions={i.key: i.value for i in props.dimensions}, | 182 dimensions={i.key: i.value for i in props.dimensions}, |
| 150 env={i.key: i.value for i in props.env}, | 183 env={i.key: i.value for i in props.env}, |
| 151 inputs_ref=inputs_ref, | 184 inputs_ref=inputs_ref) |
| 152 packages=[ | |
| 153 _rpc_to_ndb(task_request.CipdPackage, p) for p in props.packages | |
| 154 ]) | |
| 155 | 185 |
| 156 return _rpc_to_ndb( | 186 return _rpc_to_ndb( |
| 157 task_request.TaskRequest, | 187 task_request.TaskRequest, |
| 158 msg, | 188 msg, |
| 159 created_ts=now, | 189 created_ts=now, |
| 160 expiration_ts=now+datetime.timedelta(seconds=msg.expiration_secs), | 190 expiration_ts=now+datetime.timedelta(seconds=msg.expiration_secs), |
| 161 # It is set in task_request.make_request(). | 191 # It is set in task_request.make_request(). |
| 162 authenticated=None, | 192 authenticated=None, |
| 163 properties=properties) | 193 properties=properties) |
| 164 | 194 |
| (...skipping 28 matching lines...) Expand all Loading... |
| 193 kwargs['costs_usd'].append(entity.cost_usd) | 223 kwargs['costs_usd'].append(entity.cost_usd) |
| 194 kwargs['properties_hash'] = None | 224 kwargs['properties_hash'] = None |
| 195 kwargs['tags'] = [] | 225 kwargs['tags'] = [] |
| 196 kwargs['user'] = None | 226 kwargs['user'] = None |
| 197 else: | 227 else: |
| 198 assert entity.__class__ is task_result.TaskResultSummary, entity | 228 assert entity.__class__ is task_result.TaskResultSummary, entity |
| 199 kwargs['properties_hash'] = ( | 229 kwargs['properties_hash'] = ( |
| 200 entity.properties_hash.encode('hex') | 230 entity.properties_hash.encode('hex') |
| 201 if entity.properties_hash else None) | 231 if entity.properties_hash else None) |
| 202 return _ndb_to_rpc(swarming_rpcs.TaskResult, entity, **kwargs) | 232 return _ndb_to_rpc(swarming_rpcs.TaskResult, entity, **kwargs) |
| OLD | NEW |