def _run_prospector_on(filenames, tools, disabled_linters, ignore_codes=None):
    """Run prospector on filename, using the specified tools.

    This function enables us to run different tools on different
    classes of files, which is necessary in the case of tests.
    """
    from prospector.run import Prospector, ProspectorConfig

    assert len(tools) > 0

    tools = list(set(tools) - set(disabled_linters))
    return_dict = dict()
    ignore_codes = ignore_codes or list()

    # pylint doesn't like absolute paths, so convert to relative.
    all_argv = (["-F", "-D", "-M", "--no-autodetect", "-s", "veryhigh"] +
                ("-t " + " -t ".join(tools)).split(" ") +
                [os.path.relpath(f) for f in filenames])
    with _custom_argv(all_argv):
        prospector = Prospector(ProspectorConfig())
        prospector.execute()
        messages = prospector.get_messages() or list()
        for message in messages:
            message.to_absolute_path(os.getcwd())
            loc = message.location
            code = message.code

            if code in ignore_codes:
                continue

            key = _Key(loc.path, loc.line, code)
            return_dict[key] = message

    return return_dict
示例#2
0
def run_prospector(base_dir, clear_ignore_patterns=False):
    """Run prospector and apply white/blacklists to the results"""
    orig_expand_default = optparse.HelpFormatter.expand_default

    sys.argv = ['fakename']
    sys.argv.extend(PROSPECTOR_OPTIONS)
    # add/set REPO_BASE_DIR as positional path
    sys.argv.append(base_dir)

    config = ProspectorConfig()
    # prospector will sometimes wrongly autodetect django
    config.libraries = []
    prospector = Prospector(config)

    # Set defaults for prospector:
    config.profile.doc_warnings = False
    config.profile.test_warnings = False
    config.profile.autodetect = True
    config.profile.member_warnings = False
    if clear_ignore_patterns:
        config.profile.ignore_patterns = [r'.^']
        config.ignores = []
    else:
        append_pattern = r'(^|/)\..+'
        config.profile.ignore_patterns.append(append_pattern)
        config.ignores.append(re.compile(append_pattern))

    # Enable pylint Python3 compatibility tests:
    config.profile.pylint['options']['enable'] = 'python3'
    log.debug("prospector argv = %s" % sys.argv)
    log.debug("prospector profile from config = %s" % vars(config.profile))

    prospector.execute()
    log.debug("prospector profile form prospector = %s" % vars(prospector.config.profile))

    blacklist = map(re.compile, PROSPECTOR_BLACKLIST)
    whitelist = map(re.compile, PROSPECTOR_WHITELIST)

    failures = []
    for msg in prospector.get_messages():
        # example msg.as_dict():
        #  {'source': 'pylint', 'message': 'Missing function docstring', 'code': 'missing-docstring',
        #   'location': {'function': 'TestHeaders.test_check_header.lgpl', 'path': u'headers.py',
        #                'line': 122, 'character': 8, 'module': 'headers'}}
        log.debug("prospector message %s" % msg.as_dict())

        if any([bool(reg.search(msg.code) or reg.search(msg.message)) for reg in blacklist]):
            continue

        if any([bool(reg.search(msg.code) or reg.search(msg.message)) for reg in whitelist]):
            failures.append(msg.as_dict())

    # There is some ugly monkeypatch code in pylint
    #     (or logilab if no recent enough pylint is installed)
    # Make sure the original is restored
    # (before any errors are reported; no need to put this in setUp/tearDown)
    optparse.HelpFormatter.expand_default = orig_expand_default

    return failures
示例#3
0
def prospect(paths):
    config = ProspectorConfig()
    config.paths = paths
    prospector = Prospector(config)
    prospector.execute()
    all_messages = [m.as_dict() for m in prospector.messages]

    global prospector_messages
    prospector_messages = all_messages

    return prospector_messages
