s4-backupkey: Comply with [MS-BKRP] 2.2.1
[Samba.git] / buildtools / wafsamba / stale_files.py
blob2dd08e14974e6cfe7cb24df7894cec04694a537c
1 # encoding: utf-8
2 # Thomas Nagy, 2006-2010 (ita)
4 """
5 Add a pre-build hook to remove all build files
6 which do not have a corresponding target
8 This can be used for example to remove the targets
9 that have changed name without performing
10 a full 'waf clean'
12 Of course, it will only work if there are no dynamically generated
13 nodes/tasks, in which case the method will have to be modified
14 to exclude some folders for example.
15 """
17 import Logs, Build, os, samba_utils, Options, Utils
18 from Runner import Parallel
20 old_refill_task_list = Parallel.refill_task_list
21 def replace_refill_task_list(self):
22 '''replacement for refill_task_list() that deletes stale files'''
24 iit = old_refill_task_list(self)
25 bld = self.bld
27 if not getattr(bld, 'new_rules', False):
28 # we only need to check for stale files if the build rules changed
29 return iit
31 if Options.options.compile_targets:
32 # not safe when --target is used
33 return iit
35 # execute only once
36 if getattr(self, 'cleanup_done', False):
37 return iit
38 self.cleanup_done = True
40 def group_name(g):
41 tm = self.bld.task_manager
42 return [x for x in tm.groups_names if id(tm.groups_names[x]) == id(g)][0]
44 bin_base = bld.bldnode.abspath()
45 bin_base_len = len(bin_base)
47 # paranoia
48 if bin_base[-4:] != '/bin':
49 raise Utils.WafError("Invalid bin base: %s" % bin_base)
51 # obtain the expected list of files
52 expected = []
53 for i in range(len(bld.task_manager.groups)):
54 g = bld.task_manager.groups[i]
55 tasks = g.tasks_gen
56 for x in tasks:
57 try:
58 if getattr(x, 'target'):
59 tlist = samba_utils.TO_LIST(getattr(x, 'target'))
60 ttype = getattr(x, 'samba_type', None)
61 task_list = getattr(x, 'compiled_tasks', [])
62 if task_list:
63 # this gets all of the .o files, including the task
64 # ids, so foo.c maps to foo_3.o for idx=3
65 for tsk in task_list:
66 for output in tsk.outputs:
67 objpath = os.path.normpath(output.abspath(bld.env))
68 expected.append(objpath)
69 for t in tlist:
70 if ttype in ['LIBRARY','MODULE']:
71 t = samba_utils.apply_pattern(t, bld.env.shlib_PATTERN)
72 if ttype == 'PYTHON':
73 t = samba_utils.apply_pattern(t, bld.env.pyext_PATTERN)
74 p = os.path.join(x.path.abspath(bld.env), t)
75 p = os.path.normpath(p)
76 expected.append(p)
77 for n in x.allnodes:
78 p = n.abspath(bld.env)
79 if p[0:bin_base_len] == bin_base:
80 expected.append(p)
81 except:
82 pass
84 for root, dirs, files in os.walk(bin_base):
85 for f in files:
86 p = root + '/' + f
87 if os.path.islink(p):
88 link = os.readlink(p)
89 if link[0:bin_base_len] == bin_base:
90 p = link
91 if f in ['config.h']:
92 continue
93 (froot, fext) = os.path.splitext(f)
94 if fext not in [ '.c', '.h', '.so', '.o' ]:
95 continue
96 if f[-7:] == '.inst.h':
97 continue
98 if p.find("/.conf") != -1:
99 continue
100 if not p in expected and os.path.exists(p):
101 Logs.warn("Removing stale file: %s" % p)
102 os.unlink(p)
103 return iit
106 def AUTOCLEANUP_STALE_FILES(bld):
107 """automatically clean up any files in bin that shouldn't be there"""
108 old_refill_task_list = Parallel.refill_task_list
109 Parallel.refill_task_list = replace_refill_task_list
110 Parallel.bld = bld
111 Build.BuildContext.AUTOCLEANUP_STALE_FILES = AUTOCLEANUP_STALE_FILES