forked from PGBuildFarm/client-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_branches.pl
executable file
·301 lines (249 loc) · 7.33 KB
/
run_branches.pl
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#!/usr/bin/perl
=comment
Copyright (c) 2003-2017, Andrew Dunstan
See accompanying License file for license details
=cut
use strict;
use warnings;
use vars qw($VERSION); $VERSION = 'REL_8';
use Fcntl qw(:flock :seek);
use File::Spec;
use File::Basename;
use Cwd qw(getcwd);
BEGIN
{
unshift(@INC, $ENV{BFLIB}) if $ENV{BFLIB};
use lib File::Spec->rel2abs(dirname(__FILE__));
}
my $orig_dir = getcwd();
unshift @INC, $orig_dir;
use PGBuild::Options;
# older msys is ging to use a different perl to run LWP, so we can't absolutely
# require this module there
BEGIN
{
## no critic (ValuesAndExpressions::ProhibitMismatchedOperators)
# perlcritic gets confused by version comparisons - this usage is
# sanctioned by perldoc perlvar
require LWP::Simple if $^O ne 'msys' || $^V ge v5.8.0;
}
my %branch_last;
sub branch_last_sort;
my $run_build;
($run_build = $0) =~ s/run_branches/run_build/;
my ($run_all, $run_one);
my %extra_options = (
'run-all' => \$run_all,
'run-one' => \$run_one,
);
# process the command line
PGBuild::Options::fetch_options(%extra_options);
# no non-option args allowed here
die("$0: non-option arguments not permitted")
if @ARGV;
die "only one of --run-all and --run-one permitted"
if ($run_all && $run_one);
die "need one of --run-all and --run-one"
unless ($run_all || $run_one);
# set up a "branch" variable for processing the config file
use vars qw($branch);
$branch = 'global';
#
# process config file
#
require $buildconf;
die "from-source cannot be used with run_branches,pl"
if ($from_source || $from_source_clean);
PGBuild::Options::fixup_conf(\%PGBuild::conf, \@config_set);
unless (
(
ref $PGBuild::conf{branches_to_build} eq 'ARRAY'
&& @{ $PGBuild::conf{branches_to_build} }
)
|| $PGBuild::conf{branches_to_build} =~
/^(ALL|HEAD_PLUS_LATEST|HEAD_PLUS_LATEST\d)$/
)
{
die "no branches_to_build specified in $buildconf";
}
my @branches;
if (ref $PGBuild::conf{branches_to_build})
{
@branches = @{ $PGBuild::conf{branches_to_build} };
$ENV{BF_CONF_BRANCHES} = join(',', @branches);
}
elsif ($PGBuild::conf{branches_to_build} =~
/^(ALL|HEAD_PLUS_LATEST|HEAD_PLUS_LATEST(\d))$/)
{
$ENV{BF_CONF_BRANCHES} = $PGBuild::conf{branches_to_build};
my $latest = $2;
# Need to set the path here so we make sure we pick up the right perl.
# It has to be the perl that the build script would choose
# i.e. specially *not* the MinGW SDK perl that is invoked for the
# build script, which means we need to put the path back the way it was
# when we're done
my $save_path = $ENV{PATH};
$ENV{PATH} = $PGBuild::conf{build_env}->{PATH}
if ($PGBuild::conf{build_env}->{PATH});
(my $url = $PGBuild::conf{target}) =~ s/cgi-bin.*/branches_of_interest.txt/;
my $branches_of_interest;
## no critic (ValuesAndExpressions::ProhibitMismatchedOperators)
# perlcritic gets confused by version comparisons - this usage is
# sanctioned by perldoc perlvar
if ($^O eq 'msys' && $^V lt v5.8.0)
{
# msys: use perl in PATH
$branches_of_interest = `perl -MLWP::Simple -e "getprint(q{$url})"`;
}
else
{
# everyone else: use this perl
# make sure we have https protocol support if it's required
require LWP::Protocol::https if $url =~ /^https:/;
$branches_of_interest = LWP::Simple::get($url);
}
die "getting branches of interest ($url)" unless $branches_of_interest;
$ENV{PATH} = $save_path;
push(@branches, $_) foreach (split(/\s+/, $branches_of_interest));
splice(@branches, 0, -2)
if $PGBuild::conf{branches_to_build} eq 'HEAD_PLUS_LATEST';
splice(@branches, 0, 0 - ($latest + 1))
if $PGBuild::conf{branches_to_build} =~ /^HEAD_PLUS_LATEST\d$/;
}
@branches = apply_throttle(@branches);
my $global_lock_dir =
$PGBuild::conf{global_lock_dir}
|| $PGBuild::conf{build_root}
|| '';
unless ($global_lock_dir && -d $global_lock_dir)
{
die "no global lock directory: $global_lock_dir";
}
# acquire the lock
my $lockfile;
my $lockfilename = "$global_lock_dir/GLOBAL.lck";
open($lockfile, ">", "$lockfilename") || die "opening lockfile: $!";
if (!flock($lockfile, LOCK_EX | LOCK_NB))
{
print "Another process holds the lock on " . "$lockfilename. Exiting.\n"
if ($verbose);
exit(0);
}
if ($run_all)
{
foreach my $brnch (@branches)
{
run_branch($brnch);
}
}
elsif ($run_one)
{
# sort the branches by the order in which they last did actual work
# then try running them in that order until one does some work
%branch_last = map { $_ => find_last_status($_) } @branches;
foreach my $brnch (sort branch_last_sort @branches)
{
run_branch($brnch);
my $new_status = find_last_status($brnch);
last if $new_status != $branch_last{$brnch};
}
}
# clean up the lockfile when we're done.
close $lockfile;
unlink $lockfilename;
exit 0;
##########################################################
sub run_branch
{
my $branch = shift;
my @args = ($run_build, PGBuild::Options::standard_option_list(), $branch);
# On cygwin, explicitly use perl from the path (and not this perl,
# so don't use $^X)
# This script needs to run on Cygwin with non-cygwin perl if it's running
# in tandem with AS/MinGW perl, since Cygwin perl doesn't honor locks
# the same way, and the global lock fails. But the build script needs
# to run with the native perl, even on Cygwin, which it picks up from
# the path. (Head exploding yet?).
#
# So if the perl in the path is cygwin perl, we use that, otherwise we use
# this perl.
my $pathperlinfo = qx(perl -v 2>&1);
my $runperl = $pathperlinfo =~ /cygwin/ ? "perl" : $^X;
system($runperl, @args);
return;
}
sub branch_last_sort
{
return $branch_last{$a} <=> $branch_last{$b};
}
sub find_last_status
{
my $brnch = shift;
my $status_file =
"$PGBuild::conf{build_root}/$brnch/$PGBuild::conf{animal}.last.status";
return 0 unless (-e $status_file);
my $ts = file_contents($status_file);
chomp $ts;
return $ts + 0;
}
sub apply_throttle
{
my @thrbranches = @_;
return @thrbranches unless exists $PGBuild::conf{throttle};
my @result;
my %throttle = %{ $PGBuild::conf{throttle} };
# implement throttle keywords ALL !HEAD and !RECENT
my @candidates;
my $replacement;
if (exists $throttle{ALL})
{
@candidates = @thrbranches;
$replacement = $throttle{ALL};
}
elsif (exists $throttle{'!HEAD'})
{
@candidates = grep { $_ ne 'HEAD' } @thrbranches;
$replacement = $throttle{'!HEAD'};
}
elsif (exists $throttle{'!RECENT'})
{
# sort branches, make sure we get numeric major version sorting right
my @stable = grep { $_ ne 'HEAD' } @thrbranches;
s/^REL(\d)_/0$1/ foreach (@stable);
@stable = sort @stable;
s/^REL0/REL/ foreach (@stable);
pop @stable; # remove latest
@candidates = @stable;
$replacement = $throttle{'!RECENT'};
}
foreach my $cand (@candidates)
{
# only supply this for the branch if there isn't already
# a throttle
$throttle{$cand} ||= $replacement;
}
# apply throttle filters
foreach my $branch (@thrbranches)
{
my $this_throttle = $throttle{$branch};
unless (defined $this_throttle)
{
push(@result, $branch);
next;
}
my $minh = $this_throttle->{min_hours_since};
my $ts = find_last_status($branch);
next
if ( $ts
&& (defined $minh)
&& ($minh && $minh < ((time - $ts) / 3600.0)));
if (exists $this_throttle->{allowed_hours})
{
my @allowed_hours = split(/,/, $this_throttle->{allowed_hours});
my $hour = (localtime(time))[2];
next unless grep { $_ == $hour } @allowed_hours;
}
push(@result, $branch);
}
return @result;
}