remotedata.py 3.8 KB

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