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