[oe-commits] [bitbake] 03/07: bb.utils: add get_referenced_vars

git at git.openembedded.org git at git.openembedded.org
Sat Jan 18 17:47:23 UTC 2020


This is an automated email from the git hooks/post-receive script.

rpurdie pushed a commit to branch master-next
in repository bitbake.

commit 9f481b5afda5d23b7def5a767efc15a1d51092cd
Author: Chris Laplante via bitbake-devel <bitbake-devel at lists.openembedded.org>
AuthorDate: Fri Jan 17 14:39:54 2020 -0500

    bb.utils: add get_referenced_vars
    
    Given a start expression, bb.utils.get_referenced_vars returns the
    referenced variable names in a quasi-BFS order (variables within the
    same level are ordered aribitrarily).
    
    For example, given an empty data store:
    
        bb.utils.get_referenced_vars("${A} ${B} ${d.getVar('C')}", d)
    
    returns either ["A", "B", "C"], ["A", "C", "B"], or another
    permutation.
    
    If we then set A = "${F} ${G}", then the same call will return a
    permutation of [A, B, C] concatenated with a permutation of [F, G].
    
    This method is like a version of d.expandWithRefs().references that
    gives some insight into the depth of variable references.
    
    Signed-off-by: Chris Laplante <chris.laplante at agilent.com>
    Signed-off-by: Richard Purdie <richard.purdie at linuxfoundation.org>
---
 lib/bb/utils.py | 37 +++++++++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)

diff --git a/lib/bb/utils.py b/lib/bb/utils.py
index 06c8819..68ca4ef 100644
--- a/lib/bb/utils.py
+++ b/lib/bb/utils.py
@@ -1025,6 +1025,43 @@ def filter(variable, checkvalues, d):
         checkvalues = set(checkvalues)
     return ' '.join(sorted(checkvalues & val))
 
+
+def get_referenced_vars(start_expr, d):
+    """
+    :return: names of vars referenced in start_expr (recursively), in quasi-BFS order (variables within the same level
+    are ordered arbitrarily)
+    """
+
+    seen = set()
+    ret = []
+
+    # The first entry in the queue is the unexpanded start expression
+    queue = collections.deque([start_expr])
+    # Subsequent entries will be variable names, so we need to track whether or not entry requires getVar
+    is_first = True
+
+    empty_data = bb.data.init()
+    while queue:
+        entry = queue.popleft()
+        if is_first:
+            # Entry is the start expression - no expansion needed
+            is_first = False
+            expression = entry
+        else:
+            # This is a variable name - need to get the value
+            expression = d.getVar(entry, False)
+            ret.append(entry)
+
+        # expandWithRefs is how we actually get the referenced variables in the expression. We call it using an empty
+        # data store because we only want the variables directly used in the expression. It returns a set, which is what
+        # dooms us to only ever be "quasi-BFS" rather than full BFS.
+        new_vars = empty_data.expandWithRefs(expression, None).references - set(seen)
+
+        queue.extend(new_vars)
+        seen.update(new_vars)
+    return ret
+
+
 def cpu_count():
     return multiprocessing.cpu_count()
 

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the Openembedded-commits mailing list