예제 #1
0
def test_pylintrc_parentdir():
    with tempdir() as chroot:

        create_files(
            [
                "a/pylintrc",
                "a/b/__init__.py",
                "a/b/pylintrc",
                "a/b/c/__init__.py",
                "a/b/c/d/__init__.py",
                "a/b/c/d/e/.pylintrc",
            ]
        )
        with fake_home():
            assert config.find_pylintrc() is None
        results = {
            "a": join(chroot, "a", "pylintrc"),
            "a/b": join(chroot, "a", "b", "pylintrc"),
            "a/b/c": join(chroot, "a", "b", "pylintrc"),
            "a/b/c/d": join(chroot, "a", "b", "pylintrc"),
            "a/b/c/d/e": join(chroot, "a", "b", "c", "d", "e", ".pylintrc"),
        }
        for basedir, expected in results.items():
            os.chdir(join(chroot, basedir))
            assert config.find_pylintrc() == expected
예제 #2
0
    def test_pylintrc_parentdir_no_package(self):
        chroot = tempfile.mkdtemp()

        # Get real path of tempfile, otherwise test fail on mac os x
        cdir = getcwd()
        chdir(chroot)
        chroot = abspath('.')
        chdir(cdir)

        fake_home = tempfile.mkdtemp('fake-home')
        home = os.environ['HOME']
        os.environ['HOME'] = fake_home
        try:
            create_files(['a/pylintrc', 'a/b/pylintrc', 'a/b/c/d/__init__.py'], chroot)
            os.chdir(chroot)
            self.assertEqual(config.find_pylintrc(), None)
            results = {'a'       : join(chroot, 'a', 'pylintrc'),
                       'a/b'     : join(chroot, 'a', 'b', 'pylintrc'),
                       'a/b/c'   : None,
                       'a/b/c/d' : None,
                       }
            for basedir, expected in results.items():
                os.chdir(join(chroot, basedir))
                self.assertEqual(config.find_pylintrc(), expected)
        finally:
            os.environ['HOME'] = home
            rmtree(fake_home, ignore_errors=True)
            os.chdir(HERE)
            rmtree(chroot)
 def test_pylintrc_parentdir(self):
     chroot = tempfile.mkdtemp()
     try:
         create_files(['a/pylintrc', 'a/b/__init__.py', 'a/b/pylintrc',
                       'a/b/c/__init__.py', 'a/b/c/d/__init__.py'], chroot)
         os.chdir(chroot)
         fake_home = tempfile.mkdtemp('fake-home')
         home = os.environ['HOME']
         try:
             os.environ['HOME'] = fake_home
             self.assertEqual(config.find_pylintrc(), None)
         finally:
             os.environ['HOME'] = home
             os.rmdir(fake_home)
         results = {'a'       : join(chroot, 'a', 'pylintrc'),
                    'a/b'     : join(chroot, 'a', 'b', 'pylintrc'),
                    'a/b/c'   : join(chroot, 'a', 'b', 'pylintrc'),
                    'a/b/c/d' : join(chroot, 'a', 'b', 'pylintrc'),
                    }
         for basedir, expected in results.items():
             os.chdir(join(chroot, basedir))
             self.assertEqual(config.find_pylintrc(), expected)
     finally:
         os.chdir(HERE)
         rmtree(chroot)
예제 #4
0
 def test_pylintrc_parentdir(self):
     chroot = tempfile.mkdtemp()
     try:
         create_files(
             ["a/pylintrc", "a/b/__init__.py", "a/b/pylintrc", "a/b/c/__init__.py", "a/b/c/d/__init__.py"], chroot
         )
         os.chdir(chroot)
         fake_home = tempfile.mkdtemp("fake-home")
         home = os.environ["HOME"]
         try:
             os.environ["HOME"] = fake_home
             self.assertEquals(config.find_pylintrc(), None)
         finally:
             os.environ["HOME"] = home
             os.rmdir(fake_home)
         results = {
             "a": join(chroot, "a", "pylintrc"),
             "a/b": join(chroot, "a", "b", "pylintrc"),
             "a/b/c": join(chroot, "a", "b", "pylintrc"),
             "a/b/c/d": join(chroot, "a", "b", "pylintrc"),
         }
         for basedir, expected in results.items():
             os.chdir(join(chroot, basedir))
             self.assertEquals(config.find_pylintrc(), expected)
     finally:
         os.chdir(HERE)
         rmtree(chroot)
