<s> import itertools <EOL> import os <EOL> from pybuilder . utils import ( discover_modules , <EOL> discover_files_matching , <EOL> execute_command , <EOL> as_list , <EOL> read_file ) <EOL> def log_report ( logger , name , report_lines ) : <EOL> count_of_warnings = len ( report_lines ) <EOL> if count_of_warnings > <NUM_LIT:0> : <EOL> for report_line in report_lines : <EOL> logger . warn ( name + '<STR_LIT>' + report_line [ : - <NUM_LIT:1> ] ) <EOL> def discover_python_files ( directory ) : <EOL> return discover_files_matching ( directory , "<STR_LIT>" ) <EOL> def discover_affected_files ( include_test_sources , include_scripts , project ) : <EOL> source_dir = project . get_property ( "<STR_LIT>" ) <EOL> files = discover_python_files ( source_dir ) <EOL> if include_test_sources : <EOL> if project . get_property ( "<STR_LIT>" ) : <EOL> unittest_dir = project . get_property ( "<STR_LIT>" ) <EOL> files = itertools . chain ( files , discover_python_files ( unittest_dir ) ) <EOL> if project . get_property ( "<STR_LIT>" ) : <EOL> integrationtest_dir = project . get_property ( "<STR_LIT>" ) <EOL> files = itertools . chain ( files , discover_python_files ( integrationtest_dir ) ) <EOL> if include_scripts and project . get_property ( "<STR_LIT>" ) : <EOL> scripts_dir = project . get_property ( "<STR_LIT>" ) <EOL> files = itertools . chain ( files , <EOL> discover_files_matching ( scripts_dir , "<STR_LIT:*>" ) ) <EOL> return files <EOL> def discover_affected_dirs ( include_test_sources , include_scripts , project ) : <EOL> files = [ project . get_property ( "<STR_LIT>" ) ] <EOL> if include_test_sources : <EOL> if _if_property_set_and_dir_exists ( project . get_property ( "<STR_LIT>" ) ) : <EOL> files . append ( project . get_property ( "<STR_LIT>" ) ) <EOL> if _if_property_set_and_dir_exists ( project . get_property ( "<STR_LIT>" ) ) : <EOL> files . append ( project . get_property ( "<STR_LIT>" ) ) <EOL> if include_scripts and _if_property_set_and_dir_exists ( project . get_property ( "<STR_LIT>" ) ) : <EOL> files . append ( project . get_property ( "<STR_LIT>" ) ) <EOL> return files <EOL> def _if_property_set_and_dir_exists ( property_value ) : <EOL> return property_value and os . path . isdir ( property_value ) <EOL> def execute_tool_on_source_files ( project , name , command_and_arguments , logger = None , <EOL> include_test_sources = False , include_scripts = False , include_dirs_only = False ) : <EOL> if include_dirs_only : <EOL> files = discover_affected_dirs ( include_test_sources , include_scripts , project ) <EOL> else : <EOL> files = discover_affected_files ( include_test_sources , include_scripts , project ) <EOL> command = as_list ( command_and_arguments ) + [ f for f in files ] <EOL> report_file = project . expand_path ( "<STR_LIT>" . format ( name ) ) <EOL> execution_result = execute_command ( command , report_file ) , report_file <EOL> report_file = execution_result [ <NUM_LIT:1> ] <EOL> report_lines = read_file ( report_file ) <EOL> if project . get_property ( name + "<STR_LIT>" ) and logger : <EOL> log_report ( logger , name , report_lines ) <EOL> return execution_result <EOL> def execute_tool_on_modules ( project , name , command_and_arguments , extend_pythonpath = True ) : <EOL> source_dir = project . expand_path ( "<STR_LIT>" ) <EOL> modules = discover_modules ( source_dir ) <EOL> command = as_list ( command_and_arguments ) + modules <EOL> report_file = project . expand_path ( "<STR_LIT>" % name ) <EOL> env = os . environ <EOL> if extend_pythonpath : <EOL> env [ "<STR_LIT>" ] = source_dir <EOL> return execute_command ( command , report_file , env = env ) , report_file </s>
