-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClean.py
More file actions
86 lines (78 loc) · 2.26 KB
/
Copy pathClean.py
File metadata and controls
86 lines (78 loc) · 2.26 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
# delete all the comments and empty line of a C/C++ source file
import os
import sys
import string
import re
import glob
# /*..*/ //...
Rule1 = "(\/\*(\s|.)*?\*\/)|(\/\/.*)"
c1 = re.compile(Rule1)
#-------------------------------------------------------------
def usage():
print u'''
help: del_comment.py <filename | dirname>
'''
#--------------------------------------------------------------
def deal_file(src):
# file exist or not
if not os.path.exists(src):
print 'Error: file - %s doesn\'t exist.'% src
return False
if os.path.islink(src):
print 'Error: file - %s is a link.'
return False
filetype = (os.path.splitext(src))[1]
if not filetype in ['.c','.h','.cpp','.hh','.cc']:
return False
try:
if not os.access(src, os.W_OK):
os.chmod(src, 0664)
except:
print 'Error: you can not chang %s\'s mode.'% src
inputf = open(src, 'r')
outputfilename = (os.path.splitext(src))[0] + '_no_comment'+filetype
outputf = open(outputfilename, 'w')
lines=inputf.read()
inputf.close()
lines=re.sub(Rule1,"",lines)
outputf.write(lines)
outputf.close()
return True
#--------------------------------------------------------------
def deal_dir(src):
# dir exist or not
if not os.path.exists(src):
print 'Error: dir - %s is not exist.'%s (src)
return False
filelists = os.listdir(src)
for eachfile in filelists:
eachfile = src + '/' +eachfile
if os.path.isdir(eachfile):
deal_dir(eachfile)
elif os.path.isfile(eachfile):
deal_file(eachfile)
return True
#--------------------------------------------------------------
def main():
if len(sys.argv) < 2:
usage()
sys.exit(1)
src = sys.argv[1]
# get absolute dir/file path
if os.path.isdir(src):
dire = os.path.abspath(src)
dirFlag = True
elif os.path.isfile(src):
fl = os.path.abspath(src)
dirFlag = False
else:
print 'File input error'
# deal
if dirFlag:
deal_dir(dire)
else:
deal_file(fl)
print 'Successful handle file.'
#--------------------------------------------------------------
if __name__ == '__main__':
main()