Guest User

Untitled

a guest
Nov 17th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. #!/usr/bin/env python
  2. """
  3. Ensures all committed code will follow the given standards.
  4. """
  5. from __future__ import with_statement, print_function
  6. import os
  7. import re
  8. import shutil
  9. import subprocess
  10. import sys
  11. import tempfile
  12.  
  13. # don't fill in both of these
  14. select_codes = []
  15. ignore_codes = ["E402"]
  16.  
  17. overrides = ["--max-line-length=120"]
  18.  
  19.  
  20. def system(*args, **kwargs):
  21. kwargs.setdefault('stdout', subprocess.PIPE)
  22. proc = subprocess.Popen(args, **kwargs)
  23. out, err = proc.communicate()
  24. return out
  25.  
  26.  
  27. def main():
  28. modified = re.compile('^[AM]+\s+(?P<name>.*\.py$)', re.MULTILINE)
  29. files = system('git', 'status', '--porcelain').decode("utf-8")
  30. files = modified.findall(files)
  31.  
  32. tempdir = tempfile.mkdtemp()
  33. for name in files:
  34. filename = os.path.join(tempdir, name)
  35. filepath = os.path.dirname(filename)
  36.  
  37. if not os.path.exists(filepath):
  38. os.makedirs(filepath)
  39. with open(filename, 'w') as f:
  40. system('git', 'show', ':' + name, stdout=f)
  41.  
  42. args = ['pycodestyle']
  43. if select_codes and ignore_codes:
  44. print(u'Error: select and ignore codes are mutually exclusive')
  45. sys.exit(1)
  46. elif select_codes:
  47. args.extend(('--select', ','.join(select_codes)))
  48. elif ignore_codes:
  49. args.extend(('--ignore', ','.join(ignore_codes)))
  50. args.extend(overrides)
  51. args.append('.')
  52. output = system(*args, cwd=tempdir)
  53. shutil.rmtree(tempdir)
  54. if output:
  55. print(u'PEP8 style violations have been detected. Please fix them.\n')
  56. print(output.decode("utf-8"),)
  57. sys.exit(1)
  58.  
  59.  
  60. if __name__ == '__main__':
  61. main()
Add Comment
Please, Sign In to add comment