Use a finally block & make certain the loop will end
[Samba/ita.git] / buildtools / wafsamba / samba_throttle.py
blob2af70ffc888d5378308d7607e2084a9e19d2bd59
1 #! /usr/bin/env python
3 import os, shutil, re
4 import Task, Utils, Options, Build
6 """
7 Execute a method to reduce the cache size after the build is complete
8 Looking at the cache size is an expensive operation
9 """
11 re_num = re.compile('[a-zA-Z_]+(\d+)')
13 def dsize(root):
14 tot = 0
15 for (p, d, files) in os.walk(root):
16 for f in files:
17 try:
18 tot += os.path.getsize(os.path.join(p, f))
19 except:
20 pass
21 return tot / (1024*1024.0)
23 def cmp2(x, y):
24 return cmp(x[1], y[1])
26 old = Build.BuildContext.compile
27 def compile(self):
29 try:
30 old(self)
31 finally:
33 if not Options.cache_global or Options.options.nocache:
34 return
36 CACHESIZE = 1000
37 val = re_num.sub('\\1', os.path.basename(Options.cache_global))
38 try:
39 CACHESIZE = int(val)
40 except:
41 pass
43 for x in range(5):
45 # loop in case another process is still filling up the cache
46 s = dsize(Options.cache_global)
47 if s < CACHESIZE:
48 break
50 lst = [Options.cache_global + os.sep + x for x in Utils.listdir(Options.cache_global)]
52 acc = []
53 for x in lst:
54 try:
55 acc.append((x, os.path.getctime(x), dsize(x)))
56 except:
57 pass
59 # sort the files by the oldest first
60 acc.sort(cmp=cmp2)
62 tot = sum([x[2] for x in acc])
63 cur = 0
65 # remove at least 10% more, just to make sure
66 while tot - cur > 0.9 * CACHESIZE:
67 x = acc.pop(0)
68 cur += x[2]
70 # ignore if the folders cannot be removed
71 try:
72 shutil.rmtree(x[0])
73 except:
74 pass
76 Build.BuildContext.compile = compile