예제 #5
0
    def test_pylintrc_parentdir_no_package(self):
        chroot = tempfile.mkdtemp()

        # Get real path of tempfile, otherwise test fail on mac os x
        cdir = getcwd()
        chdir(chroot)
        chroot = abspath('.')
        chdir(cdir)

        fake_home = tempfile.mkdtemp('fake-home')
        home = os.environ['HOME']
        os.environ['HOME'] = fake_home
        try:
            create_files(['a/pylintrc', 'a/b/pylintrc', 'a/b/c/d/__init__.py'], chroot)
            os.chdir(chroot)
            self.assertEqual(config.find_pylintrc(), None)
            results = {'a'       : join(chroot, 'a', 'pylintrc'),
                       'a/b'     : join(chroot, 'a', 'b', 'pylintrc'),
                       'a/b/c'   : None,
                       'a/b/c/d' : None,
                       }
            for basedir, expected in results.items():
                os.chdir(join(chroot, basedir))
                self.assertEqual(config.find_pylintrc(), expected)
        finally:
            os.environ['HOME'] = home
            rmtree(fake_home, ignore_errors=True)
            os.chdir(HERE)
            rmtree(chroot)
예제 #6
0
def test_pylintrc_parentdir():
    with tempdir() as chroot:

        create_files(
            [
                "a/pylintrc",
                "a/b/__init__.py",
                "a/b/pylintrc",
                "a/b/c/__init__.py",
                "a/b/c/d/__init__.py",
                "a/b/c/d/e/.pylintrc",
            ]
        )
        with fake_home():
            assert config.find_pylintrc() is None
        results = {
            "a": join(chroot, "a", "pylintrc"),
            "a/b": join(chroot, "a", "b", "pylintrc"),
            "a/b/c": join(chroot, "a", "b", "pylintrc"),
            "a/b/c/d": join(chroot, "a", "b", "pylintrc"),
            "a/b/c/d/e": join(chroot, "a", "b", "c", "d", "e", ".pylintrc"),
        }
        for basedir, expected in results.items():
            os.chdir(join(chroot, basedir))
            assert config.find_pylintrc() == expected
 def test_pylintrc_parentdir(self):
     chroot = tempfile.mkdtemp()
     try:
         create_files([
             'a/pylintrc', 'a/b/__init__.py', 'a/b/pylintrc',
             'a/b/c/__init__.py', 'a/b/c/d/__init__.py'
         ], chroot)
         os.chdir(chroot)
         fake_home = tempfile.mkdtemp('fake-home')
         home = os.environ['HOME']
         try:
             os.environ['HOME'] = fake_home
             self.assertEqual(config.find_pylintrc(), None)
         finally:
             os.environ['HOME'] = home
             os.rmdir(fake_home)
         results = {
             'a': join(chroot, 'a', 'pylintrc'),
             'a/b': join(chroot, 'a', 'b', 'pylintrc'),
             'a/b/c': join(chroot, 'a', 'b', 'pylintrc'),
             'a/b/c/d': join(chroot, 'a', 'b', 'pylintrc'),
         }
         for basedir, expected in results.items():
             os.chdir(join(chroot, basedir))
             self.assertEqual(config.find_pylintrc(), expected)
     finally:
         os.chdir(HERE)
         rmtree(chroot)
 def test_pylintrc(self):
     try:
         self.assertEquals(config.find_pylintrc(), None)
         os.environ['PYLINTRC'] = join(tempfile.gettempdir(), '.pylintrc')
         self.assertEquals(config.find_pylintrc(), None)
         os.environ['PYLINTRC'] = '.'
         self.assertEquals(config.find_pylintrc(), None)
     finally:
         os.environ.pop('PYLINTRC', '')
         reload(config)
예제 #9
0
 def test_pylintrc(self):
     with fake_home():
         try:
             self.assertEqual(config.find_pylintrc(), None)
             os.environ['PYLINTRC'] = join(tempfile.gettempdir(),
                                           '.pylintrc')
             self.assertEqual(config.find_pylintrc(), None)
             os.environ['PYLINTRC'] = '.'
             self.assertEqual(config.find_pylintrc(), None)
         finally:
             reload_module(config)
예제 #10
0
def test_pylintrc():
    with fake_home():
        current_dir = getcwd()
        chdir(os.path.dirname(os.path.abspath(sys.executable)))
        try:
            assert config.find_pylintrc() is None
            os.environ["PYLINTRC"] = join(tempfile.gettempdir(), ".pylintrc")
            assert config.find_pylintrc() is None
            os.environ["PYLINTRC"] = "."
            assert config.find_pylintrc() is None
        finally:
            chdir(current_dir)
            reload(config)
예제 #11
0
 def test_pylintrc_parentdir_no_package(self):
     with tempdir() as chroot:
         with fake_home():
             create_files(['a/pylintrc', 'a/b/pylintrc', 'a/b/c/d/__init__.py'])
             self.assertEqual(config.find_pylintrc(), None)
             results = {'a'       : join(chroot, 'a', 'pylintrc'),
                        'a/b'     : join(chroot, 'a', 'b', 'pylintrc'),
                        'a/b/c'   : None,
                        'a/b/c/d' : None,
                        }
             for basedir, expected in results.items():
                 os.chdir(join(chroot, basedir))
                 self.assertEqual(config.find_pylintrc(), expected)
