Belle II Software light-2406-ragdoll
autoclean_dir.py
1#!/usr/bin/env python
2
3
10
11import os
12from SCons import Node
13
14
15def autoclean_dir(env, dirname):
16 """
17 Autoclean directory, deleting all files which are not known to the build
18 system, like obsolete header files
19 """
20
21 # First make a set of targets which are found inside the directory and all its subdirs
22 directory = env.Dir(dirname)
23 targets = set()
24 stack = [directory]
25 while stack:
26 dirnode = stack.pop()
27 for node in dirnode.all_children():
28 if isinstance(node, Node.FS.Dir):
29 stack.append(node)
30 elif node.is_derived():
31 targets.add(str(node))
32
33 # Now delete all files found in the directory which are not in the target set
34 for (dirname, dirs, files) in os.walk(str(directory)):
35 if "__pycache__" in dirs:
36 dirs.remove("__pycache__")
37 for filename in files:
38 fullname = os.path.join(dirname, filename)
39 if fullname not in targets and not filename.startswith('.') and not \
40 (fullname.endswith('.pyc') and fullname[:-1] in targets):
41 print(env['CLEANUPCOMSTR'] % fullname)
42 os.remove(fullname)
43
44
45def generate(env):
46 env.AddMethod(autoclean_dir, 'RemoveNonTargets')
47
48
49def exists(env):
50 return True