def run_flake8(config, filenames): """Linter for checking flake8 results. Parameters ---------- config : dict Dictionary that contains the configuration for the linter filenames : list A list of filenames to check Returns ------- messages : list The list of messages generated by the external linter. """ # get flake8 version command = ['flake8', '--version'] version_info = run_command(command, verbose=False)[0] print('USING : {0}'.format(version_info)) messages = [] if len(filenames) > 0: command = ['flake8'] + filenames if config['config'] is not None: command += ['--config={0}'.format(config['config'])] output = run_command(command, has_failed=_has_failed)[0] if len(output) > 0: for line in output.splitlines(): words = line.split(':') messages.append( Message(words[0], int(words[1]), int(words[2]), words[3].strip())) return messages
def linter_pylint(linter_config, files_lines): """Linter for checking pylint results. Parameters ---------- linter_config : dict Dictionary that contains the configuration for the linter files_lines : dict Dictionary of filename to the set of line numbers (that have been modified). See `run_diff` function in `carboardlinter`. Returns ------- messages : list The list of messages generated by the external linter. """ config = DEFAULT_CONFIG.copy() config.update(linter_config) # get Pylint version command = [ 'pylint', '--version', '--rcfile={0}'.format(config['pylintrc']) ] version_info = ''.join( run_command(command, verbose=False)[0].split('\n')[:2]) print('USING : {0}'.format(version_info)) # Get all relevant filenames filenames = [ filename for filename in files_lines if matches_filefilter(filename, config['filefilter']) ] def has_failed(returncode, _stdout, _stderr): """Determine if pylint ran correctly.""" return not 0 <= returncode < 32 messages = [] if len(filenames) > 0: command = ['pylint'] + filenames command += [ '--rcfile={0}'.format(config['pylintrc']), '--jobs=2', '--output-format=json' ] output = run_command(command, has_failed=has_failed)[0] if len(output) > 0: for plmap in json.loads(output): charno = plmap['column'] if charno == 0: charno = None messages.append( Message( plmap['path'], plmap['line'], charno, '{0} {1}'.format(plmap['symbol'], plmap['message']))) return messages
def linter_cppcheck(linter_config, files_lines): """Linter for cppcheck. Parameters ---------- linter_config : dict Dictionary that contains the configuration for the linter Not supported files_lines : dict Dictionary of filename to the set of line numbers (that have been modified) See `run_diff` function in `carboardlinter`. Returns ------- messages : list The list of messages generated by the external linter. """ config = DEFAULT_CONFIG.copy() config.update(linter_config) # Get version print('USING VERSION : {0}'.format( run_command(['cppcheck', '--version'], verbose=False)[0].strip())) # Get the relevant filenames filenames = [ filename for filename in files_lines if matches_filefilter(filename, config['filefilter']) ] messages = [] if len(filenames) > 0: # Call Cppcheck command = (['cppcheck'] + filenames + [ '-q', '--enable=all', '--language=c++', '--std=c++11', '--xml', '--suppress=missingIncludeSystem', '--suppress=unusedFunction' ]) xml_str = run_command(command)[1] etree = ElementTree.fromstring(xml_str) # Parse the output of Cppcheck into standard return values for error in etree: if 'file' not in error.attrib: continue # key = '{:<15} {:<40} {:<30}' % (error.attrib['severity'], # error.attrib['file'], # error.attrib['id']) text = '{} {} {}'.format(error.attrib['severity'], error.attrib['id'], error.attrib['msg']) messages.append( Message(error.attrib['file'], int(error.attrib['line']), None, text)) return messages
def linter_pydocstyle(linter_config, files_lines): """Linter for checking pydocstyle results. Parameters ---------- linter_config : dict Dictionary that contains the configuration for the linter files_lines : dict Dictionary of filename to the set of line numbers (that have been modified). See `run_diff` function in `carboardlinter`. Returns ------- messages : list The list of messages generated by the external linter. """ config = DEFAULT_CONFIG.copy() config.update(linter_config) # get pydocstyle version command = ['pydocstyle', '--version'] version_info = run_command(command, verbose=False)[0] print('USING : {0}'.format(version_info)) # Get all relevant filenames filenames = [ filename for filename in files_lines if matches_filefilter(filename, config['filefilter']) ] def has_failed(returncode, _stdout, _stderr): """Determine if pydocstyle ran correctly.""" return not 0 <= returncode < 2 messages = [] if len(filenames) > 0: command = ['pydocstyle'] + filenames command += ['--config={0}'.format(config['config'])] output = run_command(command, has_failed=has_failed)[0] lines = output.split('\n')[:-1] while len(lines) > 0: if 'WARNING: ' in lines[0]: lines.pop(0) else: words = lines.pop(0).split() filename, lineno = words[0].split(':') code, description = lines.pop(0).split(':', 1) code = code.strip() description = description.strip() messages.append( Message(filename, int(lineno), None, '%s %s' % (code, description))) return messages
def linter_doxygen(linter_config, files_lines): """Linter using doxygen to find undocumented cpp. Parameters ---------- linter_config : dict Dictionary that contains the configuration for the linter Not supported files_lines : dict Dictionary of filename to the set of line numbers (that have been modified) See `run_diff` function in `carboardlinter`. Returns ------- messages : list The list of messages generated by the external linter. """ config = DEFAULT_CONFIG.copy() config.update(linter_config) command = ['doxygen', '--version'] print('USING : doxygen', run_command(command, verbose=False)[0].strip()) # Get the relevant filenames filenames = [ filename for filename in files_lines if matches_filefilter(filename, config['filefilter']) ] messages = [] if len(filenames) > 0: # Call doxygen in the doc subdirectory, mute output because it only confuses command = ['doxygen', '-'] stdin = DOXYGEN_CONFIG.format(' '.join(filenames)) print('STDIN :') print(stdin) output = run_command(command, cwd=config['root'], stdin=stdin)[1] # Parse the file doxygen_warnings prefix = os.getcwd() + '/' lines = output.split('\n') for line in lines: if line.startswith('~~WARN~~'): location, description = line[9:].split(None, 1) filename, lineno = location.split(':')[:2] if filename.startswith(prefix): filename = filename[len(prefix):] message = Message(filename, int(lineno), None, description) messages.append(message) return messages
def linter_pycodestyle(linter_config, files_lines): """Linter for checking pycodestyle results. Parameters ---------- linter_config : dict Dictionary that contains the configuration for the linter files_lines : dict Dictionary of filename to the set of line numbers (that have been modified). See `run_diff` function in `carboardlinter`. Returns ------- messages : list The list of messages generated by the external linter. """ config = DEFAULT_CONFIG.copy() config.update(linter_config) # get pycodestyle version command = ['pycodestyle', '--version'] version_info = run_command(command, verbose=False)[0] print('USING : {0}'.format(version_info)) # Get all relevant filenames filenames = [ filename for filename in files_lines if matches_filefilter(filename, config['filefilter']) ] def has_failed(returncode, _stdout, _stderr): """Determine if pycodestyle ran correctly.""" return not 0 <= returncode < 2 messages = [] if len(filenames) > 0: command = ['pycodestyle'] + filenames command += ['--config={0}'.format(config['config'])] output = run_command(command, has_failed=has_failed)[0] if len(output) > 0: for line in output.splitlines(): words = line.split(':') messages.append( Message(words[0], int(words[1]), int(words[2]), words[3].strip())) return messages
def run_pydocstyle(config, filenames): """Linter for checking pydocstyle results. Parameters ---------- config : dict Dictionary that contains the configuration for the linter filenames : list A list of filenames to check Returns ------- messages : list The list of messages generated by the external linter. """ # get pydocstyle version command = ['pydocstyle', '--version'] version_info = run_command(command, verbose=False)[0] print('USING : {0}'.format(version_info)) def has_failed(returncode, _stdout, _stderr): """Determine if pydocstyle ran correctly.""" return not 0 <= returncode < 2 messages = [] if len(filenames) > 0: command = ['pydocstyle'] + filenames if config['config'] is not None: command += ['--config={0}'.format(config['config'])] output = run_command(command, has_failed=has_failed)[0] lines = output.split('\n')[:-1] while len(lines) > 0: if 'WARNING: ' in lines[0]: lines.pop(0) else: words = lines.pop(0).split() filename, lineno = words[0].split(':') code, description = lines.pop(0).split(':', 1) code = code.strip() description = description.strip() messages.append(Message( filename, int(lineno), None, '%s %s' % (code, description))) return messages
def run_cppcheck(_config, filenames): """Linter for cppcheck. Parameters ---------- config : dict Dictionary that contains the configuration for the linter Not supported filenames : list A list of filenames to check Returns ------- messages : list The list of messages generated by the external linter. """ # Get version print('USING VERSION : {0}'.format( run_command(['cppcheck', '--version'], verbose=False)[0].strip())) messages = [] if len(filenames) > 0: # Call Cppcheck command = (['cppcheck'] + filenames + ['-q', '--enable=all', '--language=c++', '--std=c++11', '--xml', '--suppress=missingIncludeSystem', '--suppress=unusedFunction']) xml_str = run_command(command)[1] etree = ElementTree.fromstring(xml_str) # Parse the output of Cppcheck into standard return values for error in etree: if 'file' not in error.attrib: continue # key = '{:<15} {:<40} {:<30}' % (error.attrib['severity'], # error.attrib['file'], # error.attrib['id']) text = '{} {} {}'.format( error.attrib['severity'], error.attrib['id'], error.attrib['msg']) lineno = int(error.attrib['line']) if lineno == 0: lineno = None messages.append(Message(error.attrib['file'], lineno, None, text)) return messages
def run_pylint(config, filenames): """Linter for checking pylint results. Parameters ---------- config : dict Dictionary that contains the configuration for the linter filenames : list A list of filenames to check Returns ------- messages : list The list of messages generated by the external linter. """ # get Pylint version command = ['pylint', '--version'] version_info = ''.join( run_command(command, verbose=False)[0].split('\n')[:2]) print('USING : {0}'.format(version_info)) def has_failed(returncode, _stdout, _stderr): """Determine if pylint ran correctly.""" return not 0 <= returncode < 32 messages = [] if len(filenames) > 0: command = ['pylint'] + filenames command += ['--jobs=2', '--output-format=json'] if config['config'] is not None: command += ['--rcfile={0}'.format(config['config'])] output = run_command(command, has_failed=has_failed)[0] if len(output) > 0: for plmap in json.loads(output): charno = plmap['column'] if charno in [0, -1]: charno = None messages.append( Message( plmap['path'], plmap['line'], charno, '{0} {1}'.format(plmap['symbol'], plmap['message']))) return messages
def run_doxygen(_config, filenames): """Linter using doxygen to find undocumented cpp. Parameters ---------- config : dict Dictionary that contains the configuration for the linter Not supported filenames : list A list of filenames to check Returns ------- messages : list The list of messages generated by the external linter. """ command = ['doxygen', '--version'] print('USING : doxygen', run_command(command, verbose=False)[0].strip()) messages = [] if len(filenames) > 0: # Call doxygen in the doc subdirectory, mute output because it only confuses command = ['doxygen', '-'] stdin = DOXYGEN_CONFIG.format(' '.join(filenames)) print('STDIN :') print(stdin) output = run_command(command, stdin=stdin)[1] # Parse the file doxygen_warnings prefix = os.getcwd() + '/' lines = output.split('\n') for line in lines: if line.startswith('~~WARN~~'): location, description = line[9:].split(None, 1) filename, lineno = location.split(':')[:2] if filename.startswith(prefix): filename = filename[len(prefix):] message = Message(filename, int(lineno), None, description) messages.append(message) return messages
def run_yamllint(config, filenames): """Linter for checking yamllint results. Parameters ---------- config : dict Dictionary that contains the configuration for the linter filenames : list A list of filenames to check Returns ------- messages : list The list of messages generated by the external linter. """ # get yamllint version command = ['yamllint', '--version'] version_info = run_command(command, verbose=False)[0] print('USING : {0}'.format(version_info)) def has_failed(returncode, _stdout, _stderr): """Determine if yamllint ran correctly.""" return not 0 <= returncode < 2 messages = [] if len(filenames) > 0: command = ['yamllint', '-f', 'parsable'] + filenames if config['config'] is not None: command += ['-c', config['config']] output = run_command(command, has_failed=has_failed)[0] if len(output) > 0: for line in output.splitlines(): words = line.split(':') messages.append( Message(words[0], int(words[1]), int(words[2]), words[3].strip())) return messages
def linter_cpplint(linter_config, files_lines): """Linter for cpplint. Parameters ---------- linter_config : dict Dictionary that contains the configuration for the linter Not supported files_lines : dict Dictionary of filename to the set of line numbers (that have been modified) See `run_diff` function in `carboardlinter`. Returns ------- messages : list The list of messages generated by the external linter. """ config = DEFAULT_CONFIG.copy() config.update(linter_config) # Get the relevant filenames filenames = [filename for filename in files_lines if matches_filefilter(filename, config['filefilter'])] messages = [] if len(filenames) > 0: # Call cpplint command = ([config['script'], '--linelength=100', '--filter=-runtime/int'] + filenames) output = run_command(command, has_failed=has_failed)[1] # Parse the output of cpplint into standard return values for line in output.split('\n')[:-1]: words = line.split() if len(words) == 0 or words[0].count(':') != 2: continue filename, lineno = words[0].split(':')[:2] description = ' '.join(words[1:-2]) tag = words[-2] priority = words[-1] messages.append(Message(filename, int(lineno), None, '%s %s %s' % ( priority, tag, description))) return messages
def run_cpplint(config, filenames): """Linter for cpplint. Parameters ---------- config : dict Dictionary that contains the configuration for the linter Not supported filenames : list A list of filenames to check Returns ------- messages : list The list of messages generated by the external linter. """ messages = [] if len(filenames) > 0: # Call cpplint command = ([config['script'], '--linelength=100', '--filter=-runtime/int'] + filenames) output = run_command(command, has_failed=_has_failed)[1] # Parse the output of cpplint into standard return values for line in output.split('\n')[:-1]: words = line.split() if len(words) == 0 or words[0].count(':') != 2: continue filename, lineno = words[0].split(':')[:2] description = ' '.join(words[1:-2]) tag = words[-2] priority = words[-1] lineno = int(lineno) if lineno == 0: lineno = None messages.append(Message(filename, lineno, None, '%s %s %s' % ( priority, tag, description))) return messages
def test_run_command(): assert run_command(['echo', 'foo']) == (u'foo\n', u'') with assert_raises(RuntimeError): run_command(['ls', 'asfdsadsafdasdfasd'])