Created
April 22, 2018 11:07
-
-
Save knktc/d562223f78a837b7199e51f700ff61e8 to your computer and use it in GitHub Desktop.
code lines counter written in python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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