Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(8)

Side by Side Diff: appengine/swarming/message_conversion.py

Issue 1946253003: swarming: refactor cipd input (Closed) Base URL: https://chromium.googlesource.com/external/github.com/luci/luci-py@default-isolate-server
Patch Set: fix import google.protobuf Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 cipd_input = _ndb_to_rpc(
108 swarming_rpcs.CipdInput,
109 entity.properties.cipd_input,
110 packages=[
111 _ndb_to_rpc(swarming_rpcs.CipdPackage, p)
112 for p in entity.properties.cipd_input.packages
113 ])
114
105 inputs_ref = None 115 inputs_ref = None
106 if entity.properties.inputs_ref: 116 if entity.properties.inputs_ref:
107 inputs_ref = _ndb_to_rpc( 117 inputs_ref = _ndb_to_rpc(
108 swarming_rpcs.FilesRef, entity.properties.inputs_ref) 118 swarming_rpcs.FilesRef, entity.properties.inputs_ref)
119
109 props = entity.properties 120 props = entity.properties
110 cmd = None 121 cmd = None
111 if props.commands: 122 if props.commands:
112 cmd = props.commands[0] 123 cmd = props.commands[0]
113 elif props.command: 124 elif props.command:
114 cmd = props.command 125 cmd = props.command
115 properties = _ndb_to_rpc( 126 properties = _ndb_to_rpc(
116 swarming_rpcs.TaskProperties, 127 swarming_rpcs.TaskProperties,
117 props, 128 props,
129 cipd_input=cipd_input,
118 command=cmd, 130 command=cmd,
119 dimensions=_string_pairs_from_dict(props.dimensions), 131 dimensions=_string_pairs_from_dict(props.dimensions),
120 env=_string_pairs_from_dict(props.env), 132 env=_string_pairs_from_dict(props.env),
121 inputs_ref=inputs_ref, 133 inputs_ref=inputs_ref)
122 packages=[
123 _ndb_to_rpc(swarming_rpcs.CipdPackage, p) for p in props.packages
124 ])
125 134
126 return _ndb_to_rpc( 135 return _ndb_to_rpc(
127 swarming_rpcs.TaskRequest, 136 swarming_rpcs.TaskRequest,
128 entity, 137 entity,
129 authenticated=entity.authenticated.to_bytes(), 138 authenticated=entity.authenticated.to_bytes(),
130 properties=properties) 139 properties=properties)
131 140
132 141
133 def new_task_request_from_rpc(msg, now): 142 def new_task_request_from_rpc(msg, now):
134 """"Returns a task_request.TaskRequest from a swarming_rpcs.NewTaskRequest.""" 143 """"Returns a task_request.TaskRequest from a swarming_rpcs.NewTaskRequest."""
135 assert msg.__class__ is swarming_rpcs.NewTaskRequest 144 assert msg.__class__ is swarming_rpcs.NewTaskRequest
136 props = msg.properties 145 props = msg.properties
137 if not props: 146 if not props:
138 raise ValueError('properties is required') 147 raise ValueError('properties is required')
148
149 cipd_input = None
150 if props.cipd_input:
151 cipd_input = _rpc_to_ndb(
152 task_request.CipdInput,
153 props.cipd_input,
154 packages=[
155 _rpc_to_ndb(task_request.CipdPackage, p)
156 for p in props.cipd_input.packages
157 ])
158
139 inputs_ref = None 159 inputs_ref = None
140 if props.inputs_ref: 160 if props.inputs_ref:
141 inputs_ref = _rpc_to_ndb(task_request.FilesRef, props.inputs_ref) 161 inputs_ref = _rpc_to_ndb(task_request.FilesRef, props.inputs_ref)
162
142 properties = _rpc_to_ndb( 163 properties = _rpc_to_ndb(
143 task_request.TaskProperties, 164 task_request.TaskProperties,
144 props, 165 props,
166 cipd_input=cipd_input,
145 # Passing command=None is supported at API level but not at NDB level. 167 # Passing command=None is supported at API level but not at NDB level.
146 command=props.command or [], 168 command=props.command or [],
147 # Legacy, ignored. 169 # Legacy, ignored.
148 commands=None, 170 commands=None,
149 dimensions={i.key: i.value for i in props.dimensions}, 171 dimensions={i.key: i.value for i in props.dimensions},
150 env={i.key: i.value for i in props.env}, 172 env={i.key: i.value for i in props.env},
151 inputs_ref=inputs_ref, 173 inputs_ref=inputs_ref)
152 packages=[
153 _rpc_to_ndb(task_request.CipdPackage, p) for p in props.packages
154 ])
155 174
156 return _rpc_to_ndb( 175 return _rpc_to_ndb(
157 task_request.TaskRequest, 176 task_request.TaskRequest,
158 msg, 177 msg,
159 created_ts=now, 178 created_ts=now,
160 expiration_ts=now+datetime.timedelta(seconds=msg.expiration_secs), 179 expiration_ts=now+datetime.timedelta(seconds=msg.expiration_secs),
161 # It is set in task_request.make_request(). 180 # It is set in task_request.make_request().
162 authenticated=None, 181 authenticated=None,
163 properties=properties) 182 properties=properties)
164 183
(...skipping 28 matching lines...) Expand all
193 kwargs['costs_usd'].append(entity.cost_usd) 212 kwargs['costs_usd'].append(entity.cost_usd)
194 kwargs['properties_hash'] = None 213 kwargs['properties_hash'] = None
195 kwargs['tags'] = [] 214 kwargs['tags'] = []
196 kwargs['user'] = None 215 kwargs['user'] = None
197 else: 216 else:
198 assert entity.__class__ is task_result.TaskResultSummary, entity 217 assert entity.__class__ is task_result.TaskResultSummary, entity
199 kwargs['properties_hash'] = ( 218 kwargs['properties_hash'] = (
200 entity.properties_hash.encode('hex') 219 entity.properties_hash.encode('hex')
201 if entity.properties_hash else None) 220 if entity.properties_hash else None)
202 return _ndb_to_rpc(swarming_rpcs.TaskResult, entity, **kwargs) 221 return _ndb_to_rpc(swarming_rpcs.TaskResult, entity, **kwargs)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698