Chinaunix首页 | 论坛 | 博客
  • 博客访问: 816855
  • 博文数量: 117
  • 博客积分: 2583
  • 博客等级: 少校
  • 技术积分: 1953
  • 用 户 组: 普通用户
  • 注册时间: 2008-12-06 22:58
个人简介

Coder

文章分类
文章存档

2013年(1)

2012年(10)

2011年(12)

2010年(77)

2009年(13)

2008年(4)

分类: Python/Ruby

2012-05-19 22:01:09


一个用于计算一个project下code规模的perl小程序,基本思路为用find命令找到project下所有的原代码文件,包括*.c、*.cpp及*.h 文件。然后将其中的每一个文件的行数相加得到code的总的规模。这个程序可以传入project的路径作为参数,若没有参数,则默认project的路径为当前路径。


点击(此处)折叠或打开

  1. #!/usr/bin/perl -w

  2. $JAVA_FILE = "\\*.java";
  3. $CPP_FILE = "\\*.cpp";
  4. $HEADER_FILE = "\\*.h";
  5. $C_FILE = "\\*.c";
  6. $PATH = "./";

  7. sub main {
  8.     my @lines;
  9.     my @files;
  10.     if(@ARGV == 1) {
  11.         $PATH = $ARGV[0];
  12.     }
  13.     @files = `find $PATH -name $C_FILE -o -name $CPP_FILE -o -name $HEADER_FILE`;
  14.     my $total_line_num = 0;
  15.     foreach my $file (@files) {
  16.         chomp $file;
  17.         open FILE, $file;
  18.         @lines = <FILE>;
  19.         close(FILE);
  20.         $line_num = @lines;
  21.         print "$file: $line_num.\n";
  22.         $total_line_num += $line_num;
  23.     }
  24.     print "Total line number in project: $total_line_num.\n";
  25. }

  26. &main;

以下是android4.03中harfbuzz, skia 及 icu4c的统计结果,统计均包括各种头文件及测试用文件。
首先,是harfbuzz的统计结果:

点击(此处)折叠或打开

  1. /mnt/android_proj/android_src/external/harfbuzz/src/harfbuzz-gsub.c: 4329.
  2. /mnt/android_proj/android_src/external/harfbuzz/src/harfbuzz-open.h: 288.
  3. /mnt/android_proj/android_src/external/harfbuzz/src/harfbuzz-gpos.h: 155.
  4. /mnt/android_proj/android_src/external/harfbuzz/src/harfbuzz-dump.c: 767.
  5. /mnt/android_proj/android_src/external/harfbuzz/src/harfbuzz-gsub.h: 148.
  6. /mnt/android_proj/android_src/external/harfbuzz/src/harfbuzz-gpos.c: 6091.
  7. /mnt/android_proj/android_src/external/harfbuzz/src/harfbuzz-dump.h: 41.
  8. /mnt/android_proj/android_src/external/harfbuzz/src/harfbuzz-gdef.c: 1163.
  9. /mnt/android_proj/android_src/external/harfbuzz/src/harfbuzz-impl.c: 84.
  10. /mnt/android_proj/android_src/external/harfbuzz/src/harfbuzz-buffer-private.h: 107.
  11. /mnt/android_proj/android_src/external/harfbuzz/contrib/harfbuzz-unicode-icu.c: 202.
  12. /mnt/android_proj/android_src/external/harfbuzz/contrib/harfbuzz-freetype.h: 9.
  13. /mnt/android_proj/android_src/external/harfbuzz/contrib/harfbuzz-freetype.c: 149.
  14. /mnt/android_proj/android_src/external/harfbuzz/contrib/tables/script-properties.h: 297.
  15. /mnt/android_proj/android_src/external/harfbuzz/contrib/tables/category-properties.h: 2869.
  16. /mnt/android_proj/android_src/external/harfbuzz/contrib/tables/mirroring-properties.h: 379.
  17. /mnt/android_proj/android_src/external/harfbuzz/contrib/tables/grapheme-break-properties.h: 1113.
  18. /mnt/android_proj/android_src/external/harfbuzz/contrib/tables/combining-properties.h: 247.
  19. /mnt/android_proj/android_src/external/harfbuzz/contrib/harfbuzz-unicode-tables.c: 84.
  20. /mnt/android_proj/android_src/external/harfbuzz/contrib/harfbuzz-unicode.h: 54.
  21. /mnt/android_proj/android_src/external/harfbuzz/contrib/harfbuzz-unicode.c: 274.
  22. /mnt/android_proj/android_src/external/harfbuzz/contrib/harfbuzz-unicode-glib.c: 169.
  23. Total line number in project: 32296.
然后是skia的:

