Esempio n. 1
0
 def setUp(self):
     self.linter = PyLinter(reporter=TextReporter())
     self.linter.disable('I')
     self.linter.config.persistent = 0
     # register checkers
     checkers.initialize(self.linter)
     os.environ.pop('PYLINTRC', None)
Esempio n. 2
0
def review_pull_request(repository,
                        pull_request,
                        pylintrc,
                        assignees=None,
                        inline=False,
                        **credentials):
    """Creates inline comments on the given pull request with the
    errors given by pylint"""
    github = GithubPullReviewClient(repository, pull_request, **credentials)

    if assignees and github.get_assignee_name() not in assignees:
        return

    files = [f.filename for f in github.get_files()]
    files = [f for f in files if f.endswith(".py")]

    handler = GithubInlineWriter(github) if inline else GithubCommentWriter(
        github)

    args = PYLINT_ARGS

    if pylintrc is not None:
        # Do not append to the existing constant
        args = args + ["--rcfile=%s" % pylintrc]

    if not files:
        return

    lint.Run(args + files, reporter=TextReporter(handler), exit=False)
    handler.flush()
Esempio n. 3
0
def run_pylint(fich):
    "run pylint on the given file and return synthetic results (note, error, reorder, warning)"
    import re
    from pylint import lint
    from pylint.reporters.text import TextReporter
    #    pyl = lint.Run(args)
    pylint_output = WritableObject()
    lint.Run([fich] + ARGS, reporter=TextReporter(pylint_output), exit=False)
    ignored_error = ('Exter', 'E1103')  # 5 first char of ignored errors
    ignored_warn = ('none', )  # 5 first char of ignored warnings
    errors = 0
    warn = 0
    reorder = 0
    note = 0
    for l in pylint_output.read():
        if l.startswith('E0001'):
            errors = 10000  # indicates crash
            break
        elif l.startswith('E'):
            if l[0:5] not in ignored_error:
                errors += 1
        elif l.startswith('W'):
            if l[0:5] not in ignored_warn:
                warn += 1
        elif l.startswith('R') and l.split()[0] != 'Raw':
            reorder += 1
        elif l.startswith('Your code has been rated'):
            m = re.match('Your code has been rated at (-?\d+.\d.)/10', l)
            note = float(m.group(1))
    if errors == 10000:  # means crash
        note = -100
    return (note, errors, warn, reorder)
Esempio n. 4
0
        def test_pylint(self):
            """ PyLint conformance """

            class WritableObject(object):
                "dummy output stream for pylint"
                def __init__(self):
                    self.content = []

                def write(self, st):
                    "dummy write"
                    self.content.append(st)

                def read(self):
                    "dummy read"
                    wholetext = ''.join(self.content)
                    return re.findall(r"^[^I]:[\s\d]+,[\s\d]+:.*\n", wholetext, re.MULTILINE)

            from pylint import lint
            from pylint.reporters.text import TextReporter
            ARGS = ["-d I,C0301,R0901,R0902,R0903,R0904,R0913,R0915,W0141,W0142,W0232,W0613"]
            pylint_output = WritableObject()
            with capture() as (dummy_out, dummy_err):
                lint.Run([filename] + ARGS, reporter=TextReporter(pylint_output), exit=False)
            msg = pylint_output.read()
            self.assertEqual(len(msg), 0, "Pylint detected %d errors (and warnings) (first 10 shown):\n%s" % (len(msg), ''.join(msg[:10])))
