1
0

list_url_patterns.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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_path_patterns":
  17. PATTERNS_V1.append(node.args[0].s)
  18. elif name == "client_v2_patterns":
  19. PATTERNS_V2.append(node.args[0].s)
  20. def find_patterns_in_code(input_code):
  21. input_ast = ast.parse(input_code)
  22. visitor = CallVisitor()
  23. visitor.visit(input_ast)
  24. def find_patterns_in_file(filepath):
  25. with open(filepath) as f:
  26. find_patterns_in_code(f.read())
  27. parser = argparse.ArgumentParser(description='Find url patterns.')
  28. parser.add_argument(
  29. "directories",
  30. nargs='+',
  31. metavar="DIR",
  32. help="Directories to search for definitions",
  33. )
  34. args = parser.parse_args()
  35. for directory in args.directories:
  36. for root, dirs, files in os.walk(directory):
  37. for filename in files:
  38. if filename.endswith(".py"):
  39. filepath = os.path.join(root, filename)
  40. find_patterns_in_file(filepath)
  41. PATTERNS_V1.sort()
  42. PATTERNS_V2.sort()
  43. yaml.dump(RESULT, sys.stdout, default_flow_style=False)