Linux | 代碼統計工具SLOCCount

SLOCCount

Linux/Unix下統計代碼行數工具
SLOC means “Source Lines of Code”

Ubuntu14.04下安裝

終端下鍵入命令:
sudo apt-get install sloccount

使用

主要參數

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

如果頂目錄包含幾個不同的項目,並且你想把不同的項目下重複的文件在每個項目中都算上一次,則使用該選項。

支持語言

Ada (.ada, .ads, .adb)
Assembly (.s, .S, .asm)
awk (.awk)
Bourne shell and variants (.sh)
C (.c)
C++ (.C, .cpp, .cxx, .cc)
C shell (.csh)
COBOL (.cob, .cbl) as of version 2.10
C# (.cs) as of version 2.11
Expect (.exp)
Fortran (.f)
Haskell (.hs) as of version 2.11
Java (.java)
lex/flex (.l)
LISP/Scheme (.el, .scm, .lsp, .jl)
Makefile (makefile) - not normally shown.
Modula-3 (.m3, .i3) as of version 2.07
Objective-C (.m)
Pascal (.p, .pas)
Perl (.pl, .pm, .perl)
PHP (.php, .php[3456], .inc) as of version 2.05
Python (.py)
Ruby (.rb) as of version 2.09
sed (.sed)
SQL (.sql) - not normally shown.
TCL (.tcl, .tk, .itk)
Yacc/Bison (.y)

轉換成html文件

官網上給出了sloc2html.py 源文件,但有幾處小bug。

下面是修正後的文件,並添加了部分註釋。
若文件名帶空格,會報錯。

#!/usr/bin/env python
# Written by Rasmus Toftdahl Olesen <[email protected]>
# 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"

# use sys.argv to attain argument of system.
# sys.argv[0] means the path of this code.
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()

# dict()
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>"

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

print "<h2>Projects</h2>"
line = ""
# read one line and move to next line until find out the string
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, "," ):
        if langs == '(none)' or lang =='(none)':
            break
        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]    # exclude the last character
    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>"

html格式呈現

把生成的結果轉換爲帶圖形統計結果的html文件。 缺點是對中文支持不好。
具體操作:
1. 將上述代碼保存爲sloc2html.py文件,放在需要統計的工程目錄下
2. 在終端進入需要統計的工程目錄,鍵入以下命令:
sloccount --wide --multiproject . > result.txt
將統計結果保存至result.txt文件中
3. 將文本結果轉化爲html文件:
python sloc2html.py reslut.txt > result.html
4. 用瀏覽器打開
這裏寫圖片描述

參考

【Linux】代碼統計工具sloccount

官網

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章