Esempio n. 5
0
def test_pylint():
    """Test codebase for pylint errors."""
    files_to_check = get_relevant_files()
    log.info("{} changed files detected".format(len(files_to_check)))
    rcfile, reps = (os.path.join(CODEBASE, '.pylintrc'), files_to_check)

    pylint_args = [
        '--rcfile={}'.format(rcfile),
    ]
    log.info('applying pylint to repository {}'.format(reps))
    pylint_args += reps

    pylint_output = _WritableOutput()
    pylint_reporter = TextReporter(pylint_output)
    lint.Run(pylint_args, reporter=pylint_reporter, do_exit=False)

    pylint_outputs = pylint_output.content

    errors = []
    for output in pylint_outputs:
        if not any([i in output for i in IGNORE_LIST]):
            errors.append(output)
        if "Your code has been rated at" in output:
            print("\n" + output)

    if errors:
        raise AssertionError(
            '{} Pylint errors found. '
            'For quick resolution, consider running this test locally before you push. '
            'Scroll down for hyperlinks to errors.\n{}'.format(
                len(errors), '\n'.join(errors)))
Esempio n. 6
0
    def run(self):
        if self.distribution.install_requires:
            self.distribution.fetch_build_eggs(
                self.distribution.install_requires)

        if self.distribution.tests_require:
            self.distribution.fetch_build_eggs(self.distribution.tests_require)
        self.lint_output = open(self.pylint_output, 'w')
        stdout, sys.stdout = sys.stdout, self.lint_output
        stderr, sys.stdout = sys.stderr, self.lint_output

        packages = find_packages(
            exclude=["*tests", "*tests*", "tests*", "tests"])

        import os.path
        setup_directory = os.path.dirname(os.path.realpath(__file__))
        args = [
            "--rcfile=" + os.path.join(setup_directory, ".pylintrc"),
            "--msg-template='{path}:{line}: [{msg_id}({symbol}), {obj}] {msg}'"
        ]

        from pylint.lint import Run
        from pylint.reporters.text import TextReporter
        Run(packages + args, reporter=TextReporter(stdout), exit=False)
        sys.stdout = stdout
        sys.stderr = stderr
    def test_lint(self):
        """Process modules for pylint tests
        """
        txtreporter = TextReporter(self.pylint_output_file)
        for root, _, files in os.walk(self.dir_path):
            for name in files:
                if name.endswith('.py') and name not in IGNORE_LIST:
                    current_path = os.path.join(root, name)
                    lint_args = [
                        current_path,
                        "--rcfile={0}".format(self.pylint_rc_path)
                    ]
                    lint_run = lint.Run(lint_args,
                                        reporter=txtreporter,
                                        exit=False)

                    if lint_run.linter.stats['by_msg']:
                        rel_file_path = os.path.join(
                            os.path.relpath(root, _BASE_PATH), name)
                        self.failed_files.append(rel_file_path)

        if self.failed_files:
            file_names = ''
            for file in self.failed_files:
                file_names += file + '\n'
            self.fail('Lint tests failed on following files\n{0}\n'
                      'For more information check {1}.'.format(
                          file_names,
                          os.path.relpath(self.output_file_path, _BASE_PATH)))
Esempio n. 8
0
def _error_check(code, function_name='filter'):
    """
    Checks provided Python code for errors. Syntax errors are checked for with
    Pylint. Returns a list of errors from linting.
    """
    # Since Pylint can only be used on files and not strings directly, we must
    # save the code to a temporary file. The file will be deleted after we are
    # finished.
    (fd, filename) = tempfile.mkstemp()
    errors = []
    try:
        tmpfile = os.fdopen(fd, 'w')
        tmpfile.write(code)
        tmpfile.close()

        output = PylintOutput()
        # We disable refactoring (R), convention (C), and warning (W) related checks
        run = Run(["-r", "n", "--disable=R,C,W", filename],
                  reporter=TextReporter(output),
                  do_exit=False)

        for line in output.read():
            # Only return lines that have error messages (lines with colons are error messages)
            sep = line.find(':')
            if sep == -1:
                continue

            if should_keep_error_message(line, function_name):
                errors.append(line[sep + 1:])
    finally:
        os.remove(filename)
    return errors
Esempio n. 9
0
 def run_pylint(self, directory):
     pylint_rc = os.path.join(ROOT_PATH, 'core', 'controllers', 'tests',
                              'pylint.rc')
     pylint_args = [directory, '-E', '--rcfile=%s' % pylint_rc]
     pylint_output = WritableObject()
     lint.Run(pylint_args, reporter=TextReporter(pylint_output), exit=False)
     return pylint_output
