2
0

csr.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #!/usr/bin/python
  2. '''
  3. The MIT License (MIT)
  4. Copyright (c) 2013-2016 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, os, cs
  24. from cs import info, trace
  25. if __name__ != "__main__":
  26. print "donot support lib"
  27. sys.exit(-1)
  28. filters="*.*pp,*.h,*.c,*.cc"
  29. except_filters="utest,doc"
  30. if len(sys.argv) <= 1:
  31. print "to stat the code and comments lines"
  32. print "Usage: python %s <dir> [filters] [except_filters]"%(sys.argv[0])
  33. print " dir: the dir contains the files to stat"
  34. print " filters: the file filters, default: *.*pp,*.h,*.c,*.cc"
  35. print " except_filters: the except file filters, default: utest,doc"
  36. print "Example:"
  37. print " python %s src"%(sys.argv[0])
  38. print " python %s src *.*pp,*.cc utest,doc"%(sys.argv[0])
  39. sys.exit(-1)
  40. dir = sys.argv[1]
  41. if len(sys.argv) > 2:
  42. filters = sys.argv[2]
  43. if len(sys.argv) > 3:
  44. except_filters = sys.argv[3]
  45. info("stat dir:%s, filters:%s, except_filters:%s"%(dir, filters, except_filters))
  46. # filters to array
  47. filters = filters.split(",")
  48. except_filters = except_filters.split(",")
  49. # find src -name "*.*pp"|grep -v utest
  50. (totals, stat_codes, commentss, stat_block_commentss, stat_line_commentss) = (0, 0, 0, 0, 0)
  51. for filter in filters:
  52. cmd = 'find %s -name "%s"'%(dir, filter)
  53. for ef in except_filters:
  54. cmd = '%s|%s'%(cmd, 'grep -v "%s"'%(ef))
  55. cmd = "%s 2>&1"%(cmd)
  56. info("scan dir, cmd:%s"%cmd)
  57. pipe = os.popen(cmd)
  58. files = pipe.read()
  59. info("scan dir, files:%s"%files)
  60. pipe.close()
  61. files = files.split("\n")
  62. for file in files:
  63. file = file.strip()
  64. if len(file) == 0:
  65. continue;
  66. info("start stat file:%s"%file)
  67. (code, total, stat_code, comments, stat_block_comments, stat_line_comments, code_file) = cs.do_stat(file)
  68. if code != 0:
  69. continue;
  70. totals += total
  71. stat_codes += stat_code
  72. commentss += comments
  73. stat_block_commentss += stat_block_comments
  74. stat_line_commentss += stat_line_comments
  75. if totals == 0:
  76. trace("no code or comments found.")
  77. else:
  78. trace("total:%s code:%s comments:%s(%.2f%%) block:%s line:%s"%(
  79. totals, stat_codes, commentss, commentss * 100.0 / totals, stat_block_commentss, stat_line_commentss
  80. ))