简体   繁体   English

用于计算Python或Bash中的代码行数的实用程序

[英]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. findwc参数可以解决您的问题。

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... 几点说明......

  1. the . 这个。 after the find is important since that's how the cut command can separate the count from the file name 查找之后很重要,因为cut命令可以将计数与文件名分开
  2. this is a multiline command, so make sure there aren't spaces after the escaping slashes 这是一个多行命令,因此请确保在转义斜杠后没有空格
  3. you might need to exclude other files like svn or something. 你可能需要排除其他文件,如svn或其他东西。 Also this will give funny values for binary files so you might want to use grep to whitelist the specific file types you are interested in, ie: grep -e .html$ -e .css$ 此外,这将为二进制文件提供有趣的值,因此您可能希望使用grep将您感兴趣的特定文件类型列入白名单,即: grep -e .html$ -e .css$

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM