Merge branch 'bug_263_fix_timeshift_live_transition'
[enigma2.git] / lib / python / Tools / Profile.py
1 # the implementation here is a bit crappy.
2 import time
3 from Directories import resolveFilename, SCOPE_CONFIG
4
5 PERCENTAGE_START = 50
6 PERCENTAGE_END = 100
7
8 profile_start = time.time()
9
10 profile_data = {}
11 total_time = 1
12 profile_file = None
13
14 try:
15         profile_old = open(resolveFilename(SCOPE_CONFIG, "profile"), "r").readlines()
16
17         t = None
18         for line in profile_old:
19                 (t, id) = line[:-1].split('\t')
20                 t = float(t)
21                 total_time = t
22                 profile_data[id] = t
23 except:
24         print "no profile data available"
25
26 try:
27         profile_file = open(resolveFilename(SCOPE_CONFIG, "profile"), "w")
28 except IOError:
29         print "WARNING: couldn't open profile file!"
30
31 def profile(id):
32         now = time.time() - profile_start
33         if profile_file:
34                 profile_file.write("%.2f\t%s\n" % (now, id))
35
36                 if id in profile_data:
37                         t = profile_data[id]
38                         perc = t * (PERCENTAGE_END - PERCENTAGE_START) / total_time + PERCENTAGE_START
39                         try:
40                                 open("/proc/progress", "w").write("%d \n" % perc)
41                         except IOError:
42                                 pass
43
44 def profile_final():
45         global profile_file
46         if profile_file is not None:
47                 profile_file.close()
48                 profile_file = None