list_url_patterns.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #! /usr/bin/python
  2. import argparse
  3. import ast
  4. import os
  5. import sys
  6. import yaml
  7. PATTERNS_V1 = []
  8. PATTERNS_V2 = []
  9. RESULT = {"v1": PATTERNS_V1, "v2": PATTERNS_V2}
  10. class CallVisitor(ast.NodeVisitor):
  11. def visit_Call(self, node):
  12. if isinstance(node.func, ast.Name):
  13. name = node.func.id
  14. else:
  15. return
  16. if name == "client_patterns":
  17. PATTERNS_V2.append(node.args[0].s)
  18. def find_patterns_in_code(input_code):
  19. input_ast = ast.parse(input_code)
  20. visitor = CallVisitor()
  21. visitor.visit(input_ast)
  22. def find_patterns_in_file(filepath):
  23. with open(filepath) as f:
  24. find_patterns_in_code(f.read())
  25. parser = argparse.ArgumentParser(description="Find url patterns.")
  26. parser.add_argument(
  27. "directories",
  28. nargs="+",
  29. metavar="DIR",
  30. help="Directories to search for definitions",
  31. )
  32. args = parser.parse_args()
  33. for directory in args.directories:
  34. for root, dirs, files in os.walk(directory):
  35. for filename in files:
  36. if filename.endswith(".py"):
  37. filepath = os.path.join(root, filename)
  38. find_patterns_in_file(filepath)
  39. PATTERNS_V1.sort()
  40. PATTERNS_V2.sort()
  41. yaml.dump(RESULT, sys.stdout, default_flow_style=False)