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