| 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 |
| 105 inputs_ref = None | 106 inputs_ref = None |
| 106 if entity.properties.inputs_ref: | 107 if entity.properties.inputs_ref: |
| 107 inputs_ref = _ndb_to_rpc( | 108 inputs_ref = _ndb_to_rpc( |
| 108 swarming_rpcs.FilesRef, entity.properties.inputs_ref) | 109 swarming_rpcs.FilesRef, entity.properties.inputs_ref) |
| 110 |
| 111 outputs_target = None |
| 112 if entity.properties.outputs_target: |
| 113 outputs_target = _ndb_to_rpc( |
| 114 swarming_rpcs.IsolatedOutputsTarget, entity.properties.outputs_target) |
| 115 |
| 109 props = entity.properties | 116 props = entity.properties |
| 110 cmd = None | 117 cmd = None |
| 111 if props.commands: | 118 if props.commands: |
| 112 cmd = props.commands[0] | 119 cmd = props.commands[0] |
| 113 elif props.command: | 120 elif props.command: |
| 114 cmd = props.command | 121 cmd = props.command |
| 115 properties = _ndb_to_rpc( | 122 properties = _ndb_to_rpc( |
| 116 swarming_rpcs.TaskProperties, | 123 swarming_rpcs.TaskProperties, |
| 117 props, | 124 props, |
| 118 command=cmd, | 125 command=cmd, |
| 119 dimensions=_string_pairs_from_dict(props.dimensions), | 126 dimensions=_string_pairs_from_dict(props.dimensions), |
| 120 env=_string_pairs_from_dict(props.env), | 127 env=_string_pairs_from_dict(props.env), |
| 121 inputs_ref=inputs_ref, | 128 inputs_ref=inputs_ref, |
| 129 outputs_target=outputs_target, |
| 122 packages=[ | 130 packages=[ |
| 123 _ndb_to_rpc(swarming_rpcs.CipdPackage, p) for p in props.packages | 131 _ndb_to_rpc(swarming_rpcs.CipdPackage, p) for p in props.packages |
| 124 ]) | 132 ]) |
| 125 | 133 |
| 126 return _ndb_to_rpc( | 134 return _ndb_to_rpc( |
| 127 swarming_rpcs.TaskRequest, | 135 swarming_rpcs.TaskRequest, |
| 128 entity, | 136 entity, |
| 129 authenticated=entity.authenticated.to_bytes(), | 137 authenticated=entity.authenticated.to_bytes(), |
| 130 properties=properties) | 138 properties=properties) |
| 131 | 139 |
| 132 | 140 |
| 133 def new_task_request_from_rpc(msg, now): | 141 def new_task_request_from_rpc(msg, now): |
| 134 """"Returns a task_request.TaskRequest from a swarming_rpcs.NewTaskRequest.""" | 142 """"Returns a task_request.TaskRequest from a swarming_rpcs.NewTaskRequest.""" |
| 135 assert msg.__class__ is swarming_rpcs.NewTaskRequest | 143 assert msg.__class__ is swarming_rpcs.NewTaskRequest |
| 136 props = msg.properties | 144 props = msg.properties |
| 137 if not props: | 145 if not props: |
| 138 raise ValueError('properties is required') | 146 raise ValueError('properties is required') |
| 147 |
| 139 inputs_ref = None | 148 inputs_ref = None |
| 140 if props.inputs_ref: | 149 if props.inputs_ref: |
| 141 inputs_ref = _rpc_to_ndb(task_request.FilesRef, props.inputs_ref) | 150 inputs_ref = _rpc_to_ndb(task_request.FilesRef, props.inputs_ref) |
| 151 |
| 152 outputs_target = None |
| 153 if props.outputs_target: |
| 154 outputs_target = _rpc_to_ndb( |
| 155 task_request.IsolatedOutputsTarget, props.outputs_target) |
| 156 |
| 142 properties = _rpc_to_ndb( | 157 properties = _rpc_to_ndb( |
| 143 task_request.TaskProperties, | 158 task_request.TaskProperties, |
| 144 props, | 159 props, |
| 145 # Passing command=None is supported at API level but not at NDB level. | 160 # Passing command=None is supported at API level but not at NDB level. |
| 146 command=props.command or [], | 161 command=props.command or [], |
| 147 # Legacy, ignored. | 162 # Legacy, ignored. |
| 148 commands=None, | 163 commands=None, |
| 149 dimensions={i.key: i.value for i in props.dimensions}, | 164 dimensions={i.key: i.value for i in props.dimensions}, |
| 150 env={i.key: i.value for i in props.env}, | 165 env={i.key: i.value for i in props.env}, |
| 151 inputs_ref=inputs_ref, | 166 inputs_ref=inputs_ref, |
| 167 outputs_target=outputs_target, |
| 152 packages=[ | 168 packages=[ |
| 153 _rpc_to_ndb(task_request.CipdPackage, p) for p in props.packages | 169 _rpc_to_ndb(task_request.CipdPackage, p) for p in props.packages |
| 154 ]) | 170 ]) |
| 155 | 171 |
| 156 return _rpc_to_ndb( | 172 return _rpc_to_ndb( |
| 157 task_request.TaskRequest, | 173 task_request.TaskRequest, |
| 158 msg, | 174 msg, |
| 159 created_ts=now, | 175 created_ts=now, |
| 160 expiration_ts=now+datetime.timedelta(seconds=msg.expiration_secs), | 176 expiration_ts=now+datetime.timedelta(seconds=msg.expiration_secs), |
| 161 # It is set in task_request.make_request(). | 177 # It is set in task_request.make_request(). |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 193 kwargs['costs_usd'].append(entity.cost_usd) | 209 kwargs['costs_usd'].append(entity.cost_usd) |
| 194 kwargs['properties_hash'] = None | 210 kwargs['properties_hash'] = None |
| 195 kwargs['tags'] = [] | 211 kwargs['tags'] = [] |
| 196 kwargs['user'] = None | 212 kwargs['user'] = None |
| 197 else: | 213 else: |
| 198 assert entity.__class__ is task_result.TaskResultSummary, entity | 214 assert entity.__class__ is task_result.TaskResultSummary, entity |
| 199 kwargs['properties_hash'] = ( | 215 kwargs['properties_hash'] = ( |
| 200 entity.properties_hash.encode('hex') | 216 entity.properties_hash.encode('hex') |
| 201 if entity.properties_hash else None) | 217 if entity.properties_hash else None) |
| 202 return _ndb_to_rpc(swarming_rpcs.TaskResult, entity, **kwargs) | 218 return _ndb_to_rpc(swarming_rpcs.TaskResult, entity, **kwargs) |
| OLD | NEW |