Esempio n. 10
0
 def run(self):
     """run lint to analyst the script """
     args = ["-r", "n"]
     lint.Run([self.script] + args,
              reporter=TextReporter(pylint_output),
              exit=False)
     self.result = stringclean(pylint_output.read())
     return None
Esempio n. 11
0
def linter():
    linter = PyLinter(reporter=TextReporter())
    linter.disable('I')
    linter.config.persistent = 0
    # register checkers
    checkers.initialize(linter)
    os.environ.pop('PYLINTRC', None)
    return linter
Esempio n. 12
0
def run_pylint(filename):
    ARGS = []  # put your own here
    pylint_output = WritableObject()
    lint.Run([filename]+ARGS, reporter=TextReporter(pylint_output), exit=False)
    fullText = ""
    for l in pylint_output.read():
        fullText = fullText + l + "\n"
    return fullText
Esempio n. 13
0
def run_pylint(filename, options):
    "run pylint on the given file"
    ARGS = ["--rcfile=./.pylintrc"]  # put your own here
    if not options.show_all:
        ARGS.append("-E")
    pylint_output = WritableObject()
    from pylint import lint
    from pylint.reporters.text import TextReporter
    from pylint import __version__ as pylint_version
    if pylint_version.startswith("2."):
        lint.Run([filename] + ARGS,
                 reporter=TextReporter(pylint_output),
                 do_exit=False)
    else:
        lint.Run([filename] + ARGS,
                 reporter=TextReporter(pylint_output),
                 exit=False)
    return pylint_output.read()
Esempio n. 14
0
def run_pylint(filename):
    "run pylint on the given file"
    from pylint import lint
    from pylint.reporters.text import TextReporter
    pylint_output = WritableObject()
    lint.Run([filename], reporter=TextReporter(pylint_output), exit=False)
    for l in pylint_output.read():
        if l.split(' ', 1)[0] == 'Your':
            return l
Esempio n. 15
0
def get_global_score(paths, rcfile=None):
    extra = ["--rcfile={}".format(rcfile)] if rcfile else []
    sio = StringIO()
    lint.Run(paths + extra, reporter=TextReporter(sio), exit=False)
    # filter out empty lines
    lines = [l for l in sio.getvalue().split("\n") if l]
    # last line should contain global score
    global_score_msg = lines[-1:][0]
    score_match = re.findall(SCORE_PATTERN, global_score_msg)[0]
    return float(score_match)
Esempio n. 16
0
 def validacao_pylint(self, arquivos):
     if self.pylint_desabilitados:
         self.pylint_args.append('--disable={}'.format(','.join(
             self.pylint_desabilitados)))
     pylint_output = WritableObject()
     lint.Run(arquivos + self.pylint_args,
              reporter=TextReporter(pylint_output),
              exit=False)
     for line in pylint_output.read():
         assert_true(False, line)
Esempio n. 17
0
def run_checker(fixture_name):
    writer = PyLintWriter()
    reporter = TextReporter(writer)
    from archimedes.checker import run

    success = run(
        [os.path.join(os.path.dirname(__file__), "fixtures", fixture_name)],
        reporter=reporter,
    )
    return writer.read(), success
