def test_pylint_add(self): """ Pylint tests for API function add """ ## skip tests if newmod command wasn't successful if self.f_newmod: raise unittest.SkipTest("setUp for API function 'add' failed") module_dir = path.join(self.test_dir, 'gr-howto') ## The check for object instantiation ## test_obj = ModToolAdd() test_obj.dir = module_dir # missing blocktype, lang, blockname self.assertRaises(ModToolException, test_obj.run) test_obj.info['blocktype'] = 'general' # missing lang, blockname self.assertRaises(ModToolException, test_obj.run) test_obj.info['lang'] = 'python' test_obj.info['blockname'] = 'mul_ff' test_obj.add_py_qa = True test_obj.run() self.assertTrue(path.exists(path.join(module_dir, 'python', 'mul_ff.py'))) self.assertTrue(path.exists(path.join(module_dir, 'python', 'qa_mul_ff.py'))) ## pylint tests ## python_dir = path.join(module_dir, 'python') py_module = path.join(python_dir, 'mul_ff.py') (pylint_stdout, pylint_stderr) = py_run(py_module+' --errors-only --disable=E0602', return_std=True) print(pylint_stdout.getvalue(), end='') py_module = path.join(python_dir, 'qa_mul_ff.py') (pylint_stdout, pylint_stderr) = py_run(py_module+' --errors-only', return_std=True) print(pylint_stdout.getvalue(), end='')
def test_pylint_add(self): """ Pylint tests for API function add """ ## skip tests if newmod command wasn't successful if self.f_newmod: raise unittest.SkipTest("setUp for API function 'add' failed") module_dir = path.join(self.test_dir, 'gr-howto') ## The check for object instantiation ## test_obj = ModToolAdd() test_obj.dir = module_dir # missing blocktype, lang, blockname self.assertRaises(ModToolException, test_obj.run) test_obj.info['blocktype'] = 'general' # missing lang, blockname self.assertRaises(ModToolException, test_obj.run) test_obj.info['lang'] = 'python' test_obj.info['blockname'] = 'mul_ff' test_obj.add_py_qa = True test_obj.run() self.assertTrue( path.exists(path.join(module_dir, 'python', 'mul_ff.py'))) self.assertTrue( path.exists(path.join(module_dir, 'python', 'qa_mul_ff.py'))) ## pylint tests ## python_dir = path.join(module_dir, 'python') py_module = path.join(python_dir, 'mul_ff.py') (pylint_stdout, pylint_stderr) = py_run(py_module + ' --errors-only --disable=E0602', return_std=True) print(pylint_stdout.getvalue(), end='') py_module = path.join(python_dir, 'qa_mul_ff.py') (pylint_stdout, pylint_stderr) = py_run(py_module + ' --errors-only', return_std=True) print(pylint_stdout.getvalue(), end='')
def lint(): '''Run linter evaluation of files committed on current feature branch''' diff_list = git.diff_name_only() py_diff_list = [pyfile for pyfile in diff_list if pyfile.endswith('.py')] print ui.info('Pylint evaluation of code:') for pyfile in py_diff_list: print '\n%s\n' % pyfile epylint.py_run(pyfile)
def run_tests(self): from pylint import epylint as lint from io import StringIO lint.py_run(' '.join(packages)) # Run nose ensuring that argv simulates running nosetests directly import nose nose.run_exit(argv=[ 'nosetests', '-m', '^test', '--with-coverage', '--cover-package', 'hostess', '--cover-min-percentage', '100' ])
def test_pylint_newmod(self): """ Pylint tests for API function newmod """ module_dir = path.join(self.test_dir, 'gr-test') ## pylint tests ## python_dir = path.join(module_dir, 'python') py_module = path.join(python_dir, 'mul_ff.py') (pylint_stdout, pylint_stderr) = py_run(py_module+' --errors-only --disable=E0602', return_std=True) print(pylint_stdout.getvalue(), end='') py_module = path.join(python_dir, 'qa_mul_ff.py') (pylint_stdout, pylint_stderr) = py_run(py_module+' --errors-only', return_std=True) print(pylint_stdout.getvalue(), end='')
def test_newmod(self): """ Tests for the API function newmod """ ## Tests for proper exceptions ## test_dict = {} test_dict['directory'] = self.test_dir # module name not specified self.assertRaises(ModToolException, ModToolNewModule(**test_dict).run) test_dict['module_name'] = 'howto' # expected module_name as a string instead of dict self.assertRaises(TypeError, ModToolNewModule(test_dict).run) # directory already exists # will not be raised if the command in setup failed self.assertRaises(ModToolException, ModToolNewModule(**test_dict).run) ## Some tests for checking the created directory, sub-directories and files ## test_dict['module_name'] = 'test' ModToolNewModule(**test_dict).run() module_dir = path.join(self.test_dir, 'gr-test') self.assertTrue(path.isdir(module_dir)) self.assertTrue(path.isdir(path.join(module_dir, 'lib'))) self.assertTrue(path.isdir(path.join(module_dir, 'python'))) self.assertTrue(path.isdir(path.join(module_dir, 'include'))) self.assertTrue(path.isdir(path.join(module_dir, 'docs'))) self.assertTrue(path.isdir(path.join(module_dir, 'cmake'))) self.assertTrue(path.isdir(path.join(module_dir, 'swig'))) self.assertTrue(path.exists(path.join(module_dir, 'CMakeLists.txt'))) ## pylint tests ## python_dir = path.join(module_dir, 'python') py_module = path.join(python_dir, 'build_utils.py') (pylint_stdout, pylint_stderr) = py_run(py_module + ' --errors-only', return_std=True) print(pylint_stdout.getvalue(), end='') py_module = path.join(python_dir, 'build_utils_codes.py') (pylint_stdout, pylint_stderr) = py_run(py_module + ' --errors-only', return_std=True) print(pylint_stdout.getvalue(), end='') ## The check for object instantiation ## test_obj = ModToolNewModule() # module name not specified self.assertRaises(ModToolException, test_obj.run) test_obj.info['modname'] = 'howto' test_obj.directory = self.test_dir # directory already exists self.assertRaises(ModToolException, test_obj.run) test_obj.info['modname'] = 'test1' test_obj.run() self.assertTrue(path.isdir(self.test_dir + '/gr-test1')) self.assertTrue(path.isdir(self.test_dir + '/gr-test1/lib')) self.assertTrue(path.exists(self.test_dir + '/gr-test1/CMakeLists.txt'))
def lint(full=False): from pylint import epylint sources = [Config.root, Config.meka, Config.skmultilearn] if full: fullReport = 'y' else: fullReport = 'n' config = "--rcfile ./utils/pylint.config --msg-template=\"{C}:{msg_id}:{line:3d},{column:2d}:{msg}({symbol})\" -r %s %s" for dir in sources: print 'lint %s' %dir epylint.py_run(config % (fullReport, dir), script='pylint')
def test_pylint_newmod(self): """ Pylint tests for API function newmod """ module_dir = path.join(self.test_dir, 'gr-test') ## pylint tests ## python_dir = path.join(module_dir, 'python') py_module = path.join(python_dir, 'mul_ff.py') (pylint_stdout, pylint_stderr) = py_run(py_module + ' --errors-only --disable=E0602', return_std=True) print(pylint_stdout.getvalue(), end='') py_module = path.join(python_dir, 'qa_mul_ff.py') (pylint_stdout, pylint_stderr) = py_run(py_module + ' --errors-only', return_std=True) print(pylint_stdout.getvalue(), end='')
def test_newmod(self): """ Tests for the API function newmod """ ## Tests for proper exceptions ## test_dict = {} test_dict['directory'] = self.test_dir # module name not specified self.assertRaises(ModToolException, ModToolNewModule(**test_dict).run) test_dict['module_name'] = 'howto' # expected module_name as a string instead of dict self.assertRaises(TypeError, ModToolNewModule(test_dict).run) # directory already exists # will not be raised if the command in setup failed self.assertRaises(ModToolException, ModToolNewModule(**test_dict).run) ## Some tests for checking the created directory, sub-directories and files ## test_dict['module_name'] = 'test' ModToolNewModule(**test_dict).run() module_dir = path.join(self.test_dir, 'gr-test') self.assertTrue(path.isdir(module_dir)) self.assertTrue(path.isdir(path.join(module_dir, 'lib'))) self.assertTrue(path.isdir(path.join(module_dir, 'python'))) self.assertTrue(path.isdir(path.join(module_dir, 'include'))) self.assertTrue(path.isdir(path.join(module_dir, 'docs'))) self.assertTrue(path.isdir(path.join(module_dir, 'cmake'))) self.assertTrue(path.isdir(path.join(module_dir, 'swig'))) self.assertTrue(path.exists(path.join(module_dir, 'CMakeLists.txt'))) ## pylint tests ## python_dir = path.join(module_dir, 'python') py_module = path.join(python_dir, 'build_utils.py') (pylint_stdout, pylint_stderr) = py_run(py_module+' --errors-only', return_std=True) print(pylint_stdout.getvalue(), end='') py_module = path.join(python_dir, 'build_utils_codes.py') (pylint_stdout, pylint_stderr) = py_run(py_module+' --errors-only', return_std=True) print(pylint_stdout.getvalue(), end='') ## The check for object instantiation ## test_obj = ModToolNewModule() # module name not specified self.assertRaises(ModToolException, test_obj.run) test_obj.info['modname'] = 'howto' test_obj.directory = self.test_dir # directory already exists self.assertRaises(ModToolException, test_obj.run) test_obj.info['modname'] = 'test1' test_obj.run() self.assertTrue(path.isdir(self.test_dir+'/gr-test1')) self.assertTrue(path.isdir(self.test_dir+'/gr-test1/lib')) self.assertTrue(path.exists(self.test_dir+'/gr-test1/CMakeLists.txt'))
def test_relative_beyond_top_level_two() -> None: output, errors = lint.py_run( f"{os.path.join(REGR_DATA, 'beyond_top_two')} -d all -e relative-beyond-top-level", return_std=True, ) top_level_function = os.path.join( REGR_DATA, "beyond_top_two/namespace_package/top_level_function.py") output2, errors2 = lint.py_run( f"{top_level_function} -d all -e relative-beyond-top-level", return_std=True, ) assert len(output.readlines()) == len(output2.readlines()) assert errors.readlines() == errors2.readlines()
def test_pylint(self): """ Test for pylint errors """ # Test if this file is specified in the arguments, if not skip the test. in_argv = False for arg in sys.argv: if arg.endswith('test_pylint.py') or arg.endswith('test_pylint'): in_argv = True break if not in_argv: raise SkipTest('test_pylint.py not specified in arguments - skipping tests using pylint.') # GIVEN: Some checks to disable and enable, and the pylint script disabled_checks = 'import-error,no-member' enabled_checks = 'missing-format-argument-key,unused-format-string-argument,bad-format-string' if is_win() or 'arch' in platform.dist()[0].lower(): pylint_script = 'pylint' else: pylint_script = 'pylint3' # WHEN: Running pylint (pylint_stdout, pylint_stderr) = \ lint.py_run('openlp --errors-only --disable={disabled} --enable={enabled} ' '--reports=no --output-format=parseable'.format(disabled=disabled_checks, enabled=enabled_checks), return_std=True, script=pylint_script) stdout = pylint_stdout.read() stderr = pylint_stderr.read() filtered_stdout = self._filter_tolerated_errors(stdout) print(filtered_stdout) print(stderr) # THEN: The output should be empty self.assertTrue(filtered_stdout == '', 'PyLint should find no errors')
def validate_passes_linting(self, repo): """ Clones the repo and runs pylint on the Python files""" if not repo["name"].startswith("Adafruit_CircuitPython"): return [] ignored_py_files = ["setup.py", "conf.py"] desination_type = TemporaryDirectory if self.keep_repos: desination_type = pathlib.Path("repos").absolute with desination_type() as tempdir: repo_dir = pathlib.Path(tempdir) / repo["name"] try: if not repo_dir.exists(): git.clone("--depth=1", repo["git_url"], repo_dir) except sh.ErrorReturnCode as err: self.output_file_data.append( f"Failed to clone repo for linting: {repo['full_name']}\n {err.stderr}" ) return [ERROR_OUTPUT_HANDLER] if self.keep_repos and (repo_dir / '.pylint-ok').exists(): return [] for file in repo_dir.rglob("*.py"): if file.name in ignored_py_files or str( file.parent).endswith("examples"): continue py_run_args = f"{file} --output-format=json" if (repo_dir / '.pylintrc').exists(): py_run_args += (f" --rcfile={str(repo_dir / '.pylintrc')}") logging.debug("Running pylint on %s", file) pylint_stdout, pylint_stderr = linter.py_run(py_run_args, return_std=True) if pylint_stderr.getvalue(): self.output_file_data.append( f"PyLint error ({repo['name']}): '{pylint_stderr.getvalue()}'" ) return [ERROR_OUTPUT_HANDLER] try: pylint_result = json.loads(pylint_stdout.getvalue()) except json.JSONDecodeError as json_err: self.output_file_data.append( f"PyLint output JSONDecodeError: {json_err.msg}") return [ERROR_OUTPUT_HANDLER] if pylint_result: return [ERROR_PYLINT_FAILED_LINTING] if self.keep_repos: with open(repo_dir / '.pylint-ok', 'w') as f: f.write(pylint_result) return []
def evaluate_pylint(text): """Create temp files for pylint parsing on user code :param text: user code :return: dictionary of pylint errors: { { "code":..., "error": ..., "message": ..., "line": ..., "error_info": ..., } ... } """ # Open temp file for specific session. # IF it doesn't exist (aka the key doesn't exist), create one with tempfile.NamedTemporaryFile(delete=False) as temp: file_name = temp.name for t in text: temp.write(t.encode("utf-8")) temp.flush() try: ARGS = " -r n --disable=R,C" (pylint_stdout, pylint_stderr) = lint.py_run(file_name + ARGS, return_std=True) except Exception as e: raise Exception(e) if pylint_stderr.getvalue(): raise Exception("Issue with pylint configuration") return format_errors(pylint_stdout.getvalue())
def test(self): out, err = lint.py_run( "%s -E --extension-pkg-whitelist=numpy --init-hook='import sys; sys.path=[%s]; sys.path.insert(0, \"%s\")'" % (script, ",".join(['"%s"' % p for p in sys.path ]), os.path.dirname(MODULE_BUILD)), return_std=True) out_lines = out.read().split('\n') err_lines = err.read().split('\n') out.close() err.close() for line in out_lines: mtch = _LINT_RE.match(line) if mtch is not None: line_no, type, info = mtch.group('line'), mtch.group( 'type'), mtch.group('info') self.assertEqual( type.find('syntax'), -1, "%s:%s - %s" % (os.path.basename(script), line_no, info)) self.assertEqual( type.find('undefined'), -1, "%s:%s - %s" % (os.path.basename(script), line_no, info)) # HACK to deal with some strangeness in lwa_cat_view.py if script.find('lwa_cat_view.py') == -1 or ( script.find('lwa_cat_view.py') != -1 and info.find('j2000_') == -1): self.assertEqual( type.find('no-member'), -1, "%s:%s - %s" % (os.path.basename(script), line_no, info))
def process_message(self, data): if data['file']: if data['file']['filetype'] == 'python': url = data['file']['url_private'] req = urllib2.Request( url, None, {"Authorization": "Bearer %s" % config['SLACK_TOKEN']}) response = urllib2.urlopen(req) text = response.read() with open('test.py', 'wb') as f: f.write(text) (pylint_stdout, pylint_stderr) = lint.py_run('test.py', return_std=True) a = pylint_stdout.getvalue() if a: self.outputs.append([data['channel'], a]) else: self.outputs.append( [data['channel'], 'Your code is perfect!']) # self.outputs.append([data['channel'], pylint_stderr.getvalue()]) os.remove('test.py') else: self.outputs.append([ data['channel'], "I'm a Python poet but {} is not my expertise!".format( data['file']['filetype']) ])
def test_epylint_does_not_block_on_huge_files(self): path = join(REGR_DATA, "huge.py") out, err = epylint.py_run(path, return_std=True) self.assertTrue(hasattr(out, "read")) self.assertTrue(hasattr(err, "read")) output = out.read(10) self.assertIsInstance(output, str)
def cmagic(self, line, cell): with open(self.temp_file, 'w') as fp: fp.write(cell) stdout, stderr = lint.py_run(self.temp_file, return_std=True) os.remove(self.temp_file) print stdout.getvalue(), stderr.getvalue()
def lint_py(ctx, targets=None): args = "--rcfile={} --reports=y".format(get_repo_path(PYLINT_RC)) files = get_matching(get_repo_path(), patterns=[r".*\.py$"], exclude_patterns=LINT_SKIP_PATTERNS) stdout, _ = epylint.py_run("{target} {args}".format(target=" ".join(files), args=args), return_std=True) try: msgs = json.load(stdout) for msg in msgs: if msg['type'].lower() == 'error': print(colored(json.dumps(msg, sort_keys=True, indent=4), "red")) else: print( colored(json.dumps(msg, sort_keys=True, indent=4), "green")) else: print(colored("Nice! No lint errors!", "green")) except JSONDecodeError: print( colored("Whoopsie Daisy! There was an issue linting your code!", "red"))
def diff_lint(diff, repo_root): changed_files = defaultdict(set) patches = unidiff.PatchSet(diff) for patch in patches: # remove a, since patch.source_file == 'b/project/main.py' path = patch.target_file[1:] if path.endswith('.py'): for hunk in patch: end = hunk.target_start + hunk.target_length changed_files[path].update(range(hunk.target_start, end)) for file_, range_ in changed_files.items(): start = time.time() cmd_tpl = '{file_name} --output-format=json' (pylint_stdout, __) = lint.py_run(cmd_tpl.format(file_name=repo_root + file_), return_std=True) for line in json.loads(pylint_stdout.buf): message_tpl = '{type_} -> {path}:{line} {message}.' if line['line'] in range_: print message_tpl.format(type_=line['type'], path=line['path'], line=line['line'], message=line['message']) seconds = time.time() - start print "Time spent: {seconds}s.\n".format(seconds=seconds)
def _test_script(self, script): self.assertTrue(os.path.exists(script)) out, err = lint.py_run( "%s -E --extension-pkg-whitelist=numpy,scipy.fftpack --init-hook='import sys; sys.path=[%s]; sys.path.insert(0, \"%s\")'" % (script, ",".join(['"%s"' % p for p in sys.path ]), os.path.dirname(BIFROST_DIR)), return_std=True) out_lines = out.read().split('\n') err_lines = err.read().split('\n') out.close() err.close() for line in out_lines: #if line.find("Module 'numpy") != -1: # continue #if line.find("module 'scipy.fftpack") != -1: # continue mtch = _LINT_RE.match(line) if mtch is not None: line_no, type, info = mtch.group('line'), mtch.group( 'type'), mtch.group('info') self.assertEqual( type, None, "%s:%s - %s" % (os.path.basename(script), line_no, info))
def get_lint(code, divid, sid): try: fn = os.path.join('/tmp', sid + divid + '.py') tfile = open(fn, 'w') tfile.write(code) tfile.close() except Exception as e: print "failed to open/write file ", sid, divid print str(e) pyl_opts = ' --msg-template="{C}: {symbol}: {msg_id}:{line:3d},{column}: {obj}: {msg}" ' pyl_opts += ' --reports=n ' pyl_opts += ' --rcfile='+os.path.join(os.getcwd(), "applications/runestone/pylintrc") #pyl_opts += ' --rcfile=' + os.path.join(os.getcwd(), "../pylintrc") try: (pylint_stdout, pylint_stderr) = lint.py_run(fn + pyl_opts, True, script='pylint') except: print "lint failed" pylint_stdout = "" try: os.unlink(fn) except: pass return pylint_stdout
def generated_function(self): mkdir_p(os.path.join(PYLINT_OUT, root)) target_filename = os.path.join(root, name) output_filename = target_filename[:-len(PYTHON_EXTENSION)] + '.txt' output_filename = os.path.join(PYLINT_OUT, output_filename) # if the output file is newer than the original file if os.path.exists(output_filename) \ and os.path.isfile(output_filename) \ and os.path.getmtime(output_filename) > \ os.path.getmtime(target_filename): # assume that the file hasn't changed, # and so read the score. with open(output_filename, 'r') as output_file: full_output = output_file.read() # just gotta run the file. else: (pylint_stdout, _) = lint.py_run(target_filename + EXTRA_OPTIONS, return_std=True) full_output = pylint_stdout.getvalue() with open(output_filename, 'w') as output_file: output_file.write(full_output) regex = r"Your code has been rated at (-?[\d\.]+)/10" matches = re.findall(regex, full_output) self.assertLessEqual(len(matches), 1) # sometimes, no matches are made if pylint is parsing an empty file if matches: score = float(matches[0]) self.assertGreaterEqual(score, threshold_score)
def parse(cls, path): from apistub import ApiView pkg_name = os.path.split(path)[-1] rcfile_path = os.path.join(ApiView.get_root_path(), "pylintrc") logging.debug(f"APIView root path: {ApiView.get_root_path()}") (pylint_stdout, pylint_stderr) = epylint.py_run( f"{path} -f json --recursive=y --rcfile {rcfile_path}", return_std=True) stderr_str = pylint_stderr.read() # strip put stray, non-json lines from stdout stdout_lines = [ x for x in pylint_stdout.readlines() if not x.startswith("Exception") ] try: json_items = json.loads("".join(stdout_lines)) plugin_failed = any( [x["symbol"] == "bad-plugin-value" for x in json_items]) if plugin_failed: logging.error( f"Unable to load pylint_guidelines_checker. Check that it is installed." ) except json.JSONDecodeError as err: logging.error(f"Error decoding pylint output:\n{stderr_str}") logging.error(f"Error content:\n{err}") logging.error(f"==STDOUT==\n{stdout_lines}") raise err cls.items = [ PylintError(pkg_name, **x) for x in json_items if x["message-id"][1:3] == PylintParser.AZURE_CHECKER_CODE ]
def check_pylint(self, repodir): """ Run pylint over any changed python files, checking only for errors. :param repodir str: directory containing repository. """ pyfiles = self._get_pyfiles() result = "" for codefile in pyfiles: filename = "{}/{}".format(repodir, codefile["filename"]) if not os.path.exists(filename): LOG.warning( "[+] Skipping pylint on '%s' as it appears removed", filename) continue LOG.warning("[+] Running pylint on: %s", filename) output = lint.py_run( "{} --persistent=n --disable=all --enable=E -E -r n --disable=import-error" .format(filename), return_std=True, ) pylint_stdout = output[0].read() pylint_stderr = output[1].read() LOG.debug("stdout: %s", pylint_stdout) LOG.debug("stderr: %s", pylint_stderr) if pylint_stdout: result += "Pylint report for {}:\n{}\n\n".format( filename, pylint_stdout) return result
def evaluate_lint(script): """ Lint one script """ std = lint.py_run(command_options=f'{script} {PYLINT_OPTIONS}', return_std=True) text = std[0].read() score = float(re.findall(r'rated at ([0-9.\-]*)/.*', text)[0]) return score
def lint_the_things(): (pylint_stdout, pylint_stderr) = lint.py_run(__file__, True) lines = [line for line in pylint_stdout.readlines() if not line.startswith('***')] print("PyLint Results") for line in lines: print("\t{0}".format(line)) return lines
def main(): # find python files filenames = [ y for x in os.walk('.') for y in glob(os.path.join(x[0], '*.py')) ] rc = 0 argv = [] for fn in sorted(filenames): if fn.find('migrations/') != -1: continue if fn.find('.env') != -1: continue print('Checking %s' % fn) argv.append(fn) # run with 8 parallel tasks argv.append('-j 8') argv.append('--rcfile contrib/pylintrc') (pylint_stdout, pylint_stderr) = epylint.py_run(' '.join(argv), return_std=True) stderr = pylint_stderr.read() stdout = pylint_stdout.read() if stderr or stdout: print(stderr, stdout) rc = 1 return rc
def test_relative_beyond_top_level_four() -> None: output, errors = lint.py_run( f"{os.path.join(REGR_DATA, 'beyond_top_four/module')} -d missing-docstring,unused-import", return_std=True, ) assert len(output.readlines()) == 5 assert errors.readlines() == []
def evaluate_pylint(filename): """Create temp files for pylint parsing on user code :param text: user code :return: dictionary of pylint errors: { { "code":..., "error": ..., "message": ..., "line": ..., "error_info": ..., } ... } """ # Open temp file for specific session. # IF it doesn't exist (aka the key doesn't exist), create one try: ARGS = " -r n --disable=R,C" (pylint_stdout, pylint_stderr) = lint.py_run(filename + ARGS, return_std=True) except Exception as e: raise Exception(e) if pylint_stderr.getvalue(): raise Exception("Issue with pylint configuration") return format_errors(pylint_stdout.getvalue())
def get_lint(code, divid, sid): try: fn = os.path.join('/tmp', sid + divid + '.py') tfile = open(fn, 'w') tfile.write(code) tfile.close() except: print "failed to open/write file ", sid, divid pyl_opts = ' --msg-template="{C}: {symbol}: {msg_id}:{line:3d},{column}: {obj}: {msg}" ' pyl_opts += ' --reports=n ' pyl_opts += ' --rcfile=' + os.path.join(os.getcwd(), "applications/runestone/pylintrc") #pyl_opts += ' --rcfile=' + os.path.join(os.getcwd(), "../pylintrc") try: (pylint_stdout, pylint_stderr) = lint.py_run(fn + pyl_opts, True, script='pylint') except: print "lint failed" pylint_stdout = "" try: os.unlink(fn) except: pass return pylint_stdout
def main(): options = get_arg_parser().parse_args() root_file_path = 'delinter/test/input/test_unused_imports.py' msg_template = r'{path}:{line}:[{msg_id}({symbol}),{obj}]{msg}' pylint_command = f"{root_file_path} --enable=W --disable=C,R,E,F --msg-template={msg_template} --score=n" out, _ = lint.py_run(pylint_command, return_std=True) result = "".join(out.readlines()).split('\n') result = [r.strip() for r in result if r.strip() and not r.strip(). startswith('************* Module ')] parsed_warnings = Delinter.parse_linter_warnings(result, options.msg_id) if os.path.isdir(root_file_path): from pathlib import Path files = Path(root_file_path).glob('**/*.py') else: files = [root_file_path] for file_path in files: with open(file_path) as f: source_code = "".join(f.readlines()) source_tree = cst.parse_module(source_code) wrapper = cst.MetadataWrapper(source_tree) local_warnings = [p for p in parsed_warnings if p.file_path == str(file_path)] fixed_module = wrapper.visit( SUPPORTED_LINTER_MAP[options.msg_id][1](local_warnings)) a_file_path = 'a/' + str(file_path) b_file_path = 'b/' + str(file_path) print("".join(difflib.unified_diff( source_code.splitlines(1), fixed_module.code.splitlines(1), fromfile=a_file_path, tofile=b_file_path )))
def test_epylint_does_not_block_on_huge_files(self): path = join(REGR_DATA, 'huge.py') out, err = epylint.py_run(path, return_std=True) self.assertTrue(hasattr(out, 'read')) self.assertTrue(hasattr(err, 'read')) output = out.read(10) self.assertIsInstance(output, str)
def define_error(filename): options = '--errors-only' # all messages will be shown pylint_stdout, pylint_stderr = epylint.py_run(filename + ' ' + options, return_std=True) #db_dict={} #Creating an Open Dictionary #Finding the keyword 'error' #keyword=re.compile('error') #obtaining the index numbers of string list containing the keywords #index=[] #Filtering process of pylint stdout explanation = pylint_stdout.getvalue() explanation = explanation.replace('(', '') explanation = explanation.replace(')', '') explanation = explanation.replace(':', ' ') explanation = explanation.replace(' ', ' ') explanation = explanation.replace(',', '') #Splitting the sentence into a list of string arrays s2 = explanation.split() s2 = explanation.split(filename) #Filtering Result List s2 = s2[1:] return s2
def test_pylint_check(self): """ Use pylint to check ID codes from pylint features List of codes: http://docs.pylint.org/features.html """ test_modules = ' '.join([settings.PROJECT_ROOT + mod for mod in\ ['/apps', '/academicspam']]) check_codes = ','.join(['W0401', 'W0611']) #'C0103' Names # Call pylint command = '--reports=n --disable=all --ignore=migrations\ --enable={checker} {modules}'.format( modules=test_modules, checker=check_codes ) # command = '--reports=n --ignore=migrations {modules}'.format( # modules=test_modules, # checker=check_codes # ) # Run pylint, get results, returns (pylint_sdout, pylint_sderr) lint_sdout = lint.py_run(command, return_std=True, script='pylint')[0] pylintlog = lint_sdout.read() self.assertTrue(pylintlog == '', 'PyLint Log not empty\n' + pylintlog)
def test_epylint_does_not_block_on_huge_files(): path = join(REGR_DATA, 'huge.py') out, err = epylint.py_run(path, return_std=True) assert hasattr(out, 'read') assert hasattr(err, 'read') output = out.read(10) assert isinstance(output, str)
def test_lint(): (out, err) = lint.py_run('macad2018demo', return_std="True") result = out.read() if not '10.00/10' in result.split(" "): print "Failed pylint!" print result assert False
def pylint_check(int_only, create_badge): try: import re import anybadge from pylint import epylint pylint_stdout, pylint_stderr = epylint.py_run("hyperglass", return_std=True) pylint_output = pylint_stdout.getvalue() pylint_score = re.search( r"Your code has been rated at (\d+\.\d+)\/10.*", pylint_output).group(1) if not pylint_score == "10.00": raise RuntimeError(f"Pylint score {pylint_score} not acceptable.") if create_badge: badge_file = os.path.join(working_directory, "pylint.svg") if os.path.exists(badge_file): os.remove(badge_file) ab_thresholds = {1: "red", 10: "green"} badge = anybadge.Badge("pylint", pylint_score, thresholds=ab_thresholds) badge.write_badge("pylint.svg") if not int_only: click.secho(f"Created Pylint badge for score: {pylint_score}", fg="blue", bold=True) if int_only: click.echo(pylint_score) except ImportError as error_exception: click.secho(f"Import error:\n{error_exception}", fg="red", bold=True)
def test_relative_beyond_top_level_three() -> None: output, errors = lint.py_run( f"{os.path.join(REGR_DATA, 'beyond_top_three/a.py')} -d all -e relative-beyond-top-level", return_std=True, ) assert len(output.readlines()) == 5 assert errors.readlines() == []
def main(stagedfiles): """Main method that is called on startup""" sys.stdout.write(stagedfiles) files = stagedfiles.split(" ") sys.stdout.write("STAGED FILES:") sys.stdout.write(str(files)) result = [] errors = [] for file in files: if not file.endswith(".py") or not os.path.exists( file) or file.find("__init__.py") >= 0: sys.stdout.write("\n Skipped file: %s" % file) continue (pylint_stdout, pylint_stderr) = lint.py_run(file, return_std=True) if str(pylint_stdout.getvalue()): sys.stdout.write('\n' + pylint_stdout.getvalue()) elif not str(pylint_stdout.getvalue()): sys.stdout.write("\nFile %s has no warning or errors!" % file) sys.stdout.write('\n') regex = re.compile(r"Your code has been rated at ([\d.]+)/10") result_regex = regex.findall(pylint_stdout.getvalue()) sys.stdout.write('\n') if len(result_regex) <= 0: errors.append(file) continue result.append(float(result_regex[0])) if len(result) == 0 and len(errors) == 0: sys.stdout.write("NO FILES TO CHECK FOUND exit..") sys.exit(0) if len(errors) > 0: sys.stdout.write("Please fix all errors to process") sys.exit(1) code_result = 0 for value in result: code_result += value code_result /= len(result) sys.stdout.write("Overall code rating: %f" % code_result) if any([(res <= PYLINT_PASS_THRESHOLD) for res in result]): sys.stdout.write("git: commit failed, Pylint tests failed\n") sys.stdout.write("Every file needs to have a greater or equal " "rating to %f \n" % PYLINT_PASS_THRESHOLD) sys.exit(1) sys.exit(0)
def test_pylint(self): """ Check if HaTeMiLe for Python is conformance with python standarts. """ for path in TestStyleguide.PATHS: stdout = epylint.py_run(path + ' --score=n', return_std=True)[0] self.assertFalse(stdout.getvalue())
def lint(self, path): stdout, stderr = epylint.py_run('%s --errors-only' % path, return_std=True) err = [] for line in [x.strip() for x in stdout] + [x.strip() for x in stderr]: if line == 'No config file found, using default configuration': continue if line: err.append(line) if len(err) > 0: self.fail('\n'.join(err))
def validate_functions_py(package_name): """Check that an integration's functions.py file is syntactically correct and that it contains necessary functions Raises: Exception if functions.py is an invalid Python program """ pylint_stdout, pylint_stderr = lint.py_run(os.path.join(package_name, 'functions.py'), True) errors = get_pylint_errors(pylint_stdout.readlines()) if errors: raise Exception("Syntax errors in functions.py: " + str(errors)) functions = imp.load_source('functions', os.path.join(package_name, 'functions.py')) if not hasattr(functions, 'get_dynamic_audience_conditions'): raise Exception("functions.py does not contain get_dynamic_audience_conditions function")
def lint(self, path): disabled = [ 'E1101', # prevents usages getters defined with @property being treated as methods ] stdout, stderr = epylint.py_run('%s --errors-only --disable=%s' % (path, ",".join(disabled)), return_std=True) err = [] for line in [x.strip() for x in stdout] + [x.strip() for x in stderr]: if line == 'No config file found, using default configuration': continue if line: err.append(line) if len(err) > 0: self.fail('\n'.join(err))
def process_python(self, path): """Process a python file.""" (pylint_stdout, pylint_stderr) = epylint.py_run(" ".join([str(path)] + self.pylint_opts), return_std=True) emap = {} print(pylint_stderr.read()) for line in pylint_stdout: sys.stderr.write(line) key = line.split(":")[-1].split("(")[0].strip() if key not in self.pylint_cats: continue if key not in emap: emap[key] = 1 else: emap[key] += 1 sys.stderr.write("\n") self.python_map[str(path)] = emap
def common_pylint(self): """ Run pylint on all modified python files""" # Find changed python files pych = {} for _item in self.items: if _item.changes: for pf in [ _ for _ in _item.changes.modified_files if _.path.endswith('.py') ]: pych[pf.path] = [] if not pych: raise unittest.SkipTest('Python changes must exist to run pylint') # Run pylint on changed files and populate pych with findings from pylint import epylint as lint for pf in pych.keys(): (pylo, _) = lint.py_run(pf, return_std=True) pych[pf] += [ line.strip() for line in pylo.readlines()[1:] ] # Store the changes in data store ds['pylint_new'] = set() for chglist in pych.values(): for chg in chglist: if chg: ds['pylint_new'].add(chg) # Show pylint issues logger.warn("\n----- Pylint issues in current test run -----") for _issue in ds['pylint_new']: logger.warn(" %s" % _issue) # Handle lint differences pylint_diff = set() if ds['pylint_old']: # Running post-merge test pylint_diff = ds['pylint_new'] - ds['pylint_old'] # Printing new issues logger.warn("\n----- New Pylint issues -----") for _issue in pylint_diff: logger.warn(" %s" % _issue) self.assertEquals(len(pylint_diff), 0, "Found new pylint issues: %s" % str(pylint_diff)) else: # Running pre-merge test ds['pylint_old'] = ds['pylint_new'] self.assertEquals(len(ds['pylint_new']), 0, "There are pylint issues")
def test_pylint_cloudant(self): """ Apply Pylint to Python-Cloudant Client Library """ # We only want the Global evaluation report (RP0004) # for the cloudant package. pkg = 'cloudant' disable = 'RP0001,RP0002,RP0003,RP0101,RP0401,RP0402,RP0701,RP0801' options = '{0} -d \"{1}\"'.format(pkg, disable) (out, _) = epylint.py_run(options, return_std=True, script='pylint') passed = False for line in out: if line.find('Your code has been rated at 10.00/10') == 0: # Found the 10.00/10 in the Global evaluation report passed = True break fail_msg = 'Pylint check failed. Run pylint cloudant for more details.' self.assertTrue(passed, fail_msg)
def visualize(): try: request_file = request.files['code_file'] # TODO: implement file-size checker, this one doesn't work in flask # if code.size > 1024*1024: # flash("file size limit exceeded :(") # return redirect("/") if request_file.filename != "": code = request_file.read() else: code = None except: code = None if code is None: code = request.form.get('code', None) if not code: flash("no code found") return redirect("/") data = dict() result = exec_script_str(code) if len(result)<=1: result = "your code doesn't seem too short to be interesting!" if type(result) == str: flash(result) return redirect("/") data['branches'], data['stats'] = result data['code'] = [None,] + code.split('\n') with tempfile.NamedTemporaryFile() as f: f.write(code) f.seek(0) out, err = epylint.py_run(f.name, True) report = out.read() raw_reports = report.split('\n')[1:] reports = dict() for report in raw_reports: regex = re.compile(r'^[^:]+:(\d+): \[([-\w]+),([^\]]*)] (.*)$',re.I) r = regex.match(report) if r is None: continue line, warning, function, message = r.groups() if warning in ("fixme",): continue line = int(line) reports[line] = { "type":warning, "function":function, "message":message } data['warnings'] = reports json_data = json.dumps(data) return render_template("visualizer.html", data=json_data)
# http://docs.pylint.org/run.html from pylint import epylint as lint lint.py_run('baseline_9\src\jac\cfm')
def test_pylint(self): (stdout, _) = lint.py_run("datacats", return_std=True) stdout_str = stdout.read().strip() self.failIf(stdout_str, stdout_str)
def pylint(): 'pylint' from pylint import epylint as lint lint.py_run('./app --disable=I,invalid-name,arguments-differ --output-format=text',script='pylint')
#encoding=utf-8 import commands from pylint import epylint if __name__ == '__main__': (pylint_stdout,pylint_stderr) = epylint.py_run('just_for_test.py',return_std=True) #(pylint_stdout,pylint_stderr) = epylint.py_run('test.py',return_std=True) print pylint_stdout.read() print pylint_stderr.read() #print epylint.py_run('test.py') #print epylint.py_run('just_for_test.py') #status,output = commands.getstatusoutput('pylint test.py') #print status #print output
def test_pylint(): # Fix the module load path for the test. here = path.split(path.abspath(__file__))[0] if not here: # if it fails use cwd instead here = path.abspath(os.getcwd()) golismero = path.join(here, "..") golismero = path.abspath(golismero) thirdparty_libs = path.join(golismero, "thirdparty_libs") pythonpath = list(sys.path) pythonpath.insert(0, thirdparty_libs) pythonpath.insert(0, golismero) os.environ['PYTHONPATH'] = path.pathsep.join(pythonpath) # False positives to filter out. try: with open(path.join(here, "test_pylint.txt"), "r") as fd: FALSE_POSITIVES = [ x.strip() for x in fd if x.strip() ] except IOError: FALSE_POSITIVES = [] try: # Run PyLint against the sources and save the log. print "Running PyLint..." with open("_tmp_pylint.log", "w") as log: from pylint import epylint as lint lint.py_run('golismero', False, log, log) # Clean up the log, filter out the false positives, and write the log to disk. print "Cleaning up the PyLint log..." if not golismero.endswith(path.sep): golismero += path.sep false_pos = [] with open("_tmp_pylint.log", "r") as log: with open("pylint.log", "w") as output: for line in log: line = line.strip() if not line: continue if line.startswith(golismero): line = line[ len(golismero) : ] try: p = line.find(":") q = line.find(":", p + 1) f = line[ : p ] d = line[ q : ] n = int( line[ p + 1 : q ] ) if os.sep != "/": f = f.replace(os.sep, "/") line = f + line[ p : ] found = False for false in FALSE_POSITIVES: if not false: continue fp = false.find(":") fq = false.find(":", fp + 1) ff = false[ : fp ] fd = false[ fq : ] fn = int( false[ fp + 1 : fq ] ) if f == ff and d == fd and (fn - 10) <= n <= (fn + 10): found = True false_pos.append( (ff, fn, fd) ) break if found: continue except Exception: pass output.write(line) output.write("\n") output.flush() # Update the false positives. if false_pos: false_pos.sort() with open(path.join(here, "test_pylint.txt"), "w") as out: for (ff, fn, fd) in false_pos: false = "%s:%d%s\n" % (ff, fn, fd) out.write(false) finally: # Delete the temporary file. try: os.unlink("_tmp_pylint.log") except: pass print "Done!"
def run_pylint(self): """Run pylint on file and return output.""" (pylint_stdout, pylint_stderr) = lint.py_run(self['Filename'], return_std=True, script='pylint') return pylint_stdout.read()
def test_add(self): """ Tests for the API function add """ ## skip tests if newmod command wasn't successful if self.f_newmod: raise unittest.SkipTest("setUp for API function 'add' failed") module_dir = path.join(self.test_dir, 'gr-howto') ## Tests for proper exceptions ## test_dict = {} test_dict['directory'] = module_dir # missing blockname, block_type, lang self.assertRaises(ModToolException, ModToolAdd(**test_dict).run) test_dict['blockname'] = 'add_ff' # missing arguments block_type, lang self.assertRaises(ModToolException, ModToolAdd(**test_dict).run) test_dict['block_type'] = 'general' # missing argument lang self.assertRaises(ModToolException, ModToolAdd(**test_dict).run) test_dict['lang'] = 'cxx' # incorrect language self.assertRaises(ModToolException, ModToolAdd(**test_dict).run) test_dict['lang'] = 'cpp' test_dict['add_cpp_qa'] = 'Wrong' # boolean is expected for add_cpp_qa self.assertRaises(ModToolException, ModToolAdd(**test_dict).run) test_dict['add_cpp_qa'] = True test_dict['block_type'] = 'generaleee' # incorrect block type self.assertRaises(ModToolException, ModToolAdd(**test_dict).run) test_dict['block_type'] = 'general' test_dict['skip_lib'] = 'fail' # boolean value is expected for skip_lib, fails in instantiation self.assertRaises(ModToolException, ModToolAdd(**test_dict).run) test_dict['skip_lib'] = True # missing relevant subdir self.assertRaises(ModToolException, ModToolAdd(**test_dict).run) ## Some tests for checking the created directory, sub-directories and files ## test_dict['skip_lib'] = False ModToolAdd(**test_dict).run() self.assertTrue(path.exists(path.join(module_dir, 'lib', 'qa_add_ff.cc'))) self.assertTrue(path.exists(path.join(module_dir, 'lib', 'add_ff_impl.cc'))) self.assertTrue(path.exists(path.join(module_dir, 'grc', 'howto_add_ff.block.yml'))) self.assertTrue(path.exists(path.join(module_dir, 'include', 'howto', 'add_ff.h'))) ## The check for object instantiation ## test_obj = ModToolAdd() test_obj.dir = module_dir # missing blocktype, lang, blockname self.assertRaises(ModToolException, test_obj.run) test_obj.info['blocktype'] = 'general' # missing lang, blockname self.assertRaises(ModToolException, test_obj.run) test_obj.info['lang'] = 'python' test_obj.info['blockname'] = 'mul_ff' test_obj.add_py_qa = True test_obj.run() self.assertTrue(path.exists(path.join(module_dir, 'python', 'mul_ff.py'))) self.assertTrue(path.exists(path.join(module_dir, 'python', 'qa_mul_ff.py'))) self.assertTrue(path.exists(path.join(module_dir, 'grc', 'howto_mul_ff.block.yml'))) ## pylint tests ## python_dir = path.join(module_dir, 'python') py_module = path.join(python_dir, 'mul_ff.py') (pylint_stdout, pylint_stderr) = py_run(py_module+' --errors-only --disable=E0602', return_std=True) print(pylint_stdout.getvalue(), end='') py_module = path.join(python_dir, 'qa_mul_ff.py') (pylint_stdout, pylint_stderr) = py_run(py_module+' --errors-only', return_std=True) print(pylint_stdout.getvalue(), end='')
def cb_lint(app_configs, **kwargs): (pylint_stdout, _) = lint.py_run("*", return_std=True) return construct_complaints(pylint_stdout)
def test_pylint(self): (stdout, _) = lint.py_run('kademlia', return_std=True) errors = stdout.read() if errors.strip(): raise LintError(errors)