正在显示
1 个修改的文件
包含
100 行增加
和
0 行删除
trunk/research/code-statistic/cs.py
0 → 100755
| 1 | +#!/usr/bin/python | ||
| 2 | +''' | ||
| 3 | +The MIT License (MIT) | ||
| 4 | + | ||
| 5 | +Copyright (c) 2013-2014 winlin | ||
| 6 | + | ||
| 7 | +Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
| 8 | +this software and associated documentation files (the "Software"), to deal in | ||
| 9 | +the Software without restriction, including without limitation the rights to | ||
| 10 | +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
| 11 | +the Software, and to permit persons to whom the Software is furnished to do so, | ||
| 12 | +subject to the following conditions: | ||
| 13 | + | ||
| 14 | +The above copyright notice and this permission notice shall be included in all | ||
| 15 | +copies or substantial portions of the Software. | ||
| 16 | + | ||
| 17 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| 18 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
| 19 | +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
| 20 | +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
| 21 | +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
| 22 | +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
| 23 | +''' | ||
| 24 | + | ||
| 25 | +################################################################################# | ||
| 26 | +# to stat the code and comments lines | ||
| 27 | +################################################################################# | ||
| 28 | +import sys | ||
| 29 | + | ||
| 30 | +def trace(msg): | ||
| 31 | + print msg | ||
| 32 | + pass | ||
| 33 | +def info(msg): | ||
| 34 | + print msg | ||
| 35 | + pass | ||
| 36 | +def verbose(msg): | ||
| 37 | + #print msg | ||
| 38 | + pass | ||
| 39 | + | ||
| 40 | +def process(f, code_file): | ||
| 41 | + info("process file success") | ||
| 42 | + (stat_code, stat_block_comments, stat_line_comments) = (0, 0, 0) | ||
| 43 | + is_block_comments = False | ||
| 44 | + is_line_comments = False | ||
| 45 | + for line in f.readlines(): | ||
| 46 | + line = line.strip() | ||
| 47 | + if is_block_comments: | ||
| 48 | + if "*/" in line: | ||
| 49 | + verbose("[block][end] %s"%line) | ||
| 50 | + is_block_comments = False | ||
| 51 | + is_line_comments = False | ||
| 52 | + else: | ||
| 53 | + verbose("[block][cont] %s"%line) | ||
| 54 | + continue | ||
| 55 | + if line.startswith("/*"): | ||
| 56 | + verbose("[block][start] %s"%line) | ||
| 57 | + is_block_comments = True | ||
| 58 | + is_line_comments = False | ||
| 59 | + stat_block_comments += 1 | ||
| 60 | + continue | ||
| 61 | + if line.startswith("//"): | ||
| 62 | + verbose("[line] %s"%line) | ||
| 63 | + is_block_comments = False | ||
| 64 | + is_line_comments = True | ||
| 65 | + stat_line_comments += 1 | ||
| 66 | + continue | ||
| 67 | + verbose("[code] %s"%line) | ||
| 68 | + is_block_comments = False | ||
| 69 | + is_line_comments = False | ||
| 70 | + stat_code += 1 | ||
| 71 | + total = stat_code + stat_block_comments + stat_line_comments | ||
| 72 | + comments = stat_block_comments + stat_line_comments | ||
| 73 | + trace("total:%s code:%s comments:%s block:%s line:%s file:%s"%(total, stat_code, comments, stat_block_comments, stat_line_comments, code_file)) | ||
| 74 | + return (0, total, stat_code, comments, stat_block_comments, stat_line_comments, code_file) | ||
| 75 | + | ||
| 76 | +def do_stat(code_file): | ||
| 77 | + f = None | ||
| 78 | + try: | ||
| 79 | + f = open(code_file, "r") | ||
| 80 | + info("open file success"); | ||
| 81 | + return process(f, code_file) | ||
| 82 | + finally: | ||
| 83 | + if f is not None: | ||
| 84 | + f.close() | ||
| 85 | + info("close file success") | ||
| 86 | + return (-1, 0, 0, 0, 0, 0, None) | ||
| 87 | + | ||
| 88 | +code_file = None | ||
| 89 | +if __name__ == "__main__": | ||
| 90 | + if len(sys.argv) <= 1: | ||
| 91 | + print "to stat the code and comments lines" | ||
| 92 | + print "Usage: python %s <code_file>"%(sys.argv[0]) | ||
| 93 | + print " code_file: the code(header or source) file to stat" | ||
| 94 | + print "Example:" | ||
| 95 | + print " python %s src/core/srs_core.hpp"%(sys.argv[0]) | ||
| 96 | + sys.exit(-1) | ||
| 97 | + | ||
| 98 | + code_file = sys.argv[1] | ||
| 99 | + info("stat %s"%(code_file)) | ||
| 100 | + do_stat(code_file) |
-
请 注册 或 登录 后发表评论