예제 #12
0
def test_pylintrc():
    with fake_home():
        current_dir = getcwd()
        chdir(os.path.dirname(os.path.abspath(sys.executable)))
        try:
            assert config.find_pylintrc() is None
            os.environ["PYLINTRC"] = join(tempfile.gettempdir(), ".pylintrc")
            assert config.find_pylintrc() is None
            os.environ["PYLINTRC"] = "."
            assert config.find_pylintrc() is None
        finally:
            chdir(current_dir)
            reload(config)
예제 #13
0
def test_pylintrc_parentdir_no_package() -> None:
    with tempdir() as chroot:
        with fake_home():
            create_files(["a/pylintrc", "a/b/pylintrc", "a/b/c/d/__init__.py"])
            assert config.find_pylintrc() is None
            results = {
                "a": join(chroot, "a", "pylintrc"),
                "a/b": join(chroot, "a", "b", "pylintrc"),
                "a/b/c": None,
                "a/b/c/d": None,
            }
            for basedir, expected in results.items():
                os.chdir(join(chroot, basedir))
                assert config.find_pylintrc() == expected
예제 #14
0
 def test_pylintrc(self):
     fake_home = tempfile.mkdtemp('fake-home')
     home = os.environ['HOME']
     try:
         os.environ['HOME'] = fake_home
         self.assertEqual(config.find_pylintrc(), None)
         os.environ['PYLINTRC'] = join(tempfile.gettempdir(), '.pylintrc')
         self.assertEqual(config.find_pylintrc(), None)
         os.environ['PYLINTRC'] = '.'
         self.assertEqual(config.find_pylintrc(), None)
     finally:
         os.environ.pop('PYLINTRC', '')
         os.environ['HOME'] = home
         rmtree(fake_home, ignore_errors=True)
         reload(config)
예제 #15
0
 def test_pylintrc(self):
     fake_home = tempfile.mkdtemp("fake-home")
     home = os.environ[HOME]
     try:
         os.environ[HOME] = fake_home
         self.assertEqual(config.find_pylintrc(), None)
         os.environ["PYLINTRC"] = join(tempfile.gettempdir(), ".pylintrc")
         self.assertEqual(config.find_pylintrc(), None)
         os.environ["PYLINTRC"] = "."
         self.assertEqual(config.find_pylintrc(), None)
     finally:
         os.environ.pop("PYLINTRC", "")
         os.environ[HOME] = home
         rmtree(fake_home, ignore_errors=True)
         reload(config)
예제 #16
0
 def test_pylintrc(self):
     fake_home = tempfile.mkdtemp('fake-home')
     home = os.environ['HOME']
     try:
         os.environ['HOME'] = fake_home
         self.assertEqual(config.find_pylintrc(), None)
         os.environ['PYLINTRC'] = join(tempfile.gettempdir(), '.pylintrc')
         self.assertEqual(config.find_pylintrc(), None)
         os.environ['PYLINTRC'] = '.'
         self.assertEqual(config.find_pylintrc(), None)
     finally:
         os.environ.pop('PYLINTRC', '')
         os.environ['HOME'] = home
         rmtree(fake_home, ignore_errors=True)
         reload(config)
예제 #17
0
    def _get_pylint_configuration(self, check_paths, config_messages,
                                  configured_by, ext_found, linter,
                                  prospector_config, pylint_options):
        if prospector_config.use_external_config("pylint"):
            # try to find a .pylintrc
            pylintrc = pylint_options.get("config_file")
            external_config = prospector_config.external_config_location(
                "pylint")
            if pylintrc is None or external_config:
                pylintrc = external_config
            if pylintrc is None:
                pylintrc = find_pylintrc()
            if pylintrc is None:
                pylintrc_path = os.path.join(prospector_config.workdir,
                                             ".pylintrc")
                if os.path.exists(pylintrc_path):
                    pylintrc = pylintrc_path

            if pylintrc is not None:
                # load it!
                configured_by = pylintrc
                ext_found = True

                self._args = linter.load_command_line_configuration(
                    check_paths)
                config_messages += self._pylintrc_configure(pylintrc, linter)
        if not ext_found:
            linter.reset_options()
            self._args = linter.load_command_line_configuration(check_paths)
            config_messages = self._prospector_configure(
                prospector_config, linter)
        return config_messages, configured_by
예제 #18
0
    def _get_pylint_configuration(self, check_paths, linter, prospector_config, pylint_options):
        self._args = linter.load_command_line_configuration(check_paths)
        linter.load_default_plugins()

        config_messages = self._prospector_configure(prospector_config, linter)
        configured_by = None

        if prospector_config.use_external_config("pylint"):
            # try to find a .pylintrc
            pylintrc = pylint_options.get("config_file")
            external_config = prospector_config.external_config_location("pylint")
            pylintrc = pylintrc or external_config or find_pylintrc()
            if pylintrc is None:  # nothing explicitly configured
                for possible in (".pylintrc", "pylintrc", "pyproject.toml", "setup.cfg"):
                    pylintrc_path = os.path.join(prospector_config.workdir, possible)
                    # TODO: pyproject and setup.cfg might not actually have any pylint config
                    #       in, they should be skipped in that case
                    if os.path.exists(pylintrc_path):
                        pylintrc = pylintrc_path
                        break

            if pylintrc is not None:
                # load it!
                configured_by = pylintrc
                config_messages += self._pylintrc_configure(pylintrc, linter)

        return config_messages, configured_by
