Code line count code implemented in Python

From , 4 Years ago, written in Python, viewed 51 times.
URL https://pastebin.vip/view/ad8eda80
  1. '''
  2.      Author: liupengfei
  3.    Function: count lines of code in a folder iteratively
  4. Shell-format: cmd [dir]
  5.   Attention: default file encode is utf8 and default file type is java-source-file. But users can customize this script by just modifing global variables.
  6. '''
  7. import sys
  8. import os
  9. import codecs
  10. from _pyio import open
  11. totalCount = 0;
  12. fileType = '.java'
  13. descLineBegin = '//'
  14. descBlockBegin = r'/**'
  15. descBlockEnd = r'*/'
  16. fileEncode = 'utf-8'
  17. def main():
  18.     DIR = os.getcwd()
  19.     if len(sys.argv) >= 2:
  20.         DIR = sys.argv[1]
  21.     if os.path.exists(DIR) and os.path.isdir(DIR):
  22.         print('target directory is %s' % DIR)
  23.         countDir(DIR)
  24.         print('total code line is %d' % totalCount)
  25.     else:
  26.         print('target should be a directory!')
  27. def isFileType(file):
  28.     return len(fileType) + file.find(fileType) == len(file)
  29. def countDir(DIR):
  30.     for file in os.listdir(DIR):
  31.         absPath = DIR + os.path.sep + file;
  32.         if os.path.exists(absPath):
  33.             if os.path.isdir(absPath):
  34.                 countDir(absPath)
  35.             elif isFileType(absPath):
  36.                 try:
  37.                     countFile(absPath)
  38.                 except UnicodeDecodeError:
  39.                     print(
  40.                         '''encode of %s is different, which
  41. is not supported in this version!'''
  42.                         )
  43. def countFile(file):
  44.     global totalCount
  45.     localCount = 0
  46.     isInBlockNow = False
  47.     f = codecs.open(file, 'r', fileEncode);
  48.     for line in f:
  49.         if (not isInBlockNow) and line.find(descLineBegin) == 0:
  50.             pass;
  51.         elif (not isInBlockNow) and line.find(descBlockBegin) >= 0:
  52.             if line.find(descBlockBegin) > 0:
  53.                 localCount += 1
  54.             isInBlockNow = True;
  55.         elif isInBlockNow and line.find(descBlockEnd) >= 0:
  56.             if line.find(descBlockEnd) + len(descBlockEnd) < len(line):
  57.                 localCount += 1
  58.             isInBlockNow = False;
  59.         elif (not isInBlockNow) and len(line.replace('\\s+', '')) > 0:
  60.             localCount += 1
  61.     f.close()
  62.     totalCount += localCount
  63.     print('%s : %d' % (file, localCount))
  64. if __name__ == '__main__':
  65.     main();
  66.  
  67. #//python/6105

Reply to "Code line count code implemented in Python"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website