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