#!/usr/bin/env python3
# Copyright (C) 2017  Daniel Watkins <daniel@daniel-watkins.co.uk>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Run a series of checks against compiled job XML."""
import os
import sys
from configparser import ConfigParser
from typing import Optional
from xml.etree import ElementTree

import click

from jenkins_job_linter.linters import LINTERS


def lint_job_xml(job_name: str, tree: ElementTree.ElementTree,
                 config: ConfigParser) -> bool:
    """Run all the linters against an XML tree."""
    success = True
    for linter in LINTERS:
        result, text = linter(tree, config).check()
        if not result.value:
            success = False
            output = '{}: {}: FAIL'.format(job_name, linter.description)
            if text is not None:
                output += ': {}'.format(text)
            print(output)
    return success


def lint_jobs_from_directory(compiled_job_directory: str,
                             config: ConfigParser) -> bool:
    """Load jobs from a directory and run linters against each one."""
    filtered_config = ConfigParser()
    filtered_config.read_dict(config)
    for section in filtered_config.sections():
        if section != 'job_linter':
            filtered_config.remove_section(section)
    success = True
    for job_file in os.listdir(compiled_job_directory):
        job_path = os.path.join(compiled_job_directory, job_file)
        result = lint_job_xml(job_file, ElementTree.parse(job_path),
                              filtered_config)
        success = success and result
    return success


@click.command()
@click.argument('compiled_job_directory',
                type=click.Path(exists=True, file_okay=False))
@click.option('--conf', type=click.Path(exists=True, dir_okay=False))
def main(compiled_job_directory: str, conf: Optional[str] = None) -> None:
    """Take a directory of Jenkins job XML and run some checks against it."""
    config = ConfigParser()
    if conf is not None:
        config.read(conf)
    result = lint_jobs_from_directory(compiled_job_directory, config)
    if not result:
        sys.exit(1)
    sys.exit(0)


if __name__ == '__main__':
    main()  # pragma: nocover

@@ -1392,401 +1392,732 @@
 def 
-lint_jobs_from_directory(compiled_job_directory: str,%0A                             config: ConfigParser) -%3E bool:%0A    %22%22%22Load jobs from a directory and run linters against each one.%22%22%22%0A    filtered_config = ConfigParser()%0A    filtered_config.read_dict(config)%0A    for section in filtered_config.sections():%0A        if section != 'job_linter':%0A            filtered_config.remove_section(section
+_filter_config(config: ConfigParser) -%3E ConfigParser:%0A    %22%22%22%0A    Return a ConfigParser with only the job_linter section of the one passed.%0A%0A    This creates a new ConfigParser and removes sections from that, so the one%0A    passed in remains unmodified.%0A    %22%22%22%0A    filtered_config = ConfigParser()%0A    filtered_config.read_dict(config)%0A    for section in filtered_config.sections():%0A        if section != 'job_linter':%0A            filtered_config.remove_section(section)%0A    return filtered_config%0A%0A%0Adef lint_jobs_from_directory(compiled_job_directory: str,%0A                             config: ConfigParser) -%3E bool:%0A    %22%22%22Load jobs from a directory and run linters against each one.%22%22%22%0A    config = _filter_config(config
 )%0A  
@@ -2327,48 +2327,9 @@
 th),
-%0A                              filtered_
+ 
 conf

Refactor config filtering to a separate function