-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathelogreorder.py
More file actions
91 lines (77 loc) · 2.1 KB
/
Copy pathelogreorder.py
File metadata and controls
91 lines (77 loc) · 2.1 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
from base import *
import os
import sys
from operator import itemgetter
__LOG_TFMT = '%m/%d/%Y %H:%M:%S'
def get_file_time( path ):
fin = open( path, 'r' )
for line in fin:
try:
segs = line.split( '.' )
tstr = segs[0]
segs = segs[1].split( '(' )
msec = int( segs[0] )
dtime = datetime.strptime( tstr, __LOG_TFMT )
fin.close()
return (total_seconds( dtime ), msec)
except:
pass
fin.close()
return (0, 0)
def untar_files( ipath ):
for root, dirs, files in os.walk(ipath):
for fname in files:
if not fname.endswith( '.gz' ):
continue
print 'untar file:', fname
segs = fname.split( '-' )
segs = segs[-1].split( '.' )
nstr = segs[0]
unpath = os.path.join( root, nstr )
mkdir( unpath )
tarpath = os.path.join( root, fname )
uncmd = 'tar -C ' + unpath + ' -xzf ' + tarpath
os.system( uncmd )
def get_ordered_list( ipath ):
loglist = list()
for root, dirs, files in os.walk(ipath):
for fname in files:
print fname
if fname.endswith('.current'):
continue
path = os.path.join( root, fname )
(time, msec) = get_file_time( path )
loglist.append( (time, root, fname, msec) )
return sorted( loglist, key=itemgetter(0) )
def reorder_logs( loglist, ipath, cover ):
odir = 'reordered'
opath = os.path.join( ipath, odir )
mkdir( opath )
count = 0
for item in loglist:
count += 1
tstr = str_seconds( item[0], '%Y%m%d-%H:%M:%S' ) + '.' + str( item[3] )
ifile = os.path.join( item[1], item[2] )
nfname = tstr + '_' + item[2]
if not cover:
nfname += '.' + str(count)
ofile = os.path.join( opath, nfname )
print 'rename file:', ifile, 'to', ofile
os.rename( ifile, ofile )
def reorder_elog_edir( ipath, cover ):
loglist = get_ordered_list( ipath )
reorder_logs( loglist, ipath, cover )
if __name__ == '__main__':
ipath = sys.argv[1]
cover = False
untarFile = False
if len(sys.argv) > 2 and sys.argv[2] == '-c':
cover = True
if len(sys.argv) > 3 and sys.argv[3] == '-x':
untarFile = True
print untarFile
if untarFile:
untar_files( ipath )
loglist = get_ordered_list( ipath )
print loglist
reorder_logs( loglist, ipath, cover )