Esempio n. 18
0
def check_code():
    #Get textarea text from AJAX call
    text = request.args.get('text')

    #Open temp file which will be parsed
    '''
    f = open("temp.py","r+")
    f.seek()
    f.write(text)
    f.truncate()
    f.close()
    '''
    with open("temp.py", "r") as in_file:
        buf = in_file.readlines()
    with open("temp.py", "w") as out_file:
        for line in range(13):
            out_file.write(buf[line])
        out_file.write("\n")
        for line in text:
            out_file.write(line)

    #Writable Object that will be used as a TextReporter
    class WritableObject(object):
        def __init__(self):
            self.content = []

        def write(self, st):
            self.content.append(st)

        def read(self):
            return self.content

    #Remember that you can configure with a seperate file for more specific limitations => --rcfile=/path/to/config.file .
    #See http://stackoverflow.com/a/10138997/4698963
    #Add "--disable=R,C" to ARGs to print only errors & warnings
    ARGS = [
        "-r", "n", "--disable=R,C",
        "--msg-template={path}:{line}: [{msg_id}({symbol}), {obj}] {msg}"
    ]

    pylint_output = WritableObject()

    #Run Pylint, textreporter will redirect to writable object
    lint.Run(["temp.py"] + ARGS,
             reporter=TextReporter(pylint_output),
             exit=False)
    pylint_list = pylint_output.content

    #Clear Cache. VERY IMPORTANT! This will make sure that there's no funky issues. See: http://stackoverflow.com/questions/2028268/invoking-pylint-programmatically#comment5393474_4803466
    MANAGER.astroid_cache.clear()

    #Return json object, which is the pylint_output seperated by each newline
    response = jsonify(pylint_list)
    response.headers.add('Access-Control-Allow-Origin', '*')
    return response
Esempio n. 19
0
def test_issue_5724() -> None:
    """Regression test for parsing of pylint disable pragma's."""
    with pytest.raises(SystemExit) as cm:
        Run(
            [
                os.path.join(REGR_DATA, "issue_5724.py"),
                "--enable=missing-final-newline",
            ],
            reporter=TextReporter(),
        )
    assert cm.value.code == 0
Esempio n. 20
0
 def test_output_file_can_be_combined_with_custom_reporter(self, tmpdir):
     path = join(HERE, "regrtest_data", "unused_variable.py")
     output_file = tmpdir / "output.txt"
     # It does not really have to be a truly custom reporter.
     # It is only important that it is being passed explicitly to ``Run``.
     myreporter = TextReporter()
     self._run_pylint(
         [path, f"--output={output_file}"],
         out=sys.stdout,
         reporter=myreporter,
     )
     assert output_file.exists()
Esempio n. 21
0
 def test_all(self):
     """Make pylint check itself."""
     reporters = [
         TextReporter(StringIO()),
         ColorizedTextReporter(StringIO()),
         JSONReporter(StringIO()),
     ]
     self._runtest(
         [join(HERE, "functional", "a", "arguments.py")],
         reporter=MultiReporter(reporters),
         code=2,
     )
Esempio n. 22
0
def run_pylint(fn):

    print "checking for PyLint warnings or errors..."

    # run PyLint on file, catch output
    pylint_output = WritableObject()
    pylint.lint.Run([fn, "-r", "n"],
                    reporter=TextReporter(pylint_output),
                    exit=False)

    # count number of warnings/errors
    warning_re = re.compile(r"^W:")
    error_re = re.compile(r"^E:")

    # warnings/errors we choose to ignore
    ignores_re = [
        re.compile(
            r"^W:\s*[0-9,]*:[A-Za-z0-9_]*.configure: Arguments number differs from overridden method"
        ),
        re.compile(
            r"^W:\s*[0-9,]*:[A-Za-z0-9_]*.make: Arguments number differs from overridden method"
        )
    ]

    warning_cnt = 0
    error_cnt = 0
    for line in pylint_output.read():
        ignore = False
        for ignore_re in ignores_re:
            if ignore_re.match(line):
                print "Ignoring %s" % line
                ignore = True
        if ignore:
            continue
        # check for warnings
        if warning_re.match(line):
            warning("PyLint: %s" % line)
            warning_cnt += 1
        # check for errors
        if error_re.match(line):
            warning("PyLint: %s" % line)
            error_cnt += 1

    print
    if warning_cnt > 0 or error_cnt > 0:
        warning(
            "PyLint is still reporting warnings (%d) and/or errors (%d).\n" %
            (warning_cnt, error_cnt))
        return False

    else:
        print "No warnings or errors reported by PyLint we care about, nice job!\n"
        return True
