3 # Thomas Nagy, 2006-2010 (ita)
6 Add a pre-build hook to remove all build files
7 which do not have a corresponding target
9 This can be used for example to remove the targets
10 that have changed name without performing
13 Of course, it will only work if there are no dynamically generated
14 nodes/tasks, in which case the method will have to be modified
15 to exclude some folders for example.
18 import Logs
, Build
, os
, samba_utils
, Options
, Utils
19 from Runner
import Parallel
21 old_refill_task_list
= Parallel
.refill_task_list
22 def replace_refill_task_list(self
):
23 '''replacement for refill_task_list() that deletes stale files'''
25 iit
= old_refill_task_list(self
)
28 if not getattr(bld
, 'new_rules', False):
29 # we only need to check for stale files if the build rules changed
32 if Options
.options
.compile_targets
:
33 # not safe when --target is used
37 if getattr(self
, 'cleanup_done', False):
39 self
.cleanup_done
= True
42 tm
= self
.bld
.task_manager
43 return [x
for x
in tm
.groups_names
if id(tm
.groups_names
[x
]) == id(g
)][0]
45 bin_base
= bld
.bldnode
.abspath()
46 bin_base_len
= len(bin_base
)
49 if bin_base
[-4:] != '/bin':
50 raise Utils
.WafError("Invalid bin base: %s" % bin_base
)
52 # obtain the expected list of files
54 for i
in range(len(bld
.task_manager
.groups
)):
55 g
= bld
.task_manager
.groups
[i
]
59 if getattr(x
, 'target'):
60 tlist
= samba_utils
.TO_LIST(getattr(x
, 'target'))
62 p
= os
.path
.join(x
.path
.abspath(bld
.env
), t
)
63 p
= os
.path
.normpath(p
)
66 p
= n
.abspath(bld
.env
)
67 if p
[0:bin_base_len
] == bin_base
:
72 for root
, dirs
, files
in os
.walk(bin_base
):
77 if link
[0:bin_base_len
] == bin_base
:
81 if f
[-2:] not in [ '.c', '.h' ]:
83 if f
[-7:] == '.inst.h':
85 if p
.find("/.conf") != -1:
88 Logs
.warn("Removing stale file: %s" % p
)
93 def AUTOCLEANUP_STALE_FILES(bld
):
94 """automatically clean up any files in bin that shouldn't be there"""
95 old_refill_task_list
= Parallel
.refill_task_list
96 Parallel
.refill_task_list
= replace_refill_task_list
98 Build
.BuildContext
.AUTOCLEANUP_STALE_FILES
= AUTOCLEANUP_STALE_FILES