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