예제 #19
0
def test_pylintrc_parentdir():
    with tempdir() as chroot:

        create_files(['a/pylintrc', 'a/b/__init__.py', 'a/b/pylintrc',
                      'a/b/c/__init__.py', 'a/b/c/d/__init__.py',
                      'a/b/c/d/e/.pylintrc'])
        with fake_home():
            assert config.find_pylintrc() is None
        results = {'a'       : join(chroot, 'a', 'pylintrc'),
                   'a/b'     : join(chroot, 'a', 'b', 'pylintrc'),
                   'a/b/c'   : join(chroot, 'a', 'b', 'pylintrc'),
                   'a/b/c/d' : join(chroot, 'a', 'b', 'pylintrc'),
                   'a/b/c/d/e' : join(chroot, 'a', 'b', 'c', 'd', 'e', '.pylintrc'),
                   }
        for basedir, expected in results.items():
            os.chdir(join(chroot, basedir))
            assert config.find_pylintrc() == expected
 def test_pylintrc_parentdir_no_package(self):
     chroot = tempfile.mkdtemp()
     try:
         create_files(['a/pylintrc', 'a/b/pylintrc', 'a/b/c/d/__init__.py'], chroot)
         os.chdir(chroot)
         self.assertEquals(config.find_pylintrc(), None)
         results = {'a'       : join(chroot, 'a', 'pylintrc'),
                    'a/b'     : join(chroot, 'a', 'b', 'pylintrc'),
                    'a/b/c'   : None, 
                    'a/b/c/d' : None, 
                    }
         for basedir, expected in results.items():
             os.chdir(join(chroot, basedir))
             self.assertEquals(config.find_pylintrc(), expected)
     finally:
         os.chdir(HERE)
         shutil.rmtree(chroot)
예제 #21
0
def test_pylintrc_parentdir():
    with tempdir() as chroot:

        create_files(['a/pylintrc', 'a/b/__init__.py', 'a/b/pylintrc',
                      'a/b/c/__init__.py', 'a/b/c/d/__init__.py',
                      'a/b/c/d/e/.pylintrc'])
        with fake_home():
            assert config.find_pylintrc() is None
        results = {'a'       : join(chroot, 'a', 'pylintrc'),
                   'a/b'     : join(chroot, 'a', 'b', 'pylintrc'),
                   'a/b/c'   : join(chroot, 'a', 'b', 'pylintrc'),
                   'a/b/c/d' : join(chroot, 'a', 'b', 'pylintrc'),
                   'a/b/c/d/e' : join(chroot, 'a', 'b', 'c', 'd', 'e', '.pylintrc'),
                   }
        for basedir, expected in results.items():
            os.chdir(join(chroot, basedir))
            assert config.find_pylintrc() == expected
예제 #22
0
 def test_pylintrc_parentdir_no_package(self):
     with tempdir() as chroot:
         fake_home = tempfile.mkdtemp('fake-home')
         home = os.environ[HOME]
         os.environ[HOME] = fake_home
         try:
             create_files(['a/pylintrc', 'a/b/pylintrc', 'a/b/c/d/__init__.py'])
             self.assertEqual(config.find_pylintrc(), None)
             results = {'a'       : join(chroot, 'a', 'pylintrc'),
                        'a/b'     : join(chroot, 'a', 'b', 'pylintrc'),
                        'a/b/c'   : None,
                        'a/b/c/d' : None,
                        }
             for basedir, expected in results.items():
                 os.chdir(join(chroot, basedir))
                 self.assertEqual(config.find_pylintrc(), expected)
         finally:
             os.environ[HOME] = home
             rmtree(fake_home, ignore_errors=True)
예제 #23
0
 def test_pylintrc_parentdir_no_package(self):
     with tempdir() as chroot:
         fake_home = tempfile.mkdtemp('fake-home')
         home = os.environ[HOME]
         os.environ[HOME] = fake_home
         try:
             create_files(['a/pylintrc', 'a/b/pylintrc', 'a/b/c/d/__init__.py'])
             self.assertEqual(config.find_pylintrc(), None)
             results = {'a'       : join(chroot, 'a', 'pylintrc'),
                        'a/b'     : join(chroot, 'a', 'b', 'pylintrc'),
                        'a/b/c'   : None,
                        'a/b/c/d' : None,
                        }
             for basedir, expected in list(results.items()):
                 os.chdir(join(chroot, basedir))
                 self.assertEqual(config.find_pylintrc(), expected)
         finally:
             os.environ[HOME] = home
             rmtree(fake_home, ignore_errors=True)
