-
Notifications
You must be signed in to change notification settings - Fork 3
/
gc.pike
179 lines (146 loc) · 4.03 KB
/
gc.pike
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
176
177
178
179
// Xenofarm Garbage Collect
// By Martin Nilsson
string out_dir;
string result_dir;
int gc_poll = 60*60*2;
int dists_left = 1;
int results_left = 11;
int save_per_system = 0;
void debug(string msg, mixed ... args) {
write("[" + Calendar.ISO.now()->format_tod() + "] "+msg, @args);
}
void clean_out_dir(string dir, int save) {
array files = get_dir(dir);
array times = map(files,
lambda(string in) {
return file_stat( combine_path(dir,in) )->ctime;
} );
sort(times, files);
files = files[..sizeof(files)-save-1];
foreach(files, string file)
if(!rm(combine_path(dir,file)))
debug("Could not delete %s.\n", combine_path(dir,file));
else
debug("Removed file %s\n", combine_path(dir,file));
}
void rm_dir(string dir, string file) {
if( !Stdio.recursive_rm(combine_path(dir,file)) )
debug("Could not delete %s.\n", combine_path(dir,file));
else
debug("Removed directory %s\n", combine_path(dir,file));
}
void clean_res_dir(string dir, int save_builds, int save_systems) {
array files = get_dir(dir);
multiset|array builds = ({});
mapping(int:multiset|array) systems = ([]);
foreach(files, string fn) {
int build, system;
if( sscanf(fn, "%d_%d", build, system)!=2 ) continue;
builds += ({ build });
if(!systems[system])
systems[system] = ({ build });
else
systems[system] += ({ build });
}
builds = Array.uniq(sort(builds));
builds = builds[..sizeof(builds)-1-save_builds];
builds = (multiset)builds;
// These builds can be removed unless save_systems says otherwise.
foreach(indices(systems), int system) {
array b = systems[system];
b = b[sizeof(b)-save_systems..];
if(!sizeof(b))
m_delete(systems, system);
else
systems[system] = (multiset)b;
}
// These results must not be removed to comply with save_systems.
// Remove the results.
foreach(files, string fn) {
int build, system;
if( sscanf(fn, "%d_%d", build, system)!=2 ) continue;
if( !builds[build] ) continue;
if( systems[system] && systems[system][build] ) continue;
rm_dir(dir, fn);
}
// Save export information about non-removed results.
foreach(values(systems), multiset b)
builds -= b;
// Remove export results.
foreach(files, string fn) {
if(has_value(fn, "_")) continue;
if(fn == (string)(int)fn && builds[(int)fn])
rm_dir(dir, fn);
}
}
void check_settings() {
if(!out_dir) {
write("No out directory found.\n");
exit(1);
}
if(!file_stat(out_dir) || !file_stat(out_dir)->isdir) {
write("Out directory %s does not exist.\n", out_dir);
exit(1);
}
if(!result_dir) {
write("No result directory found.\n");
exit(1);
}
if(!file_stat(result_dir) || !file_stat(result_dir)->isdir) {
write("Result directory %s does not exist.\n", result_dir);
exit(1);
}
}
int main(int n, array(string) args) {
foreach(Getopt.find_all_options(args, ({
({ "dists", Getopt.HAS_ARG, "--dists-left" }),
({ "help", Getopt.NO_ARG, "--help" }),
({ "out_dir", Getopt.HAS_ARG, "--out-dir" }),
({ "poll", Getopt.HAS_ARG, "--poll" }),
({ "result_dir",Getopt.HAS_ARG, "--result-dir" }),
({ "results", Getopt.HAS_ARG, "--results-left" }),
({ "systems", Getopt.HAS_ARG, "--save-per-system"}),
}) ),array opt)
{
switch(opt[0])
{
case "dists":
dists_left = (int)opt[1];
break;
case "help":
write(prog_doc);
return 0;
case "out_dir":
out_dir = opt[1];
break;
case "poll":
gc_poll = (int)opt[1];
break;
case "result_dir":
result_dir = opt[1];
break;
case "results":
results_left = (int)opt[1];
break;
case "systems":
save_per_system = (int)opt[1];
break;
}
}
check_settings();
while(1) {
clean_out_dir(out_dir, dists_left);
clean_res_dir(result_dir, results_left, save_per_system);
debug("Waiting...\n");
sleep(gc_poll);
}
}
constant prog_doc = #"
--dists-left
--help
--out-dir
--poll
--result-dir
--results-left
--save-per-system
";