Esempio n. 23
0
    def _run(self, config):
        """
        Run the pylint check with the given config.
        
        :param config: The config object described by Config.
        :type config: Config
        """

        files = self.transaction.get_files(config.check_files,
                                           config.ignore_files)
        # Exit when no files has to be checked.
        if not files:
            self.logger.debug("PyLint check skipped. No files for check.")
            return self.success()

        # Defining pylint home directory.
        os.environ['PYLINTHOME'] = config.pylint_home
        self.logger.debug("PyLint Home is used at '%s'.", config.pylint_home)

        # Determine which pylintrc file is used for the validation.
        if config.pylintrc:
            self.logger.debug("Pylintrc is used at '%s'.", config.pylintrc)
            os.environ['PYLINTRC'] = config.pylintrc
        else:
            self.logger.debug("Default PyLintRC is used.")

        # Only added or updated files will be checked.
        files = [
            self.transaction.get_file(name)
            for name, attr in files.iteritems() if attr in ["A", "U", "UU"]
        ]

        if not files:
            self.logger.debug("No files to validate. PyLint check skipped.")
            return self.success()

        output = StringIO.StringIO()
        reporter = TextReporter(output)

        # Mock to prevent the sys.exit called by pylint.lint.Run.__init__
        lint.sys.exit = lambda _: 0

        self.logger.debug("PyLint is running...")
        lint.Run(["--reports=n"] + files, reporter=reporter)

        output = output.getvalue()
        self.logger.debug("PyLint output:\n %s", output)
        if output:
            return self.error(output)
        else:
            return self.success()
Esempio n. 24
0
def check_python_lint(repo_root, staged_files, pylint_file):
    """Runs pylint on all python scripts staged for commit."""

    class TextReporterBuffer(object):
        """Stores the output produced by the pylint TextReporter."""

        def __init__(self):
            """init"""
            self.content = []

        def write(self, input_str):
            """write"""
            self.content.append(input_str)

        def read(self):
            """read"""
            return self.content

    # Parse each pylint output line individualy and searches
    # for errors in the code.
    pylint_errors = []
    for changed_file in staged_files:
        if not os.path.isfile(changed_file):
            continue
        if re.search(r'\.py$', changed_file):

            print("Running pylint on \'{}\'".format(repo_root + "/" +
                                                    changed_file))
            pylint_output = TextReporterBuffer()
            pylint_args = [
                "--rcfile=" + pylint_file, "-rn",
                repo_root + "/" + changed_file
            ]
            from pylint.reporters.text import TextReporter
            pylint.lint.Run(pylint_args,
                            reporter=TextReporter(pylint_output),
                            exit=False)

            for output_line in pylint_output.read():
                if re.search(r'^(E|C|W):', output_line):
                    print(changed_file + ": " + output_line)
                    pylint_errors.append(output_line)

    if pylint_errors:
        print("=" * 80)
        print("Found {} pylint errors".format(len(pylint_errors)))
        print("=" * 80)
        return False
    else:
        return True
Esempio n. 25
0
 def __init__(self,
              options=(),
              reporter=None,
              option_groups=(),
              pylintrc=None):
     # some stuff has to be done before ancestors initialization...
     #
     # checkers / reporter / astng manager
     self.reporter = None
     self._checkers = {}
     self._ignore_file = False
     # visit variables
     self.base_name = None
     self.base_file = None
     self.current_name = None
     self.current_file = None
     self.stats = None
     # init options
     self.options = options + PyLinter.options
     self.option_groups = option_groups + PyLinter.option_groups
     self._options_methods = {
         'enable': self.enable,
         'disable': self.disable
     }
     self._bw_options_methods = {
         'disable-msg': self.disable,
         'enable-msg': self.enable
     }
     full_version = '%%prog %s, \nastng %s, common %s\nPython %s' % (
         version, astng_version, common_version, sys.version)
     OptionsManagerMixIn.__init__(self,
                                  usage=__doc__,
                                  version=full_version,
                                  config_file=pylintrc or config.PYLINTRC)
     MessagesHandlerMixIn.__init__(self)
     ReportsHandlerMixIn.__init__(self)
     BaseRawChecker.__init__(self)
     # provided reports
     self.reports = (
         ('RP0001', 'Messages by category', report_total_messages_stats),
         ('RP0002', '% errors / warnings by module',
          report_messages_by_module_stats),
         ('RP0003', 'Messages', report_messages_stats),
         ('RP0004', 'Global evaluation', self.report_evaluation),
     )
     self.register_checker(self)
     self._dynamic_plugins = []
     self.load_provider_defaults()
     self.set_reporter(reporter or TextReporter(sys.stdout))