예제 #24
0
 def test_pylintrc_parentdir_no_package(self):
     with tempdir() as chroot:
         fake_home = tempfile.mkdtemp("fake-home")
         home = os.environ[HOME]
         os.environ[HOME] = fake_home
         try:
             create_files(["a/pylintrc", "a/b/pylintrc", "a/b/c/d/__init__.py"])
             self.assertEqual(config.find_pylintrc(), None)
             results = {
                 "a": join(chroot, "a", "pylintrc"),
                 "a/b": join(chroot, "a", "b", "pylintrc"),
                 "a/b/c": None,
                 "a/b/c/d": None,
             }
             for basedir, expected in results.items():
                 os.chdir(join(chroot, basedir))
                 self.assertEqual(config.find_pylintrc(), expected)
         finally:
             os.environ[HOME] = home
             rmtree(fake_home, ignore_errors=True)
예제 #25
0
    def test_pylintrc_parentdir(self):
        with tempdir() as chroot:

            create_files(['a/pylintrc', 'a/b/__init__.py', 'a/b/pylintrc',
                          'a/b/c/__init__.py', 'a/b/c/d/__init__.py'])
            fake_home = tempfile.mkdtemp('fake-home')
            home = os.environ[HOME]
            try:
                os.environ[HOME] = fake_home
                self.assertEqual(config.find_pylintrc(), None)
            finally:
                os.environ[HOME] = home
                os.rmdir(fake_home)
            results = {'a'       : join(chroot, 'a', 'pylintrc'),
                       'a/b'     : join(chroot, 'a', 'b', 'pylintrc'),
                       'a/b/c'   : join(chroot, 'a', 'b', 'pylintrc'),
                       'a/b/c/d' : join(chroot, 'a', 'b', 'pylintrc'),
                       }
            for basedir, expected in list(results.items()):
                os.chdir(join(chroot, basedir))
                self.assertEqual(config.find_pylintrc(), expected)
예제 #26
0
    def test_pylintrc_parentdir(self):
        with tempdir() as chroot:

            create_files(['a/pylintrc', 'a/b/__init__.py', 'a/b/pylintrc',
                          'a/b/c/__init__.py', 'a/b/c/d/__init__.py'])
            fake_home = tempfile.mkdtemp('fake-home')
            home = os.environ[HOME]
            try:
                os.environ[HOME] = fake_home
                self.assertEqual(config.find_pylintrc(), None)
            finally:
                os.environ[HOME] = home
                os.rmdir(fake_home)
            results = {'a'       : join(chroot, 'a', 'pylintrc'),
                       'a/b'     : join(chroot, 'a', 'b', 'pylintrc'),
                       'a/b/c'   : join(chroot, 'a', 'b', 'pylintrc'),
                       'a/b/c/d' : join(chroot, 'a', 'b', 'pylintrc'),
                       }
            for basedir, expected in list(results.items()):
                os.chdir(join(chroot, basedir))
                self.assertEqual(config.find_pylintrc(), expected)
예제 #27
0
def run_linter(files=None):
    if files is None:
        return
    logger.info('Linting {}'.format(', '.join(files)))
    rep = ColorizedTextReporter()
    linter = PyLinter(reporter=rep, pylintrc=find_pylintrc())
    linter.load_default_plugins()
    linter.disable('I')
    linter.enable('c-extension-no-member')
    linter.read_config_file()
    linter.load_config_file()
    linter.check(files)
    linter.generate_reports()
예제 #28
0
def test_pylintrc_parentdir_no_package() -> None:
    """Test that we don't find a pylintrc in sub-packages."""
    with tempdir() as chroot:
        with fake_home():
            chroot_path = Path(chroot)
            testutils.create_files(
                ["a/pylintrc", "a/b/pylintrc", "a/b/c/d/__init__.py"]
            )
            assert config.find_pylintrc() is None
            results = {
                "a": chroot_path / "a" / "pylintrc",
                "a/b": chroot_path / "a" / "b" / "pylintrc",
                "a/b/c": None,
                "a/b/c/d": None,
            }
            for basedir, expected in results.items():
                os.chdir(chroot_path / basedir)
                assert next(config.find_default_config_files(), None) == expected
