cs.py 3.8 KB

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