/src/etc/tidy.py

http://github.com/jruderman/rust · Python · 50 lines · 36 code · 12 blank · 2 comment · 20 complexity · 40dd39526555dbec033721f894ae3cf6 MD5 · raw file

  1. #!/usr/bin/env python
  2. import sys, fileinput, subprocess, re
  3. err=0
  4. cols=78
  5. # Be careful to support Python 2.4, 2.6, and 3.x here!
  6. config_proc=subprocess.Popen([ "git", "config", "core.autocrlf" ],
  7. stdout=subprocess.PIPE)
  8. result=config_proc.communicate()[0]
  9. true="true".encode('utf8')
  10. autocrlf=result.strip() == true if result is not None else False
  11. def report_err(s):
  12. global err
  13. print("%s:%d: %s" % (fileinput.filename(), fileinput.filelineno(), s))
  14. err=1
  15. file_names = [s for s in sys.argv[1:] if (not s.endswith("_gen.rs"))
  16. and (not s.startswith(".#"))]
  17. try:
  18. for line in fileinput.input(file_names,
  19. openhook=fileinput.hook_encoded("utf-8")):
  20. if fileinput.filename().find("tidy.py") == -1:
  21. if line.find("FIXME") != -1:
  22. if re.search("FIXME.*#\d+", line) == None:
  23. report_err("FIXME without issue number")
  24. if line.find("TODO") != -1:
  25. report_err("TODO is deprecated; use FIXME")
  26. if (line.find('\t') != -1 and
  27. fileinput.filename().find("Makefile") == -1):
  28. report_err("tab character")
  29. if not autocrlf and line.find('\r') != -1:
  30. report_err("CR character")
  31. if line.endswith(" \n") or line.endswith("\t\n"):
  32. report_err("trailing whitespace")
  33. line_len = len(line)-2 if autocrlf else len(line)-1
  34. if line_len > cols:
  35. report_err("line longer than %d chars" % cols)
  36. except UnicodeDecodeError, e:
  37. report_err("UTF-8 decoding error " + str(e))
  38. sys.exit(err)