示例#4
0
    def test_prospector(self):
        """Run prospector.run.main, but apply white/blacklists to the results"""
        orig_expand_default = optparse.HelpFormatter.expand_default

        if not HAS_PROSPECTOR:
            if sys.version_info < (2, 7):
                log.info('No protector tests are ran on py26 or older.')
            else:
                log.info('No protector tests are ran, install prospector manually first')

                # This is fatal on jenkins/...
                if 'JENKINS_URL' in os.environ:
                    self.assertTrue(False, 'prospector must be installed in jenkins environment')

            return

        sys.argv = ['fakename']
        sys.argv.extend(self.PROSPECTOR_OPTIONS)
        # add/set REPO_BASE_DIR as positional path
        sys.argv.append(self.setup.REPO_BASE_DIR)

        config = ProspectorConfig()
        # prospector will sometimes wrongly autodetect django
        config.libraries = []
        prospector = Prospector(config)

        prospector.execute()

        blacklist = map(re.compile, self.PROSPECTOR_BLACKLIST)
        whitelist = map(re.compile, self.PROSPECTOR_WHITELIST)

        failures = []
        for msg in prospector.get_messages():
            # example msg.as_dict():
            #  {'source': 'pylint', 'message': 'Missing function docstring', 'code': 'missing-docstring',
            #   'location': {'function': 'TestHeaders.test_check_header.lgpl', 'path': u'headers.py',
            #                'line': 122, 'character': 8, 'module': 'headers'}}
            log.debug("prospector message %s" % msg.as_dict())

            if any([bool(reg.search(msg.code) or reg.search(msg.message)) for reg in blacklist]):
                continue

            if any([bool(reg.search(msg.code) or reg.search(msg.message)) for reg in whitelist]):
                failures.append(msg.as_dict())

        # There is some ugly monkeypatch code in pylint
        #     (or logilab if no recent enough pylint is installed)
        # Make sure the original is restored
        # (before any errors are reported; no need to put this in setUp/tearDown)
        optparse.HelpFormatter.expand_default = orig_expand_default

        self.assertFalse(failures, "prospector failures: %s" % pprint.pformat(failures))
示例#5
0
    def test_prospector(self):
        """Run prospector.run.main, but apply white/blacklists to the results"""
        orig_expand_default = optparse.HelpFormatter.expand_default

        if not HAS_PROTECTOR:
            if sys.version_info < (2, 7):
                log.info('No protector tests are ran on py26 or older.')
            else:
                log.info('No protector tests are ran, install prospector manually first')

                # This is fatal on jenkins/...
                if 'JENKINS_URL' in os.environ:
                    self.assertTrue(False, 'prospector must be installed in jenkins environment')

            return

        sys.argv = ['fakename']
        sys.argv.extend(self.PROSPECTOR_OPTIONS)
        # add/set REPO_BASE_DIR as positional path
        sys.argv.append(self.setup.REPO_BASE_DIR)

        config = ProspectorConfig()
        # prospector will sometimes wrongly autodetect django
        config.libraries = []
        prospector = Prospector(config)

        prospector.execute()

        blacklist = map(re.compile, self.PROSPECTOR_BLACKLIST)
        whitelist = map(re.compile, self.PROSPECTOR_WHITELIST)

        failures = []
        for msg in prospector.get_messages():
            # example msg.as_dict():
            #  {'source': 'pylint', 'message': 'Missing function docstring', 'code': 'missing-docstring',
            #   'location': {'function': 'TestHeaders.test_check_header.lgpl', 'path': u'headers.py',
            #                'line': 122, 'character': 8, 'module': 'headers'}}
            log.debug("prospector message %s" % msg.as_dict())

            if any([bool(reg.search(msg.code) or reg.search(msg.message)) for reg in blacklist]):
                continue

            if any([bool(reg.search(msg.code) or reg.search(msg.message)) for reg in whitelist]):
                failures.append(msg.as_dict())

        # There is some ugly monkeypatch code in pylint
        #     (or logilab if no recent enough pylint is installed)
        # Make sure the original is restored
        # (before any errors are reported; no need to put this in setUp/tearDown)
        optparse.HelpFormatter.expand_default = orig_expand_default

        self.assertFalse(failures, "prospector failures: %s" % pprint.pformat(failures))
