def lint_python_file(repository, base_tree, pr_file): # TODO: Need to add logic for deleted files but need to see what the status for that is pr_lines = repository.get_blob_as_lines(pr_file.sha) pr_report = PullRequestReport() pr_checker = pep8.Checker( filename=pr_file.filename, lines=pr_lines, report=pr_report, ) pr_checker.check_all() if pr_file.status == 'modified': base_lines = repository.get_blob_as_lines( repository.get_file_hash_from_tree(base_tree, pr_file.filename).sha ) base_report = PullRequestReport() base_checker = pep8.Checker( filename=pr_file.filename, lines=base_lines, report=base_report, ) base_checker.check_all() remove_preexisting_violations( base_lines, base_report, pr_lines, pr_report, ) return pr_report
def test_pep8_conformance(self): """ test pep8 """ stl = pep8.StyleGuide(quiet=True) f1 = "models/rectangle.py" f2 = "tests/test_models/test_rectangle.py" fchecker = pep8.Checker(f1, show_source=True) f2check = pep8.Checker(f2, show_source=True) file_errors = fchecker.check_all() file_errors_2 = f2check.check_all()
def check_file(self, path): """ Check one regular file with pylint for py syntax errors. :param path: Path to a regular file. :return: False, if pylint found syntax problems, True, if pylint didn't find problems, or path is not a python module or script. """ inspector = PathInspector(path) if not inspector.is_python(): return True opt_obj = pep8.StyleGuide().options ignore_list = self.ignored_errors.split(',') + list(opt_obj.ignore) opt_obj.ignore = tuple(set(ignore_list)) runner = pep8.Checker(filename=path, options=opt_obj) status = runner.check_all() if status != 0: log.error('PEP8 check fail: %s', path) self.failed_paths.append(path) log.error('Trying to fix errors with autopep8') try: opt_obj = autopep8.parse_args( [path, '--ignore', self.ignored_errors, '--in-place']) autopep8.fix_file(path, opt_obj) except Exception, details: log.error('Not able to fix errors: %s', details)
def test_pep8_conformance(self): """Test that models/base_model.py conforms to PEP8.""" for path in ['models/base_model.py', 'tests/test_models/test_base_model.py']: with self.subTest(path=path): errors = pycodestyle.Checker(path).check_all() self.assertEqual(errors, 0)
def check_text(text, temp_dir, logger=None): # pep8 옵션 추가 pep8_option_list = [] with open('pep8-option.txt', 'r') as f: for val in f.read().splitlines(): pep8_option_list.append(val) code_file, code_filename = tempfile.mkstemp(dir=temp_dir) with open(code_filename, 'w') as code_file: code_file.write(text.encode('utf8')) pep8style = pep8.StyleGuide(parse_argv=False, config_file=False) options = pep8style.options temp_outfile = StringIO.StringIO() sys.stdout = temp_outfile checker = pep8.Checker(code_filename, options=options) checker.check_all() sys.stdout = sys.__stdout__ result = temp_outfile.buflist[:] temp_outfile.close() code_file.close() os.remove(code_filename) fullResultList = pep8parser(result) fullResultList.sort(key=lambda x: (int(x['line']), int(x["place"]))) if (pep8_option_list is not None): for val in pep8_option_list: for idx, result in enumerate(fullResultList): if (result.get('code') == val[1:]) and (result.get('type') == val[0]): fullResultList.pop(idx) if logger: logger.debug(result) return fullResultList
def _check_pep8(self, path): if not install_flg['pep8']: return [{'line': 0, 'msg': 'no install pep8'}] return print(path) checker = pep8.Checker(path) t = tempfile.TemporaryFile() sys.stdout = t checker.check_all() t.seek(0) s = t.read() sys.stdout.close() sys.stdout = sys.__stdout__ res = [] arr = s.split('\n') for e in arr: if e.strip() == '': continue cols = e.split(':') res.append({ 'line': int(cols[1]), 'msg': ':'.join(cols[3:]) }) return res
def _pep8_annotations(text, ignore=None, max_line_length=None): import pep8 class _Pep8AnnotationReport(pep8.BaseReport): def __init__(self, options): super().__init__(options) self.annotations = [] def error(self, line_number, offset, text, check): # If super doesn't return code, this one is ignored if not super().error(line_number, offset, text, check): return annotation = _AnalyzerAnnotation(self.line_offset + line_number, text, _Source.pep8, Style.warning) self.annotations.append(annotation) # pep8 requires you to include \n at the end of lines lines = text.splitlines(True) style_guide = pep8.StyleGuide(reporter=_Pep8AnnotationReport, ) options = style_guide.options if ignore: options.ignore = tuple(ignore) else: options.ignore = tuple() if max_line_length: options.max_line_length = max_line_length checker = pep8.Checker(None, lines, options, None) checker.check_all() return checker.report.annotations
def test_pep8(self): fchecker = pep8.Checker('aerospike.py', show_source=True, ignore='E221') file_errors = fchecker.check_all() self.assertEqual(file_errors, 0, "Found code style errors (and warnings).")
def test_pep8(self): # NOTE(jecarey): Add tests marked as off_by_default to enable testing turn_on = set(['H106']) if self.options.select: turn_on.update(self.options.select) self.options.select = tuple(turn_on) report = pep8.BaseReport(self.options) checker = pep8.Checker(filename=self.filename, lines=self.lines, options=self.options, report=report) checker.check_all() self.addDetail('doctest', content.text_content(self.raw)) if self.code == 'Okay': self.assertThat( len(report.counters), matchers.Not( matchers.GreaterThan(len(self.options.benchmark_keys))), "incorrectly found %s" % ', '.join([ key for key in report.counters if key not in self.options.benchmark_keys ])) else: self.addDetail( 'reason', content.text_content("Failed to trigger rule %s" % self.code)) self.assertIn(self.code, report.counters)
def get_pep8_errors(filename, **pep8_options): """Execute pep8 via python method calls.""" class QuietReport(pep8.BaseReport): """Version of checker that does not print.""" def __init__(self, options): super(QuietReport, self).__init__(options) self.__full_error_results = [] def error(self, line_number, offset, text, _): """Collect errors.""" code = super(QuietReport, self).error(line_number, offset, text, _) if code: self.__full_error_results.append({ 'id': code, 'line': line_number, 'column': offset + 1, 'info': text }) def full_error_results(self): """Return error results in detail. Results are in the form of a list of dictionaries. Each dictionary contains 'id', 'line', 'column', and 'info'. """ return self.__full_error_results checker = pep8.Checker(filename, reporter=QuietReport, **pep8_options) checker.check_all() return checker.report.full_error_results()
def check_text(text, temp_dir, logger=None): """ check text for pep8 requirements """ #prepare code code_file, code_filename = tempfile.mkstemp(dir=temp_dir) with open(code_filename, 'w') as code_file: code_file.write(text.encode('utf8')) #initialize pep8 checker pep8style = pep8.StyleGuide(parse_argv=False, config_file=False) options = pep8style.options #redirect print and get result temp_outfile = StringIO.StringIO() sys.stdout = temp_outfile checker = pep8.Checker(code_filename, options=options) checker.check_all() sys.stdout = sys.__stdout__ result = temp_outfile.buflist[:] #clear all temp_outfile.close() code_file.close() os.remove(code_filename) fullResultList = pep8parser(result) fullResultList.sort(key=lambda x: (int(x['line']), int(x["place"]))) if logger: logger.debug(result) return fullResultList
def check_pep8(filename, **kwargs): """Perform static analysis on the given file. :param filename: path of file to check. :type filename: str :param ignore: codes to ignore, e.g. ``('E111', 'E123')`` :type ignore: `list` :param select: codes to explicitly select. :type select: `list` :param pyflakes: run the pyflakes checks too (default ``True``) :type pyflakes: bool :return: errors :rtype: `list` .. seealso:: :py:class:`pycodestyle.Checker` """ options = { "ignore": kwargs.get("ignore"), "select": kwargs.get("select"), } if not _registered_pyflakes_check and kwargs.get("pyflakes", True): _register_pyflakes_check() checker = pep8.Checker(filename, reporter=_Report, **options) checker.check_all() errors = [] for error in sorted(checker.report.errors, key=lambda x: x[0]): errors.append("{0}:{1}: {3}".format(*error)) return errors
def find_pep8_errors(cls, filename=None, lines=None): try: sys.stdout = cStringIO.StringIO() config = {} # Ignore long lines on test files, as the test names can get long # when following our test naming standards. if cls._is_test(filename): config['ignore'] = ['E501'] checker = pep8.Checker(filename=filename, lines=lines, **config) checker.check_all() output = sys.stdout.getvalue() finally: sys.stdout = sys.__stdout__ errors = [] for line in output.split('\n'): parts = line.split(' ', 2) if len(parts) == 3: location, error, desc = parts line_no = location.split(':')[1] errors.append('%s ln:%s %s' % (error, line_no, desc)) return errors
def test_pep8(self): """Test pep8""" for path in ['models/base_model.py', 'tests/test_models/test_base_model.py']: with self.subTest(path=path): errors = pycodestyle.Checker(path).check_all() self.assertEqual(errors, 0)
def test_generate_files(self): """Test generate_files returns a tuple.""" self.prototype.setup_env(self.api_version) details = self.prototype.generate_files() self.assertIsInstance(details, list) # namedtuples in tuple for file_details in details: self.assertIsInstance(file_details, tuple) self.assertIsInstance(file_details.filename, str) self.assertIsInstance(file_details.filecontent, str) name, contents = file_details if name.endswith(".py"): # We have a "coding utf-8" line in there, we need to encode contents = contents.encode("utf-8") ast.parse(contents) if pep8: checker = pep8.Checker( name, contents.splitlines(True)) res = checker.check_all() self.assertFalse( res, "Python file %s has pep8 errors:\n" "%s\n%s" % (name, checker.report.messages, repr(contents)) ) elif name.endswith(".xml"): # TODO validate valid odoo xml lxml.etree.fromstring(contents)
def test_pep8(self): pep8.process_options() pep8.options.repeat = True pep8_errors = [] pep8_warnings = [] for fname, text in get_source_file_contents(): def report_error(line_number, offset, text, check): code = text[:4] if code in self.pep8_ignore: code = 'W' + code[1:] text = code + text[4:] print "%s:%s: %s" % (fname, line_number, text) summary = (fname, line_number, offset, text, check) if code[0] == 'W': pep8_warnings.append(summary) else: pep8_errors.append(summary) lines = text.splitlines(True) checker = pep8.Checker(fname, lines) checker.report_error = report_error checker.check_all() if len(pep8_errors) > 0: d = {} for (fname, line_no, offset, text, check) in pep8_errors: d.setdefault(fname, []).append(line_no - 1) self.fail( self._format_message( d, 'There were %d PEP8 errors:' % len(pep8_errors)))
def pep8_check(code, filename, ignore=None, max_line_length=pep8.MAX_LINE_LENGTH): messages = [] _lines = code.split('\n') if _lines: class SublimeLinterReport(pep8.BaseReport): def error(self, line_number, offset, text, check): """Report an error, according to options.""" code = text[:4] message = text[5:] if self._ignore_code(code): return if code in self.counters: self.counters[code] += 1 else: self.counters[code] = 1 self.messages[code] = message # Don't care about expected errors or warnings if code in self.expected: return self.file_errors += 1 self.total_errors += 1 if code.startswith('E'): messages.append( Pep8Error(filename, line_number, offset, code, message)) else: messages.append( Pep8Warning(filename, line_number, offset, code, message)) return code _ignore = ignore + pep8.DEFAULT_IGNORE.split(',') options = pep8.StyleGuide(reporter=SublimeLinterReport, ignore=_ignore).options options.max_line_length = max_line_length good_lines = [l + '\n' for l in _lines] good_lines[-1] = good_lines[-1].rstrip('\n') if not good_lines[-1]: good_lines = good_lines[:-1] try: pep8.Checker(filename, good_lines, options=options).check_all() except Exception as e: print("An exception occured when running pep8 checker: %s" % e) return messages
def run_check(self, code): pep8.register_check(self.get_checker()) lines = textwrap.dedent(code).strip().splitlines(True) checker = pep8.Checker(lines=lines) checker.check_all() checker.report._deferred_print.sort() return checker.report._deferred_print
def pep8_test_testbase(self): """ Test pep8 for Unitesst base module """ path = 'tests/test_models/test_base_model.py' with self.subTest(path=path): Error = pcs.Checker(path).check_all() self.assertEqual(Error, 0)
def pep8_test_base(self): """ Test pep8 for base module """ path = "models/base_model.py" with self.subTest(path=path): Error = pcs.Checker(path).check_all() self.assertEqual(Error, 0)
def check_pep8(srcdir): print(">>> Running pep8...") clean = True pep8.process_options(['']) for pyfile in findpy(srcdir): if pep8.Checker(pyfile).check_all() != 0: clean = False return clean
def _lint_pep8(file_to_lint, lint_output): reporter = PEP8TerseReporter() checker = pep8.Checker(filename=file_to_lint, report=reporter) num_problems = checker.check_all() if num_problems > 0: lint_output._warnings_present = True lint_output.output += reporter.output return lint_output
def _run_check(self, code, checker, filename=None): pep8.register_check(checker) lines = textwrap.dedent(code).strip().splitlines(True) checker = pep8.Checker(filename=filename, lines=lines) checker.check_all() checker.report._deferred_print.sort() return checker.report._deferred_print
def test_local_check(self): flake8_style = engine.get_style_guide(parse_argv=False, ignore='F') report = pep8.BaseReport(flake8_style.options) line = ["#this-is-the-test-phrase"] checker = pep8.Checker(lines=line, options=flake8_style.options, report=report) checker.check_all() self.assertIn("L100", report.counters)
def pep8_error_count(path): # process_options initializes some data structures and MUST be called before each Checker().check_all() pep8.process_options([ 'pep8', '--ignore=E202,E221,E222,E241,E301,E302,E401,E501,E701,W391,W601,W602', '--show-source', 'dummy_path' ]) error_count = pep8.Checker(path).check_all() return error_count
def test_pep8(self): """Should not show any erros""" for file in glob.glob('./' + '/**/*.py', recursive=True): if '/app/env/' not in file and '/migrations/' not in file: print(file) fchecker = pep8.Checker(file, show_source=True) file_errors = fchecker.check_all() self.assertEqual(file_errors, 0)
def test_pep8(self): """[Test pep8] """ for path in [ 'models/base_model.py', 'tests/test_models/test_base_model.py' ]: with self.subTest(path=path): err = pep8.Checker(path).check_all() self.assertEqual(err, 0)
def run_check(self, code): pep8.register_check(checks.check_oslo_namespace_imports) lines = textwrap.dedent(code).strip().splitlines(True) checker = pep8.Checker(lines=lines) checker.check_all() checker.report._deferred_print.sort() return checker.report._deferred_print
def check_pep8_errors(code): """Runs PEP8 validation for given chunck of code using an in-memory file. Returns the pep8 Report object with the result of the validation. http://pep8.readthedocs.org/en/latest/api.html#pep8.BaseReport """ mem_file = StringIO(code) fchecker = pep8.Checker(lines=mem_file.readlines(), show_source=True) fchecker.check_all() return fchecker.report
def get_filecontent(sub_data, form_field, files, token): """ Etsii halutun tiedoston ja tallentaa sen tiedot listaan sanakirjamuodossa. :param sub_data: (list) Opiskelijan palautuksen olennaiset tiedot dicteinä. :param form_field: (dict) Tehtävän (yaml-tiedosto) palautuskentän tiedot. :param files: (dict) Palautettujen tiedostojen nimet, urlit jne. :return: None """ for file in files: if file["param_name"] == form_field["key"]: resp = requests.get(file["url"], headers={"Authorization": f"Token {token}"}) resp.encoding = "utf-8" title = None text = None style = None try: lexer = get_lexer_for_filename(file["filename"]) code = highlight(resp.text, lexer, HtmlFormatter(linenos="inline")) except ClassNotFound: code = None # Run PEP8 style check for Python file if code and file["filename"].endswith(".py"): lines = resp.text.rstrip("\n").split("\n") lines = [line + "\n" for line in lines] style_checker = pep8.Checker(lines=lines, show_source=True) # check_all -metodi printtaa tulokset stdouttiin, # joten luodaan bufferi, johon saadaan tulokset talteen buffer = io.StringIO() sys.stdout = buffer style_checker.check_all() # Palautetaan alkuperäinen stdout ja # haetaan tarkastuksen tulokset bufferista sys.stdout = sys.__stdout__ style = buffer.getvalue() elif code is None: title = form_field["title"] text = "Follow the link to download file" sub_data.append({ "title": title, "url": file["url"], "text": text, "code": code, "style": style }) return