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