add 'profile': log individual starting times, and use them on next run to estimate...
[vuplus_dvbapp] / lib / python / Tools / Profile.py
1 import time
2 from Directories import resolveFilename, SCOPE_SYSETC
3
4 PERCENTAGE_START = 50
5 PERCENTAGE_END = 100
6
7 profile_start = time.time()
8
9 profile_data = {}
10 total_time = 1
11
12 try:
13         profile_old = open(resolveFilename(SCOPE_SYSETC, "profile"), "r").readlines()
14
15         t = None
16         for line in profile_old:
17                 (t, id) = line[:-1].split('\t')
18                 t = float(t)
19                 total_time = t
20                 profile_data[id] = t
21 except:
22         print "no profile data available"
23
24 profile_file = open(resolveFilename(SCOPE_SYSETC, "profile"), "w")
25
26 def profile(id):
27         now = time.time() - profile_start
28         if profile_file:
29                 profile_file.write("%.2f\t%s\n" % (now, id))
30         if id in profile_data:
31                 t = profile_data[id]
32                 perc = t * (PERCENTAGE_END - PERCENTAGE_START) / total_time + PERCENTAGE_START
33                 try:
34                         open("/proc/progress", "w").write("%d \n" % perc)
35                 except IOError:
36                         pass
37
38 def profile_final():
39         global profile_file
40         if profile_file is not None:
41                 profile_file.close()
42                 profile_file = None