#!/usr/bin/env python
# -*- coding: utf-8 -*-


"""This script runs `pylint` with plugins and special configuration files.
Use the following types of pylint configuration files:

 1) Global
 2) Pull request
 3) Version
 4) Beta


1) Global

  Used to check enabled messages on this file in all modules of the project.
  The result affects your build status.
  File name: `travis_run_pylint.cfg`


2) Pull request

  Used to check enabled messages on this file only in modules
  changed in a pull request of the project.
  The result affects your build status.
  File name: `travis_run_pylint_pr.cfg`


3) Version Exclusions

  Used to disable previously enabled messages on `global` or `pull request`
  configuration files based on `VERSION` variable environment.
  File name: `travis_run_pylint_exclude_{VERSION}.cfg`


4) Beta

  Used to add the previously enabled messages in `global` or `pull request`
  configuration files that you want to display, without affecting your build
  status.

  File name: `travis_run_pylint_beta.cfg`
"""


import os

import run_pylint
import travis_helpers


git_work_dir = os.environ.get('TRAVIS_BUILD_DIR', False)
is_pull_request = os.environ.get(
    'TRAVIS_PULL_REQUEST', 'false') != 'false'
version = os.environ.get('VERSION', '')
beta_msgs = run_pylint.get_beta_msgs()
expected_errors = int(os.environ.get('PYLINT_EXPECTED_ERRORS', 0))
exit_status = 0
result = run_pylint.pylint_run(is_pull_request, version, git_work_dir)
count_errors = sum(result.values())
if count_errors == -1:
    print(travis_helpers.yellow('Python modules not found'))
elif count_errors != expected_errors:
    print(travis_helpers.red("pylint expected errors {expected_errors}, "
          "found {number_errors}!".format(
              expected_errors=expected_errors,
              number_errors=count_errors)))
    exit_status = 1
if beta_msgs and count_errors >= 0:
    print(travis_helpers.green(
        "\nNext checks are still in beta "
        "they won't affect your build status for now: "
        '\n' + ', '.join(sorted(beta_msgs))))

exit(exit_status)
