我们有一个 Python 应用程序,想要计算特定目录及其子目录下的所有代码行。
我们不需要忽略注释,但我们想忽略所有包含测试用例的文件。
测试用例文件的路径在路径中始终包含
。/tests/
(例如/python/trieus/persistence/service/tests/batch_service_tests.py)
我使用下面的命令来查找计数,但它没有排除测试文件。
find . -name "*.py" -not -path "./tests*" | xargs wc -l | sort
这里正确的语法是什么?
最佳答案
你可以这样做:
find . -type d -name tests -prune -o -type f -name '*.py' \
-exec grep -hxc '.*' {} + | paste -sd+ | bc
查找 . -type d -nametests -prune
排除tests
目录-type f -name '*.py'
仅匹配.py
文件-exec grep -hxc '.*' {} +
获取单独的行,您可以修改正则表达式模式以满足您的需要paste -sd+
将输出格式化为一行,中间有+
bc
对 STDIN 数据进行加法
我建议您一步一步地进行,以便更好地理解整个事情。
顺便说一句,这不会为您提供实际的 LOC,而只会为您提供行数。
在我的系统上的示例目录中运行的示例:
% find . -type d -name tests -prune -o -type f -name '*.py' -exec grep -hxc '.*' {} + | paste -sd+ | bc
5594
关于python - 查找不包括测试用例的 Python 代码总行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56757921/