5 Copyright (c) 2003-2010, Andrew Dunstan
7 See accompanying License file for license details
11 use vars qw($VERSION); $VERSION = 'REL_0.1';
15 use Fcntl qw(:flock :seek);
16 use EximBuild::Options;
19 if ($0 =~ /(.*)\.pl$/) {
20 die "$0: Please use `@{[join ' ' => $1, @ARGV]}' instead.\n"
29 ($run_build = $0) =~ s/run_branches/run_build/;
31 my($run_all, $run_one);
33 'run-all' => \$run_all,
34 'run-one' => \$run_one,
37 # process the command line
38 EximBuild::Options::fetch_options(%extra_options);
40 # no non-option args allowed here
41 die("$0: non-option arguments not permitted")
44 die "only one of --run-all and --run-one permitted"
45 if ($run_all && $run_one);
47 die "need one of --run-all and --run-one"
48 unless ($run_all || $run_one);
51 die "need group searchable homedir"
52 unless (stat($ENV{HOME}) & 0550 == 0550);
54 # set up a "branch" variable for processing the config file
65 ref $EximBuild::conf{branches_to_build} eq 'ARRAY'
66 &&@{$EximBuild::conf{branches_to_build}}
68 ||$EximBuild::conf{branches_to_build} =~
69 /^(ALL|HEAD_PLUS_LATEST|HEAD_PLUS_LATEST2)$/
72 die "no branches_to_build specified in $buildconf";
76 if (ref $EximBuild::conf{branches_to_build})
78 @branches = @{$EximBuild::conf{branches_to_build}};
80 elsif ($EximBuild::conf{branches_to_build} =~
81 /^(ALL|HEAD_PLUS_LATEST|HEAD_PLUS_LATEST2)$/ )
84 # Need to set the path here so we make sure we pick up the right perl.
85 # It has to be the perl that the build script would choose
86 # i.e. specially *not* the MinGW SDK perl that is invoked for the
87 # build script, which means we need to put the path back the way it was
89 my $save_path = $ENV{PATH};
90 $ENV{PATH} = $EximBuild::conf{build_env}->{PATH}
91 if ($EximBuild::conf{build_env}->{PATH});
92 (my $url = $EximBuild::conf{target}) =~s/cgi-bin.*/branches_of_interest.txt/;
93 my $branches_of_interest = `perl -MLWP::Simple -e "getprint(q{$url})"`;
94 die "getting branches of interest" unless $branches_of_interest;
95 $ENV{PATH} = $save_path;
96 push(@branches,$_)foreach (split(/\s+/,$branches_of_interest));
97 #splice(@branches,0,-2)
98 # if $EximBuild::conf{branches_to_build} eq 'HEAD_PLUS_LATEST';
99 #splice(@branches,0,-3)
100 # if $EximBuild::conf{branches_to_build} eq 'HEAD_PLUS_LATEST2';
103 @branches = apply_throttle(@branches);
105 my $global_lock_dir =
106 $EximBuild::conf{global_lock_dir}
107 ||$EximBuild::conf{build_root}
110 unless ($global_lock_dir && -d $global_lock_dir)
112 die "no global lock directory: $global_lock_dir";
119 my $lockfilename = "$global_lock_dir/GLOBAL.lck";
121 open($lockfile, ">$lockfilename") || die "opening lockfile: $!";
123 if ( !flock($lockfile,LOCK_EX|LOCK_NB) )
125 print "Another process holds the lock on " ."$lockfilename. Exiting.\n"
132 foreach my $brnch(@branches)
140 # sort the branches by the order in which they last did actual work
141 # then try running them in that order until one does some work
143 %branch_last = map {$_ => find_last_status($_)} @branches;
144 foreach my $brnch(sort branch_last_sort @branches)
147 my $new_status = find_last_status($brnch);
148 last if $new_status != $branch_last{$brnch};
154 ##########################################################
159 my @args = ($run_build,EximBuild::Options::standard_option_list(), $branch);
161 # Explicitly use perl from the path (and not this perl, so don't use $^X)
162 # This script needs to run on Cygwin with non-cygwin perl if it's running
163 # in tandem with AS/MinGW perl, since Cygwin perl doesn't honor locks
164 # the samne way, and the global lock fails. But the build script needs
165 # to run with the native perl, even on Cygwin, which it picks up from
166 # the path. (Head exploding yet?).
167 system("perl",@args);
172 return $branch_last{$a} <=> $branch_last{$b};
179 "$EximBuild::conf{build_root}/$brnch/$EximBuild::conf{animal}.last.status";
180 return 0 unless (-e $status_file);
182 open($handle,$status_file) || dir $!;
192 return @branches unless exists $EximBuild::conf{throttle};
194 my %throttle = %{$EximBuild::conf{throttle}};
196 # implement throttle keywords ALL !HEAD and !RECENT
199 if (exists $throttle{ALL})
201 @candidates = @branches;
202 $replacement = $throttle{ALL};
204 elsif (exists $throttle{'!HEAD'})
206 @candidates = grep { $_ ne 'HEAD' } @branches;
207 $replacement = $throttle{'!HEAD'};
209 elsif (exists $throttle{'!RECENT'})
212 # sort branches, make sure we get numeric major version sorting right
213 my @stable = grep { $_ ne 'HEAD' } @branches;
214 s/^REL(\d)_/0$1/ foreach (@stable);
215 @stable = sort @stable;
216 s/^REL0/REL/ foreach (@stable);
217 pop @stable; # remove latest
218 @candidates = @stable;
219 $replacement = $throttle{'!RECENT'};
221 foreach my $cand (@candidates)
224 # only supply this for the branch if there isn't already
226 $throttle{$cand} ||= $replacement;
229 # apply throttle filters
230 foreach my $branch(@branches)
232 my $this_throttle = $throttle{$branch};
233 unless (defined $this_throttle)
235 push(@result,$branch);
238 my $minh = $this_throttle->{min_hours_since};
239 my $ts = find_last_status($branch);
243 &&($minh && $minh < ((time - $ts) / 3600.0)));
244 if (exists $this_throttle->{allowed_hours})
246 my @allowed_hours = split(/,/,$this_throttle->{allowed_hours});
247 my $hour = (localtime(time))[2];
248 next unless grep {$_ == $hour} @allowed_hours;
250 push(@result,$branch);