Calculate the cyclomatic complexity of the source code.
Current supported languages:
The tool is not published to Pypi, you can install it directly from source code.
pip install git+https://github.com/brian-villanueva/mccabe.git
You can use mccabe
both as a CLI program and as a module in your program.
> mcc --help
Usage: mcc [OPTIONS]
Calculate the cyclomatic complexity of the source code
Options:
-V, --version Show the version and exit.
-d, --directory TEXT The source code directory
-f, --file TEXT The source code file
-m, --min INTEGER The min threshold value
-l, --language [py|go|js] The source code language [required]
--help Show this message and exit.
You can calculate the cyclomatic complexity for a single source file.
> mcc -l py -f tests/test_languages.py
{
"tests/test_languages.py": 9
}
You can also calculate the cyclomatic complexity for all files inside a directory, which leverages parallel computing.
> mcc -l py -d tests
{
"tests/test_languages.py": 9,
"tests/__init__.py": 0,
"tests/test_provider.py": 4,
"tests/test_cyclomaticc_complexity.py": 8,
"tests/sources/py.py": 24
}
You can set a minimun threshold value by using the -m
flag.
> mcc -l py -d tests -m 5
{
"tests/test_languages.py": 9,
"tests/test_cyclomaticc_complexity.py": 8,
"tests/sources/py.py": 24
}
Also, you can load it as a module in your programs.
from mcc import get_provider_class
from mcc.languages import Lang
cls = get_provider_class(Lang.py)
ret = cls(file="test.py").run()
print(ret)
First, you need to clone the repo.
git clone https://github.com/brian-villanueva/mccabe.git
Then install the requirements.
# first you may need to install poetry: https://python-poetry.org/docs/
pip install -U poetry
make deps
Before commiting your code, you should check the code style and syntax.
make style ci
If you want to add other language support by tree-sitter
like c++
, just follow the steps:
-
Run
git submodule add https://github.com/tree-sitter/tree-sitter-cpp.git vendor/tree-sitter-cpp
-
Update
build.py
to addcpp
support.Language.build_library( 'languages.so', [ 'vendor/tree-sitter-go', 'vendor/tree-sitter-javascript', 'vendor/tree-sitter-python', 'vendor/tree-sitter-cpp' ] )
-
Update
languages.py
addcpp
enum.class Lang(str, Enum): py = "py" go = "go" js = "js" cpp = "cpp"
-
Create new file
cpp.py
and write a class which inheritMccabe
.from mcc.providers import Mccabe from mcc.languages import Lang class MccabeCpp(Mccabe): suffix = ".cpp" language = Lang.cpp judge_nodes = [...]
That's all! You can now calculate the cyclomatic complexity of cpp
source files!
This project is licensed under the Apache-2.0 License.