def print_filename(self, line_indent, state_symbol, state_ansi_color_code, message=None): """Prints the formatted filename on console. Args: line_indent: a integer indicating the line indent. state_symbol: a string of the state symbol put before the filename. state_ansi_color_code: a string of ANSI color code that will be applied to print the state symbol. message: an optional string that will be printed on the next line of the filename. """ current_file_has_error = (state_symbol == self.error) if self.processed_files == 0 or \ (self.previous_file_has_error and not current_file_has_error): print('') print(utility.get_ansi_code('FOREGROUND_RESET') + ' ' * line_indent + state_ansi_color_code + state_symbol + ' ' + utility.get_ansi_code('FOREGROUND_RESET') + utility.get_ansi_code('STYLE_NORMAL') + self.filename) if message: print(' ' * line_indent + utility.get_ansi_code('FOREGROUND_CYAN') + utility.get_ansi_code('STYLE_DIM') + ' // {:}'.format(message) + utility.get_ansi_code('STYLE_RESET_ALL')) self.processed_files += 1 self.previous_file_has_error = current_file_has_error
def print_filename(self, line_indent, state_symbol, state_ansi_color_code, message=None): """Prints the formatted filename on console. Args: line_indent: a integer indicating the line indent. state_symbol: a string of the state symbol put before the filename. state_ansi_color_code: a string of ANSI color code that will be applied to print the state symbol. message: an optional string that will be printed on the next line of the filename. """ current_file_has_error = (state_symbol == self.error) if self.processed_files == 0 or \ (self.previous_file_has_error and not current_file_has_error): print('') print( utility.get_ansi_code('FOREGROUND_RESET') + ' ' * line_indent + state_ansi_color_code + state_symbol + ' ' + utility.get_ansi_code('FOREGROUND_RESET') + utility.get_ansi_code('STYLE_NORMAL') + self.filename) if message: print(' ' * line_indent + utility.get_ansi_code('FOREGROUND_CYAN') + utility.get_ansi_code('STYLE_DIM') + ' // {:}'.format(message) + utility.get_ansi_code('STYLE_RESET_ALL')) self.processed_files += 1 self.previous_file_has_error = current_file_has_error
def write(self, output): """Formats the output message.""" # Restores the output stream temporarily so interpreter trackback # can be displayed as usually. sys.stderr = self.original_stderr line_indent = _FILE_STREAM_INITIAL_LINE_INDENT # Exits when done processing a file. if output.startswith('Done'): if self.error_counts == 0: self.print_filename(line_indent, self.ok, utility.get_ansi_code('FOREGROUND_GREEN')) sys.stderr = self return # Exits when receiving a warning message. for beginning_match, seprator in _WARNING_PATTERNS: if output.startswith(beginning_match): message = output.split(seprator, 1)[1].strip() message = message[0].lower() + message[1:] self.print_filename(line_indent, self.warning, utility.get_ansi_code('FOREGROUND_YELLOW'), message) sys.stderr = self return # Found an error. Increments the `error_counts`. self.error_counts += 1 self.total_error_counts += 1 if self.error_counts == 1: if self.processed_files > 0: print('') self.print_filename(line_indent, self.error, utility.get_ansi_code('FOREGROUND_RED')) line_number, description = output.split(':', 2)[1:] line_indent += 2 print(' ' * line_indent + utility.get_ansi_code('FOREGROUND_YELLOW') + '#' + line_number + utility.get_ansi_code('FOREGROUND_WHITE') + ': ' + utility.get_ansi_code('STYLE_DIM') + description.strip() + utility.get_ansi_code('STYLE_RESET_ALL')) # Reindirects the output stream to this instance so we can keep # intercepting the messages from Google's cpplint. sys.stderr = self
def execute_from_command_line(): """Executes the cpplint client with added features. This function is the entry point of the cclint client. """ start_time = time.time() update_cpplint_usage() options, cpplint_filenames = parse_arguments() # Determines the list of filenames to process. if options['expanddir'] == 'no': filenames = cpplint_filenames else: filenames = list() recursive = (options['expanddir'] == 'recursive') expand_directory = utility.expand_directory for filename in cpplint_filenames: if os.path.isfile(filename): filenames.append(filename) elif os.path.isdir(filename): expanded_filenames = expand_directory( filename, recursive=recursive, excludedirs=options['excludedirs']) filenames.extend(expanded_filenames) # Prints the cclint's header message. print( utility.get_ansi_code('FOREGROUND_CYAN') + utility.get_ansi_code('STYLE_BRIGHT') + '\n=== CCLINT ===' + utility.get_ansi_code('FOREGROUND_RESET') + utility.get_ansi_code('STYLE_RESET_ALL')) # Initializes the stream for intercepting and formatting cpplint's output. stream = file_stream.FileStream(sys.stderr, codecs.getreader('utf8'), codecs.getwriter('utf8'), 'replace') cpplint_state = cpplint._CppLintState() # pylint: disable=protected-access cpplint_state.ResetErrorCounts() for filename in filenames: stream.begin(filename) cpplint.ProcessFile(filename, cpplint_state.verbose_level) stream.end() # Prints the succeeded messages. print( utility.get_ansi_code('FOREGROUND_GREEN') + utility.get_ansi_code('STYLE_BRIGHT') + '\n** LINT SUCCEEDED **' + utility.get_ansi_code('STYLE_RESET_ALL') + utility.get_ansi_code('FOREGROUND_WHITE') + utility.get_ansi_code('STYLE_DIM') + ' ({0:.3f} seconds)\n\n'.format(time.time() - start_time) + utility.get_ansi_code('FOREGROUND_RESET') + utility.get_ansi_code('STYLE_RESET_ALL')) # Shows how many errors are found. total_error_counts = stream.total_error_counts if total_error_counts: print( utility.get_ansi_code('FOREGROUND_RED') + 'Total errors found: {0:d}'.format(total_error_counts)) print(utility.get_ansi_code('FOREGROUND_RESET') + 'Done.\n') sys.exit(total_error_counts > 0)
def execute_from_command_line(): """Executes the cpplint client with added features. This function is the entry point of the cclint client. """ start_time = time.time() update_cpplint_usage() options, cpplint_filenames = parse_arguments() exclude_paths = [os.path.abspath(f) for f in cpplint._excludes or []] # Determines the list of filenames to process. if options['expanddir'] == 'no': filenames = cpplint_filenames else: filenames = list() recursive = (options['expanddir'] == 'recursive') expand_directory = utility.expand_directory for filename in cpplint_filenames: if os.path.isfile(filename): filenames.append(filename) elif os.path.isdir(filename): expanded_filenames = expand_directory(filename, None, recursive, options['excludedirs'], exclude_paths) filenames.extend(expanded_filenames) # Prints the cclint's header message. print(utility.get_ansi_code('FOREGROUND_CYAN') + utility.get_ansi_code('STYLE_BRIGHT') + '\n=== CCLINT ===' + utility.get_ansi_code('FOREGROUND_RESET') + utility.get_ansi_code('STYLE_RESET_ALL')) # Initializes the stream for intercepting and formatting cpplint's output. stream = file_stream.FileStream(sys.stderr, codecs.getreader('utf8'), codecs.getwriter('utf8'), 'replace') cpplint_state = cpplint._CppLintState() # pylint: disable=protected-access cpplint_state.ResetErrorCounts() for filename in filenames: stream.begin(filename) cpplint.ProcessFile(filename, cpplint_state.verbose_level) stream.end() # Prints the succeeded messages. print(utility.get_ansi_code('FOREGROUND_GREEN') + utility.get_ansi_code('STYLE_BRIGHT') + '\n** LINT SUCCEEDED **' + utility.get_ansi_code('STYLE_RESET_ALL') + utility.get_ansi_code('FOREGROUND_WHITE') + utility.get_ansi_code('STYLE_DIM') + ' ({0:.3f} seconds)\n\n'.format(time.time() - start_time) + utility.get_ansi_code('FOREGROUND_RESET') + utility.get_ansi_code('STYLE_RESET_ALL')) # Shows how many errors are found. total_error_counts = stream.total_error_counts if total_error_counts: print(utility.get_ansi_code('FOREGROUND_RED') + 'Total errors found: {0:d}'.format(total_error_counts)) print(utility.get_ansi_code('FOREGROUND_RESET') + 'Done.\n') sys.exit(total_error_counts > 0)