list_url_patterns.py 1.3 KB

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