点击(此处)折叠或打开

  1. /mnt/android_proj/android_src/external/skia/gm/points.cpp: 67.
  2. /mnt/android_proj/android_src/external/skia/gm/complexclip.cpp: 152.
  3. /mnt/android_proj/android_src/external/skia/gm/filltypes.cpp: 83.
  4. /mnt/android_proj/android_src/external/skia/gm/xfermodes.cpp: 176.
  5. /mnt/android_proj/android_src/external/skia/gm/shapes.cpp: 123.
  6. /mnt/android_proj/android_src/external/skia/gm/poly2poly.cpp: 108.
  7. /mnt/android_proj/android_src/external/skia/gm/nocolorbleed.cpp: 75.
  8. /mnt/android_proj/android_src/external/skia/gm/blurs.cpp: 97.
  9. /mnt/android_proj/android_src/external/skia/gm/tilemodes.cpp: 155.
  10. /mnt/android_proj/android_src/external/skia/gm/gm.h: 45.
  11. /mnt/android_proj/android_src/external/skia/gm/strokerects.cpp: 91.
  12. /mnt/android_proj/android_src/external/skia/gm/shadertext.cpp: 196.
  13. /mnt/android_proj/android_src/external/skia/gm/shadows.cpp: 114.
  14. /mnt/android_proj/android_src/external/skia/gm/gradients.cpp: 126.
  15. Total line number in project: 218197.
最后是icu4c的:

点击(此处)折叠或打开

  1. /mnt/android_proj/android_src/external/icu4c/test/letest/letest.cpp: 1126.
  2. /mnt/android_proj/android_src/external/icu4c/test/letest/FontObject.h: 235.
  3. /mnt/android_proj/android_src/external/icu4c/test/letest/letsutil.cpp: 113.
  4. /mnt/android_proj/android_src/external/icu4c/test/letest/letsutil.h: 37.
  5. /mnt/android_proj/android_src/external/icu4c/test/letest/cletest.c: 742.
  6. /mnt/android_proj/android_src/external/icu4c/test/letest/gendata.cpp: 297.
  7. /mnt/android_proj/android_src/external/icu4c/test/letest/PortableFontInstance.cpp: 436.
  8. /mnt/android_proj/android_src/external/icu4c/test/letest/testdata.cpp: 638.
  9. /mnt/android_proj/android_src/external/icu4c/test/letest/PortableFontInstance.h: 117.
  10. Total line number in project: 739801.
icu4c的scale真是够惊人的,竟有如此的庞大。不过也难怪,一个连正则表达式处理引擎都有的project。

一个稍有改进的版本,使用Spreadsheet::WriteExcel module来增加将结果写入Excel表格的功能:

点击(此处)折叠或打开

  1. #!/usr/bin/perl -w

  2. use Spreadsheet::WriteExcel;

  3. $JAVA_FILE = "\\*.java";
  4. $CPP_FILE = "\\*.cpp";
  5. $HEADER_FILE = "\\*.h";
  6. $C_FILE = "\\*.c";
  7. $PATH = "./";
  8. $OUTPUT_FILE = "output.xls";
  9. sub main {
  10.     my @lines;
  11.     my @files;
  12.     if(defined $ARGV[0]) {
  13.         $PATH = $ARGV[0];
  14.     }
  15.     if(defined $ARGV[1]) {
  16.         $OUTPUT_FILE = $ARGV[1];
  17.     }
  18.     @files = `find $PATH -name $C_FILE -o -name $CPP_FILE -o -name $HEADER_FILE`;
  19.     my $file_num = @files;
  20.     die "Can't find file $C_FILE or $CPP_FILE or $HEADER_FILE.\n" unless $file_num;
  21.     my $total_line_num = 0;
  22.     my $dest_book = Spreadsheet::WriteExcel->new($OUTPUT_FILE);
  23.     my $dest_sheet = $dest_book->addworksheet("linum");
  24.     my $current_row = 0;
  25.     $dest_sheet->write($current_row, 0, "File");
  26.     $dest_sheet->write($current_row, 1, "Line Number");
  27.     $current_row ++;
  28.     foreach my $file (@files) {
  29.         chomp $file;
  30.         next unless open FILE, $file;
  31.         @lines = <FILE>;
  32.         close(FILE);
  33.         $line_num = @lines;
  34.         print "$file: $line_num.\n";
  35.         $dest_sheet->write($current_row, 0, $file);
  36.         $dest_sheet->write($current_row, 1, $line_num);
  37.         $current_row ++;
  38.         $total_line_num += $line_num;
  39.     }
  40.     $dest_sheet->write($current_row, 0, "Total line number:");
  41.     $dest_sheet->write($current_row, 1, $total_line_num);
  42.     print "Total line number in project: $total_line_num.\n";
  43.     $dest_book->close();
  44. }

  45. &main;
参考文档:
功能丰富的 Perl: 用 Perl 读写 Excel 文件, http://www.ibm.com/developerworks/cn/linux/sdk/perl/culture-8/index.html
阅读(3895) | 评论(2) | 转发(0) |
给主人留下些什么吧!~~

3783335812012-05-23 11:22:34

可以看看,借鉴一下

1471893852012-05-21 21:55:18

呵呵,程序不错,顶~