-
Notifications
You must be signed in to change notification settings - Fork 0
/
code_counter.py
118 lines (106 loc) · 3.38 KB
/
code_counter.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#-*- coding:utf-8 -*-
'''
Created on 2013-9-2
@author: pkking
'''
import datetime
import os
class time(object):
'''
compute running time
'''
def __init__(self):
'''
Constructor
'''
def start(self):
'''
start count
'''
self.start_time = datetime.datetime.now()
def end(self):
'''
end count
'''
self.end_time = datetime.datetime.now()
def count_time(self):
'''
print the time it cost
'''
t = self.end_time - self.start_time
print('running takes ' + str(t.days) + ' days ' + str(t.seconds)+' seconds and ' + str(t.microseconds)+' microsecond')
class the_code(object):
'''
the code class
'''
def __init__(self):
'''
Constructor
'''
self.c_file_counter =0
self.py_file_counter =0
self.c_file_list = []
self.py_file_list = []
self.c_line_counter = 0
self.py_line_counter = 0
for root_dir,dirs,files in os.walk('.'):
if(self.is_git_dir(root_dir)):
continue
for this_file in files:
tail = os.path.splitext(this_file)
if(tail[1] == '.py'):
self.py_file_list.append(os.path.join(root_dir, this_file))
self.py_file_counter +=1
elif(tail[1] == '.c' or tail[1] == '.h'):
self.c_file_list.append(os.path.join(root_dir, this_file))
self.c_file_counter +=1
def source_files_count(self, file_type):
'''
count code lines for c files
'''
if file_type == 'c' or file_type == 'C':
for c_file in self.c_file_list:
fobj = open(c_file,'r+')
lines = fobj.readlines()
for line in lines:
if(';' in line or '#' in line):
self.c_line_counter +=1
fobj.close()
elif file_type == 'py' or file_type == 'PY' or file_type == 'pY' or file_type == 'Py':
for py_file in self.py_file_list:
fobj = open(py_file,'r+')
lines = fobj.readlines()
for line in lines:
if( '\r\n' in line or '\n' in line or '\r' in line):
self.py_line_counter +=1
fobj.close()
else:
print('no this kind source file!')
exit()
def is_git_dir(self,source_dir):
'''
if the dir is a git repo
'''
if '.git' == source_dir:
return True
else:
return False
def print_line(self):
'''
print the line num
'''
if(self.c_file_counter):
print'there are ' + str(self.c_file_counter) + ' C source files'
print'the c and h source files lines are :' + str(self.c_line_counter)
if(self.py_file_counter):
print'there are ' + str(self.py_file_counter) + ' Python source files'
print'the Python source files lines are:' + str(self.py_line_counter)
t = time()
t.start()
my_code = the_code()
my_code.source_files_count('c')
my_code.source_files_count('py')
t.end()
my_code.print_line()
t.count_time()
raw_input("Press Enter to continue...")