예제 #29
0
    def configure(self, prospector_config, found_files):

        config_messages = []
        extra_sys_path = found_files.get_minimal_syspath()

        # create a list of packages, but don't include packages which are
        # subpackages of others as checks will be duplicated
        packages = [
            os.path.split(p)
            for p in found_files.iter_package_paths(abspath=False)
        ]
        packages.sort(key=len)
        check_paths = set()
        for package in packages:
            package_path = os.path.join(*package)
            if len(package) == 1:
                check_paths.add(package_path)
                continue
            for i in range(1, len(package)):
                if os.path.join(*package[:-i]) in check_paths:
                    break
            else:
                check_paths.add(package_path)

        for filepath in found_files.iter_module_paths(abspath=False):
            package = os.path.dirname(filepath).split(os.path.sep)
            for i in range(0, len(package)):
                if os.path.join(*package[:i + 1]) in check_paths:
                    break
            else:
                check_paths.add(filepath)

        check_paths = [found_files.to_absolute_path(p) for p in check_paths]

        # insert the target path into the system path to get correct behaviour
        self._orig_sys_path = sys.path
        # note: we prepend, so that modules are preferentially found in the
        # path given as an argument. This prevents problems where we are
        # checking a module which is already on sys.path before this
        # manipulation - for example, if we are checking 'requests' in a local
        # checkout, but 'requests' is already installed system wide, pylint
        # will discover the system-wide modules first if the local checkout
        # does not appear first in the path
        sys.path = list(extra_sys_path) + sys.path

        ext_found = False
        configured_by = None

        linter = ProspectorLinter(found_files)
        if prospector_config.use_external_config('pylint'):
            # try to find a .pylintrc
            pylint_options = prospector_config.tool_options('pylint')
            pylintrc = pylint_options.get('config_file')
            external_config = prospector_config.external_config_location(
                'pylint')
            if pylintrc is None or external_config:
                pylintrc = external_config
            if pylintrc is None:
                pylintrc = find_pylintrc()
            if pylintrc is None:
                pylintrc_path = os.path.join(prospector_config.workdir,
                                             '.pylintrc')
                if os.path.exists(pylintrc_path):
                    pylintrc = pylintrc_path

            if pylintrc is not None:
                # load it!
                configured_by = pylintrc
                ext_found = True

                self._args = linter.load_command_line_configuration(
                    check_paths)
                config_messages += self._pylintrc_configure(pylintrc, linter)

        if not ext_found:
            linter.reset_options()
            self._args = linter.load_command_line_configuration(check_paths)
            self._prospector_configure(prospector_config, linter)

        # Pylint 1.4 introduced the idea of explicitly specifying which
        # C-extensions to load. This is because doing so allows them to
        # execute any code whatsoever, which is considered to be unsafe.
        # The following option turns off this, allowing any extension to
        # load again, since any setup.py can execute arbitrary code and
        # the safety gained through this approach seems minimal. Leaving
        # it on means that the inference engine cannot do much inference
        # on modules with C-extensions which is a bit useless.
        linter.set_option('unsafe-load-any-extension', True)

        # we don't want similarity reports right now
        linter.disable('similarities')

        # use the collector 'reporter' to simply gather the messages
        # given by PyLint
        self._collector = Collector(linter.msgs_store)
        linter.set_reporter(self._collector)

        self._linter = linter
        return configured_by, config_messages
예제 #30
0
import os
import sys

from pylint.config import find_pylintrc


src = os.path.join(os.path.dirname(find_pylintrc()), 'src')
tests = os.path.join(os.path.dirname(find_pylintrc()), 'tests')
current = os.path.dirname(find_pylintrc())

