ソースコードの行数を数える

メモです。コメントも数える。最近インデントがスペース2個になりつつある。

import os
import sys
import re

def main():
  if len(sys.argv) != 3:
    print 'syntax:\n  scriptname directory ext1|ext2|...'
    return
  iter = os.walk(sys.argv[1])
  re_ext = re.compile(r'^\.(%s)$' % sys.argv[2])
  count = 0
  file_count = 0
  for current, dirs, files in iter:
    for fn in files:
      base, ext = os.path.splitext(fn)
      if re_ext.match(ext):
        file_count += 1
        f = open(os.path.join(current, fn), 'r')
        count += len(f.readlines())
        f.close()
  print 'total %d files, %d lines.' % (file_count, count)

if __name__ == '__main__':
  main()