[英]Utility To Count Number Of Lines Of Code In Python Or Bash
Is there a quick and dirty way in either python or bash script, that can recursively descend a directory and count the total number of lines of code? 在python或bash脚本中是否存在快速而肮脏的方式,它可以递归地下降目录并计算代码行的总数? We would like to be able to exclude certain directories though.
我们希望能够排除某些目录。
For example: 例如:
start at: /apps/projects/reallycoolapp
exclude: lib/, frameworks/
The excluded directories should be recursive as well. 排除的目录也应该是递归的。 For example:
例如:
/app/projects/reallycool/lib SHOULD BE EXCLUDED
/app/projects/reallycool/modules/apple/frameworks SHOULD ALSO BE EXCLUDED
This would be a really useful utility. 这将是一个非常有用的实用程序。
Found an awesome utility CLOC. 找到了一个很棒的实用程序CLOC。 https://github.com/AlDanial/cloc
https://github.com/AlDanial/cloc
Here is the command we ran: 这是我们运行的命令:
perl cloc.pl /apps/projects/reallycoolapp --exclude-dir=lib,frameworks
And here is the output 这是输出
--------------------------------------------------------------------------------
Language files blank comment code
--------------------------------------------------------------------------------
PHP 32 962 1352 2609
Javascript 5 176 225 920
Bourne Again Shell 4 45 70 182
Bourne Shell 12 52 113 178
HTML 1 0 0 25
--------------------------------------------------------------------------------
SUM: 54 1235 1760 3914
--------------------------------------------------------------------------------
The find
and wc
arguments alone can solve your problem. 仅
find
和wc
参数可以解决您的问题。
With find
you can specify very complex logic like this: 使用
find
您可以指定非常复杂的逻辑,如下所示:
find /apps/projects/reallycoolapp -type f -iname '*.py' ! -path '*/lib/*' ! -path '*/frameworks/*' | xargs wc -l
Here the !
在这里
!
invert the condition so this command will count the lines for each python files not in 'lib/' or in 'frameworks/' directories. 反转条件,因此该命令将计算不在'lib /'或'frameworks /'目录中的每个python文件的行。
Just dont forget the '*' or it will not match anything. 只是不要忘记'*'或它不会匹配任何东西。
find ./apps/projects/reallycool -type f | \
grep -v -e /app/projects/reallycool/lib \
-e /app/projects/reallycool/modules/apple/frameworks | \
xargs wc -l | \
cut -d '.' -f 1 | \
awk 'BEGIN{total=0} {total += $1} END{print total}'
A few notes... 几点说明......
cut
command can separate the count from the file name cut
命令可以将计数与文件名分开 grep -e .html$ -e .css$
grep -e .html$ -e .css$
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.