Advertisement
Guest User

Untitled

a guest
Dec 18th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.83 KB | None | 0 0
  1. #! /usr/bin/env python
  2. # -*- encoding: utf-8 -*-
  3.  
  4. import os
  5. import git
  6.  
  7. repo = git.Repo.init("C:/altyn-android")
  8. reader = repo.config_reader()
  9. checkstyle_jar = reader.get("checkstyle", "jar")
  10. checkstyle_cfg = reader.get("checkstyle", "checkfile")
  11.  
  12. # Config
  13. #checkstyle_jar = os.getenv('CHECKSTYLE_JAR', "checkstyle-5.7-all.jar")
  14. #checkstyle_cfg = os.getenv('CHECKSTYLE_CFG', "checkstyle.xml")
  15.  
  16. failing_test_should_prevent_commit = True
  17.  
  18. # Limit Checkstyle test to new classes only. You can use the following
  19. # template:
  20. #    
  21. #     export CHECKSTYLE_START_DATE='2014-01-01 00:00:00 +0200'
  22. #
  23. min_date_of_first_commit = os.getenv('CHECKSTYLE_START_DATE', None)
  24.  
  25. java_cmd = "java"
  26. git_cmd = "git"
  27.  
  28. # Init
  29. import datetime
  30. import re
  31. import subprocess
  32. import sys
  33.  
  34. def parse_date(val):
  35.     # We're not using python-dateutil, since it's not available out of the box on some machines
  36.     m = re.match('[0-9]{4}-[0-9]{2}-[0-9]{2}', val)
  37.     if m:
  38.         return datetime.datetime.strptime(m.group(0), '%Y-%m-%d')
  39.     else:
  40.         raise Exception("Invalid date format: %s" % val)
  41.  
  42. try:
  43.     import colorama
  44.    
  45.     colorama.init()
  46.    
  47.     style_ok = colorama.Fore.GREEN
  48.     style_err = colorama.Fore.RED
  49.     style_warn = colorama.Fore.YELLOW
  50.     style_details = colorama.Style.DIM
  51.     style_reset = colorama.Style.RESET_ALL
  52. except ImportError as err:
  53.     # Colorama dependency is optional, since some people may not be able to install it locally
  54.     style_ok = ""
  55.     style_err = ""
  56.     style_warn = ""
  57.     style_details = ""
  58.     style_reset = ""
  59.  
  60. if not os.path.isfile(checkstyle_jar):
  61.     sys.stderr.write("checkstyle JAR not found (%s)\n" % checkstyle_jar)
  62.     sys.exit(1)
  63.  
  64. if not os.path.isfile(checkstyle_cfg):
  65.     sys.stderr.write("checkstyle config not found (%s)\n" % checkstyle_cfg)
  66.     sys.exit(1)
  67.  
  68. if "check_output" not in dir( subprocess ):
  69.     sys.stderr.write("python >= 2.7 is required\n")
  70.     sys.exit(1)
  71.  
  72. if min_date_of_first_commit is not None:
  73.      min_date_of_first_commit = parse_date(min_date_of_first_commit)
  74.  
  75. # Fetch files to commit
  76. file_list = subprocess.check_output([git_cmd, "diff", "--cached", "--name-only", "--diff-filter=ACM"])
  77. file_list = [f for f in file_list.splitlines() if f.endswith(b'.java')]
  78.  
  79. # Run Checkstyle
  80. def first_commit_before_date(source_file, dt):
  81.     commits = subprocess.check_output([git_cmd, "log", "--format=%ai", source_file])
  82.     if len(commits) == 0:
  83.         return False
  84.     first_commit = commits.splitlines()[-1]
  85.     first_commit = parse_date(first_commit)
  86.     return first_commit < dt
  87.  
  88. failed_checks = 0
  89. for source_file in file_list:
  90.     if min_date_of_first_commit is not None and first_commit_before_date(source_file, min_date_of_first_commit):
  91.         print('Checkstyle: ' + source_file + style_warn + ' SKIPPED (too old)' + style_reset)
  92.         continue
  93.  
  94.     try:
  95.         subprocess.check_output([java_cmd, "-jar", checkstyle_jar, "-c", checkstyle_cfg, source_file])
  96.         print('Checkstyle: ' + source_file + style_ok + ' OK' + style_reset)
  97.     except subprocess.CalledProcessError as err:
  98.         failed_checks = failed_checks + 1
  99.         print('Checkstyle: ' + source_file + style_err + ' FAILED' + style_reset)
  100.         # Print Checkstyle details
  101.         checkRegexp = re.compile(r'\.java:')
  102.     messages = [checkRegexp.split(m)[-1] for m in err.output.splitlines() if checkRegexp.search(m) is not None]
  103.     for m in messages:
  104.         print(' => ' + style_details + m + style_reset)
  105.  
  106. # Decisions
  107. if failed_checks == 0:
  108.     print("%d Checkstyle tests passed" % len(file_list))
  109. elif failing_test_should_prevent_commit:
  110.     print(style_err + "%d Checkstyle tests failed" % failed_checks + style_reset)
  111.     sys.exit(2)
  112. else:
  113.     print(style_warn + "%d Checkstyle tests failed (won't block the commit)" % failed_checks + style_reset)
  114.     sys.exit(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement