-
Notifications
You must be signed in to change notification settings - Fork 3
/
cmake-clean.py
116 lines (95 loc) · 3.03 KB
/
cmake-clean.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env python
"""
Clean cmake generated files.
"""
import optparse
import os
import shutil
import subprocess
import sys
# Do not cleanup anything in these subdirectories.
PRUNE_DIRS = [".svn", ".git", "CVS"]
def make_clean(directory):
"""Run 'make clean' in directory.
Arguments:
- `directory`: target directory
"""
args = [
"make",
"--directory=%s" % directory,
"--quiet",
"clean"
]
process = subprocess.Popen(args)
return process.wait()
def clean(
directory
):
"""Clean cmake files.
Arguments:
- `directory`: target directory
"""
# Toplevel files.
for filename in [
"CMakeCache.txt",
"CPackConfig.cmake",
"CPackSourceConfig.cmake",
"install_manifest.txt"
]:
pathname = os.path.join(directory, filename)
if os.path.exists(pathname):
os.remove(pathname)
# Toplevel directories.
for dirname in ["_CPack_Packages"]:
pathname = os.path.join(directory, dirname)
if os.path.exists(pathname):
shutil.rmtree(pathname)
# CMakeFiles, Makefile, cmake_install.cmake.
for dirpath, dirnames, filenames in os.walk(directory):
# Prune subdirs.
for dirname in dirnames:
if dirname in PRUNE_DIRS:
dirnames.remove(dirname)
if "CMakeFiles" in dirnames:
for filename in ["Makefile", "cmake_install.cmake"]:
if filename in filenames:
pathname = os.path.join(dirpath, filename)
if os.path.exists(pathname):
os.remove(pathname)
shutil.rmtree(os.path.join(dirpath, "CMakeFiles"))
dirnames.remove("CMakeFiles")
# Remove empty directories. The "repeat" construct is needed
# because the dirnames list for the parent is generated before the
# parent is processed. When a directory is removed, there is no
# way to remove it from the parent's dirnames list. Note that
# setting topdown=False will not help here, and it complicates the
# pruning logic.
repeat = True
while repeat:
repeat = False
for dirpath, dirnames, filenames in os.walk(directory):
# We must check for emptiness before pruning. Otherwise
# we could try to remove a directory that contains only
# prunable subdirs.
if len(dirnames) == 0 and len(filenames) == 0:
os.rmdir(dirpath)
repeat = True
# Prune subdirs.
for dirname in dirnames:
if dirname in PRUNE_DIRS:
dirnames.remove(dirname)
def main():
"""main"""
option_parser = optparse.OptionParser(
usage="usage: %prog [DIR...]\n" +
" Clean cmake generated files."
)
(_, args) = option_parser.parse_args()
if len(args) == 0:
args.append(".")
for arg in args:
#make_clean(arg)
clean(arg)
return 0
if __name__ == "__main__":
sys.exit(main())