Prechádzať zdrojové kódy

补全AsyncFunctionDef逻辑

Sjim 2 rokov pred
rodič
commit
f0a7899461
5 zmenil súbory, kde vykonal 47 pridanie a 11 odobranie
  1. 3 0
      .idea/misc.xml
  2. 6 3
      parse/parse.py
  3. 1 1
      parse/parse2nd.py
  4. 6 7
      utils/funclink.py
  5. 31 0
      utils/tmp.gv

+ 3 - 0
.idea/misc.xml

@@ -1,4 +1,7 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <project version="4">
   <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.8 (PrivacyScan)" project-jdk-type="Python SDK" />
+  <component name="PyCharmProfessionalAdvertiser">
+    <option name="shown" value="true" />
+  </component>
 </project>

+ 6 - 3
parse/parse.py

@@ -5,6 +5,7 @@ from models.funcnode import FuncNode, get_script, match_data_type, \
 from models.sentencenode import SuspectedSentenceNode
 from utils import log
 from utils.ERRORLIST import error_list
+
 logging = log.getlogger()
 
 
@@ -30,7 +31,7 @@ def parse_tree(source, lattices, file_name, tree_node, code_lines, node_list=Non
         node_list = []
     data_type = lattices["dataType"]
     purpose_dict = lattices["purpose"]
-    if isinstance(tree_node, ast.FunctionDef):
+    if isinstance(tree_node, ast.FunctionDef) or isinstance(tree_node, ast.AsyncFunctionDef):
         func_node = FuncNode(tree_node, file_name, lattices, code_lines)
         try:
             all_nodes = func_node.get_sentence_nodes()
@@ -55,7 +56,7 @@ def parse_tree(source, lattices, file_name, tree_node, code_lines, node_list=Non
     elif isinstance(tree_node, ast.ClassDef):
         class_name = tree_node.name
         for node_son in tree_node.body:
-            if isinstance(node_son, ast.FunctionDef):
+            if isinstance(node_son, ast.FunctionDef) or isinstance(tree_node, ast.AsyncFunctionDef):
                 node_list, func_dict = parse_tree(source, lattices, file_name, node_son, code_lines, node_list,
                                                   func_dict, class_name)
 
@@ -139,7 +140,9 @@ def add_code(lattices, file_name, tree_node, code_lines, node_list):
     """
     data_type = lattices["dataType"]
     purpose_dict = lattices["purpose"]
-    if not isinstance(tree_node, ast.FunctionDef) and not isinstance(tree_node, ast.ClassDef) and not isinstance(
+    if not isinstance(tree_node, ast.FunctionDef) and not isinstance(tree_node,
+                                                                     ast.AsyncFunctionDef) and not isinstance(tree_node,
+                                                                                                              ast.ClassDef) and not isinstance(
             tree_node, ast.Import) and not isinstance(tree_node, ast.Module):
         line_no = tree_node.lineno
         script_ori, script = get_script(tree_node, code_lines)

+ 1 - 1
parse/parse2nd.py

@@ -158,7 +158,7 @@ def parse_tree2nd(source_dir, p, node, lines, func_node_dict, node_list_1st, fil
             node_list = parse_tree2nd(source_dir, p, node_son, lines, func_node_dict, node_list_1st,
                                       file_name, node_list, func_name=node.name,
                                       class_name=node.name)
-    elif isinstance(node, ast.FunctionDef):
+    elif isinstance(node, ast.FunctionDef) or isinstance(node, ast.AsyncFunctionDef):
         for node_son in node.body:
             node_list = parse_tree2nd(source_dir, p, node_son, lines, func_node_dict, node_list_1st,
                                       file_name, node_list, func_name=node.name,

+ 6 - 7
utils/funclink.py

@@ -313,12 +313,11 @@ def get_call_flow(source_dir, file_list):
 
 
 if __name__ == '__main__':
-    project = "/Users/liufan/Documents/实验室/隐私扫描项目/SAP检测项目/mini"
-    file_list = walk_files_path(project)
-
-    p = ProjectAnalyzer(project, file_list)
-    for method in p.get_methods():
-        print(method, p.find_all_call_func(method))
-    print(p.get_methods())
+    p = ProjectAnalyzer("D:\\study\\python\\test",
+                        ["D:\\study\\python\\test\\die.py", "D:\\study\\python\\test\\live.py"])
+    print(p.find_all_call_func("live.getup"))
+    print(p.find_direct_callee_func("live.getup"))
+    print(p.find_direct_call_func("live.getup"))
+    print("s")
 
     # graghviz("program.gv", ["D:\\study\\python\\test\\main.py","D:\\study\\python\\test\\live.py"])

+ 31 - 0
utils/tmp.gv

@@ -0,0 +1,31 @@
+digraph G {
+    graph [rankdir=LR, clusterrank="local"];
+    subgraph cluster_G {
+
+        graph [style="filled,rounded", fillcolor="#80808018", label=""];
+        die [label="die", style="filled", fillcolor="#ffffffb2", fontcolor="#000000", group="0"];
+        live [label="live", style="filled", fillcolor="#ffffffb2", fontcolor="#000000", group="1"];
+        subgraph cluster_die {
+
+            graph [style="filled,rounded", fillcolor="#80808018", label="die"];
+            die__play [label="play", style="filled", fillcolor="#feccccb2", fontcolor="#000000", group="0"];
+        }
+        subgraph cluster_live {
+
+            graph [style="filled,rounded", fillcolor="#80808018", label="live"];
+            live__eat [label="eat", style="filled", fillcolor="#ccfeccb2", fontcolor="#000000", group="1"];
+            live__getup [label="getup", style="filled", fillcolor="#ccfeccb2", fontcolor="#000000", group="1"];
+            live__test [label="test", style="filled", fillcolor="#ccfeccb2", fontcolor="#000000", group="1"];
+        }
+    }
+        die -> die__play [style="dashed",  color="#838b8b"];
+        live -> live__test [style="dashed",  color="#838b8b"];
+        live -> live__eat [style="dashed",  color="#838b8b"];
+        live -> live__getup [style="dashed",  color="#838b8b"];
+        die -> die__play [style="solid",  color="#000000"];
+        die -> live__getup [style="solid",  color="#000000"];
+        die__play -> live__getup [style="solid",  color="#000000"];
+        live__eat -> live__test [style="solid",  color="#000000"];
+        live__getup -> live__eat [style="solid",  color="#000000"];
+        live -> live__getup [style="solid",  color="#000000"];
+    }