推荐一些维护大型 Python 项目的工具

原视频地址:Tools to Manage Large Python Codebases | Fabio Fleitas @ PyBay2018

更多相关文章还可参考:

  • Ask HN: How do you handle large Python projects?

工具一览

  • pipenv

  • flake8

  • coverage.py

  • python-dotenv

  • bandit

  • safety

  • pre-commit

  • services

环境管理:pipenv

github 地址:https://github.com/pypa/pipenv

介绍 pipenv的文章很多了,我就不多细说了,可以参考下面这篇优秀的文章:

  • pipenv 更优雅的管理你的python开发环境

640?wx_fmt=png

style & linting : flake8

github 地址:https://github.com/PyCQA/flake8

文档地址:http://flake8.pycqa.org/en/latest/index.html#

基本使用方法:

检查整个项目:

flake8 path/to/your_project/

检查单个文件:

flake8 path/to/your_file.py 

检测某个特定类型的 flag,比如我想检查一下哪些 tryexcept没有指定具体的 Exception类型:

flake8 --select E722 . | more

640?wx_fmt=png

详细的有哪些 flag 可以在这里查看:http://flake8.pycqa.org/en/latest/user/error-codes.html。

flake8配置文件

文档在:http://flake8.pycqa.org/en/latest/user/configuration.html

可以添加在项目根目录的 tox.inisetup.cfg.flake8文件。

[flake8]	
ignore = D203	
exclude =	
    # No need to traverse our git directory	
    .git,	
    # There's no value in checking cache directories	
    __pycache__,	
    # The conf file is mostly autogenerated, ignore it	
    docs/source/conf.py,	
    # The old directory contains Flake8 2.0	
    old,	
    # This contains our built documentation	
    build,	
    # This contains builds of flake8 that we don't want to check	
    dist	
max-complexity = 10

就等价于:

flake8 --ignore D203 \	
    --exclude .git,__pycache__,docs/source/conf.py,old,build,dist \	
    --max-complexity 10

pre-commit hooks

pre-commit 的文档可见:https://pre-commit.com/#pre-commit-configyaml---hooks

示例:把下面这个配置添加到 .pre-commit-config.yaml

-   repo: https://gitlab.com/pycqa/flake8	
    rev: ''  # pick a git hash / tag to point to	
    hooks:	
    -   id: flake8

code coverage : coverage.py

github 地址:https://github.com/nedbat/coveragepy

简单介绍一下什么是 code coverage

Code coverage is a measurement of how many lines/blocks/arcs of your code are executed while the automated tests are running.来源于此。

就是你运行自动化测试的时候,哪些代码行、块真正被执行了,哪些没有。是衡量 unittest 全面程度的一个重要指标。

直接用 pip就能安装: pip install coverage

比如在 django 中:

coverage run --source='.' manage.py test	
coverage html

就会在项目根目录的 htmlcov/目录生成很多 HTML 文件,在 htmldov/目录下运行http-server (or whatever you like),就能看到所有文件的统计结果了:

640?wx_fmt=png

点开某个文件,就能看到具体哪些行在 ./manage.py test的时候被执行了:

640?wx_fmt=jpeg

如果你只是想看一下简要的信息,可以用 coverage report

区分线上线下环境配置:python-dotenv

github 地址:https://github.com/theskumar/python-dotenv

简单来说就是把配置信息放到一个单独的配置文件里面(比如 .env),和代码解耦。

这个我在上一篇文章中详细说到过,只不过用的是 python-decouple,但是思想都是一样的。这里就不讲了。

640?wx_fmt=png

安全漏洞检测:bandit

github 地址:https://github.com/PyCQA/bandit

基本用法(-r表示检查当前目录下所有文件):

bandit -r . | more

640?wx_fmt=png

能够定位到具体的那一行,还有最有的文档链接。比如这里检测到了 pseudo-random generators,它给出了对应的文档地址:https://bandit.readthedocs.io/en/latest/blacklists/blacklist_calls.html#b311-random

最后面还有一个总结情况:

640?wx_fmt=png

bandit 还有一个特定的配置文件,请看官方文档。

第三方依赖漏洞检测 : safety

github 地址:https://github.com/pyupio/safety

bindit 检查的是你自己的代码,而 safety 检查的是按照的第三方依赖。

safety check 

640?wx_fmt=png

这里检测出了 django有一个编号ID为 36769的安全漏洞。

该编号对应的详情可以在这个文件里面查看。CVE 编号可以在这个网站查到。

640?wx_fmt=png

对应的 django 官方说明位于:https://www.djangoproject.com/weblog/2019/jan/04/security-releases/。指的是 django.views.defaults.page_not_found()这个方法,攻击者有可能会在404页面插入特定内容。

比如一个默认的404页面,之前是这样的:会把path /hello显示出来。

640?wx_fmt=jpeg

如果攻击者把 /hello改一下:

640?wx_fmt=jpeg

就有可能诱导用户点击钓鱼网站。

上诉两张图片来源于:关于Django漏洞CVE-2019-3498的评论

pre-commit

pre-commit 就是一些钩子 bash 脚本,在执行特定 git 操作的时候可以运行这些脚本。比如你可以在 commit或者 push之前运行一下 flake8banditsafety这些工具。

640?wx_fmt=png

其他 services

CI

640?wx_fmt=png

Error Tracking

  • https://github.com/getsentry/sentry

640?wx_fmt=png

640?wx_fmt=jpeg

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值