likes
comments
collection
share

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

作者站长头像
站长
· 阅读数 6

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

更多相关文章还可参考:

工具一览

  • pipenv
  • flake8
  • coverage.py
  • python-dotenv
  • bandit
  • safety
  • pre-commit
  • services

环境管理:pipenv

github 地址:github.com/pypa/pipenv

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

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

style & linting : flake8

github 地址:github.com/PyCQA/flake…

文档地址:flake8.pycqa.org/en/latest/i…

基本使用方法:

检查整个项目:

flake8 path/to/your_project/

检查单个文件:

flake8 path/to/your_file.py 

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

flake8 --select E722 . | more

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

详细的有哪些 flag 可以在这里查看:flake8.pycqa.org/en/latest/u…

flake8配置文件

文档在:flake8.pycqa.org/en/latest/u…

可以添加在项目根目录的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 的文档可见:pre-commit.com/#pre-commit…

示例:把下面这个配置添加到.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 地址:github.com/nedbat/cove…

简单介绍一下什么是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),就能看到所有文件的统计结果了:

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

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

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

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

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

github 地址:github.com/theskumar/p…

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

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

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

安全漏洞检测:bandit

github 地址:github.com/PyCQA/bandi…

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

bandit -r . | more

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

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

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

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

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

第三方依赖漏洞检测 : safety

github 地址:github.com/pyupio/safe…

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

safety check 

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

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

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

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

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

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

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

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

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

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

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

pre-commit

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

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

其他 services

CI

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

Error Tracking

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

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

如果你像我一样真正热爱计算机科学,喜欢研究底层逻辑,欢迎关注我的微信公众号:

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

转载自:https://juejin.cn/post/6844903838382555143
评论
请登录