Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

hooks.py 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. """isort.py.
  2. Defines a git hook to allow pre-commit warnings and errors about import order.
  3. usage:
  4. exit_code = git_hook(strict=True)
  5. Copyright (C) 2015 Helen Sherwood-Taylor
  6. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  7. documentation files (the "Software"), to deal in the Software without restriction, including without limitation
  8. the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
  9. to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all copies or
  11. substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  13. TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  14. THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  15. CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  16. OTHER DEALINGS IN THE SOFTWARE.
  17. """
  18. import subprocess
  19. from isort import SortImports
  20. def get_output(command):
  21. """
  22. Run a command and return raw output
  23. :param str command: the command to run
  24. :returns: the stdout output of the command
  25. """
  26. return subprocess.check_output(command.split())
  27. def get_lines(command):
  28. """
  29. Run a command and return lines of output
  30. :param str command: the command to run
  31. :returns: list of whitespace-stripped lines output by command
  32. """
  33. stdout = get_output(command)
  34. return [line.strip().decode('utf-8') for line in stdout.splitlines()]
  35. def git_hook(strict=False):
  36. """
  37. Git pre-commit hook to check staged files for isort errors
  38. :param bool strict - if True, return number of errors on exit,
  39. causing the hook to fail. If False, return zero so it will
  40. just act as a warning.
  41. :return number of errors if in strict mode, 0 otherwise.
  42. """
  43. # Get list of files modified and staged
  44. diff_cmd = "git diff-index --cached --name-only --diff-filter=ACMRTUXB HEAD"
  45. files_modified = get_lines(diff_cmd)
  46. errors = 0
  47. for filename in files_modified:
  48. if filename.endswith('.py'):
  49. # Get the staged contents of the file
  50. staged_cmd = "git show :%s" % filename
  51. staged_contents = get_output(staged_cmd)
  52. sort = SortImports(
  53. file_path=filename,
  54. file_contents=staged_contents.decode(),
  55. check=True
  56. )
  57. if sort.incorrectly_sorted:
  58. errors += 1
  59. return errors if strict else 0