-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcount.py
104 lines (80 loc) · 2.76 KB
/
count.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
"""
봇 프로젝트 파일 수 및 코드 줄 수 확인을 위한 코드입니다
이 코드는 다음 코드에서 수정하여 사용했습니다
https://gist.github.com/yaboong/97c3990818220404795fe091a1e7f565
"""
import glob
import operator
# 조사할 디렉토리 경로
ROOT_DIR = './'
# 조사할 확장자 목록
extensions = [
'*.json',
'*.py'
]
# 무시할 디렉토리 경로 목록
ignore_paths = [
'.\\logs',
'.\\venv',
'.\\.git',
'.\\.vs',
'.\\.vscode'
]
# 무시할 파일 목록
ignore_files = [
'.\\.gitignore',
'.\\LICENSE',
'.\\README.md',
'.\\count.py',
'.\\.pylintrc'
]
total_line_count = 0
total_file_count = 0
files_grabbed = []
# dictionary 를 value 기준으로 정렬된 tuple 로 변환
def dict_to_sorted_by_val(tmp_dict, reverse=False):
return sorted(tmp_dict.items(), key=operator.itemgetter(1), reverse=reverse)
# 카운트 함수
def start_count():
global total_line_count, total_file_count
line_count_dict = dict()
extension_count_dict = dict.fromkeys(extensions, 0)
# 설정한 확장자가 포함된 파일 리스트 생성
[files_grabbed.extend(glob.glob(f'{ROOT_DIR}/**/{extension}', recursive=True)) for extension in extensions]
# 파일별로 라인수, 확장자별 갯수, 라인총합, 확장자별 갯수 총합 구함
for file_name_with_path in files_grabbed:
file_name = file_name_with_path.split('/')[-1]
ext = file_name.split('.')[-1]
if file_name in ignore_files:
continue
is_ignored = False
for ignore_path in ignore_paths:
if file_name_with_path.find(ignore_path) != -1:
is_ignored = True
break
if is_ignored:
continue
extension_count_dict['*.' + ext] += 1
line_count = sum(1 for line in open(file_name_with_path, encoding='ISO-8859-1'))
line_count_dict[file_name_with_path] = line_count
total_line_count += line_count
total_file_count += 1
# reverse=True 면 value 기준으로 내림차순 정렬
sorted_line_count = dict_to_sorted_by_val(line_count_dict, reverse=True)
sorted_file_count = dict_to_sorted_by_val(extension_count_dict, reverse=True)
return sorted_line_count, sorted_file_count
# 카운트 함수 실행
line_count, file_count = start_count()
# 출력
for result in line_count:
file = result[0]
count = result[1]
print('{:>4} {:<0}'.format(count, file))
print('\n지정한 확장자별 파일 개수')
for result in file_count:
file = result[0]
count = result[1]
print('{:<7} {:>3} 개'.format(file, count))
print(f'\n프로젝트 전체 파일 수: {total_file_count} 개')
print(f'프로젝트 전체 코드 라인 수: {total_line_count} 줄\n')
input()