2
0

csr.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #!/usr/bin/python
  2. #
  3. # Copyright (c) 2013-2021 Winlin
  4. #
  5. # SPDX-License-Identifier: MIT
  6. #
  7. #################################################################################
  8. # to stat the code and comments lines
  9. #################################################################################
  10. import sys, os, cs
  11. from cs import info, trace
  12. if __name__ != "__main__":
  13. print "donot support lib"
  14. sys.exit(-1)
  15. filters="*.*pp,*.h,*.c,*.cc"
  16. except_filters="utest,doc"
  17. if len(sys.argv) <= 1:
  18. print "to stat the code and comments lines"
  19. print "Usage: python %s <dir> [filters] [except_filters]"%(sys.argv[0])
  20. print " dir: the dir contains the files to stat"
  21. print " filters: the file filters, default: *.*pp,*.h,*.c,*.cc"
  22. print " except_filters: the except file filters, default: utest,doc"
  23. print "Example:"
  24. print " python %s src"%(sys.argv[0])
  25. print " python %s src *.*pp,*.cc utest,doc"%(sys.argv[0])
  26. sys.exit(-1)
  27. dir = sys.argv[1]
  28. if len(sys.argv) > 2:
  29. filters = sys.argv[2]
  30. if len(sys.argv) > 3:
  31. except_filters = sys.argv[3]
  32. info("stat dir:%s, filters:%s, except_filters:%s"%(dir, filters, except_filters))
  33. # filters to array
  34. filters = filters.split(",")
  35. except_filters = except_filters.split(",")
  36. # find src -name "*.*pp"|grep -v utest
  37. (totals, stat_codes, commentss, stat_block_commentss, stat_line_commentss) = (0, 0, 0, 0, 0)
  38. for filter in filters:
  39. cmd = 'find %s -name "%s"'%(dir, filter)
  40. for ef in except_filters:
  41. cmd = '%s|%s'%(cmd, 'grep -v "%s"'%(ef))
  42. cmd = "%s 2>&1"%(cmd)
  43. info("scan dir, cmd:%s"%cmd)
  44. pipe = os.popen(cmd)
  45. files = pipe.read()
  46. info("scan dir, files:%s"%files)
  47. pipe.close()
  48. files = files.split("\n")
  49. for file in files:
  50. file = file.strip()
  51. if len(file) == 0:
  52. continue;
  53. info("start stat file:%s"%file)
  54. (code, total, stat_code, comments, stat_block_comments, stat_line_comments, code_file) = cs.do_stat(file)
  55. if code != 0:
  56. continue;
  57. totals += total
  58. stat_codes += stat_code
  59. commentss += comments
  60. stat_block_commentss += stat_block_comments
  61. stat_line_commentss += stat_line_comments
  62. if totals == 0:
  63. trace("no code or comments found.")
  64. else:
  65. trace("total:%s code:%s comments:%s(%.2f%%) block:%s line:%s"%(
  66. totals, stat_codes, commentss, commentss * 100.0 / totals, stat_block_commentss, stat_line_commentss
  67. ))