Esempio n. 26
0
    def run(self):
        """Run the pylint static linter.

        :return: return code of the linter run
        :rtype: int
        """
        args = self._prepare_args()

        print("Running pylint with args: ", args)

        pylint.lint.Run(args,
                        reporter=TextReporter(self._stdout),
                        do_exit=False)

        return self._process_output()
Esempio n. 27
0
 def test_useless_suppression() -> None:
     """Tests that duplicate code and useless-suppression work well together."""
     path = join(DATA, "useless_suppression")
     pylint_output = StringIO()
     reporter = TextReporter(pylint_output)
     runner = Run(
         [
             path,
             "-e=duplicate-code, useless-suppression",
             "-d=missing-module-docstring, unused-import",
         ],
         reporter=reporter,
         exit=False,
     )
     assert not runner.linter.stats.by_msg
Esempio n. 28
0
def run_pylint(filename):
    """
    Runs pylint on a given file
    :param filename:
    :return: list of pylint errors
    """
    ARGS = ['-r', 'n']
    pylint_output = WritableObject()
    Run([filename] + ARGS, reporter=TextReporter(pylint_output), exit=False)

    lines = []
    for line in pylint_output.read():
        if not line.startswith('*') and line != '\n':
            lines.append(line)

    return lines
Esempio n. 29
0
 def __lint(self, user_id, case_id):
     # 使用pylint检查代码质量
     file_name = '.\\codes\\' + str(user_id) + '\\' + str(
         case_id) + '\\main.py'
     buff = StringIO()
     reporter = TextReporter(output=buff)
     results = pylint.lint.Run([file_name], reporter=reporter, exit=False)
     try:
         score = results.linter.stats['global_note']
         out_str = buff.getvalue()
         Output.write_code_lint(out_str, user_id, case_id)
         # print(score)
         return score
     except Exception as e:
         # 总有莫名其妙 有得分却没提交记录的人,返回0分
         return 0
def complile(fileorfoldername):
    from pylint import lint
    from pylint.reporters.text import TextReporter
    pylint_output = clsOutput()
    lint.Run(['--errors-only', fileorfoldername], reporter=TextReporter(pylint_output), exit=False)

    file = {}
    cnt = 0
    for l in pylint_output.getalllogs():
        if "*****" in l:
            key = l.replace("*","")
            if key not in file :
                file[l.replace("*","")] = {}
                continue
        # below warnings are suppressed.
        if "Module 'numpy' has no 'floating' member" in l:
            continue
        if "Too many arguments for format string (too-many-format-args)" in l:
            continue

        if l == "" or "\n" in l:
            continue

        file[key][cnt]=l

    filterederror = {}
    for key,val in file.items():
        if len(val)>0:
            filterederror[key] = val

    cnt = 0
    for key, val in filterederror.items():
        cnt = cnt + len(val)

    if cnt>0:
        print(str(cnt)+" Error(s) Found")
        for key, val in filterederror.items():
            print("File :"+key)
            for key1, val1 in val.items():
                print("Error : " + val1)

        print("Build Failed with {0} error(s)".format(cnt))
        return False
    else:
        print("Build Succeeded")
        return True