Linux下源代码行数统计工具(sloccount, cloc等)

CSDNGitHub
Linux下源代码行数统计工具(sloccount, cloc等)AderXCoding/system/tools/sourcecount


知识共享许可协议本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可

在windows下总是有很多源代码统计工具, 比如SourceCounter(源代码统计精灵)等工具

之前我总是使用如下命令统计源代码的信息, 繁琐而可读性差

find . -type f -name "*.[hc]" | xargs cat | wc -l

或者

 find . -name "*.[hc]" | xargs -L 1 wc -l | awk '{print $1}' | while read num; do total=$((total+num)); echo $total; done

1 sloccount源代码行数统计工具


sloccount=Count Source Lines Of Code

官网 : http://www.dwheeler.com/sloccount/

1.1 Ubuntu安装


sudo apt-get install sloccount

1.2 使用


sloccount  [--version]  [--cached]  [--append]  [  --datadir directory ]
       [--follow]  [--duplicates]  [--crossdups]  [--autogen]  [--multiproject]
       [--filecount] [--wide] [--details] [ --effort F E ] [ --schedule F E ] [
       --personcost cost ] [ --overhead overhead ] [  --addlang  language  ]  [
       --addlangall ] [--] directories
--cached

跳过计算过程,直接使用上次结果

参数描述
–multiproject如果该文件夹包括一系列的子文件夹,而它们中的每一个都是相对独立开发的不同的项目,那么使用”–multiproject”选项,评估将会正确的考虑到这一点
–filecount显示文件数目而非代码行数
–details显示每个源文件的详细信息
–duplicates算上所有重复的(默认情况下如果文件有相同的内容,则只算一个)
–crossdups如果顶目录包含几个不同的项目,并且你想把不同的项目下重复的文件在每个项目中都算上一次,则使用该选项

1.3 转换成html文件


有一个sloc2html.py可以把生成的结果转换为带图形统计结果的html文件. 缺点是对中文支持不好

例如:

sloccount --wide --multiproject SourceDirectory > result.txt
sloc2html.py result.txt > result.html

再打开result.html即可看到结果形如:

result.html示例

下载地址 http://www.dwheeler.com/sloccount/sloc2html.py.txt

输出样例 http://www.dwheeler.com/sloccount/sloc2html-example.html

wget http://www.dwheeler.com/sloccount/sloc2html.py.txt -O sloc2html.py

sloc2html.py文件源代码如下

#!/usr/bin/env python
# Written by Rasmus Toftdahl Olesen <rto@pohldata.dk>
# Modified slightly by David A. Wheeler
# Released under the GNU General Public License v. 2 or higher
from string import *
import sys

NAME = "sloc2html"
VERSION = "0.0.2"

if len(sys.argv) != 2:
    print "Usage:"
    print "\t" + sys.argv[0] + " <sloc output file>"
    print "\nThe output of sloccount should be with --wide and --multiproject formatting"
    sys.exit()

colors = { "python" : "blue",
           "ansic" : "yellow",
           "perl" : "purple",
           "cpp" : "green",
           "sh" : "red",
           "yacc" : "brown",
           "lex" : "silver"
           # Feel free to make more specific colors.
           "ruby" : "maroon",
           "cs" : "gray",
           "java" : "navy",
           "ada" : "olive",
           "lisp" : "fuchsia",
           "objc" : "purple",
           "fortran" : "purple",
           "cobol" : "purple",
           "pascal" : "purple",
           "asm" : "purple",
           "csh" : "purple",
           "tcl" : "purple",
           "exp" : "purple",
           "awk" : "purple",
           "sed" : "purple",
           "makefile" : "purple",
           "sql" : "purple",
           "php" : "purple",
           "modula3" : "purple",
           "ml" : "purple",
           "haskell" : "purple"
          }




print "<html>"
print "<head>"
print "<title>Counted Source Lines of Code (SLOC)</title>"
print "</head>"
print "<body>"
print "<h1>Counted Source Lines of Code</h1>"

file = open ( sys.argv[1], "r" )

print "<h2>Projects</h2>"
line = ""
while line != "SLOC\tDirectory\tSLOC-by-Language (Sorted)\n":
    line = file.readline()

