Skip to content

Instantly share code, notes, and snippets.

@knktc
Created April 22, 2018 11:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save knktc/d562223f78a837b7199e51f700ff61e8 to your computer and use it in GitHub Desktop.
Save knktc/d562223f78a837b7199e51f700ff61e8 to your computer and use it in GitHub Desktop.
code lines counter written in python
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# generate code details with path and code lines count
# usage: python code_lines_counter.py FULL_PATH_TO_YOUR_CODE
"""
@author:knktc
@contact:me@knktc.com
@create:2018-04-22 18:00
"""
import os
import sys
import fnmatch
__author__ = 'knktc'
__version__ = '0.1'
# config
IGNORE_DIR = ['.git', '.idea', 'migrations']
IGNORE_FILETYPE = ['*.pyc', '__init__.py', '.DS_Store', '*.png', '*.gif', '*.jpg']
def line_counter(filepath):
"""
from https://stackoverflow.com/a/1019572
:param :
:return:
:rtype:
"""
return sum(1 for line in open(filepath))
def main():
"""
main process
"""
root_dir = sys.argv[1]
for root, dirs, files in os.walk(root_dir):
# ignore dir
ignore_dir_tag = False
for single_ignore_dir in IGNORE_DIR:
if single_ignore_dir in root:
ignore_dir_tag = True
break
if ignore_dir_tag:
continue
# generate filepath
for single_file in files:
# ignore file
ignore_file_tag = False
for single_ignore_filetype in IGNORE_FILETYPE:
if fnmatch.fnmatch(single_file, single_ignore_filetype):
ignore_file_tag = True
break
if ignore_file_tag:
continue
single_filepath = os.path.join(root, single_file)
line_count = line_counter(single_filepath)
# cut prefix
new_single_filepath = single_filepath.replace(root_dir, '')
print('{}\t{}'.format(new_single_filepath, line_count))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment