forked from luci/luci-py
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessage_conversion.py
More file actions
202 lines (169 loc) · 6.18 KB
/
message_conversion.py
File metadata and controls
202 lines (169 loc) · 6.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# Copyright 2015 The LUCI Authors. All rights reserved.
# Use of this source code is governed by the Apache v2.0 license that can be
# found in the LICENSE file.
"""This module facilitates conversion from dictionaries to ProtoRPC messages.
Given a dictionary whose keys' names and values' types comport with the
fields defined for a protorpc.messages.Message subclass, this module tries to
generate a Message instance that corresponds to the provided dictionary. The
"normal" use case is for ndb.Models which need to be represented as a
ProtoRPC.
"""
import datetime
import functools
import json
import logging
import re
import endpoints
from protorpc import message_types
from protorpc import messages
from protorpc import remote
import swarming_rpcs
from components import utils
from server import task_request
from server import task_result
### Private API.
def _string_pairs_from_dict(dictionary):
return [
swarming_rpcs.StringPair(key=k, value=v)
for k, v in sorted(dictionary.iteritems())
]
def _string_list_pairs_from_dict(dictionary):
return [
swarming_rpcs.StringListPair(key=k, value=v)
for k, v in sorted((dictionary or {}).iteritems())
]
def _ndb_to_rpc(cls, entity, **overrides):
members = (f.name for f in cls.all_fields())
kwargs = {m: getattr(entity, m) for m in members if not m in overrides}
kwargs.update(overrides)
return cls(**{k: v for k, v in kwargs.iteritems() if v is not None})
def _rpc_to_ndb(cls, entity, **overrides):
kwargs = {
m: getattr(entity, m) for m in cls._properties if not m in overrides
}
kwargs.update(overrides)
return cls(**{k: v for k, v in kwargs.iteritems() if v is not None})
### Public API.
def epoch_to_datetime(value):
"""Converts a messages.FloatField that represents a timestamp since epoch in
seconds to a datetime.datetime.
Returns None when input is 0 or None.
"""
if not value:
return None
try:
return utils.timestamp_to_datetime(value*1000000.)
except OverflowError as e:
raise ValueError(e)
def bot_info_to_rpc(entity, now):
""""Returns a swarming_rpcs.BotInfo from a bot.BotInfo."""
return _ndb_to_rpc(
swarming_rpcs.BotInfo,
entity,
dimensions=_string_list_pairs_from_dict(entity.dimensions),
is_dead=entity.is_dead(now),
bot_id=entity.id,
state=json.dumps(entity.state, sort_keys=True, separators=(',', ':')))
def bot_event_to_rpc(entity):
""""Returns a swarming_rpcs.BotEvent from a bot.BotEvent."""
return _ndb_to_rpc(
swarming_rpcs.BotEvent,
entity,
dimensions=_string_list_pairs_from_dict(entity.dimensions),
state=json.dumps(entity.state, sort_keys=True, separators=(',', ':')),
task_id=entity.task_id if entity.task_id else None)
def task_request_to_rpc(entity):
""""Returns a swarming_rpcs.TaskRequest from a task_request.TaskRequest."""
assert entity.__class__ is task_request.TaskRequest
inputs_ref = None
if entity.properties.inputs_ref:
inputs_ref = _ndb_to_rpc(
swarming_rpcs.FilesRef, entity.properties.inputs_ref)
props = entity.properties
cmd = None
if props.commands:
cmd = props.commands[0]
elif props.command:
cmd = props.command
properties = _ndb_to_rpc(
swarming_rpcs.TaskProperties,
props,
command=cmd,
dimensions=_string_pairs_from_dict(props.dimensions),
env=_string_pairs_from_dict(props.env),
inputs_ref=inputs_ref,
packages=[
_ndb_to_rpc(swarming_rpcs.CipdPackage, p) for p in props.packages
])
return _ndb_to_rpc(
swarming_rpcs.TaskRequest,
entity,
authenticated=entity.authenticated.to_bytes(),
properties=properties)
def new_task_request_from_rpc(msg, now):
""""Returns a task_request.TaskRequest from a swarming_rpcs.NewTaskRequest."""
assert msg.__class__ is swarming_rpcs.NewTaskRequest
props = msg.properties
if not props:
raise ValueError('properties is required')
inputs_ref = None
if props.inputs_ref:
inputs_ref = _rpc_to_ndb(task_request.FilesRef, props.inputs_ref)
properties = _rpc_to_ndb(
task_request.TaskProperties,
props,
# Passing command=None is supported at API level but not at NDB level.
command=props.command or [],
# Legacy, ignored.
commands=None,
dimensions={i.key: i.value for i in props.dimensions},
env={i.key: i.value for i in props.env},
inputs_ref=inputs_ref,
packages=[
_rpc_to_ndb(task_request.CipdPackage, p) for p in props.packages
])
return _rpc_to_ndb(
task_request.TaskRequest,
msg,
created_ts=now,
expiration_ts=now+datetime.timedelta(seconds=msg.expiration_secs),
# It is set in task_request.make_request().
authenticated=None,
properties=properties)
def task_result_to_rpc(entity, send_stats):
""""Returns a swarming_rpcs.TaskResult from a task_result.TaskResultSummary or
task_result.TaskRunResult.
"""
outputs_ref = (
_ndb_to_rpc(swarming_rpcs.FilesRef, entity.outputs_ref)
if entity.outputs_ref else None)
performance_stats = None
if send_stats and entity.performance_stats.is_valid:
performance_stats = _ndb_to_rpc(
swarming_rpcs.PerformanceStats,
entity.performance_stats,
isolated_download=_ndb_to_rpc(
swarming_rpcs.IsolatedOperation,
entity.performance_stats.isolated_download),
isolated_upload=_ndb_to_rpc(
swarming_rpcs.IsolatedOperation,
entity.performance_stats.isolated_upload))
kwargs = {
'bot_dimensions': _string_list_pairs_from_dict(entity.bot_dimensions or {}),
'performance_stats': performance_stats,
'outputs_ref': outputs_ref,
'state': swarming_rpcs.StateField(entity.state),
}
if entity.__class__ is task_result.TaskRunResult:
kwargs['costs_usd'] = []
if entity.cost_usd is not None:
kwargs['costs_usd'].append(entity.cost_usd)
kwargs['properties_hash'] = None
kwargs['tags'] = []
kwargs['user'] = None
else:
assert entity.__class__ is task_result.TaskResultSummary, entity
kwargs['properties_hash'] = (
entity.properties_hash.encode('hex')
if entity.properties_hash else None)
return _ndb_to_rpc(swarming_rpcs.TaskResult, entity, **kwargs)