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

发布于 2024-12-05 14:05:53 字数 374 浏览 3 评论 0原文

python 或 bash 脚本中是否有一种快速而肮脏的方法,可以递归地下降目录并计算代码的总行数?不过,我们希望能够排除某些目录。

例如:

start at: /apps/projects/reallycoolapp
exclude: lib/, frameworks/

排除的目录也应该是递归的。例如:

/app/projects/reallycool/lib SHOULD BE EXCLUDED
/app/projects/reallycool/modules/apple/frameworks SHOULD ALSO BE EXCLUDED

这将是一个非常有用的实用程序。

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? 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清引 2024-12-12 14:05:53

发现了一个很棒的实用程序 CLOC。 https://github.com/AlDanial/cloc

这是我们运行的命令:

perl cloc.pl /apps/projects/reallycoolapp --exclude-dir=lib,frameworks

这是输出

--------------------------------------------------------------------------------
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
--------------------------------------------------------------------------------

Found an awesome utility 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
--------------------------------------------------------------------------------
半世蒼涼 2024-12-12 14:05:53

findwc 参数就可以解决您的问题。

使用 find 您可以指定非常复杂的逻辑,如下所示:

find /apps/projects/reallycoolapp -type f -iname '*.py' ! -path '*/lib/*' ! -path '*/frameworks/*' | xargs wc -l

这里 ! 反转条件,因此该命令将计算不在 'lib/' 或 'lib/' 中的每个 python 文件的行数“框架/”目录。

只是不要忘记“*”,否则它不会匹配任何内容。

The find and wc arguments alone can solve your problem.

With find you can specify very complex logic like this:

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.

Just dont forget the '*' or it will not match anything.

南巷近海 2024-12-12 14:05:53
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}'

一些注释

  1. ....查找之后很重要,因为这就是 cut 命令如何将计数与文件名分开,
  2. 这是一个多行命令,因此请确保转义斜杠后面没有空格,
  3. 您可能需要排除其他斜杠像svn之类的文件。此外,这将为二进制文件提供有趣的值,因此您可能需要使用 grep 将您感兴趣的特定文件类型列入白名单,即: grep -e .html$ -e .css$
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
  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. 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$
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文