示例#6
0
    def test_prospector(self):
        """Run prospector on project."""
        sys.argv = ['fakename']
        sys.argv.append(self.REPO_BASE_DIR)

        config = ProspectorConfig()
        prospector = Prospector(config)
        prospector.execute()

        failures = []
        for msg in prospector.get_messages():
            failures.append(msg.as_dict())

        self.assertFalse(failures, "prospector failures: %s" % pprint.pformat(failures))
示例#7
0
def refresh_prospect_with_recently_changed_files(paths):
    config = ProspectorConfig()
    config.paths = paths
    config.explicit_file_mode = all(map(os.path.isfile, config.paths))
    prospector = Prospector(config)
    prospector.execute()

    global prospector_messages
    new_messages = [m.as_dict() for m in prospector.messages]
    messages_kept = [msg for msg in prospector_messages if not any([msg['location']['path'] in path for path in paths])]
    prospector_messages = new_messages + messages_kept

    socketio.emit('message', "refresh")
    return prospector_messages
示例#8
0
def _run_prospector_on(filenames,
                       tools,
                       disabled_linters,
                       show_lint_files,
                       ignore_codes=None):
    """Run prospector on filename, using the specified tools.

    This function enables us to run different tools on different
    classes of files, which is necessary in the case of tests.
    """
    from prospector.run import Prospector, ProspectorConfig

    assert len(tools) > 0

    tools = list(set(tools) - set(disabled_linters))
    return_dict = dict()
    ignore_codes = ignore_codes or list()

    # Early return if all tools were filtered out
    if not len(tools):
        return return_dict

    # pylint doesn't like absolute paths, so convert to relative.
    all_argv = (["-F", "-D", "-M", "--no-autodetect", "-s", "veryhigh"] +
                ("-t " + " -t ".join(tools)).split(" "))

    for filename in filenames:
        _debug_linter_status("prospector", filename, show_lint_files)

    with _custom_argv(all_argv + [os.path.relpath(f) for f in filenames]):
        prospector = Prospector(ProspectorConfig())
        prospector.execute()
        messages = prospector.get_messages() or list()
        for message in messages:
            message.to_absolute_path(os.getcwd())
            loc = message.location
            code = message.code

            if code in ignore_codes:
                continue

            key = _Key(loc.path, loc.line, code)
            return_dict[key] = message

    return return_dict
示例#9
0
    def test_prospector(self):
        """Run prospector.run.main, but apply white/blacklists to the results"""

        if not HAS_PROTECTOR:
            if sys.version_info < (2, 7):
                log.info('No protector tests are ran on py26 or older.')
            else:
                log.info('No protector tests are ran, install prospector manually first')

                # This is fatal on jenkins/...
                if 'JENKINS_URL' in os.environ:
                    self.assertTrue(False, 'prospector must be installed in jenkins environment')

            return

        sys.argv = ['fakename']
        sys.argv.extend(self.PROSPECTOR_OPTIONS)
        # add/set REPO_BASE_DIR as positional path
        sys.argv.append(REPO_BASE_DIR)

        config = ProspectorConfig()
        prospector = Prospector(config)

        prospector.execute()

        blacklist = map(re.compile, self.PROSPECTOR_BLACKLIST)
        whitelist = map(re.compile, self.PROSPECTOR_WHITELIST)

        failures = []
        for msg in prospector.get_messages():
            # example msg.as_dict():
            #  {'source': 'pylint', 'message': 'Missing function docstring', 'code': 'missing-docstring',
            #   'location': {'function': 'TestHeaders.test_check_header.lgpl', 'path': u'headers.py',
            #                'line': 122, 'character': 8, 'module': 'headers'}}
            log.debug("prospector message %s" % msg.as_dict())

            if any([bool(reg.search(msg.code) or reg.search(msg.message)) for reg in blacklist]):
                continue

            if any([bool(reg.search(msg.code) or reg.search(msg.message)) for reg in whitelist]):
                failures.append(msg.as_dict())

        self.assertFalse(failures, "prospector failures: %s" % pprint.pformat(failures))
