Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1# Copyright (C) 2018 Bloomberg LP 

2# 

3# Licensed under the Apache License, Version 2.0 (the "License"); 

4# you may not use this file except in compliance with the License. 

5# You may obtain a copy of the License at 

6# 

7# <http://www.apache.org/licenses/LICENSE-2.0> 

8# 

9# Unless required by applicable law or agreed to in writing, software 

10# distributed under the License is distributed on an "AS IS" BASIS, 

11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

12# See the License for the specific language governing permissions and 

13# limitations under the License. 

14 

15 

16import uuid 

17 

18from buildgrid._protos.google.devtools.remoteworkers.v1test2 import worker_pb2 

19 

20 

21class Device: 

22 """ Describes a device. 

23 

24 A device available to the :class:`Worker`. The first device is known as 

25 the Primary Device which is running a bot and responsible for actually 

26 executing commands. All other devices are known as Attatched Devices and 

27 must be controlled by the Primary Device. 

28 """ 

29 

30 def __init__(self, properties=None): 

31 """ Initialises a :class:`Device` instances. 

32 

33 Property keys supported: `os`, `has-docker`. 

34 

35 Args: 

36 properties (dict(string : list(string))) : Properties of device. Keys may have 

37 multiple values. 

38 """ 

39 

40 self.__properties = {} 

41 self.__property_keys = ['OSFamily', 'has-docker'] 

42 self.__name = str(uuid.uuid4()) 

43 

44 if properties: 

45 for k, l in properties.items(): 

46 for v in l: 

47 self._add_property(k, v) 

48 

49 @property 

50 def name(self): 

51 """Returns the name of the device which is in the form of a `uuid4`.""" 

52 return self.__name 

53 

54 @property 

55 def properties(self): 

56 """Returns the device properties.""" 

57 return self.__properties 

58 

59 def get_pb2(self): 

60 """Returns the protobuffer representation of the :class:`Device`.""" 

61 device = worker_pb2.Device(handle=self.__name) 

62 for k, v in self.__properties.items(): 

63 for prop in v: 

64 property_message = worker_pb2.Device.Property() 

65 property_message.key = k 

66 property_message.value = prop 

67 device.properties.extend([property_message]) 

68 return device 

69 

70 def _add_property(self, key, value): 

71 """Adds a property to the :class:`Device` instance.""" 

72 if key in self.__property_keys: 

73 prop = self.__properties.get(key) 

74 if not prop: 

75 self.__properties[key] = [value] 

76 else: 

77 prop.append(value) 

78 

79 else: 

80 raise KeyError(f'Key not supported: [{key}]')