xref: /OK3568_Linux_fs/yocto/poky/bitbake/lib/bb/remotedata.py (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun"""
2*4882a593SmuzhiyunBitBake 'remotedata' module
3*4882a593Smuzhiyun
4*4882a593SmuzhiyunProvides support for using a datastore from the bitbake client
5*4882a593Smuzhiyun"""
6*4882a593Smuzhiyun
7*4882a593Smuzhiyun# Copyright (C) 2016  Intel Corporation
8*4882a593Smuzhiyun#
9*4882a593Smuzhiyun# SPDX-License-Identifier: GPL-2.0-only
10*4882a593Smuzhiyun#
11*4882a593Smuzhiyun
12*4882a593Smuzhiyunimport bb.data
13*4882a593Smuzhiyun
14*4882a593Smuzhiyunclass RemoteDatastores:
15*4882a593Smuzhiyun    """Used on the server side to manage references to server-side datastores"""
16*4882a593Smuzhiyun    def __init__(self, cooker):
17*4882a593Smuzhiyun        self.cooker = cooker
18*4882a593Smuzhiyun        self.datastores = {}
19*4882a593Smuzhiyun        self.locked = []
20*4882a593Smuzhiyun        self.datastores[0] = self.cooker.data
21*4882a593Smuzhiyun        self.nextindex = 1
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun    def __len__(self):
24*4882a593Smuzhiyun        return len(self.datastores)
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun    def __getitem__(self, key):
27*4882a593Smuzhiyun        # Cooker could have changed its datastore from under us
28*4882a593Smuzhiyun        self.datastores[0] = self.cooker.data
29*4882a593Smuzhiyun        return self.datastores[key]
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun    def items(self):
32*4882a593Smuzhiyun        return self.datastores.items()
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun    def store(self, d, locked=False):
35*4882a593Smuzhiyun        """
36*4882a593Smuzhiyun        Put a datastore into the collection. If locked=True then the datastore
37*4882a593Smuzhiyun        is understood to be managed externally and cannot be released by calling
38*4882a593Smuzhiyun        release().
39*4882a593Smuzhiyun        """
40*4882a593Smuzhiyun        idx = self.nextindex
41*4882a593Smuzhiyun        self.datastores[idx] = d
42*4882a593Smuzhiyun        if locked:
43*4882a593Smuzhiyun            self.locked.append(idx)
44*4882a593Smuzhiyun        self.nextindex += 1
45*4882a593Smuzhiyun        return idx
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun    def check_store(self, d, locked=False):
48*4882a593Smuzhiyun        """
49*4882a593Smuzhiyun        Put a datastore into the collection if it's not already in there;
50*4882a593Smuzhiyun        in either case return the index
51*4882a593Smuzhiyun        """
52*4882a593Smuzhiyun        for key, val in self.datastores.items():
53*4882a593Smuzhiyun            if val is d:
54*4882a593Smuzhiyun                idx = key
55*4882a593Smuzhiyun                break
56*4882a593Smuzhiyun        else:
57*4882a593Smuzhiyun            idx = self.store(d, locked)
58*4882a593Smuzhiyun        return idx
59*4882a593Smuzhiyun
60*4882a593Smuzhiyun    def release(self, idx):
61*4882a593Smuzhiyun        """Discard a datastore in the collection"""
62*4882a593Smuzhiyun        if idx in self.locked:
63*4882a593Smuzhiyun            raise Exception('Tried to release locked datastore %d' % idx)
64*4882a593Smuzhiyun        del self.datastores[idx]
65*4882a593Smuzhiyun
66