-
Notifications
You must be signed in to change notification settings - Fork 1
/
comment_remove.py
60 lines (49 loc) · 1.46 KB
/
comment_remove.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
import os
import re
import time
def comment_remover(text):
def replacer(match):
s = match.group(0)
if s.startswith('/'):
return ""
else:
return s
pattern = re.compile(
r'//.*?$|/\*.*?\*/|\'(?:\\.|[^\\\'])*\'|"(?:\\.|[^\\"])*"',
re.DOTALL | re.MULTILINE
)
return re.sub(pattern, replacer, text)
def file_check(name):
if name.endswith('.c') or name.endswith('.java') or name.endswith('.cpp'):
total_line = 0
write_line = 0
writefilename = name + ".bak"
readfile = open(name, "r")
writefile = open(writefilename, "w")
while readfile:
line = readfile.readline()
if not line: break
total_line += 1
writefile.writelines(comment_remover(line))
readfile.close()
writefile.close()
print name, " file is \t\t", total_line, " lines"
return
def file_list(dir):
for name in os.listdir(dir):
checkname = name
checkname = checkname.replace('.', '')
checkname = checkname.replace('/', '')
if checkname == 'git' or checkname == 'repo':
continue
fullname = os.path.join(dir, name)
if os.path.isdir(fullname):
file_list(fullname)
else:
file_check(fullname)
return
print "===================================================================="
print "=== Program of Delete comments by Owl ==="
print "===================================================================="
print "=== start time : ", time.ctime()
file_list('.')