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