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