如何通过tox将*.py传递给pycco?

2024-05-13 02:24:57 发布

您现在位置:Python中文网/ 问答频道 /正文

以下命令起作用:

$ pycco *.py
# generates literate-style documentation
# for all .py files in the current folder

并且我的tox.ini文件中的以下代码段按预期工作:

[testenv:pycco]
deps =
    pycco
commands =
    pycco manage.py
# generates literate-style documentation
# for manage.py

但如果我尝试使用glob:

[testenv:pycco]
deps =
    pycco
commands =
    pycco *.py

…我得到以下错误:

  File "/home/user/Documents/project/.tox/pycco/lib/python3.7/site-packages/pycco/main.py", line 79, in generate_documentation
    code = open(source, "rb").read().decode(encoding)
FileNotFoundError: [Errno 2] No such file or directory: '*.py'

如何通过tox将*.py传递给pycco


Tags: depsinpy命令toxformanagestyle
2条回答

这里的问题是pycco不支持glob扩展。使pycco *.py工作的原因是,在执行之前,shell实际上将*.py转换为实际文件;然后将其传递给操作系统以运行它

当tox运行您的命令时,没有涉及shell,因此您编写的任何内容都是as,并传递给操作系统,因此现在pycco实际上获取as参数*.py,因此出现了错误

您可以通过显式列出文件路径或使用python解释器进行扩展来解决此问题:

python -c 'from glob import glob; import subprocess; subprocess.check_call(["pycco"] + glob("*.py"))'

将上面的命令放在tox命令中,事情现在就可以工作了,因为python现在是shell,正在将“*.py”扩展到实际的文件列表

您不能直接这样做,因为pycco(当前)不支持全局扩展。相反,您可以创建一个shell脚本execute_pycco.sh,如下所示:

#!/bin/sh

pycco *.py

更新tox.ini如下:

[testenv:pycco]
deps =
    pycco
commands =
    ./execute_pycco.sh

现在,您将在tox创建的“pycco”环境中执行shell脚本。此方法还允许您定义更详细的脚本:

#!/bin/sh

filelist=$( find . -name '*.py' | grep -v ".tox" )
# make a list of all .py files in all subfolders,
# except the .tox/ subfolder

pycco -ip $filelist
# generate literate-style documentation for all
# files in the list

相关问题 更多 >