forked from Show-Me-the-Code/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComputeCodeLines.py
More file actions
221 lines (180 loc) · 6.23 KB
/
ComputeCodeLines.py
File metadata and controls
221 lines (180 loc) · 6.23 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
File: ComputeCodeLines.py
Author: Zeyue Liang
Date: September 2, 2015
Email: zeyue.liang@gmail.com
Github: https://github.com/zeyue
Description:
A program that can count code line numbers in a directory.
Usage: Launce the program in console with directory as argument, it can add
several directories in one time:
$python -3 ComputeCodeLines.py <first directory> [second] [...]
ATTENTION: use "/" in the place of "\"
This program has been tested using some files in C and in Python,
maybe support java as well.
"""
import os
import sys
"""
Global variables
"""
file_suffix = "not defined"
inline_comment_syntax = "not defined"
start_comment_syntax = "not defined"
end_comment_syntax = "not defined"
multilineCommentStartFlag = 0
result = {"Code files": 0,
"No blank lines": 0,
"Code lines": 0,
"Comment lines": 0,
"Blank lines": 0}
"""
Funcitions
"""
def getFilesList(directory):
"""Get files's list in a directory
:directory: files' directory
:returns: file path list
"""
file_paths = []
for root, directories, files in os.walk(directory):
for filename in files:
file_path = os.path.join(root, filename)
file_paths.append(file_path)
return file_paths
def getSuffix(full_file_path):
"""Get the suffix of current file.
:full_file_path: path of one file
:returns: suffix
"""
return os.path.splitext(full_file_path)[1][1:]
def identifyFileType(suffix):
"""Identify the file type and return correct syntax.
:suffix: file suffix
:returns: [inline comment syntax, multiple line comment syntax]
"""
if suffix == "py":
return "#", "\"\"\"", "\"\"\""
elif suffix == "c" or suffix == "h" or suffix == "cpp" or suffix == "hpp":
return "//", "/*", "*/"
elif suffix == "java":
return "//", "/*", "*/"
else:
return "not defined"
def isInlineComment(string_line):
"""Check if string line is an inline comment or not.
:string_line: input line
:returns: true or false
"""
commentLen = len(inline_comment_syntax)
if string_line[0:commentLen] == inline_comment_syntax:
# print("zero")
return True
else:
return False
def isMultilineComment(string_line):
"""Check if the string line is a multiple comment or not.
:string_line: one string line in the file
:returns: True or False
"""
global multilineCommentStartFlag
commentLen = len(str(start_comment_syntax))
# print(multilineCommentStartFlag)
if(string_line[0:commentLen] == start_comment_syntax and
multilineCommentStartFlag == 0):
multilineCommentStartFlag = 1
# print("one")
if(len(string_line) > commentLen and
string_line[-commentLen:] == end_comment_syntax):
multilineCommentStartFlag = 0
return True
elif(string_line[0:commentLen] != start_comment_syntax and
multilineCommentStartFlag == 1):
# print("two")
if(len(string_line) > commentLen and
string_line[-commentLen:] == end_comment_syntax):
multilineCommentStartFlag = 0
return True
elif(string_line[-commentLen:] == end_comment_syntax and
multilineCommentStartFlag == 1):
multilineCommentStartFlag = 0
# print("three")
# print(string_line[-commentLen:])
return True
else:
return False
def countOneFile(full_file_path):
"""Count code lines in one file.
:full_file_path: full path of the file
:returns: null
"""
global inline_comment_syntax, start_comment_syntax, end_comment_syntax
global multilineCommentStartFlag
local_result = {"No blank lines": 0,
"Comment lines": 0,
"Code lines": 0,
"Blank lines": 0}
file_suffix = getSuffix(full_file_path)
if(identifyFileType(file_suffix) != "not defined"):
result["Code files"] += 1
(inline_comment_syntax,
start_comment_syntax,
end_comment_syntax) = identifyFileType(file_suffix)
with open(full_file_path, "r") as lines:
for line in lines:
line = line.strip()
if line.strip():
local_result["No blank lines"] += 1
if isInlineComment(line):
local_result["Comment lines"] += 1
elif isMultilineComment(line):
local_result["Comment lines"] += 1
else:
# print("Oops...")
local_result["Code lines"] += 1
else:
local_result["Blank lines"] += 1
print(full_file_path)
for key in local_result:
print(str(key) + ": " + str(local_result[key]))
result["No blank lines"] += local_result["No blank lines"]
result["Comment lines"] += local_result["Comment lines"]
result["Code lines"] += local_result["Code lines"]
result["Blank lines"] += local_result["Blank lines"]
def countInDirectory(directory):
"""Count code lines in one directory.
:directory: the directory that will be counted
:return: null
"""
full_file_paths = getFilesList(directory)
for f in full_file_paths:
countOneFile(f)
def main():
"""main function.
"""
global file_suffix, inline_comment_syntax, start_comment_syntax
global end_comment_syntax, multilineCommentStartFlag, result
for directory in sys.argv[1:]:
file_suffix = "not defined"
inline_comment_syntax = "not defined"
start_comment_syntax = "not defined"
end_comment_syntax = "not defined"
multilineCommentStartFlag = 0
result = {"Code files": 0,
"No blank lines": 0,
"Code lines": 0,
"Comment lines": 0,
"Blank lines": 0}
print("this is the directory: " + directory)
countInDirectory(directory)
print()
print("Final result in the directory")
print(directory)
for key in result:
print(str(key) + ": " + str(result[key]))
"""
Launch
"""
main()