#!/usr/bin/perl -w # # $Id: //guest/daniel_kionka/bootstrap/bin/build-bootstrap.pl#1 $ # # Copyright (c) 2005 Daniel P. Kionka; all rights reserved # # Permission is hereby granted, free of charge, to any person obtaining # a copy of this software and associated documentation files (the # "Software"), to deal in the Software without restriction, including # without limitation the rights to use, copy, modify, merge, publish, # distribute, sublicense, and/or sell copies of the Software, and to # permit persons to whom the Software is furnished to do so, subject to # the following conditions: # # The above copyright notice and this permission notice shall be # included in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. # # Originally written by Daniel P. Kionka while contracting for CiraNova. # http://www.ciranova.com/ # use strict; use warnings; # # variables # # constants my $debug = 1; my @rcRequired = qw(P4CLIENT START LOG ROOT); my $logP4Change = "p4change"; my $logP4Client = "p4client"; my $p4SyncErr = "p4sync.err"; my $p4SyncOut = "p4sync.out"; my $startErr = "start.err"; my $startOut = "start.out"; my $p4exeDefault = "/opt/perforce/p4"; my $p4portDefault = "public.perforce.com:1666"; # global hashes with config file values and p4 client info my (%rc, %client); # full path to p4 executable my $p4exe; # other globals my $errorFlag = 0; # # functions # sub debugPrint(@) { print @_, "\n" if ($debug); } sub errorPrint(@) { print STDERR @_, "\n"; } sub mySystem($) { my ($cmd) = @_; if ($errorFlag != 0) { errorPrint("skipping system after error: $cmd"); return; } debugPrint("system: $cmd"); my $err = system($cmd); if ($err != 0) { $errorFlag++; errorPrint("system command failed: $cmd"); } } sub renameOld($) { my ($file) = @_; debugPrint("renaming: $file"); my $fold = "$file.old"; if (-r $fold) { mySystem("rm -rf $fold"); } rename($file, "$fold"); } sub readRC($) { my ($rcfile) = @_; my ($line); open RC, "<$rcfile" || die "open $rcfile"; while (defined($line = )) { chomp $line; $line =~ s/\s*#.*//; next if (! $line); my ($key, $val) = ($line =~ m/([^=]+)=(.*)/); debugPrint("key=$key,val=$val"); if (defined($val)) { $rc{$key} = $val; $ENV{$key} = $val if ($key =~ m/^P4/); # export P$ variables } } close RC; if (! defined($rc{ROOT})) { $rc{ROOT} = `$p4exe client -o $rc{P4CLIENT} | sed -n 's/^Root: *//p'`; die "rc{ROOT} undefined" if (! defined($rc{ROOT})); chomp $rc{ROOT}; } foreach $line (@rcRequired) { # maybe we should look at environment vars first die "missing RC line: $line" if (! defined($rc{$line})); } } # # mainline # my ($rcfile) = @ARGV; # 1. setup and sanity checks $p4exe = `which p4`; chomp $p4exe; $p4exe = $p4exeDefault if (! $p4exe); $ENV{P4PORT} = $p4portDefault if (! exists($ENV{P4PORT})); mySystem("$p4exe set"); die "Can not run p4" if ($errorFlag); # 2. read config file readRC($rcfile); # 3. rename source directory renameOld($rc{ROOT}); # 4. set up log directory mkdir($rc{ROOT}); chdir($rc{ROOT}) || die "Can not chdir $rc{ROOT}"; delete($ENV{PWD}); # PWD confuses p4 mySystem("mkdir -p $rc{LOG}"); # 5. p4 commands: sync -f, get p4 change# mySystem("$p4exe -c $rc{P4CLIENT} sync -f " . "1> $rc{LOG}/$p4SyncOut 2> $rc{LOG}/$p4SyncErr"); my $change = `$p4exe -c $rc{P4CLIENT} changes -m 1 ...#have`; chomp($change); debugPrint("change: $change"); $change =~ s/Change (\d+) .*/$1/; die "No p4 change" if (! $change); # save p4 info for build scripts mySystem("echo $change > $rc{LOG}/$logP4Change"); mySystem("echo $rc{P4CLIENT} > $rc{LOG}/$logP4Client"); # 6. run start mySystem("./$rc{START} > $rc{LOG}/$startErr 2> $rc{LOG}/$startOut"); # 7. wrapup print "$0: ", ($errorFlag == 0) ? "Succeeded" : "Failed", "\n"; sleep(2); # finish print before ending ssh exit($errorFlag);