File: check_lua_classes.py

package info (click to toggle)
corsix-th 0.69.1-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 19,660 kB
  • sloc: cpp: 19,080; java: 8,422; ansic: 3,613; xml: 914; python: 285; lex: 82; sh: 76; yacc: 44; makefile: 15
file content (42 lines) | stat: -rwxr-xr-x 1,533 bytes parent folder | download | duplicates (4)
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
#!/usr/bin/env python3

import os
import re
import sys


# This regex can't find all class declaration mistakes and only checks the
# first few lines:
# Regex: ^class "(.+)".*\n\n(?!---@type \1\nlocal \1 = _G\["\1"])
regex = r"^class \"(.+)\".*\n\n(?!---@type \1\nlocal \1 = _G\[\"\1\"])"

print_root_regex = re.compile("Lua.*")
script_dir = os.path.join(os.path.dirname(__file__), "..", "CorsixTH", "Lua")
ignored = os.listdir(os.path.join(script_dir, "languages"))
count = 0
problem_found = False
for root, _, files in os.walk(script_dir):
    for script in files:
        if script.endswith(".lua") and script not in ignored:
            count += 1
            script_string = open(os.path.join(root, script), 'r').read()
            for found_class in re.findall(regex, script_string, re.MULTILINE):
                if not problem_found:
                    print("******* CHECK CLASS DECLARATIONS *******")
                    problem_found = True
                    print("Invalid/Improper Class Declarations Found:")
                path = print_root_regex.search(root).group(0)
                print("*{}:{}".format(os.path.join(path, script), found_class))

print("Checked {} files".format(count))
if problem_found:
    print("\nReason: The class declaration(s) didn't begin as follows:")
    print("")
    print("class \"Name\" / class \"Name\" (Parent)")
    print("")
    print("---@type Name")
    print("local Name = _G[\"Name\"]")
    print("-----------------------------------------\n")
    sys.exit(1)

sys.exit(0)