| 1 | #!/usr/bin/perl -w
|
|---|
| 2 | #
|
|---|
| 3 | # Project Builder main application
|
|---|
| 4 | #
|
|---|
| 5 | # $Id$
|
|---|
| 6 | #
|
|---|
| 7 | # Copyright B. Cornec 2007
|
|---|
| 8 | # Provided under the GPL v2
|
|---|
| 9 |
|
|---|
| 10 | # Syntax: pb [-p project] <action> [<params>...]
|
|---|
| 11 |
|
|---|
| 12 | use strict;
|
|---|
| 13 | use Switch;
|
|---|
| 14 | use Getopt::Std;
|
|---|
| 15 | use Data::Dumper;
|
|---|
| 16 | use English;
|
|---|
| 17 | use AppConfig qw(:argcount :expand);
|
|---|
| 18 | use File::Basename;
|
|---|
| 19 | use Archive::Tar;
|
|---|
| 20 | use Time::localtime qw(localtime);
|
|---|
| 21 | use POSIX qw(strftime);
|
|---|
| 22 |
|
|---|
| 23 | use lib qw (lib);
|
|---|
| 24 | use vars qw (%defpkgdir %extpkgdir %version %param %filteredfiles);
|
|---|
| 25 | use common qw (env_init);
|
|---|
| 26 | use pb qw (defpkgdir extpkgdir version param filteredfiles pb_init);
|
|---|
| 27 | use distro qw (distro_init);
|
|---|
| 28 | use cms;
|
|---|
| 29 | use changelog qw (changelog);
|
|---|
| 30 |
|
|---|
| 31 | my %opts; # CLI Options
|
|---|
| 32 | my $action; # action to realize
|
|---|
| 33 | my $test = "FALSE";
|
|---|
| 34 | my $option = "";
|
|---|
| 35 | my @pkgs;
|
|---|
| 36 | my $pbtag; # Global TAG variable
|
|---|
| 37 | my $pbver; # Global VERSION variable
|
|---|
| 38 | my $pbrev; # GLOBAL REVISION variable
|
|---|
| 39 | my @date=(localtime->sec(), localtime->min(), localtime->hour(), localtime->mday(), localtime->mon(), localtime->year(), localtime->wday(), localtime->yday(), localtime->isdst());
|
|---|
| 40 | my $pbdate = strftime("%Y-%m-%d", @date);
|
|---|
| 41 |
|
|---|
| 42 | getopts('p:t',\%opts);
|
|---|
| 43 |
|
|---|
| 44 | # Handles project name if any
|
|---|
| 45 | if (defined $opts{'p'}) {
|
|---|
| 46 | $ENV{'PBPROJ'} = env_init($opts{'p'});
|
|---|
| 47 | } else {
|
|---|
| 48 | $ENV{'PBPROJ'} = env_init();
|
|---|
| 49 | }
|
|---|
| 50 | # Handles test option
|
|---|
| 51 | if (defined $opts{'t'}) {
|
|---|
| 52 | $test = "TRUE";
|
|---|
| 53 | # Works only for SVN
|
|---|
| 54 | $option = "-r BASE";
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | # Get Action
|
|---|
| 58 | $action = shift @ARGV;
|
|---|
| 59 | die "Syntax: pb [-p project] <action> [<params>...]" if (not defined $action);
|
|---|
| 60 |
|
|---|
| 61 | print "Project $ENV{'PBPROJ'}\n";
|
|---|
| 62 | #print "Action: $action - ARGV:".Dumper(\@ARGV);
|
|---|
| 63 |
|
|---|
| 64 | # Act depending on action
|
|---|
| 65 | if ($action =~ /^cms2build$/) {
|
|---|
| 66 | print "Action: cms2build\n";
|
|---|
| 67 | # Get packages list
|
|---|
| 68 | if (not defined $ARGV[0]) {
|
|---|
| 69 | @pkgs = keys %defpkgdir;
|
|---|
| 70 | } elsif ($ARGV[0] =~ /^all$/) {
|
|---|
| 71 | @pkgs = keys %defpkgdir;
|
|---|
| 72 | push(@pkgs, keys %extpkgdir);
|
|---|
| 73 | } else {
|
|---|
| 74 | @pkgs = @ARGV;
|
|---|
| 75 | }
|
|---|
| 76 | print "Packages:\n";
|
|---|
| 77 | print Dumper(@pkgs);
|
|---|
| 78 | cms_init();
|
|---|
| 79 |
|
|---|
| 80 | foreach my $pkg (@pkgs) {
|
|---|
| 81 |
|
|---|
| 82 | if (-f "$ENV{'PBROOT'}/$pkg/VERSION") {
|
|---|
| 83 | open(V,"$ENV{'PBROOT'}/$pkg/VERSION") || die "Unable to open $ENV{'PBROOT'}/$pkg/VERSION";
|
|---|
| 84 | $pbver = <V>;
|
|---|
| 85 | chomp($pbver);
|
|---|
| 86 | close(V);
|
|---|
| 87 | } else {
|
|---|
| 88 | $pbver = $ENV{'PBVER'};
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | if (-f "$ENV{'PBROOT'}/$pkg/TAG") {
|
|---|
| 92 | open(T,"$ENV{'PBROOT'}/$pkg/TAG") || die "Unable to open $ENV{'PBROOT'}/$pkg/TAG";
|
|---|
| 93 | $pbtag = <T>;
|
|---|
| 94 | chomp($pbtag);
|
|---|
| 95 | close(T);
|
|---|
| 96 | } else {
|
|---|
| 97 | $pbtag = $ENV{'PBTAG'};
|
|---|
| 98 | }
|
|---|
| 99 | $pbrev = $ENV{'PBREVISION'};
|
|---|
| 100 | print "Management of $pkg $pbver-$pbtag (rev $pbrev)\n";
|
|---|
| 101 | die "Unable to get env var PBDESTDIR" if (not defined $ENV{'PBDESTDIR'});
|
|---|
| 102 | # Clean up dest if necessary. The export will recreate it
|
|---|
| 103 | my $dest = "$ENV{'PBDESTDIR'}/$pkg-$pbver";
|
|---|
| 104 | pbrm_rf($dest) if (-d $dest);
|
|---|
| 105 |
|
|---|
| 106 | # Export CMS tree for the concerned package to dest
|
|---|
| 107 | # And generate some additional files
|
|---|
| 108 | $OUTPUT_AUTOFLUSH=1;
|
|---|
| 109 | print "$ENV{'PBCMSEXP'} of $pkg...";
|
|---|
| 110 | # computes in which dir we have to work
|
|---|
| 111 | my $dir = $defpkgdir{$pkg};
|
|---|
| 112 | $dir = $extpkgdir{$pkg} if (not defined $dir);
|
|---|
| 113 | system("$ENV{'PBCMSEXP'} $option $ENV{'PBROOT'}/$dir $dest 1>/dev/null");
|
|---|
| 114 | if ($? == -1) {
|
|---|
| 115 | print "failed to execute: $!\n";
|
|---|
| 116 | } elsif ($? & 127) {
|
|---|
| 117 | printf "child died with signal %d, %s coredump\n", ($? & 127), ($? & 128) ? 'with' : 'without';
|
|---|
| 118 | } else {
|
|---|
| 119 | print " Done under $dest\n";
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | # Creates a REVISION file
|
|---|
| 123 | open(R,"> $dest/REVISION") || die "Unable to create $dest/REVISION";
|
|---|
| 124 | print R "$pbrev\n";
|
|---|
| 125 | close(R);
|
|---|
| 126 |
|
|---|
| 127 | # Extract cms log history and store it
|
|---|
| 128 | system("$ENV{'PBCMSLOG'} $option $ENV{'PBROOT'}/$dir > $dest/$ENV{'PBCMSLOGFILE'}");
|
|---|
| 129 | print "$ENV{'PBCMSLOG'} of $pkg...";
|
|---|
| 130 | if ($? == -1) {
|
|---|
| 131 | print "failed to execute: $!\n";
|
|---|
| 132 | } elsif ($? & 127) {
|
|---|
| 133 | printf "child died with signal %d, %s coredump\n", ($? & 127), ($? & 128) ? 'with' : 'without';
|
|---|
| 134 | } else {
|
|---|
| 135 | print " OK\n";
|
|---|
| 136 | }
|
|---|
| 137 | open(D,"$ENV{'PBCONF'}/DISTROS") || die "Unable to find $ENV{'PBCONF'}/DISTROS\n";
|
|---|
| 138 | while (<D>) {
|
|---|
| 139 | my $d = $_;
|
|---|
| 140 | my ($dir,$ver) = split(/_/,$d);
|
|---|
| 141 | chomp($ver);
|
|---|
| 142 | print "Generating build files for $dir ($ver)\n";
|
|---|
| 143 | my ($ddir, $dver, $dfam, $dtype, $dsuf) = distro_init($dir,$ver);
|
|---|
| 144 | #print Dumper($ddir, $dver, $dfam, $dtype, $dsuf);
|
|---|
| 145 | #print "Filtering DDD => $pbdate, TTT => $pbtag, RRR => $pbtag$dsuf, VVV => $pbver\n";
|
|---|
| 146 |
|
|---|
| 147 | # Filter build files from the less precise up to the most with overloading
|
|---|
| 148 | # Filter all files found, keeping the name, and generating in dest
|
|---|
| 149 |
|
|---|
| 150 | # Find all build files first relatively to PBROOT
|
|---|
| 151 | my %bfiles;
|
|---|
| 152 | #print "dir: $ENV{'PBCONF'}/$pkg\n";
|
|---|
| 153 | if (-d "$ENV{'PBCONF'}/$pkg/$dtype") {
|
|---|
| 154 | opendir(BDIR,"$ENV{'PBCONF'}/$pkg/$dtype" || die "Unable to open dir $ENV{'PBCONF'}/$pkg/$dtype: $!");
|
|---|
| 155 | foreach my $f (readdir(BDIR)) {
|
|---|
| 156 | next if ($f =~ /^\./);
|
|---|
| 157 | $bfiles{$f} = "$ENV{'PBCONF'}/$pkg/$dtype/$f";
|
|---|
| 158 | $bfiles{$f} =~ s~$ENV{'PBROOT'}~~;
|
|---|
| 159 | }
|
|---|
| 160 | closedir(BDIR);
|
|---|
| 161 | } elsif (-d "$ENV{'PBCONF'}/$pkg/$dfam") {
|
|---|
| 162 | opendir(BDIR,"$ENV{'PBCONF'}/$pkg/$dfam" || die "Unable to open dir $ENV{'PBCONF'}/$pkg/$dfam: $!");
|
|---|
| 163 | foreach my $f (readdir(BDIR)) {
|
|---|
| 164 | next if ($f =~ /^\./);
|
|---|
| 165 | $bfiles{$f} = "$ENV{'PBCONF'}/$pkg/$dfam/$f";
|
|---|
| 166 | $bfiles{$f} =~ s~$ENV{'PBROOT'}~~;
|
|---|
| 167 | }
|
|---|
| 168 | closedir(BDIR);
|
|---|
| 169 | } elsif (-d "$ENV{'PBCONF'}/$pkg/$ddir") {
|
|---|
| 170 | opendir(BDIR,"$ENV{'PBCONF'}/$pkg/$ddir" || die "Unable to open dir $ENV{'PBCONF'}/$pkg/$ddir: $!");
|
|---|
| 171 | foreach my $f (readdir(BDIR)) {
|
|---|
| 172 | next if ($f =~ /^\./);
|
|---|
| 173 | $bfiles{$f} = "$ENV{'PBCONF'}/$pkg/$ddir/$f";
|
|---|
| 174 | $bfiles{$f} =~ s~$ENV{'PBROOT'}~~;
|
|---|
| 175 | }
|
|---|
| 176 | closedir(BDIR);
|
|---|
| 177 | } elsif (-d "$ENV{'PBCONF'}/$pkg/$ddir-$dver") {
|
|---|
| 178 | opendir(BDIR,"$ENV{'PBCONF'}/$pkg/$ddir-$dver" || die "Unable to open dir $ENV{'PBCONF'}/$pkg/$ddir-$dver: $!");
|
|---|
| 179 | foreach my $f (readdir(BDIR)) {
|
|---|
| 180 | next if ($f =~ /^\./);
|
|---|
| 181 | $bfiles{$f} = "$ENV{'PBCONF'}/$pkg/$ddir-$dver/$f";
|
|---|
| 182 | $bfiles{$f} =~ s~$ENV{'PBROOT'}~~;
|
|---|
| 183 | }
|
|---|
| 184 | closedir(BDIR);
|
|---|
| 185 | } else {
|
|---|
| 186 | print "No Build Files found for $ddir-$dver\n";
|
|---|
| 187 | next;
|
|---|
| 188 | }
|
|---|
| 189 | print "bfiles: ".Dumper(\%bfiles)."\n";
|
|---|
| 190 |
|
|---|
| 191 | # Get all filters to apply
|
|---|
| 192 | # They're cumulative from less specific to most specific
|
|---|
| 193 | # suffix is .pbf
|
|---|
| 194 | my @ffiles;
|
|---|
| 195 | my ($ffile0, $ffile1, $ffile2, $ffile3);
|
|---|
| 196 | if (-d "$ENV{'PBCONF'}/$pkg/pbfilter") {
|
|---|
| 197 | $ffile0 = "$ENV{'PBCONF'}/$pkg/pbfilter/$dtype.pbf" if (-f "$ENV{'PBCONF'}/$pkg/pbfilter/$dtype.pbf");
|
|---|
| 198 | $ffile1 = "$ENV{'PBCONF'}/$pkg/pbfilter/$dfam.pbf" if (-f "$ENV{'PBCONF'}/$pkg/pbfilter/$dfam.pbf");
|
|---|
| 199 | $ffile2 = "$ENV{'PBCONF'}/$pkg/pbfilter/$ddir.pbf" if (-f "$ENV{'PBCONF'}/$pkg/pbfilter/$ddir.pbf");
|
|---|
| 200 | $ffile3 = "$ENV{'PBCONF'}/$pkg/pbfilter/$ddir-$dver.pbf" if (-f "$ENV{'PBCONF'}/$pkg/pbfilter/$ddir-$dver.pbf");
|
|---|
| 201 | push @ffiles,$ffile0 if (defined $ffile0);
|
|---|
| 202 | push @ffiles,$ffile1 if (defined $ffile1);
|
|---|
| 203 | push @ffiles,$ffile2 if (defined $ffile2);
|
|---|
| 204 | push @ffiles,$ffile3 if (defined $ffile3);
|
|---|
| 205 | }
|
|---|
| 206 | my $config = AppConfig->new({
|
|---|
| 207 | # Auto Create variables mentioned in Conf file
|
|---|
| 208 | CREATE => 1,
|
|---|
| 209 | DEBUG => 0,
|
|---|
| 210 | GLOBAL => {
|
|---|
| 211 | # Each conf item is a hash
|
|---|
| 212 | ARGCOUNT => AppConfig::ARGCOUNT_HASH
|
|---|
| 213 | }
|
|---|
| 214 | });
|
|---|
| 215 | print "ffiles: ".Dumper(\@ffiles)."\n";
|
|---|
| 216 | if (@ffiles) {
|
|---|
| 217 | $config->file(@ffiles);
|
|---|
| 218 | my $ptr = $config->get("filter");
|
|---|
| 219 | print "f:".Dumper($ptr)."\n";
|
|---|
| 220 |
|
|---|
| 221 | # Apply now all the filters on all the files concerned
|
|---|
| 222 | # All files are relative to PBROOT
|
|---|
| 223 | # destination dir depends on the type of file
|
|---|
| 224 | if (defined $ptr) {
|
|---|
| 225 | foreach my $f (values %bfiles) {
|
|---|
| 226 | filter_file($f,$ptr,"$dest/pbconf/$ddir-$dver/".basename($f),$pkg,$dtype,$dsuf);
|
|---|
| 227 | }
|
|---|
| 228 | foreach my $f (keys %filteredfiles) {
|
|---|
| 229 | filter_file($f,$ptr,"$dest/$f",$pkg,$dtype,$dsuf);
|
|---|
| 230 | }
|
|---|
| 231 | }
|
|---|
| 232 | }
|
|---|
| 233 | # Prepare the dest directory for archive
|
|---|
| 234 | if (-x "$ENV{'PBCONF'}/$pkg/pbpkginit") {
|
|---|
| 235 | system("cd $dest ; $ENV{'PBCONF'}/$pkg/pbinit");
|
|---|
| 236 | if ($? == -1) {
|
|---|
| 237 | print "failed to execute: $!\n";
|
|---|
| 238 | } elsif ($? & 127) {
|
|---|
| 239 | printf "child died with signal %d, %s coredump\n", ($? & 127), ($? & 128) ? 'with' : 'without';
|
|---|
| 240 | } else {
|
|---|
| 241 | print " $dest\n";
|
|---|
| 242 | }
|
|---|
| 243 | }
|
|---|
| 244 | # Archive dest dir
|
|---|
| 245 | chdir "$dest/..";
|
|---|
| 246 | print "Creating $pkg tar files (gzip... ";
|
|---|
| 247 | system("tar cfphz $pkg-$pbver.tar.gz $pkg-$pbver");
|
|---|
| 248 | if ($? == -1) {
|
|---|
| 249 | print "failed to execute: $!\n";
|
|---|
| 250 | } elsif ($? & 127) {
|
|---|
| 251 | printf "child died with signal %d, %s coredump\n", ($? & 127), ($? & 128) ? 'with' : 'without';
|
|---|
| 252 | } else {
|
|---|
| 253 | print " OK)\n";
|
|---|
| 254 | print "Under $dest/../$pkg-$pbver.tar.gz\n";
|
|---|
| 255 | }
|
|---|
| 256 | }
|
|---|
| 257 | close(D);
|
|---|
| 258 | }
|
|---|
| 259 | } else {
|
|---|
| 260 | print "'$action' is not available\n";
|
|---|
| 261 | print "Available actions are:\n";
|
|---|
| 262 | print " cms2build\n";
|
|---|
| 263 | }
|
|---|
| 264 |
|
|---|
| 265 | # Function which applies filter on files
|
|---|
| 266 | sub filter_file {
|
|---|
| 267 |
|
|---|
| 268 | my $f=shift;
|
|---|
| 269 | my $ptr=shift;
|
|---|
| 270 | my %filter=%$ptr;
|
|---|
| 271 | my $destfile=shift;
|
|---|
| 272 | my $pkg=shift;
|
|---|
| 273 | my $dtype=shift;
|
|---|
| 274 | my $dsuf=shift;
|
|---|
| 275 |
|
|---|
| 276 | print "From $f to $destfile\n";
|
|---|
| 277 | pbmkdir_p(dirname($destfile)) if (! -d dirname($destfile));
|
|---|
| 278 | open(DEST,"> $destfile") || die "Unable to create $destfile";
|
|---|
| 279 | open(FILE,"$ENV{'PBROOT'}/$f") || die "Unable to open $f: $!";
|
|---|
| 280 | while (<FILE>) {
|
|---|
| 281 | my $line = $_;
|
|---|
| 282 | foreach my $s (keys %filter) {
|
|---|
| 283 | # Process single variables
|
|---|
| 284 | #print "debug: $filter{$s}\n";
|
|---|
| 285 | my $tmp = $filter{$s};
|
|---|
| 286 | next if (not defined $tmp);
|
|---|
| 287 | # Expand variables if any single one found
|
|---|
| 288 | if ($tmp =~ /\$/) {
|
|---|
| 289 | eval { $tmp =~ s/(\$\w+)/$1/eeg };
|
|---|
| 290 | # special case for ChangeLog
|
|---|
| 291 | } elsif (($tmp =~ /^yes$/) && ($s =~ /^PBLOG$/)) {
|
|---|
| 292 | $tmp = changelog($dtype, $pkg, $pbtag, $dsuf, \*DEST);
|
|---|
| 293 | }
|
|---|
| 294 | $line =~ s|$s|$tmp|;
|
|---|
| 295 | }
|
|---|
| 296 | print DEST $line;
|
|---|
| 297 | }
|
|---|
| 298 | close(FILE);
|
|---|
| 299 | close(DEST);
|
|---|
| 300 | }
|
|---|