def main(argv, stdout=sys.stdout, stderr=sys.stderr):
<EOL>linesep = os.linesep<EOL>if sys.version_info[<NUM_LIT:0>] < <NUM_LIT:3>:<EOL><INDENT>if stdout == sys.stdout:<EOL><INDENT>stdout = codecs.getwriter("<STR_LIT:utf-8>")(stdout)<EOL><DEDENT>if stderr == sys.stderr:<EOL><INDENT>stderr = codecs.getwriter("<STR_LIT:utf-8>")(stderr)<EOL><DEDENT>linesep = unicode(os.linesep)  <EOL><DEDENT>arguments = docopt.docopt(<EOL>__doc__, argv=argv[<NUM_LIT:1>:], version='<STR_LIT>' % __VERSION__)<EOL>json_output = arguments['<STR_LIT>']<EOL>vcs, repository_root = get_vcs_root()<EOL>if vcs is None:<EOL><INDENT>stderr.write('<STR_LIT>' + linesep)<EOL>return <NUM_LIT><EOL><DEDENT>commit = None<EOL>if arguments['<STR_LIT>']:<EOL><INDENT>commit = vcs.last_commit()<EOL><DEDENT>if arguments['<STR_LIT>']:<EOL><INDENT>invalid_filenames = find_invalid_filenames(arguments['<STR_LIT>'],<EOL>repository_root)<EOL>if invalid_filenames:<EOL><INDENT>invalid_filenames.append(('<STR_LIT>', '<STR_LIT>'))<EOL>stderr.write(<EOL>linesep.join(invalid[<NUM_LIT:1>] for invalid in invalid_filenames))<EOL>return <NUM_LIT:2><EOL><DEDENT>changed_files = vcs.modified_files(<EOL>repository_root,<EOL>tracked_only=arguments['<STR_LIT>'],<EOL>commit=commit)<EOL>modified_files = {}<EOL>for filename in arguments['<STR_LIT>']:<EOL><INDENT>normalized_filename = os.path.abspath(filename)<EOL>modified_files[normalized_filename] = changed_files.get(<EOL>normalized_filename)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>modified_files = vcs.modified_files(<EOL>repository_root,<EOL>tracked_only=arguments['<STR_LIT>'],<EOL>commit=commit)<EOL><DEDENT>linter_not_found = False<EOL>files_with_problems = <NUM_LIT:0><EOL>gitlint_config = get_config(repository_root)<EOL>json_result = {}<EOL>with futures.ThreadPoolExecutor(max_workers=multiprocessing.cpu_count())as executor:<EOL><INDENT>processfile = functools.partial(process_file, vcs, commit,<EOL>arguments['<STR_LIT>'], gitlint_config)<EOL>for filename, result in executor.map(<EOL>processfile, [(filename, modified_files[filename])<EOL>for filename in sorted(modified_files.keys())]):<EOL><INDENT>rel_filename = os.path.relpath(filename)<EOL>if not json_output:<EOL><INDENT>stdout.write('<STR_LIT>' % (termcolor.colored(<EOL>rel_filename, attrs=('<STR_LIT>', )), linesep))<EOL><DEDENT>output_lines = []<EOL>if result.get('<STR_LIT:error>'):<EOL><INDENT>output_lines.extend('<STR_LIT>' % (ERROR, reason)<EOL>for reason in result.get('<STR_LIT:error>'))<EOL>linter_not_found = True<EOL><DEDENT>if result.get('<STR_LIT>'):<EOL><INDENT>output_lines.extend('<STR_LIT>' % (SKIPPED, reason)<EOL>for reason in result.get('<STR_LIT>'))<EOL><DEDENT>if not result.get('<STR_LIT>', []):<EOL><INDENT>if not output_lines:<EOL><INDENT>output_lines.append(OK)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>files_with_problems += <NUM_LIT:1><EOL>for data in result['<STR_LIT>']:<EOL><INDENT>formatted_message = format_comment(data)<EOL>output_lines.append(formatted_message)<EOL>data['<STR_LIT>'] = formatted_message<EOL><DEDENT><DEDENT>if json_output:<EOL><INDENT>json_result[filename] = result<EOL><DEDENT>else:<EOL><INDENT>output = linesep.join(output_lines)<EOL>stdout.write(output)<EOL>stdout.write(linesep + linesep)<EOL><DEDENT><DEDENT><DEDENT>if json_output:<EOL><INDENT>stdout.write(<EOL>json.dumps(json_result,<EOL>ensure_ascii=False).encode('<STR_LIT:utf-8>').decode('<STR_LIT:utf-8>'))<EOL><DEDENT>if files_with_problems > <NUM_LIT:0>:<EOL><INDENT>return <NUM_LIT:1><EOL><DEDENT>if linter_not_found:<EOL><INDENT>return <NUM_LIT:4><EOL><DEDENT>return <NUM_LIT:0><EOL>
Main gitlint routine. To be called from scripts.