xref: /xnu-11417.140.69/tools/lldbmacros/lint/syntax_checker.py (revision 43a90889846e00bfb5cf1d255cdc0a701a1e05a4)
1*43a90889SApple OSS Distributions#!/usr/bin/env python3
2*43a90889SApple OSS Distributions
3*43a90889SApple OSS Distributionshelpdoc = """
4*43a90889SApple OSS DistributionsA simple utility that verifies the syntax for python scripts.
5*43a90889SApple OSS DistributionsThe checks it does are :
6*43a90889SApple OSS Distributions  * Check for 'tab' characters in .py files
7*43a90889SApple OSS Distributions  * Compile errors in py sources
8*43a90889SApple OSS DistributionsUsage:
9*43a90889SApple OSS Distributions  python syntax_checker.py <python_source_file> [<python_source_file> ..]
10*43a90889SApple OSS Distributions"""
11*43a90889SApple OSS Distributionsimport sys
12*43a90889SApple OSS Distributionsimport os
13*43a90889SApple OSS Distributionsimport re
14*43a90889SApple OSS Distributions
15*43a90889SApple OSS Distributionstabs_search_rex = re.compile(r"^\s*\t+",re.MULTILINE|re.DOTALL)
16*43a90889SApple OSS Distributions
17*43a90889SApple OSS Distributionsdef find_non_ascii(s):
18*43a90889SApple OSS Distributions    for c in s:
19*43a90889SApple OSS Distributions        if ord(c) >= 0x80: return True
20*43a90889SApple OSS Distributions    return False
21*43a90889SApple OSS Distributions
22*43a90889SApple OSS Distributionsif __name__ == "__main__":
23*43a90889SApple OSS Distributions    if len(sys.argv) < 2:
24*43a90889SApple OSS Distributions        print("Error: Unknown arguments", file=sys.stderr)
25*43a90889SApple OSS Distributions        print(helpdoc)
26*43a90889SApple OSS Distributions        sys.exit(1)
27*43a90889SApple OSS Distributions    for fname in sys.argv[1:]:
28*43a90889SApple OSS Distributions        if not os.path.exists(fname):
29*43a90889SApple OSS Distributions            print("Error: Cannot recognize %s as a file" % fname, file=sys.stderr)
30*43a90889SApple OSS Distributions            sys.exit(1)
31*43a90889SApple OSS Distributions        if fname.split('.')[-1] != 'py':
32*43a90889SApple OSS Distributions            print("Note: %s is not a valid python file. Skipping." % fname)
33*43a90889SApple OSS Distributions            continue
34*43a90889SApple OSS Distributions        fh = open(fname)
35*43a90889SApple OSS Distributions        strdata = fh.readlines()
36*43a90889SApple OSS Distributions        lineno = 0
37*43a90889SApple OSS Distributions        syntax_fail = False
38*43a90889SApple OSS Distributions        for linedata in strdata:
39*43a90889SApple OSS Distributions            lineno += 1
40*43a90889SApple OSS Distributions            if len(tabs_search_rex.findall(linedata)) > 0 :
41*43a90889SApple OSS Distributions                print("Error: Found a TAB character at %s:%d" % (fname, lineno), file=sys.stderr)
42*43a90889SApple OSS Distributions                syntax_fail = True
43*43a90889SApple OSS Distributions        if find_non_ascii(linedata):
44*43a90889SApple OSS Distributions            print("Error: Found a non ascii character at %s:%d" % (fname, lineno), file=sys.stderr)
45*43a90889SApple OSS Distributions            syntax_fail = True
46*43a90889SApple OSS Distributions        if syntax_fail:
47*43a90889SApple OSS Distributions            print("Error: Syntax check failed. Please fix the errors and try again.", file=sys.stderr)
48*43a90889SApple OSS Distributions            sys.exit(1)
49*43a90889SApple OSS Distributions        #now check for error in compilation
50*43a90889SApple OSS Distributions        try:
51*43a90889SApple OSS Distributions            with open(fname, 'r') as file:
52*43a90889SApple OSS Distributions                source = file.read() + '\n'
53*43a90889SApple OSS Distributions                compile_result = compile(source, fname, 'exec')
54*43a90889SApple OSS Distributions        except Exception as exc:
55*43a90889SApple OSS Distributions            print(str(exc), file=sys.stderr)
56*43a90889SApple OSS Distributions            print("Error: Compilation failed. Please fix the errors and try again.", file=sys.stderr)
57*43a90889SApple OSS Distributions            sys.exit(1)
58*43a90889SApple OSS Distributions        print("Success: Checked %s. No syntax errors found." % fname)
59*43a90889SApple OSS Distributions    sys.exit(0)
60*43a90889SApple OSS Distributions
61