-
Notifications
You must be signed in to change notification settings - Fork 12
/
clean
executable file
·177 lines (154 loc) · 6.75 KB
/
clean
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/python
import os
import shutil
import tarfile
import sys
import getopt
import re
import shutil
import glob
import subprocess
## clean
##
## Script for deleting prior builds, tests, and results. The script has three options:
##
## 1. "clean all": delete all prior WRF builds and tests; does not delete test result summaries
## 2. "clean fail": delete all WRF build directories whose compilations failed, and
## also delete all test directories where there was some kind of failure.
## 3. "clean check": list the directories that will be deleted when running "clean fail".
##
## Author: Brian Bonnlander
## Modification history:
## Sep 2016, Michael Kavulich, Jr.: Ported original ksh script to python, fixed errors
def usage():
print("usage: " + __file__ + " all|fail|check|name='name_of_build'|namecheck='name_of_build'")
print("")
print(" " + __file__ + " all : delete all WRF build directories and test directories (keeps RESULTS)")
print(" " + __file__ + " fail : delete only WRF build directories and test directories with failures")
print(" " + __file__ + " check : show what '" + __file__ + " fail' would delete.")
print(" " + __file__ + " name : delete only WRF build directories starting with the specified name")
print(" " + __file__ + " namecheck : show what '" + __file__ + " name' would delete.")
sys.exit(1)
def main():
# Start of main script: check for valid options
# A single parameter must be passed in, and it must be "all","fail", "check", "name='name_of_build'", or "namecheck='name_of_build'.
if not len(sys.argv)==2:
usage()
cleantype=sys.argv[1]
if cleantype=="all":
cleanall=True
dryrun=False
cleanname=None
elif cleantype=="fail":
cleanall=False
dryrun=False
cleanname=None
elif cleantype=="check":
cleanall=False
dryrun=True
cleanname=None
elif "name=" in cleantype:
cleantypename = cleantype.split('=',1)
cleanall=True
dryrun=False
cleanname=cleantypename[1]
elif "namecheck=" in cleantype:
cleantypename = cleantype.split('=',1)
cleanall=True
dryrun=True
cleanname=cleantypename[1]
else:
print("unrecognized option: " + cleantype.split('=',1)[0])
usage()
if dryrun==True:
print("\nThis is a dry run: nothing will actually be deleted!!\n")
if cleanname is not None:
print("\nOnly builds and runs from a test named '" + cleanname + "' will be deleted\n")
# Define relevant directories.
wtf_rootdir=os.getcwd()
build_dir=wtf_rootdir + "/Builds"
runs_dir=wtf_rootdir + "/Runs"
trash_dir=wtf_rootdir + "/.Trash/"
# Make sure builds and runs directories exist.
if not os.path.isdir(build_dir):
print "Error: " + build_dir + " does not exist"
print "Be sure you are running this script from the top-level WTF directory"
sys.exit(2)
elif not os.path.isdir(runs_dir):
print "Error: " + runs_dir + " does not exist"
print "Be sure you are running this script from the top-level WTF directory"
sys.exit(2)
# Create trash if it does not exist
if not os.path.isdir(trash_dir):
os.mkdir(trash_dir)
# If "clean all", move all code builds and test runs directories to the trash
if cleanall:
# Build directories
if cleanname is None:
del_dirs = [dir for dir in os.listdir(build_dir) if re.search(r'\S+\.\d+$', dir)]
else:
del_dirs = [dir for dir in os.listdir(build_dir) if re.search(r'^'+cleanname+'\.\d+$', dir)]
for f in del_dirs:
fullf = build_dir + '/' + f
if dryrun:
print(fullf + ' would be moved to ' + trash_dir + "build" + f)
else:
os.system('mv ' + fullf + ' ' + trash_dir + "build" + f)
print(fullf + ' is being moved to ' + trash_dir)
# Run directories
if cleanname is None:
del_dirs = [dir for dir in os.listdir(runs_dir) if re.search(r'\S+\.\d+$', dir)]
else:
del_dirs = [dir for dir in os.listdir(runs_dir) if re.search(r'^'+cleanname+'\.\d+$', dir)]
for f in del_dirs:
fullf = runs_dir + '/' + f
if dryrun:
print(f + ' would be moved to ' + trash_dir + "run" + f)
else:
os.system('mv ' + fullf + ' ' + trash_dir + "run" + f)
print(f + ' is being moved to ' + trash_dir + "run" + f)
else: # If "clean fail" or "clean check", find build directories with FAIL*.tst in them
print("Searching for failed builds and runs to delete...")
del_dirs = []
if cleanname is None:
failbuilds = glob.glob(build_dir + "/*/*/*/FAIL*.tst") #First check build failures
failruns = glob.glob(runs_dir + "/*/*/*/FAIL*.tst") #Then check run failures
else:
failbuilds = glob.glob(build_dir + "/" + cleanname + "*/*/*/FAIL*.tst") #First check build failures
failruns = glob.glob(runs_dir + "/" + cleanname + "*/*/*/FAIL*.tst") #Then check run failures
# Loop over Builds and Runs separately to avoid collisions
#Build directories
for full_dir in failbuilds: # For each parallelism type
del_struct = full_dir.split('/') # decompose directory structure into del_struct
del_dir = '/'.join(del_struct[:-2]) # recompose "del_struct" (minus 2 lowest level directories) into path "del_dir"
del_dirs.append(del_dir) # Add path "del_dir" to list "del_dirs"
del_dirs = list(set(del_dirs)) # Removes duplicate items on the list
for f in del_dirs:
f_struct = f.split('/')
f1 = f_struct[-1]
f2 = f_struct[-2]
if dryrun:
print(f + ' would be moved to ' + trash_dir + "build" + f2 + f1)
else:
os.system('mv ' + f + ' ' + trash_dir + "build" + f2 + f1)
print(f + ' is being moved to ' + trash_dir + "build" + f2 + f1)
#Run directories
for full_dir in failruns: # For each parallelism type
del_struct = full_dir.split('/') # decompose directory structure into del_struct
del_dir = '/'.join(del_struct[:-2]) # recompose "del_struct" (minus 2 lowest level directories) into path "del_dir"
del_dirs.append(del_dir) # Add path "del_dir" to list "del_dirs"
del_dirs = list(set(del_dirs)) # Removes duplicate items on the list
for f in del_dirs:
f_struct = f.split('/')
f1 = f_struct[-1]
f2 = f_struct[-2]
if dryrun:
print(f + ' would be moved to ' + trash_dir + "run" + f2 + f1)
else:
os.system('mv ' + f + ' ' + trash_dir + "run" + f2 + f1)
print(f + ' is being moved to ' + trash_dir + "run" + f2 + f1)
if not dryrun:
print("Deleting everything in " + trash_dir)
proc = subprocess.Popen("rm -rf " + trash_dir + "/* &", shell=True, stdin=None, stdout=None, stderr=None, close_fds=True)
if __name__ == "__main__":
main()