示例#10
0
    def test_prospector(self):
        """Run prospector tests"""

        PROSPECTOR_OPTIONS = [
            '--strictness',
            'medium',
            '--max-line-length',
            '120',
            '--absolute-paths',
        ]
        sys.argv = ['fakename']
        sys.argv.extend(PROSPECTOR_OPTIONS)
        sys.argv.append(REPO_BASE_DIR)
        config = ProspectorConfig()
        config.libraries = []
        prospector = Prospector(config)
        prospector.execute()
        failures = [msg.as_dict() for msg in prospector.get_messages()]
        self.assertFalse(failures,
                         "prospector failures: %s" % pprint.pformat(failures))
示例#11
0
def run_prospector(base_dir, clear_ignore_patterns=False):
    """Run prospector and apply white/blacklists to the results"""
    orig_expand_default = optparse.HelpFormatter.expand_default

    log.info("Using prosector version %s", prospector_version)

    sys.argv = ['fakename']
    sys.argv.extend(PROSPECTOR_OPTIONS)

    if PROSPECTOR_USE_LIBS:
        sys.argv.extend(["--uses", ",".join(PROSPECTOR_USE_LIBS)])

    # add/set REPO_BASE_DIR as positional path
    sys.argv.append(base_dir)
    log.debug("prospector commandline %s" % sys.argv)

    config = ProspectorConfig()
    # prospector will sometimes wrongly autodetect django
    config.libraries = []
    prospector = Prospector(config)

    # Set defaults for prospector:
    config.profile.doc_warnings = False
    config.profile.test_warnings = False
    config.profile.autodetect = True
    config.profile.member_warnings = False
    if clear_ignore_patterns:
        config.profile.ignore_patterns = [r'.^']
        config.ignores = []
    else:
        append_pattern = r'(^|/)\..+'
        config.profile.ignore_patterns.append(append_pattern)
        config.ignores.append(re.compile(append_pattern))

    # Enable pylint Python3 compatibility tests:
    config.profile.pylint['options']['enable'] = 'python3'
    log.debug("prospector argv = %s" % sys.argv)
    log.debug("prospector profile from config = %s" % vars(config.profile))

    prospector.execute()
    log.debug("prospector profile form prospector = %s" %
              vars(prospector.config.profile))

    # map yields a generator object in Python 3, but since we want to iterate over the whitelist/blacklist
    # multiple times, we need to make sure it's a list (since you can only iterate once over a generator)
    blacklist = list(map(re.compile, PROSPECTOR_BLACKLIST))
    whitelist = list(map(re.compile, PROSPECTOR_WHITELIST))

    failures = []
    for msg in prospector.get_messages():
        # example msg.as_dict():
        #  {'source': 'pylint', 'message': 'Missing function docstring', 'code': 'missing-docstring',
        #   'location': {'function': 'TestHeaders.test_check_header.lgpl', 'path': u'headers.py',
        #                'line': 122, 'character': 8, 'module': 'headers'}}
        log.debug("prospector message %s" % msg.as_dict())

        if any([
                bool(reg.search(msg.code) or reg.search(msg.message))
                for reg in blacklist
        ]):
            continue

        if any([
                bool(reg.search(msg.code) or reg.search(msg.message))
                for reg in whitelist
        ]):
            failures.append(msg.as_dict())

    # There is some ugly monkeypatch code in pylint
    #     (or logilab if no recent enough pylint is installed)
    # Make sure the original is restored
    # (before any errors are reported; no need to put this in setUp/tearDown)
    optparse.HelpFormatter.expand_default = orig_expand_default

    return failures