Revision a60088f6 py_logs_splitter.py

View differences:

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

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

  
12 13
def name_splitter(folder):
14
# returns a dictionary of type: [timestamp] -> group_name
13 15
	session_groups = {}
14 16
	for logfile in os.listdir(folder):
15 17
		if is_source_from_filename(logfile):
......
20 22
				session_groups[time_from_filename(logfile)] = 'standard'
21 23
	return session_groups
22 24

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

  
34
	if (logfile.split('.')[0].split('_')[3]) in session_groups.values():
35
		group = logfile.split('.')[0].split('_')[3]
36
	else:
37
		group = None
30 38
	return group
31 39

  
32 40
def session_independent(logfile):
......
37 45

  
38 46
def multiple_dst_copy(folder,logfile,dsts):
39 47
	for dst in dsts:
40
		sh.copy2(folder+'/'+logfile,dst)
48
		if not os.path.exists(dst+'/'+logfile):
49
			os.symlink(folder+'/'+logfile,dst+'/'+logfile)
41 50

  
42 51
def splitter(folder):
52
	# get the dictionary of time and groups names
43 53
	session_groups = name_splitter(folder)
54

  
55
	# prepare the folders
44 56
	for name in set(session_groups.values()):
45 57
		if not os.path.exists(folder+'/'+name):
46 58
			os.makedirs(folder+'/'+name)
47 59

  
60
	# symlinks logfiles in the correct folders
48 61
	for logfile in os.listdir(folder):
49 62
		if os.path.isfile(folder+"/"+logfile):
63
			logtime = time_from_filename(logfile) 
64

  
65

  
50 66
			if session_independent(logfile):
51 67
				multiple_dst_copy(folder,logfile,[folder +'/'+ v for v in set(session_groups.values())])
52 68
			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:
69
				group = get_group(logfile,session_groups,logtime)
70
				if group is not None:
58 71
					newfolder = folder+'/'+group+'/'
59
					sh.move(folder+'/'+logfile,newfolder+logfile)
72
					if not os.path.exists(newfolder+logfile):
73
						os.symlink(folder+'/'+logfile,newfolder+logfile)
74
				else:
75
					print "[ERROR] group not found for "+logfile
60 76

  
61
def main(argv):
77
	return set(session_groups.values())
78

  
79
def splitter_main(argv):
80
        images = False
62 81
	try:
63
		opts,args = getopt.getopt(argv,"hf:",["help","folder"])
82
		opts,args = getopt.getopt(argv,"hif:",["help","images","folder"])
64 83
	except getopt.GetoptError:
65 84
		sys.exit(2)
66 85
	for opt,arg in opts:
67 86
		if opt in ("-h","--help"):
68 87
			sys.exit()
69 88
		elif opt in ("-f","--folder"):
70
			folder = arg
71
	
89
			folder = os.path.abspath(arg)
90
		elif opt in ("-i","--images"):
91
			images = True
92

  
72 93
	try:
73 94
		folder
74 95
	except NameError:
75 96
		print "please provide a folder path"
76 97
		sys.exit()
77 98
	print "folder is " + folder
78
	splitter(folder)
79
	analyse.main(['-f',folder])
99
	groups = splitter(folder)
100
	print "groups found:" + str(groups) 
101
	for group in groups:
102
		analyze_main(['-f',folder+'/'+group])
103
		if images:
104
			visual_main(['-s','-f',folder+'/'+group])
80 105

  
81 106

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

Also available in: Unified diff