-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.py
More file actions
192 lines (176 loc) · 5.57 KB
/
util.py
File metadata and controls
192 lines (176 loc) · 5.57 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
#coding=utf-8
import os
import sys
import shutil
import subprocess
import fileinput
import time
import gzip
import cmd
import multiprocessing
def __test1():
'''
strPath = r"D:\tmp\ticket\TMobile\08-08\log"
basePath = os.path.dirname(strPath)
'''
shutil.move(r"D:\tmp\ticket\TMobile\08-08\a\1.txt", r"D:\tmp\ticket\TMobile\08-08\a\next")
print os.path.splitext("a.txt")
def shell_execute(cmd):
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
return (p.communicate())
def getFilelist(strPath,strExtName):
matchedFileList = []
fileList = os.listdir(strPath)
for fileName in fileList:
filePath = strPath+os.sep+fileName
if os.path.isfile(filePath):
splitResult = os.path.splitext(fileName)
if splitResult[1]==strExtName:
matchedFileList.append(filePath)
elif os.path.isdir(filePath):
tempMatchList = getFilelist(filePath,strExtName)
matchedFileList.extend(tempMatchList)
return matchedFileList
def shell_executeInProcessor(cmd):
p = multiprocessing.Process(target=shell_execute,args=(cmd,))
p.start()
def filterFiles(strSourcePath):
#strSourcePath = r"D:\tmp\ticket\TMobile\08-08\log"
csvList = getFilelist(strSourcePath, ".csv")
pcapList = getFilelist(strSourcePath, ".pcap")
basePath = os.path.dirname(strSourcePath)
strCSVDstPath = basePath+os.sep+"CSV"
strPCAPPath = basePath+os.sep+"Tcpdump"
if not os.path.exists(strCSVDstPath):
os.mkdir(strCSVDstPath)
if not os.path.exists(strPCAPPath):
os.mkdir(strPCAPPath)
for csv in csvList:
shutil.move(csv, strCSVDstPath)
for pcap in pcapList:
shutil.move(pcap, strPCAPPath)
print "%d csv files and %d pcap files are moved\n" %(len(csvList),len(pcapList))
def extractGzipFile(strGZFile):
#strGZFile = r"D:\temp\test\crcs_2013-08-05.log.gz"
splitResult = os.path.splitext(strGZFile)
inputFilePath = splitResult[0]
gzFile = gzip.open(strGZFile, 'rb')
try:
with open(inputFilePath,"wb") as resultFile:
resultFile.write(gzFile.read())
finally:
gzFile.close()
pass
def runPCD(pcdPath):
runPCDCommand = "java -Xmx1048m -jar %s" %(pcdPath,)
result = shell_execute(runPCDCommand)
def testCase1():
strPath = r"D:\tmp\ticket\TMobile\08-08\a"
#test1()
findList = getFilelist(strPath,".txt")
for findFile in findList:
print findFile
def testCase2():
strPath = r"D:\tmp\ticket\TMobile\08-08\log"
csvList = getFilelist(strPath, ".csv")
print "csv list, num %d" %(len(csvList))
for csv in csvList:
print csv
def testGZipExtractFile():
strGZFile = r"D:\temp\test\crcs_2013-08-05.log.gz"
extractGzipFile(strGZFile)
def testSplitFile():
strCRCSPath = ""
if len(sys.argv)>1:
strCRCSPath = sys.argv[1].strip()
if not strCRCSPath:
strCRCSPath = os.curdir+os.sep+"crcs.csv"
if os.path.exists(strCRCSPath):
splitCRCS(strCRCSPath)
else:
print "file %s does not exist" %(strCRCSPath)
def testFilterFiles():
dirPath = ""
if len(sys.argv)>1:
dirPath = sys.argv[1].strip()
if not dirPath:
dirPath = os.curdir+os.sep+"log"
if os.path.exists(dirPath):
filterFiles(dirPath)
else:
print "Dir %s does not exist" %(dirPath)
class UtilCmd(cmd.Cmd):
def __init__(self):
cmd.Cmd.__init__(self)
self.prompt = '''You can execute following command:
1) filter log, command format:
filterLog logfolderPath
2) split crcs, command format:
splitCRCS crcsFilePath
10) exist, input quit or exit.
'''
#self.intro = "Simple command processor example."
def do_test(self,str):
print "test, %s" %(str)
def do_filterLog(self,logPath):
dirPath = ""
if logPath and len(logPath)>0:
dirPath = logPath.strip()
if not dirPath:
dirPath = os.curdir+os.sep+"log"
if os.path.exists(dirPath):
filterFiles(dirPath)
else:
print "Dir %s does not exist" %(dirPath)
'''
def do_splitCRCS(self,crcsPath):
strCRCSPath = ""
if crcsPath and len(crcsPath)>0:
strCRCSPath = crcsPath.strip()
if not strCRCSPath:
strCRCSPath = os.curdir+os.sep+"crcs.csv"
if os.path.exists(strCRCSPath):
splitCRCS(strCRCSPath)
else:
print "file %s does not exist" %(strCRCSPath)
'''
#输入认不出命令时
def default(self,line):
print "The command you input is not supported"
def do_runPCD(self, pcdPath):
if not pcdPath:
'''
print "please input PCD path"
return
'''
pcdPath = r"D:\Project\Analysis\pcd.jar"
pcdPath = pcdPath.strip()
if os.path.exists(pcdPath):
result = runPCD(pcdPath)
print result
pass
else:
print "PCD does not exist"
pass
#输入quit时退出
def do_quit(self, arg):
print "Quit"
sys.exit(1)
#输入exit时退出
def do_exit(self, arg):
print "Exit"
sys.exit(1)
def do_EOF(self, line):
return True
if __name__=="__main__":
print "start"
time.clock()
#test1()
#testCase1()
#testFilterFiles()
#testGZipExtractFile()
#testSplitFile()
UtilCmd().cmdloop()
print "finished"
print "cost time: %d seconds" %(time.clock())
pass