sys.path.append(src)
sys.path.append(tests)
sys.path.append(current)
예제 #31
0
def check_repo(
        limit, pylint='pylint', pylintrc=None, pylint_params='',
        suppress_report=False, always_show_violations=False, ignored_files=None):
    """ Main function doing the checks

    :type limit: float
    :param limit: Minimum score to pass the commit
    :type pylint: str
    :param pylint: Path to pylint executable
    :type pylintrc: str
    :param pylintrc: Path to pylintrc file
    :type pylint_params: str
    :param pylint_params: Custom pylint parameters to add to the pylint command
    :type suppress_report: bool
    :param suppress_report: Suppress report if score is below limit
    :type always_show_violations: bool
    :param always_show_violations: Show violations in case of pass as well
    :type ignored_files: list
    :param ignored_files: List of files to exclude from the validation
    """
    # Lists are mutable and should not be assigned in function arguments
    if ignored_files is None:
        ignored_files = []

    # List of checked files and their results
    python_files = []

    # Set the exit code
    all_filed_passed = True

    if pylintrc is None:
        # If no config is found, use the old default '.pylintrc'
        pylintrc = pylint_config.find_pylintrc() or '.pylintrc'

    # Stash any unstaged changes while we look at the tree
    with _stash_unstaged():
        # Find Python files
        for filename in _get_list_of_committed_files():
            try:
                if _is_python_file(filename) and \
                        not _is_ignored(filename, ignored_files):
                    python_files.append((filename, None))
            except IOError:
                print('File not found (probably deleted): {}\t\tSKIPPED'.format(
                    filename))

        # Don't do anything if there are no Python files
        if len(python_files) == 0:
            sys.exit(0)

        # Load any pre-commit-hooks options from a .pylintrc file (if there is one)
        if os.path.exists(pylintrc):
            conf = configparser.SafeConfigParser()
            conf.read(pylintrc)
            if conf.has_option('pre-commit-hook', 'command'):
                pylint = conf.get('pre-commit-hook', 'command')
            if conf.has_option('pre-commit-hook', 'params'):
                pylint_params += ' ' + conf.get('pre-commit-hook', 'params')
            if conf.has_option('pre-commit-hook', 'limit'):
                limit = float(conf.get('pre-commit-hook', 'limit'))

        # Pylint Python files
        i = 1
        for python_file, score in python_files:
            # Allow __init__.py files to be completely empty
            if os.path.basename(python_file) == '__init__.py':
                if os.stat(python_file).st_size == 0:
                    print(
                        'Skipping pylint on {} (empty __init__.py)..'
                        '\tSKIPPED'.format(python_file))

                    # Bump parsed files
                    i += 1
                    continue

            # Start pylinting
            sys.stdout.write("Running pylint on {} (file {}/{})..\t".format(
                python_file, i, len(python_files)))
            sys.stdout.flush()
            try:
                command = [pylint]
                if pylint_params:
                    command += pylint_params.split()
                    if '--rcfile' not in pylint_params:
                        command.append('--rcfile={}'.format(pylintrc))
                else:
                    command.append('--rcfile={}'.format(pylintrc))

                command.append(python_file)
                proc = subprocess.Popen(
                    command,
                    stdout=subprocess.PIPE,
                    stderr=subprocess.PIPE)

                out, _ = proc.communicate()
            except OSError:
                print("\nAn error occurred. Is pylint installed?")
                sys.exit(1)

            # Verify the score
            score = _parse_score(out)
            ignored = _check_ignore(out)
            if ignored or score >= float(limit):
                status = 'PASSED'
            else:
                status = 'FAILED'
                all_filed_passed = False

            # Add some output
            print('{:.2}/10.00\t{}{}'.format(
                decimal.Decimal(score),
                status,
                ignored and '\tIGNORED' or ''))

            status_check_list = ['FAILED']

            if always_show_violations:
                status_check_list.append('PASSED')

            if status in status_check_list:
                if suppress_report:
                    command.append('--reports=n')
                    proc = subprocess.Popen(
                        command,
                        stdout=subprocess.PIPE,
                        stderr=subprocess.PIPE)
                    out, _ = proc.communicate()

                print(_futurize_str(out))

            # Bump parsed files
            i += 1

    return all_filed_passed
예제 #32
0
def check_repo(limit,
               pylint='pylint',
               pylintrc=None,
               pylint_params='',
               suppress_report=False,
               always_show_violations=False,
               ignored_files=None,
               stash=False):
    """ Main function doing the checks

    :type limit: float
    :param limit: Minimum score to pass the commit
    :type pylint: str
    :param pylint: Path to pylint executable
    :type pylintrc: str
    :param pylintrc: Path to pylintrc file
    :type pylint_params: str
    :param pylint_params: Custom pylint parameters to add to the pylint command
    :type suppress_report: bool
    :param suppress_report: Suppress report if score is below limit
    :type always_show_violations: bool
    :param always_show_violations: Show violations in case of pass as well
    :type ignored_files: list
    :param ignored_files: List of files to exclude from the validation
    :type stash: bool
    :param stash: Stash any unstaged changes while linting
    """
    # Lists are mutable and should not be assigned in function arguments
    if ignored_files is None:
        ignored_files = []

    # List of checked files and their results
    python_files = []

    # Set the exit code
    all_filed_passed = True

    if pylintrc is None:
        # If no config is found, use the old default '.pylintrc'
        pylintrc = pylint_config.find_pylintrc() or '.pylintrc'

    if stash:
        maybe_stash_unstaged = _stash_unstaged
    else:
        maybe_stash_unstaged = _noop

    # Optionally stash any unstaged changes while we look at the tree
    with maybe_stash_unstaged():
        # Find Python files
        for filename in _get_list_of_committed_files():
            try:
                if _is_python_file(filename) and \
                        not _is_ignored(filename, ignored_files):
                    python_files.append((filename, None))
            except IOError:
                print(
                    'File not found (probably deleted): {}\t\tSKIPPED'.format(
                        filename))

        # Don't do anything if there are no Python files
        if len(python_files) == 0:
            return True

        # Load any pre-commit-hooks options from a .pylintrc file (if there is one)
        if os.path.exists(pylintrc):
            conf = configparser.SafeConfigParser()
            conf.read(pylintrc)
            if conf.has_option('pre-commit-hook', 'command'):
                pylint = conf.get('pre-commit-hook', 'command')
            if conf.has_option('pre-commit-hook', 'params'):
                pylint_params += ' ' + conf.get('pre-commit-hook', 'params')
            if conf.has_option('pre-commit-hook', 'limit'):
                limit = float(conf.get('pre-commit-hook', 'limit'))

        # Pylint Python files
        i = 1
        for python_file, score in python_files:
            # Allow __init__.py files to be completely empty
            if os.path.basename(python_file) == '__init__.py':
                if os.stat(python_file).st_size == 0:
                    print('Skipping pylint on {} (empty __init__.py)..'
                          '\tSKIPPED'.format(python_file))

                    # Bump parsed files
                    i += 1
                    continue

            # Start pylinting
            sys.stdout.write("Running pylint on {} (file {}/{})..\t".format(
                python_file, i, len(python_files)))
            sys.stdout.flush()
            try:
                command = [pylint]
                if pylint_params:
                    command += pylint_params.split()
                    if '--rcfile' not in pylint_params:
                        command.append('--rcfile={}'.format(pylintrc))
                else:
                    command.append('--rcfile={}'.format(pylintrc))

                command.append(python_file)
                proc = subprocess.Popen(command,
                                        stdout=subprocess.PIPE,
                                        stderr=subprocess.PIPE)

                out, _ = proc.communicate()
            except OSError:
                print("\nAn error occurred. Is pylint installed?")
                return False

            # Verify the score
            score = _parse_score(out)
            ignored = _check_ignore(out)
            if ignored or score >= float(limit):
                status = 'PASSED'
            else:
                status = 'FAILED'
                all_filed_passed = False

            # Add some output
            print('{:.2}/10.00\t{}{}'.format(decimal.Decimal(score), status,
                                             ignored and '\tIGNORED' or ''))

            status_check_list = ['FAILED']

            if always_show_violations:
                status_check_list.append('PASSED')

            if status in status_check_list:
                if suppress_report:
                    command.append('--reports=n')
                    proc = subprocess.Popen(command,
                                            stdout=subprocess.PIPE,
                                            stderr=subprocess.PIPE)
                    out, _ = proc.communicate()

                print(_futurize_str(out))

            # Bump parsed files
            i += 1

    return all_filed_passed
