2016-02-05 15:17:47 +00:00
|
|
|
#! /usr/bin/python
|
|
|
|
|
|
|
|
import argparse
|
2018-10-20 00:16:55 +00:00
|
|
|
import ast
|
2016-02-05 15:17:47 +00:00
|
|
|
import os
|
|
|
|
import sys
|
2018-10-20 00:16:55 +00:00
|
|
|
|
2016-02-05 15:17:47 +00:00
|
|
|
import yaml
|
|
|
|
|
2016-02-05 15:43:27 +00:00
|
|
|
PATTERNS_V1 = []
|
|
|
|
PATTERNS_V2 = []
|
2016-02-05 15:17:47 +00:00
|
|
|
|
2018-10-20 00:16:55 +00:00
|
|
|
RESULT = {"v1": PATTERNS_V1, "v2": PATTERNS_V2}
|
|
|
|
|
2016-02-05 15:17:47 +00:00
|
|
|
|
|
|
|
class CallVisitor(ast.NodeVisitor):
|
|
|
|
def visit_Call(self, node):
|
|
|
|
if isinstance(node.func, ast.Name):
|
|
|
|
name = node.func.id
|
|
|
|
else:
|
|
|
|
return
|
|
|
|
|
2019-06-03 11:28:59 +00:00
|
|
|
if name == "client_patterns":
|
2016-02-05 15:43:27 +00:00
|
|
|
PATTERNS_V2.append(node.args[0].s)
|
2016-02-05 15:17:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
def find_patterns_in_code(input_code):
|
|
|
|
input_ast = ast.parse(input_code)
|
|
|
|
visitor = CallVisitor()
|
|
|
|
visitor.visit(input_ast)
|
|
|
|
|
|
|
|
|
|
|
|
def find_patterns_in_file(filepath):
|
|
|
|
with open(filepath) as f:
|
|
|
|
find_patterns_in_code(f.read())
|
|
|
|
|
|
|
|
|
2019-06-20 09:32:02 +00:00
|
|
|
parser = argparse.ArgumentParser(description="Find url patterns.")
|
2016-02-05 15:17:47 +00:00
|
|
|
|
|
|
|
parser.add_argument(
|
2018-10-20 00:16:55 +00:00
|
|
|
"directories",
|
2019-06-20 09:32:02 +00:00
|
|
|
nargs="+",
|
2018-10-20 00:16:55 +00:00
|
|
|
metavar="DIR",
|
|
|
|
help="Directories to search for definitions",
|
2016-02-05 15:17:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
|
|
|
|
for directory in args.directories:
|
|
|
|
for root, dirs, files in os.walk(directory):
|
|
|
|
for filename in files:
|
|
|
|
if filename.endswith(".py"):
|
|
|
|
filepath = os.path.join(root, filename)
|
|
|
|
find_patterns_in_file(filepath)
|
|
|
|
|
2016-02-05 15:43:27 +00:00
|
|
|
PATTERNS_V1.sort()
|
|
|
|
PATTERNS_V2.sort()
|
2016-02-05 15:17:47 +00:00
|
|
|
|
2016-02-05 15:43:27 +00:00
|
|
|
yaml.dump(RESULT, sys.stdout, default_flow_style=False)
|