Add a .cardboardlint.yml
to the root of your source tree. It should at least contain
a linters
section with a list of linters, e.g.
.. code:: yaml
linters:
- pylint:
pylintrc: tools/your_custom_pylintrc
- cppcheck:
- import:
...
A list of supported linters is easily deduced from the source code. Just look into
the cardboardlint/linter_*.py
files. Each of these files has a module docstring with
some explanations and a DEFAULT_CONFIG dictionary with the available configuration vars.
You can repeat any linter multiple times with different configuration settings. This can
be useful, e.g. when unit tests must be linted differently than the rest of the source
code. This is a simple example where unit tests have a different pylint config:
.. code:: yaml
pre_filefilter: ['+ tools/demo/*.py', '- tools/*', '+ *']
linters:
- pylint:
pylintrc: tools/pylintrc
filefilter: ['- test_*.py', '+ *.py']
exclude:
- pylint:
pylintrc: tools/pylintrc_tests
filefilter: ['+ test_*.py']
- import:
...
When cardboardlint starts, it makes a list of files not ignored by
git in the current repository. These filenames are first filtered by a
so-called pre_filefilter
. Files that pass the pre_filefilter
are then
tested with linter-specific filefilters
to end up with a list of files to
be checked by a given linter.
A pre_filefilter
or filefilter
consists of a list of rules to test if
a file should be considered for linting or not. Each rule starts with a
possible outcome, +
(include) or a -
(exclude), followed by a glob
pattern. At the moment, the pattern ignores the presence of directory
separators and treats the complete path as a single string on which the
pattern is tested, using Pythons fnmatch
builtin module. The rules are
tested in order and when a pattern matches, the corresponding decision is made
(include and exclude), without considering subsequent rules. When no patterns
give a match, the file is excluded.
The following tricks might be useful:
-
If you would like to include files that did not match any pattern, add
'+ *'
as last pattern, which is often useful for the pre_filefilter
.
-
If you would like to include all python files, in all directories, use
'+ *.py'
. The wildcard will also match directories containing the Python
file. For example it would match a/b
in the path a/b.py
.
Install the linters you intend to run (either locally or in your CI environment). These
dependencies are not installed automatically because you may not want to use all of
them.
Conda packages for all supported linters can be found in the main conda
channels an in conda-forge (https://anaconda.org/conda-forge). We have added
packages to conda-forge for cppcheck
and cpplint
. All other linters
were already available. To install all of them, we can recommend the following
commands:
.. code:: bash
Add conda-forga channel with lower priority as the default channels. This
prevents your conda env from being flooded by conda-forga packages.
conda config --append channels conda-forge
Install all linters for which cardboardlint has wrappers:
conda install pycodestyle pydocstyle cppcheck cpplint yamllint flake8
doxygen pylint autopep8, yapf, black