/Tools/scripts/patchcheck.py

http://unladen-swallow.googlecode.com/ · Python · 90 lines · 65 code · 13 blank · 12 comment · 15 complexity · 183ce3ab3ea04ecdea69bb48ac694019 MD5 · raw file

  1. import os.path
  2. import subprocess
  3. import sys
  4. import reindent
  5. def status(message, modal=False, info=None):
  6. """Decorator to output status info to stdout."""
  7. def decorated_fxn(fxn):
  8. def call_fxn(*args, **kwargs):
  9. sys.stdout.write(message + ' ... ')
  10. sys.stdout.flush()
  11. result = fxn(*args, **kwargs)
  12. if not modal and not info:
  13. print "done"
  14. elif info:
  15. print info(result)
  16. else:
  17. if result:
  18. print "yes"
  19. else:
  20. print "NO"
  21. return result
  22. return call_fxn
  23. return decorated_fxn
  24. @status("Getting the list of files that have been added/changed",
  25. info=lambda x: "%s files" % len(x))
  26. def changed_files():
  27. """Run ``svn status`` and return a set of files that have been
  28. changed/added."""
  29. cmd = 'svn status --quiet --non-interactive --ignore-externals'
  30. svn_st = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
  31. svn_st.wait()
  32. output = [line.strip() for line in svn_st.stdout.readlines()]
  33. files = set()
  34. for line in output:
  35. if not line[0] in ('A', 'M'):
  36. continue
  37. line_parts = line.split()
  38. path = line_parts[-1]
  39. if os.path.isfile(path):
  40. files.add(path)
  41. return files
  42. @status("Fixing whitespace", info=lambda x: "%s files" % x)
  43. def normalize_whitespace(file_paths):
  44. """Make sure that the whitespace for .py files have been normalized."""
  45. reindent.makebackup = False # No need to create backups.
  46. result = map(reindent.check, (x for x in file_paths if x.endswith('.py')))
  47. return sum(result)
  48. @status("Docs modified", modal=True)
  49. def docs_modified(file_paths):
  50. """Report if any files in the Docs directory."""
  51. for path in file_paths:
  52. if path.startswith("Doc"):
  53. return True
  54. return False
  55. @status("Misc/ACKS updated", modal=True)
  56. def credit_given(file_paths):
  57. """Check if Misc/ACKS has been changed."""
  58. return 'Misc/ACKS' in file_paths
  59. @status("Misc/NEWS updated", modal=True)
  60. def reported_news(file_paths):
  61. """Check if Misc/NEWS has been changed."""
  62. return 'Misc/NEWS' in file_paths
  63. def main():
  64. file_paths = changed_files()
  65. # PEP 7/8 verification.
  66. normalize_whitespace(file_paths)
  67. # Docs updated.
  68. docs_modified(file_paths)
  69. # Misc/ACKS changed.
  70. credit_given(file_paths)
  71. # Misc/NEWS changed.
  72. reported_news(file_paths)
  73. # Test suite run and passed.
  74. print
  75. print "Did you run the test suite?"
  76. if __name__ == '__main__':
  77. main()