一个用于计算一个project下code规模的perl小程序,基本思路为用find命令找到project下所有的原代码文件,包括*.c、*.cpp及*.h 文件。然后将其中的每一个文件的行数相加得到code的总的规模。这个程序可以传入project的路径作为参数,若没有参数,则默认project的路径为当前路径。
- #!/usr/bin/perl -w
- $JAVA_FILE = "\\*.java";
- $CPP_FILE = "\\*.cpp";
- $HEADER_FILE = "\\*.h";
- $C_FILE = "\\*.c";
- $PATH = "./";
- sub main {
- my @lines;
- my @files;
- if(@ARGV == 1) {
- $PATH = $ARGV[0];
- }
- @files = `find $PATH -name $C_FILE -o -name $CPP_FILE -o -name $HEADER_FILE`;
- my $total_line_num = 0;
- foreach my $file (@files) {
- chomp $file;
- open FILE, $file;
- @lines = <FILE>;
- close(FILE);
- $line_num = @lines;
- print "$file: $line_num.\n";
- $total_line_num += $line_num;
- }
- print "Total line number in project: $total_line_num.\n";
- }
- &main;
以下是android4.03中harfbuzz, skia 及 icu4c的统计结果,统计均包括各种头文件及测试用文件。首先,是harfbuzz的统计结果:
- /mnt/android_proj/android_src/external/harfbuzz/src/harfbuzz-gsub.c: 4329.
- /mnt/android_proj/android_src/external/harfbuzz/src/harfbuzz-open.h: 288.
- /mnt/android_proj/android_src/external/harfbuzz/src/harfbuzz-gpos.h: 155.
- /mnt/android_proj/android_src/external/harfbuzz/src/harfbuzz-dump.c: 767.
- /mnt/android_proj/android_src/external/harfbuzz/src/harfbuzz-gsub.h: 148.
- /mnt/android_proj/android_src/external/harfbuzz/src/harfbuzz-gpos.c: 6091.
- /mnt/android_proj/android_src/external/harfbuzz/src/harfbuzz-dump.h: 41.
- /mnt/android_proj/android_src/external/harfbuzz/src/harfbuzz-gdef.c: 1163.
- /mnt/android_proj/android_src/external/harfbuzz/src/harfbuzz-impl.c: 84.
- /mnt/android_proj/android_src/external/harfbuzz/src/harfbuzz-buffer-private.h: 107.
- /mnt/android_proj/android_src/external/harfbuzz/contrib/harfbuzz-unicode-icu.c: 202.
- /mnt/android_proj/android_src/external/harfbuzz/contrib/harfbuzz-freetype.h: 9.
- /mnt/android_proj/android_src/external/harfbuzz/contrib/harfbuzz-freetype.c: 149.
- /mnt/android_proj/android_src/external/harfbuzz/contrib/tables/script-properties.h: 297.
- /mnt/android_proj/android_src/external/harfbuzz/contrib/tables/category-properties.h: 2869.
- /mnt/android_proj/android_src/external/harfbuzz/contrib/tables/mirroring-properties.h: 379.
- /mnt/android_proj/android_src/external/harfbuzz/contrib/tables/grapheme-break-properties.h: 1113.
- /mnt/android_proj/android_src/external/harfbuzz/contrib/tables/combining-properties.h: 247.
- /mnt/android_proj/android_src/external/harfbuzz/contrib/harfbuzz-unicode-tables.c: 84.
- /mnt/android_proj/android_src/external/harfbuzz/contrib/harfbuzz-unicode.h: 54.
- /mnt/android_proj/android_src/external/harfbuzz/contrib/harfbuzz-unicode.c: 274.
- /mnt/android_proj/android_src/external/harfbuzz/contrib/harfbuzz-unicode-glib.c: 169.
- Total line number in project: 32296.
然后是skia的:- /mnt/android_proj/android_src/external/skia/gm/points.cpp: 67.
- /mnt/android_proj/android_src/external/skia/gm/complexclip.cpp: 152.
- /mnt/android_proj/android_src/external/skia/gm/filltypes.cpp: 83.
- /mnt/android_proj/android_src/external/skia/gm/xfermodes.cpp: 176.
- /mnt/android_proj/android_src/external/skia/gm/shapes.cpp: 123.
- /mnt/android_proj/android_src/external/skia/gm/poly2poly.cpp: 108.
- /mnt/android_proj/android_src/external/skia/gm/nocolorbleed.cpp: 75.
- /mnt/android_proj/android_src/external/skia/gm/blurs.cpp: 97.
- /mnt/android_proj/android_src/external/skia/gm/tilemodes.cpp: 155.
- /mnt/android_proj/android_src/external/skia/gm/gm.h: 45.
- /mnt/android_proj/android_src/external/skia/gm/strokerects.cpp: 91.
- /mnt/android_proj/android_src/external/skia/gm/shadertext.cpp: 196.
- /mnt/android_proj/android_src/external/skia/gm/shadows.cpp: 114.
- /mnt/android_proj/android_src/external/skia/gm/gradients.cpp: 126.
- Total line number in project: 218197.
最后是icu4c的:- /mnt/android_proj/android_src/external/icu4c/test/letest/letest.cpp: 1126.
- /mnt/android_proj/android_src/external/icu4c/test/letest/FontObject.h: 235.
- /mnt/android_proj/android_src/external/icu4c/test/letest/letsutil.cpp: 113.
- /mnt/android_proj/android_src/external/icu4c/test/letest/letsutil.h: 37.
- /mnt/android_proj/android_src/external/icu4c/test/letest/cletest.c: 742.
- /mnt/android_proj/android_src/external/icu4c/test/letest/gendata.cpp: 297.
- /mnt/android_proj/android_src/external/icu4c/test/letest/PortableFontInstance.cpp: 436.
- /mnt/android_proj/android_src/external/icu4c/test/letest/testdata.cpp: 638.
- /mnt/android_proj/android_src/external/icu4c/test/letest/PortableFontInstance.h: 117.
- Total line number in project: 739801.
icu4c的scale真是够惊人的,竟有如此的庞大。不过也难怪,一个连正则表达式处理引擎都有的project。
一个稍有改进的版本,使用Spreadsheet::WriteExcel module来增加将结果写入Excel表格的功能:
- #!/usr/bin/perl -w
- use Spreadsheet::WriteExcel;
- $JAVA_FILE = "\\*.java";
- $CPP_FILE = "\\*.cpp";
- $HEADER_FILE = "\\*.h";
- $C_FILE = "\\*.c";
- $PATH = "./";
- $OUTPUT_FILE = "output.xls";
- sub main {
- my @lines;
- my @files;
- if(defined $ARGV[0]) {
- $PATH = $ARGV[0];
- }
- if(defined $ARGV[1]) {
- $OUTPUT_FILE = $ARGV[1];
- }
- @files = `find $PATH -name $C_FILE -o -name $CPP_FILE -o -name $HEADER_FILE`;
- my $file_num = @files;
- die "Can't find file $C_FILE or $CPP_FILE or $HEADER_FILE.\n" unless $file_num;
- my $total_line_num = 0;
- my $dest_book = Spreadsheet::WriteExcel->new($OUTPUT_FILE);
- my $dest_sheet = $dest_book->addworksheet("linum");
- my $current_row = 0;
- $dest_sheet->write($current_row, 0, "File");
- $dest_sheet->write($current_row, 1, "Line Number");
- $current_row ++;
- foreach my $file (@files) {
- chomp $file;
- next unless open FILE, $file;
- @lines = <FILE>;
- close(FILE);
- $line_num = @lines;
- print "$file: $line_num.\n";
- $dest_sheet->write($current_row, 0, $file);
- $dest_sheet->write($current_row, 1, $line_num);
- $current_row ++;
- $total_line_num += $line_num;
- }
- $dest_sheet->write($current_row, 0, "Total line number:");
- $dest_sheet->write($current_row, 1, $total_line_num);
- print "Total line number in project: $total_line_num.\n";
- $dest_book->close();
- }
- &main;
参考文档:
阅读(3908) | 评论(2) | 转发(0) |