5 Copyright (c) 2003-2010, Andrew Dunstan
7 See accompanying License file for license details
11 use vars qw($VERSION); $VERSION = 'REL_0.1';
17 use Fcntl qw(:flock :seek);
19 use FindBin qw($RealBin);
23 use EximBuild::Options;
27 # Complain on old-style use (.pl extension), but only if a
28 # terminal is available.
29 if ($0 =~ /(.*)\.pl$/) {
30 die "$0: Please use `@{[join ' ' => $1, @ARGV]}' instead.\n"
35 # Most of the client code assumes that our working directory
36 # is the client code directory.
40 argv => [@ARGV], # get a copy!
42 chdir $RealBin or die "$0: Can't chdir to '$RealBin': $!\n";
43 say "Changed working directory to '$RealBin'" if -t;
46 my $run_build = './run_build';
48 my($run_all, $run_one);
50 'run-all' => \$run_all,
51 'run-one' => \$run_one,
54 # process the command line
55 EximBuild::Options::fetch_options(%extra_options);
57 # no non-option args allowed here
58 die("$0: non-option arguments not permitted")
61 die "$0: only one of --run-all and --run-one permitted"
62 if ($run_all && $run_one);
64 die "$0: need one of --run-all and --run-one"
65 unless ($run_all || $run_one);
68 die "$0: need group searchable homedir"
69 unless (stat($ENV{HOME}) & 0550 == 0550);
71 # set up a "branch" variable for processing the config file
80 # Check if auto-update is wanted and possible
81 if (not exists $EximBuild::Conf{auto_update} or $EximBuild::Conf{auto_update})
83 my $remote = $EximBuild::Conf{auto_update} // 'origin';
85 die "$0: auto-update not possible: need write permissions in @{[cwd]}\n"
88 # http://stackoverflow.com/questions/3258243/check-if-pull-needed-in-git
89 system("git fetch $remote") == 0 or die $? >> 8;
90 my ($upstream, $local, $base) = qx'git rev-parse ...@{upstream}'; die $? >> 8 if $?;
93 if ($upstream ne $local) {
94 die "$0: the merge base is not local anymore. Refusing to `git pull`\n"
97 say "Doing git pull ..." if $verbose;
99 # if we're the merge base, the ff-only should workkkkkk
100 system 'git pull --ff-only' == 0 or die $? >> 8;
101 say "$0: re-execute after update";
102 chdir $CALLED{cwd} or die "$0: Can't chdir to $CALLED{cwd}: $!\n";
103 exec $CALLED{argv0}, @{$CALLED{argv}};
104 die "Can't re-exec\n";
110 ref $EximBuild::conf{branches_to_build} eq 'ARRAY'
111 &&@{$EximBuild::conf{branches_to_build}}
113 ||$EximBuild::conf{branches_to_build} =~
114 /^(ALL|HEAD_PLUS_LATEST|HEAD_PLUS_LATEST2)$/
117 die "no branches_to_build specified in $buildconf";
121 if (ref $EximBuild::conf{branches_to_build})
123 @branches = @{$EximBuild::conf{branches_to_build}};
125 elsif ($EximBuild::conf{branches_to_build} =~
126 /^(ALL|HEAD_PLUS_LATEST|HEAD_PLUS_LATEST2)$/ )
129 # Need to set the path here so we make sure we pick up the right perl.
130 # It has to be the perl that the build script would choose
131 # i.e. specially *not* the MinGW SDK perl that is invoked for the
132 # build script, which means we need to put the path back the way it was
134 local $ENV{PATH} = $EximBuild::conf{build_env}->{PATH}
135 if ($EximBuild::conf{build_env}->{PATH});
136 (my $url = $EximBuild::conf{target}) =~s/cgi-bin.*/branches_of_interest.txt/;
137 my $branches_of_interest = `perl -MLWP::Simple -e "getprint(q{$url})"`;
138 die "getting branches of interest" unless $branches_of_interest;
139 push(@branches,$_)foreach (split(/\s+/,$branches_of_interest));
140 #splice(@branches,0,-2)
141 # if $EximBuild::conf{branches_to_build} eq 'HEAD_PLUS_LATEST';
142 #splice(@branches,0,-3)
143 # if $EximBuild::conf{branches_to_build} eq 'HEAD_PLUS_LATEST2';
146 @branches = apply_throttle(@branches);
148 my $global_lock_dir =
149 $EximBuild::conf{global_lock_dir}
150 ||$EximBuild::conf{build_root}
153 unless ($global_lock_dir && -d $global_lock_dir)
155 die "no global lock directory: $global_lock_dir";
162 my $lockfilename = "$global_lock_dir/GLOBAL.lck";
164 open($lockfile, ">$lockfilename") || die "opening lockfile: $!";
166 if ( !flock($lockfile,LOCK_EX|LOCK_NB) )
168 print "Another process holds the lock on " ."$lockfilename. Exiting.\n"
175 foreach my $brnch(@branches)
183 # sort the branches by the order in which they last did actual work
184 # then try running them in that order until one does some work
186 %branch_last = map {$_ => find_last_status($_)} @branches;
187 foreach my $brnch(sort branch_last_sort @branches)
190 my $new_status = find_last_status($brnch);
191 last if $new_status != $branch_last{$brnch};
197 ##########################################################
202 my @args = ($run_build,EximBuild::Options::standard_option_list(), $branch);
204 # Explicitly use perl from the path (and not this perl, so don't use $^X)
205 # This script needs to run on Cygwin with non-cygwin perl if it's running
206 # in tandem with AS/MinGW perl, since Cygwin perl doesn't honor locks
207 # the samne way, and the global lock fails. But the build script needs
208 # to run with the native perl, even on Cygwin, which it picks up from
209 # the path. (Head exploding yet?).
210 system("perl",@args);
215 return $branch_last{$a} <=> $branch_last{$b};
222 "$EximBuild::conf{build_root}/$brnch/$EximBuild::conf{animal}.last.status";
223 return 0 unless (-e $status_file);
225 open($handle,$status_file) || dir $!;
235 return @branches unless exists $EximBuild::conf{throttle};
237 my %throttle = %{$EximBuild::conf{throttle}};
239 # implement throttle keywords ALL !HEAD and !RECENT
242 if (exists $throttle{ALL})
244 @candidates = @branches;
245 $replacement = $throttle{ALL};
247 elsif (exists $throttle{'!HEAD'})
249 @candidates = grep { $_ ne 'HEAD' } @branches;
250 $replacement = $throttle{'!HEAD'};
252 elsif (exists $throttle{'!RECENT'})
255 # sort branches, make sure we get numeric major version sorting right
256 my @stable = grep { $_ ne 'HEAD' } @branches;
257 s/^REL(\d)_/0$1/ foreach (@stable);
258 @stable = sort @stable;
259 s/^REL0/REL/ foreach (@stable);
260 pop @stable; # remove latest
261 @candidates = @stable;
262 $replacement = $throttle{'!RECENT'};
264 foreach my $cand (@candidates)
267 # only supply this for the branch if there isn't already
269 $throttle{$cand} ||= $replacement;
272 # apply throttle filters
273 foreach my $branch(@branches)
275 my $this_throttle = $throttle{$branch};
276 unless (defined $this_throttle)
278 push(@result,$branch);
281 my $minh = $this_throttle->{min_hours_since};
282 my $ts = find_last_status($branch);
286 &&($minh && $minh < ((time - $ts) / 3600.0)));
287 if (exists $this_throttle->{allowed_hours})
289 my @allowed_hours = split(/,/,$this_throttle->{allowed_hours});
290 my $hour = (localtime(time))[2];
291 next unless grep {$_ == $hour} @allowed_hours;
293 push(@result,$branch);