print "<table>"
print "<tr><th>Lines</th><th>Project</th><th>Language distribution</th></tr>"
line = file.readline()
while line != "\n":
    num, project, langs = split ( line )
    print "<tr><td>" + num + "</td><td>" + project + "</td><td>"
    print "<table width=\"500\"><tr>"
    for lang in split ( langs, "," ):
        l, n = split ( lang, "=" )
        print "<td bgcolor=\"" + colors[l] + "\" width=\"" + str( float(n) / float(num) * 500 ) + "\">" + l + "=" + n + "&nbsp;(" + str(int(float(n) / float(num) * 100)) + "%)</td>"
    print "</tr></table>"
    print "</td></tr>"
    line = file.readline()
print "</table>"

print "<h2>Languages</h2>"
while line != "Totals grouped by language (dominant language first):\n":
    line = file.readline()

print "<table>"
print "<tr><th>Language</th><th>Lines</th></tr>"
line = file.readline()
while line != "\n":
    lang, lines, per = split ( line )
    lang = lang[:-1]
    print "<tr><td bgcolor=\"" + colors[lang] + "\">" + lang + "</td><td>" + lines + " " + per + "</td></tr>"
    line = file.readline()
print "</table>"

print "<h2>Totals</h2>"
while line == "\n":
    line = file.readline()

print "<table>"
print "<tr><td>Total Physical Lines of Code (SLOC):</td><td>" + strip(split(line,"=")[1]) + "</td></tr>"
line = file.readline()
print "<tr><td>Estimated development effort:</td><td>" + strip(split(line,"=")[1]) + " person-years (person-months)</td></tr>"
line = file.readline()
line = file.readline()
print "<tr><td>Schedule estimate:</td><td>" + strip(split(line,"=")[1]) + " years (months)</td></tr>"
line = file.readline()
line = file.readline()
print "<tr><td>Total estimated cost to develop:</td><td>" + strip(split(line,"=")[1]) + "</td></tr>"
print "</table>"

file.close()

print "Please credit this data as \"generated using 'SLOCCount' by David A. Wheeler.\"\n"
print "</body>"
print "</html>"

2 cloc代码行数统计工具


cloc也可以用来统计源代码的行数, 其本质是一个perl的脚本

安装

sudo apt-get install cloc

使用

进入到需要统计的目录执行

cloc .

cloc统计源代码行数

其本质是一个perl的脚本, 可以用

file `which cloc`

file <code>which cloc</code>

可以使用

cat `which cloc`

查看其源代码的信息

查看cloc源代码的信息

Python代码行数统计工具有很多种,以下是其中几种常用的工具: 1. cloccloc是一款开源的多语言代码行数统计工具,可以统计各种编程语言的代码行数,包括Python。它可以生成详细的代码统计报告,包括代码行数、空行数、注释行数等。使用cloc,您可以通过命令行或者图形界面界面来统计Python代码的行数。 2. Pygount:Pygount是一个基于Python的代码行数统计工具,它可以统计各种编程语言的代码行数,包括Python。Pygount提供了一个简单易用的命令行界面,可以输出代码行数统计结果。 3. SLOCCountSLOCCount是一个流行的代码行数统计工具,可以统计多种编程语言的代码行数。它可以生成详细的代码行数统计报告,包括代码行数、空行数、注释行数等。SLOCCount可以通过命令行界面或者图形界面来统计Python代码的行数。 4. Radon:Radon是一个Python代码复杂性分析工具,它也可以用来统计代码行数。Radon提供了各种度量方法,包括LOC (Lines of Code)、LLOC (Logical Lines of Code)、SLOC (Source Lines of Code)等,可以帮助您更全面地了解代码的复杂性和行数。 以上是几种常用的Python代码行数统计工具,您可以根据自己的需求和喜好选择适合的工具进行使用。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Python实现代码行数统计工具](https://blog.csdn.net/weixin_30664539/article/details/99054675)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [chatgpt赋能python:Python代码行数统计-统计Python代码行数的常用工具与使用方法](https://blog.csdn.net/findyi123/article/details/130980303)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值