remotedata.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. """
  2. BitBake 'remotedata' module
  3. Provides support for using a datastore from the bitbake client
  4. """
  5. # Copyright (C) 2016 Intel Corporation
  6. #
  7. # SPDX-License-Identifier: GPL-2.0-only
  8. #
  9. # This program is free software; you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License version 2 as
  11. # published by the Free Software Foundation.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License along
  19. # with this program; if not, write to the Free Software Foundation, Inc.,
  20. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. import bb.data
  22. class RemoteDatastores:
  23. """Used on the server side to manage references to server-side datastores"""
  24. def __init__(self, cooker):
  25. self.cooker = cooker
  26. self.datastores = {}
  27. self.locked = []
  28. self.nextindex = 1
  29. def __len__(self):
  30. return len(self.datastores)
  31. def __getitem__(self, key):
  32. if key is None:
  33. return self.cooker.data
  34. else:
  35. return self.datastores[key]
  36. def items(self):
  37. return self.datastores.items()
  38. def store(self, d, locked=False):
  39. """
  40. Put a datastore into the collection. If locked=True then the datastore
  41. is understood to be managed externally and cannot be released by calling
  42. release().
  43. """
  44. idx = self.nextindex
  45. self.datastores[idx] = d
  46. if locked:
  47. self.locked.append(idx)
  48. self.nextindex += 1
  49. return idx
  50. def check_store(self, d, locked=False):
  51. """
  52. Put a datastore into the collection if it's not already in there;
  53. in either case return the index
  54. """
  55. for key, val in self.datastores.items():
  56. if val is d:
  57. idx = key
  58. break
  59. else:
  60. idx = self.store(d, locked)
  61. return idx
  62. def release(self, idx):
  63. """Discard a datastore in the collection"""
  64. if idx in self.locked:
  65. raise Exception('Tried to release locked datastore %d' % idx)
  66. del self.datastores[idx]
  67. def receive_datastore(self, remote_data):
  68. """Receive a datastore object sent from the client (as prepared by transmit_datastore())"""
  69. dct = dict(remote_data)
  70. d = bb.data_smart.DataSmart()
  71. d.dict = dct
  72. while True:
  73. if '_remote_data' in dct:
  74. dsindex = dct['_remote_data']['_content']
  75. del dct['_remote_data']
  76. if dsindex is None:
  77. dct['_data'] = self.cooker.data.dict
  78. else:
  79. dct['_data'] = self.datastores[dsindex].dict
  80. break
  81. elif '_data' in dct:
  82. idct = dict(dct['_data'])
  83. dct['_data'] = idct
  84. dct = idct
  85. else:
  86. break
  87. return d
  88. @staticmethod
  89. def transmit_datastore(d):
  90. """Prepare a datastore object for sending over IPC from the client end"""
  91. # FIXME content might be a dict, need to turn that into a list as well
  92. def copy_dicts(dct):
  93. if '_remote_data' in dct:
  94. dsindex = dct['_remote_data']['_content'].dsindex
  95. newdct = dct.copy()
  96. newdct['_remote_data'] = {'_content': dsindex}
  97. return list(newdct.items())
  98. elif '_data' in dct:
  99. newdct = dct.copy()
  100. newdata = copy_dicts(dct['_data'])
  101. if newdata:
  102. newdct['_data'] = newdata
  103. return list(newdct.items())
  104. return None
  105. main_dict = copy_dicts(d.dict)
  106. return main_dict