【问题标题】:How to clean a tox environment after running?运行后如何清理tox环境?
【发布时间】:2020-01-02 12:55:45
【问题描述】:

我有以下tox.ini 文件:

[tox]
envlist = flake8,py{35,36,37,38}{,-keyring}

[testenv]
usedevelop = True
install_command = pip install -U {opts} {packages}
deps =
    .[test]
    keyring: .[keyring]
setenv =
    COVERAGE_FILE = .coverage.{envname}
commands=
    pytest {toxinidir}/tests -n 4 {posargs}

[testenv:flake8]
basepython = python3
deps = flake8
commands=
    flake8 src tests

[flake8]
ignore: F401,E402,E501,W605,W503

当我运行tox 命令时,它会创建一个.tox 文件夹,其中包含在tox.ini[tox] 部分中指定的每个环境的文件夹。

我想在运行tox 时自动读取这些特定文件夹,而无需手动运行rm -rf .tox/NAME_OF_THE_ENV。我搜索了 tox 文档,但一无所获。

有可能吗?如果是,怎么做?

【问题讨论】:

    标签: python tox


    【解决方案1】:

    我知道这不是您所要求的,但值得一提的是,tox 的 -r / --recreate 标志将 force recreation of virtual environments

    【讨论】:

      【解决方案2】:

      tox 没有办法。原因是tox 将这些环境保存为缓存:下次运行tox 时,这些环境将被重用,从而节省时间。

      您可以在运行toxrm -rf .tox 后立即删除它们。

      【讨论】:

        【解决方案3】:

        我通过创建一个毒钩找到了一种方法。在 env 中运行测试后,此挂钩运行 shutil.rmtree 命令。

        tox_clean_env.py 文件中:

        import shutil
        from tox import hookimpl
        
        @hookimpl
        def tox_runtest_post(venv):
            try:
                shutil.rmtree(venv.path)
            except Exception as e:
                print("An exception occurred while removing '{}':".format(venv.path))
                print(e)
        

        我围绕这段代码创建了一个包,我只需要使用pip 安装它。

        在我的setup.py 中,在setup 函数中:

            entry_points={"tox": ["clean_env = tox_clean_env"]},
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-06-10
          • 2020-05-17
          • 1970-01-01
          • 2020-04-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多