blob: 6c9864dd6b327510065ccaf0892003b606a270ae [file] [log] [blame]
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001"""
2BitBake 'remotedata' module
3
4Provides support for using a datastore from the bitbake client
5"""
6
7# Copyright (C) 2016 Intel Corporation
8#
Brad Bishopc342db32019-05-15 21:57:59 -04009# SPDX-License-Identifier: GPL-2.0-only
Brad Bishop6e60e8b2018-02-01 10:27:11 -050010#
Brad Bishop6e60e8b2018-02-01 10:27:11 -050011
12import bb.data
13
14class RemoteDatastores:
15 """Used on the server side to manage references to server-side datastores"""
16 def __init__(self, cooker):
17 self.cooker = cooker
18 self.datastores = {}
19 self.locked = []
Andrew Geissler82c905d2020-04-13 13:39:40 -050020 self.datastores[0] = self.cooker.data
Brad Bishop6e60e8b2018-02-01 10:27:11 -050021 self.nextindex = 1
22
23 def __len__(self):
24 return len(self.datastores)
25
26 def __getitem__(self, key):
Andrew Geissler82c905d2020-04-13 13:39:40 -050027 # Cooker could have changed its datastore from under us
28 self.datastores[0] = self.cooker.data
29 return self.datastores[key]
Brad Bishop6e60e8b2018-02-01 10:27:11 -050030
31 def items(self):
32 return self.datastores.items()
33
34 def store(self, d, locked=False):
35 """
36 Put a datastore into the collection. If locked=True then the datastore
37 is understood to be managed externally and cannot be released by calling
38 release().
39 """
40 idx = self.nextindex
41 self.datastores[idx] = d
42 if locked:
43 self.locked.append(idx)
44 self.nextindex += 1
45 return idx
46
47 def check_store(self, d, locked=False):
48 """
49 Put a datastore into the collection if it's not already in there;
50 in either case return the index
51 """
52 for key, val in self.datastores.items():
53 if val is d:
54 idx = key
55 break
56 else:
57 idx = self.store(d, locked)
58 return idx
59
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