Statistics
| Branch: | Revision:

peerstreamer-logs-analyzer / py_logs_splitter.py @ 1a481d7b

History | View | Annotate | Download (2.21 KB)

1
#!/usr/bin/python
2
import getopt,os,sys
3
sys.path.insert(0,'lib')
4
from utilities import *
5
from peerstreamer_logs import *
6
import shutil as sh
7
import py_logs_analyzer as analyse
8

    
9
SESSION_TOLERANCE=15
10
SESSION_INDEPENDENT_KEYWORDS=['rtt','timespread']
11

    
12
def name_splitter(folder):
13
        session_groups = {}
14
        for logfile in os.listdir(folder):
15
                if is_source_from_filename(logfile):
16
                        source_info = logfile.split('.')[0].split('_')
17
                        if len(source_info) > 4:
18
                                session_groups[time_from_filename(logfile)] = source_info[3]
19
                        else:
20
                                session_groups[time_from_filename(logfile)] = 'standard'
21
        return session_groups
22

    
23
def get_group(session_groups,time):
24
        group = None
25
        for keytime in session_groups.keys():
26
#                print "comparing  " + str(time) + " with " + str(int(keytime))
27
                if (int(time) < (int(keytime) + SESSION_TOLERANCE)) and \
28
                                (int(time) > (int(keytime) - SESSION_TOLERANCE)):
29
                        group = session_groups[keytime]
30
        return group
31

    
32
def session_independent(logfile):
33
        for elm in SESSION_INDEPENDENT_KEYWORDS:
34
                if elm in logfile:
35
                        return True
36
        return False
37

    
38
def multiple_dst_copy(folder,logfile,dsts):
39
        for dst in dsts:
40
                sh.copy2(folder+'/'+logfile,dst)
41

    
42
def splitter(folder):
43
        session_groups = name_splitter(folder)
44
        for name in set(session_groups.values()):
45
                if not os.path.exists(folder+'/'+name):
46
                        os.makedirs(folder+'/'+name)
47

    
48
        for logfile in os.listdir(folder):
49
                if os.path.isfile(folder+"/"+logfile):
50
                        if session_independent(logfile):
51
                                multiple_dst_copy(folder,logfile,[folder +'/'+ v for v in set(session_groups.values())])
52
                        else:
53
                                logtime = time_from_filename(logfile) 
54
                                group = get_group(session_groups,logtime)
55
                                if group == None:
56
                                        print "[ERROR] group not found for "+logfile
57
                                else:
58
                                        newfolder = folder+'/'+group+'/'
59
                                        sh.move(folder+'/'+logfile,newfolder+logfile)
60

    
61
def main(argv):
62
        try:
63
                opts,args = getopt.getopt(argv,"hf:",["help","folder"])
64
        except getopt.GetoptError:
65
                sys.exit(2)
66
        for opt,arg in opts:
67
                if opt in ("-h","--help"):
68
                        sys.exit()
69
                elif opt in ("-f","--folder"):
70
                        folder = arg
71
        
72
        try:
73
                folder
74
        except NameError:
75
                print "please provide a folder path"
76
                sys.exit()
77
        print "folder is " + folder
78
        splitter(folder)
79
        analyse.main(['-f',folder])
80

    
81

    
82
if __name__ == "__main__":
83
        main(sys.argv[1:])