summary refs log tree commit diff
path: root/scripts-dev/list_url_patterns.py
blob: e85420dea876e45f820d9abda03779723517c5a0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#! /usr/bin/python

import argparse
import ast
import os
import sys

import yaml

PATTERNS_V1 = []
PATTERNS_V2 = []

RESULT = {"v1": PATTERNS_V1, "v2": PATTERNS_V2}


class CallVisitor(ast.NodeVisitor):
    def visit_Call(self, node):
        if isinstance(node.func, ast.Name):
            name = node.func.id
        else:
            return

        if name == "client_patterns":
            PATTERNS_V2.append(node.args[0].s)


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())


parser = argparse.ArgumentParser(description="Find url patterns.")

parser.add_argument(
    "directories",
    nargs="+",
    metavar="DIR",
    help="Directories to search for definitions",
)

args = parser.parse_args()


for directory in args.directories:
    for root, _, files in os.walk(directory):
        for filename in files:
            if filename.endswith(".py"):
                filepath = os.path.join(root, filename)
                find_patterns_in_file(filepath)

PATTERNS_V1.sort()
PATTERNS_V2.sort()

yaml.dump(RESULT, sys.stdout, default_flow_style=False)