Python代码审计工具
项目描述
Python代码审计工具。Pylama封装了这些工具
pycodestyle (以前称为pep8) © 2012-2013, Florent Xicluna;
pydocstyle (以前称为pep257 by Vladimir Keleshev) © 2014, Amir Rachum;
PyFlakes © 2005-2013, Kevin Watters;
Mccabe © Ned Batchelder;
Pylint © 2013, Logilab;
Radon © Michele Lacchia
eradicate © Steven Myint;
Mypy © Jukka Lehtosalo and contributors;
Vulture © Jendrik Seipp and contributors;
文档可在https://klen.github.io/pylama/找到。增强和/或修复文档的拉取请求非常棒且最受欢迎。
要求
Python(3.7、3.8、3.9、3.10)
如果你的测试在Win平台上失败,你缺少: curses - http://www.lfd.uci.edu/~gohlke/pythonlibs/(curses库为基于文本的终端提供终端无关的屏幕绘制和键盘处理功能)
对于小于3.7的Python版本,请安装pylama 7.7.1
安装
Pylama可以使用pip安装
$ pip install pylama
TOML配置可以选择启用
$ pip install pylama[toml]
你可以选择性地使用库安装需求
$ pip install pylama[mypy] $ pip install pylama[pylint] $ pip install pylama[eradicate] $ pip install pylama[radon] $ pip install pylama[vulture]
或者全部安装它们
$ pip install pylama[all]
快速入门
Pylama易于使用,并且检查代码质量非常有趣。只需运行pylama即可从所有pylama插件(pycodestyle、PyFlakes等)获得常见输出
递归检查当前目录。
$ pylama
递归检查一个路径。
$ pylama <path_to_directory_or_file>
忽略错误
$ pylama -i W,E501
选择代码检查器
$ pylama -l "pycodestyle,mccabe"
设置Pylama(检查器)选项
命令行选项
$ pylama --help usage: pylama [-h] [--version] [--verbose] [--options FILE] [--linters LINTERS] [--from-stdin] [--concurrent] [--format {pydocstyle,pycodestyle,pylint,parsable,json}] [--abspath] [--max-line-length MAX_LINE_LENGTH] [--select SELECT] [--ignore IGNORE] [--skip SKIP] [--sort SORT] [--report REPORT] [--hook] [--max-complexity MAX_COMPLEXITY] [--pydocstyle-convention {pep257,numpy,google}] [--pylint-confidence {HIGH,INFERENCE,INFERENCE_FAILURE,UNDEFINED}] [paths ...] Code audit tool for python. positional arguments: paths Paths to files or directories for code check. optional arguments: -h, --help show this help message and exit --version show program's version number and exit --verbose, -v Verbose mode. --options FILE, -o FILE Specify configuration file. Looks for pylama.ini, setup.cfg, tox.ini, or pytest.ini in the current directory (default: None) --linters LINTERS, -l LINTERS Select linters. (comma-separated). Choices are eradicate,mccabe,mypy,pycodestyle,pydocstyle,pyflakes,pylint,isort. --from-stdin Interpret the stdin as a python script, whose filename needs to be passed as the path argument. --concurrent, --async Enable async mode. Useful for checking a lot of files. --format {pydocstyle,pycodestyle,pylint,parsable,json}, -f {pydocstyle,pycodestyle,pylint,parsable,json} Choose output format. --abspath, -a Use absolute paths in output. --max-line-length MAX_LINE_LENGTH, -m MAX_LINE_LENGTH Maximum allowed line length --select SELECT, -s SELECT Select errors and warnings. (comma-separated list) --ignore IGNORE, -i IGNORE Ignore errors and warnings. (comma-separated) --skip SKIP Skip files by masks (comma-separated, Ex. */messages.py) --sort SORT Sort result by error types. Ex. E,W,D --report REPORT, -r REPORT Send report to file [REPORT] --hook Install Git (Mercurial) hook. --max-complexity MAX_COMPLEXITY Max complexity threshold
文件模式行
你可以在源文件内部设置
Pylama的选项。为此,在文件中的任何位置使用pylama modeline。
格式
# pylama:{name1}={value1}:{name2}={value2}:...
例如,忽略除了W301之外的警告
# pylama:ignore=W:select=W301
禁用当前文件的代码检查
# pylama:skip=1
这些选项具有更高的优先级。
跳过行(noqa)
只需在行的末尾添加# noqa即可忽略
def urgent_fuction(): unused_var = 'No errors here' # noqa
配置文件
Pylama将在当前目录中查找配置文件。
你可以使用存储在主目录中.pylama.ini的“全局”配置。这将被用作后备配置。
程序将按照命令行参数的目录顺序搜索第一个匹配的配置文件。Pylama将按照以下顺序查找配置
./pylama.ini ./pyproject.toml ./setup.cfg ./tox.ini ./pytest.ini ~/.pylama.ini
可以使用--option / -o参数来指定配置文件。
INI样式配置
Pylama会搜索以pylama开头的部分。
pylama部分配置全局选项,如linters和skip。
[pylama] format = pylint skip = */.tox/*,*/.env/* linters = pylint,mccabe ignore = F0401,C0111,E731
设置代码检查器的选项
你可以使用pylama配置为特殊代码检查器设置选项。
[pylama:pyflakes] builtins = _ [pylama:pycodestyle] max_line_length = 100 [pylama:pylint] max_line_length = 100 disable = R
有关更多信息,请参阅代码检查器的文档。请注意,连字符被下划线替换(例如,Pylint的max-line-length变为max_line_length)。
设置文件(文件组)的选项
你可以使用部分为特殊文件(文件组)设置选项
这些选项的优先级高于pylama部分。
[pylama:*/pylama/main.py] ignore = C901,R0914,W0212 select = R [pylama:*/tests.py] ignore = C0110 [pylama:*/setup.py] skip = 1
TOML配置
Pylama会搜索以tool.pylama开头的部分。
tool.pylama部分配置全局选项,如linters和skip。
[tool.pylama] format = "pylint" skip = "*/.tox/*,*/.env/*" linters = "pylint,mccabe" ignore = "F0401,C0111,E731"
设置代码检查器的选项
你可以使用pylama配置为特殊代码检查器设置选项。
[tool.pylama.linter.pyflakes] builtins = "_" [tool.pylama.linter.pycodestyle] max_line_length = 100 [tool.pylama.linter.pylint] max_line_length = 100 disable = "R"
有关更多信息,请参阅代码检查器的文档。请注意,连字符被下划线替换(例如,Pylint的max-line-length变为max_line_length)。
设置文件(文件组)的选项
你可以使用部分为特殊文件(文件组)设置选项
这些选项的优先级高于tool.pylama部分。
[[tool.pylama.files]] path = "*/pylama/main.py" ignore = "C901,R0914,W0212" select = "R" [[tool.pylama.files]] path = "pylama:*/tests.py" ignore = "C0110" [[tool.pylama.files]] path = "pylama:*/setup.py" skip = 1
Pytest集成
Pylama具有Pytest支持。在安装过程中,该包会自动将自己注册为pytest插件。Pylama还支持pytest_cache插件。
使用pylama检查文件
pytest --pylama ...
使用pytest设置pylama选项的推荐方法是配置文件(见下文)。
编写代码检查器
你可以为Pylama编写自定义扩展。自定义代码检查器应该是一个Python模块。其名称应类似于‘pylama_<name>’。
在‘setup.py’中,应该定义‘pylama.linter’入口点。
setup( # ... entry_points={ 'pylama.linter': ['lintername = pylama_lintername.main:Linter'], } # ... )
‘Linter’应该是‘pylama.lint.Linter’类的实例。它必须实现两个方法
allow接受一个path参数,如果代码检查器可以检查此文件的错误,则返回true。
run接受一个path参数和一个关键字参数,并返回错误列表。
示例
只是一个虚拟的“WOW”检查器。
setup.py
setup( name='pylama_wow', install_requires=[ 'setuptools' ], entry_points={ 'pylama.linter': ['wow = pylama_wow.main:Linter'], } # ... )
pylama_wow.py
from pylama.lint import Linter as BaseLinter class Linter(BaseLinter): def allow(self, path): return 'wow' in path def run(self, path, **meta): with open(path) as f: if 'wow' in f.read(): return [{ lnum: 0, col: 0, text: '"wow" has been found.', type: 'WOW' }]
从Python代码运行pylama
from pylama.main import check_paths, parse_options # Use and/or modify 0 or more of the options defined as keys in the variable my_redefined_options below. # To use defaults for any option, remove that key completely. my_redefined_options = { 'linters': ['pep257', 'pydocstyle', 'pycodestyle', 'pyflakes' ...], 'ignore': ['D203', 'D213', 'D406', 'D407', 'D413' ...], 'select': ['R1705' ...], 'sort': 'F,E,W,C,D,...', 'skip': '*__init__.py,*/test/*.py,...', 'async': True, 'force': True ... } # relative path of the directory in which pylama should check my_path = '...' options = parse_options([my_path], **my_redefined_options) errors = check_paths(my_path, options, rootdir='.')
错误追踪器
如果您有任何建议、错误报告或不满,请通过以下链接提交到问题跟踪器:https://github.com/klen/pylama/issues
贡献
pylama 的开发在 GitHub 上进行:https://github.com/klen/pylama
贡献者
查看贡献者。
许可证
这是一款免费软件。您可以在 MIT 许可证的条款下使用、复制、修改、合并、发布、分发、再许可和/或出售其副本。有关完整许可证,请参阅 LICENSE 文件。
本软件提供无任何保证;甚至没有关于其可销售性或适用于特定目的的暗示性保证。有关完整免责声明,请参阅 LICENSE 文件。