All of lore.kernel.org
 help / color / mirror / Atom feed
From: git@git.openembedded.org
To: bitbake-devel@lists.openembedded.org
Subject: Christopher Larson : codeparser: simplify how we compare the called node names
Date: Tue,  8 Nov 2011 17:37:54 +0000 (UTC)	[thread overview]
Message-ID: <20111108173754.28F6110330@opal> (raw)

Module: bitbake.git
Branch: master
Commit: 84e535b5165c7e936c5b1486bdf4626ed3649f5f
URL:    http://git.openembedded.org/?p=bitbake.git&a=commit;h=84e535b5165c7e936c5b1486bdf4626ed3649f5f

Author: Christopher Larson <kergoth@gmail.com>
Date:   Thu Oct 27 22:37:11 2011 -0700

codeparser: simplify how we compare the called node names

With the previous method, using the compare_name methods, we split the
requested match name by '.', reversed it, then compared them piecemeal
during the node traversal. The new method walks the nodes and hands back
the name of what's being called, and then we check that. This also
consolidates the two different implementations of traversal of the
attribute/name nodes (one in compare_name, one for the execs).

Signed-off-by: Christopher Larson <kergoth@gmail.com>

---

 lib/bb/codeparser.py |   72 ++++++++++++++------------------------------------
 1 files changed, 20 insertions(+), 52 deletions(-)

diff --git a/lib/bb/codeparser.py b/lib/bb/codeparser.py
index 0f3d646..7715613 100644
--- a/lib/bb/codeparser.py
+++ b/lib/bb/codeparser.py
@@ -156,40 +156,6 @@ class PythonParser():
     execfuncs = ("bb.build.exec_func", "bb.build.exec_task")
 
     @classmethod
-    def _compare_name(cls, strparts, node):
-        """Given a sequence of strings representing a python name,
-        where the last component is the actual Name and the prior
-        elements are Attribute nodes, determine if the supplied node
-        matches.
-        """
-
-        if not strparts:
-            return True
-
-        current, rest = strparts[0], strparts[1:]
-        if isinstance(node, ast.Attribute):
-            if current == node.attr:
-                return cls._compare_name(rest, node.value)
-        elif isinstance(node, ast.Name):
-            if current == node.id:
-                return True
-        return False
-
-    @classmethod
-    def compare_name(cls, value, node):
-        """Convenience function for the _compare_node method, which
-        can accept a string (which is split by '.' for you), or an
-        iterable of strings, in which case it checks to see if any of
-        them match, similar to isinstance.
-        """
-
-        if isinstance(value, basestring):
-            return cls._compare_name(tuple(reversed(value.split("."))),
-                                        node)
-        else:
-            return any(cls.compare_name(item, node) for item in value)
-
-    @classmethod
     def warn(cls, func, arg):
         """Warn about calls of bitbake APIs which pass a non-literal
         argument for the variable name, as we're not able to track such
@@ -206,39 +172,41 @@ class PythonParser():
                             "not a literal", funcstr, argstr)
 
     def visit_Call(self, node):
-        if self.compare_name(self.getvars, node.func):
+        name = self.called_node_name(node.func)
+        if name in self.getvars:
             if isinstance(node.args[0], ast.Str):
                 self.var_references.add(node.args[0].s)
             else:
                 self.warn(node.func, node.args[0])
-        elif self.compare_name(self.expands, node.func):
+        elif name in self.expands:
             if isinstance(node.args[0], ast.Str):
                 self.warn(node.func, node.args[0])
                 self.var_expands.add(node.args[0].s)
             elif isinstance(node.args[0], ast.Call) and \
-                    self.compare_name(self.getvars, node.args[0].func):
+                 self.called_node_name(node.args[0].func) in self.getvars:
                 pass
             else:
                 self.warn(node.func, node.args[0])
-        elif self.compare_name(self.execfuncs, node.func):
+        elif name in self.execfuncs:
             if isinstance(node.args[0], ast.Str):
                 self.var_execs.add(node.args[0].s)
             else:
                 self.warn(node.func, node.args[0])
-        elif isinstance(node.func, ast.Name):
-            self.execs.add(node.func.id)
-        elif isinstance(node.func, ast.Attribute):
-            # We must have a qualified name.  Therefore we need
-            # to walk the chain of 'Attribute' nodes to determine
-            # the qualification.
-            attr_node = node.func.value
-            identifier = node.func.attr
-            while isinstance(attr_node, ast.Attribute):
-                identifier = attr_node.attr + "." + identifier
-                attr_node = attr_node.value
-            if isinstance(attr_node, ast.Name):
-                identifier = attr_node.id + "." + identifier
-            self.execs.add(identifier)
+        elif name and isinstance(node.func, (ast.Name, ast.Attribute)):
+            self.execs.add(name)
+
+    def called_node_name(self, node):
+        """Given a called node, return its original string form"""
+        components = []
+        while node:
+            if isinstance(node, ast.Attribute):
+                components.append(node.attr)
+                node = node.value
+            elif isinstance(node, ast.Name):
+                components.append(node.id)
+                return '.'.join(reversed(components))
+            else:
+                break
 
     def __init__(self):
         self.var_references = set()




                 reply	other threads:[~2011-11-08 17:43 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20111108173754.28F6110330@opal \
    --to=git@git.openembedded.org \
    --cc=bitbake-devel@lists.openembedded.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.