예제 #33
0
    def configure(self, prospector_config, found_files):

        config_messages = []
        extra_sys_path = found_files.get_minimal_syspath()

        # create a list of packages, but don't include packages which are
        # subpackages of others as checks will be duplicated
        packages = [p.split(os.path.sep) for p in found_files.iter_package_paths(abspath=False)]
        packages.sort(key=len)
        check_paths = set()
        for package in packages:
            package_path = os.path.join(*package)
            if len(package) == 1:
                check_paths.add(package_path)
                continue
            for i in range(1, len(package)):
                if os.path.join(*package[:-i]) in check_paths:
                    break
            else:
                check_paths.add(package_path)

        for filepath in found_files.iter_module_paths(abspath=False):
            package = os.path.dirname(filepath).split(os.path.sep)
            for i in range(0, len(package)):
                if os.path.join(*package[:i + 1]) in check_paths:
                    break
            else:
                check_paths.add(filepath)

        check_paths = [found_files.to_absolute_path(p) for p in check_paths]

        # insert the target path into the system path to get correct behaviour
        self._orig_sys_path = sys.path
        # note: we prepend, so that modules are preferentially found in the
        # path given as an argument. This prevents problems where we are
        # checking a module which is already on sys.path before this
        # manipulation - for example, if we are checking 'requests' in a local
        # checkout, but 'requests' is already installed system wide, pylint
        # will discover the system-wide modules first if the local checkout
        # does not appear first in the path
        sys.path = list(extra_sys_path) + sys.path

        ext_found = False
        configured_by = None

        linter = ProspectorLinter(found_files)
        if prospector_config.use_external_config('pylint'):
            # try to find a .pylintrc
            pylintrc = prospector_config.external_config_location('pylint')
            if pylintrc is None:
                pylintrc = find_pylintrc()
            if pylintrc is None:
                pylintrc_path = os.path.join(prospector_config.workdir, '.pylintrc')
                if os.path.exists(pylintrc_path):
                    pylintrc = pylintrc_path

            if pylintrc is not None:
                # load it!
                configured_by = pylintrc
                ext_found = True

                self._args = linter.load_command_line_configuration(check_paths)
                config_messages += self._pylintrc_configure(pylintrc, linter)

        if not ext_found:
            linter.reset_options()
            self._args = linter.load_command_line_configuration(check_paths)
            self._prospector_configure(prospector_config, linter)

        # Pylint 1.4 introduced the idea of explicitly specifying which C-extensions
        # to load. This is because doing so allows them to execute any code whatsoever,
        # which is considered to be unsafe. The following option turns off this, allowing
        # any extension to load again, since any setup.py can execute arbitrary code and
        # the safety gained through this approach seems minimal. Leaving it on means
        # that the inference engine cannot do much inference on modules with C-extensions
        # which is a bit useless.
        linter.set_option('unsafe-load-any-extension', True)

        # we don't want similarity reports right now
        linter.disable('similarities')

        # use the collector 'reporter' to simply gather the messages
        # given by PyLint
        self._collector = Collector(linter.msgs_store)
        linter.set_reporter(self._collector)

        self._linter = linter
        return configured_by, config_messages