[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 |
|
---|
[5] | 12 | use strict;
|
---|
| 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 |
|
---|
[17] | 23 | use lib qw (lib);
|
---|
| 24 | use vars qw (%defpkgdir %extpkgdir %version %param %filteredfiles);
|
---|
[9] | 25 | use common qw (env_init);
|
---|
[17] | 26 | use pb qw (defpkgdir extpkgdir version param filteredfiles 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;
|
---|
| 72 | push(@pkgs, keys %extpkgdir);
|
---|
| 73 | } else {
|
---|
| 74 | @pkgs = @ARGV;
|
---|
| 75 | }
|
---|
| 76 | print "Packages:\n";
|
---|
| 77 | print Dumper(@pkgs);
|
---|
| 78 | cms_init();
|
---|
| 79 |
|
---|
[17] | 80 | foreach my $pkg (@pkgs) {
|
---|
[9] | 81 |
|
---|
[17] | 82 | if (-f "$ENV{'PBROOT'}/$pkg/VERSION") {
|
---|
| 83 | open(V,"$ENV{'PBROOT'}/$pkg/VERSION") || die "Unable to open $ENV{'PBROOT'}/$pkg/VERSION";
|
---|
[16] | 84 | $pbver = <V>;
|
---|
| 85 | chomp($pbver);
|
---|
[9] | 86 | close(V);
|
---|
| 87 | } else {
|
---|
[16] | 88 | $pbver = $ENV{'PBVER'};
|
---|
[9] | 89 | }
|
---|
| 90 |
|
---|
[17] | 91 | if (-f "$ENV{'PBROOT'}/$pkg/TAG") {
|
---|
| 92 | open(T,"$ENV{'PBROOT'}/$pkg/TAG") || die "Unable to open $ENV{'PBROOT'}/$pkg/TAG";
|
---|
[16] | 93 | $pbtag = <T>;
|
---|
| 94 | chomp($pbtag);
|
---|
[9] | 95 | close(T);
|
---|
| 96 | } else {
|
---|
[16] | 97 | $pbtag = $ENV{'PBTAG'};
|
---|
[9] | 98 | }
|
---|
[16] | 99 | $pbrev = $ENV{'PBREVISION'};
|
---|
[17] | 100 | print "Management of $pkg $pbver-$pbtag (rev $pbrev)\n";
|
---|
[9] | 101 | die "Unable to get env var PBDESTDIR" if (not defined $ENV{'PBDESTDIR'});
|
---|
[16] | 102 | # Clean up dest if necessary. The export will recreate it
|
---|
[17] | 103 | my $dest = "$ENV{'PBDESTDIR'}/$pkg-$pbver";
|
---|
[9] | 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;
|
---|
[17] | 109 | print "$ENV{'PBCMSEXP'} of $pkg...";
|
---|
[9] | 110 | # computes in which dir we have to work
|
---|
[17] | 111 | my $dir = $defpkgdir{$pkg};
|
---|
| 112 | $dir = $extpkgdir{$pkg} if (not defined $dir);
|
---|
[9] | 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";
|
---|
[16] | 124 | print R "$pbrev\n";
|
---|
[9] | 125 | close(R);
|
---|
| 126 |
|
---|
| 127 | # Extract cms log history and store it
|
---|
| 128 | system("$ENV{'PBCMSLOG'} $option $ENV{'PBROOT'}/$dir > $dest/$ENV{'PBCMSLOGFILE'}");
|
---|
[17] | 129 | print "$ENV{'PBCMSLOG'} of $pkg...";
|
---|
[9] | 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 | }
|
---|
[11] | 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);
|
---|
[16] | 145 | #print "Filtering DDD => $pbdate, TTT => $pbtag, RRR => $pbtag$dsuf, VVV => $pbver\n";
|
---|
[13] | 146 |
|
---|
[16] | 147 | # Filter build files from the less precise up to the most with overloading
|
---|
[13] | 148 | # Filter all files found, keeping the name, and generating in dest
|
---|
[16] | 149 |
|
---|
| 150 | # Find all build files first relatively to PBROOT
|
---|
| 151 | my %bfiles;
|
---|
[17] | 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: $!");
|
---|
[16] | 155 | foreach my $f (readdir(BDIR)) {
|
---|
| 156 | next if ($f =~ /^\./);
|
---|
[17] | 157 | $bfiles{$f} = "$ENV{'PBCONF'}/$pkg/$dtype/$f";
|
---|
[16] | 158 | $bfiles{$f} =~ s~$ENV{'PBROOT'}~~;
|
---|
| 159 | }
|
---|
[13] | 160 | closedir(BDIR);
|
---|
[17] | 161 | } elsif (-d "$ENV{'PBCONF'}/$pkg/$dfam") {
|
---|
| 162 | opendir(BDIR,"$ENV{'PBCONF'}/$pkg/$dfam" || die "Unable to open dir $ENV{'PBCONF'}/$pkg/$dfam: $!");
|
---|
[16] | 163 | foreach my $f (readdir(BDIR)) {
|
---|
| 164 | next if ($f =~ /^\./);
|
---|
[17] | 165 | $bfiles{$f} = "$ENV{'PBCONF'}/$pkg/$dfam/$f";
|
---|
[16] | 166 | $bfiles{$f} =~ s~$ENV{'PBROOT'}~~;
|
---|
| 167 | }
|
---|
| 168 | closedir(BDIR);
|
---|
[17] | 169 | } elsif (-d "$ENV{'PBCONF'}/$pkg/$ddir") {
|
---|
| 170 | opendir(BDIR,"$ENV{'PBCONF'}/$pkg/$ddir" || die "Unable to open dir $ENV{'PBCONF'}/$pkg/$ddir: $!");
|
---|
[16] | 171 | foreach my $f (readdir(BDIR)) {
|
---|
| 172 | next if ($f =~ /^\./);
|
---|
[17] | 173 | $bfiles{$f} = "$ENV{'PBCONF'}/$pkg/$ddir/$f";
|
---|
[16] | 174 | $bfiles{$f} =~ s~$ENV{'PBROOT'}~~;
|
---|
| 175 | }
|
---|
[13] | 176 | closedir(BDIR);
|
---|
[17] | 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: $!");
|
---|
[16] | 179 | foreach my $f (readdir(BDIR)) {
|
---|
| 180 | next if ($f =~ /^\./);
|
---|
[17] | 181 | $bfiles{$f} = "$ENV{'PBCONF'}/$pkg/$ddir-$dver/$f";
|
---|
[16] | 182 | $bfiles{$f} =~ s~$ENV{'PBROOT'}~~;
|
---|
| 183 | }
|
---|
[13] | 184 | closedir(BDIR);
|
---|
| 185 | } else {
|
---|
| 186 | print "No Build Files found for $ddir-$dver\n";
|
---|
| 187 | next;
|
---|
| 188 | }
|
---|
[16] | 189 | print "bfiles: ".Dumper(\%bfiles)."\n";
|
---|
[13] | 190 |
|
---|
[15] | 191 | # Get all filters to apply
|
---|
| 192 | # They're cumulative from less specific to most specific
|
---|
| 193 | # suffix is .pbf
|
---|
[16] | 194 | my @ffiles;
|
---|
| 195 | my ($ffile0, $ffile1, $ffile2, $ffile3);
|
---|
[17] | 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");
|
---|
[16] | 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";
|
---|
[15] | 220 |
|
---|
[16] | 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) {
|
---|
[17] | 226 | filter_file($f,$ptr,"$dest/pbconf/$ddir-$dver/".basename($f),$pkg,$dtype,$dsuf);
|
---|
[16] | 227 | }
|
---|
[17] | 228 | foreach my $f (keys %filteredfiles) {
|
---|
| 229 | filter_file($f,$ptr,"$dest/$f",$pkg,$dtype,$dsuf);
|
---|
[16] | 230 | }
|
---|
| 231 | }
|
---|
[15] | 232 | }
|
---|
[17] | 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 | }
|
---|
[11] | 256 | }
|
---|
| 257 | close(D);
|
---|
[9] | 258 | }
|
---|
| 259 | } else {
|
---|
| 260 | print "'$action' is not available\n";
|
---|
| 261 | print "Available actions are:\n";
|
---|
| 262 | print " cms2build\n";
|
---|
| 263 | }
|
---|
[16] | 264 |
|
---|
[17] | 265 | # Function which applies filter on files
|
---|
[16] | 266 | sub filter_file {
|
---|
| 267 |
|
---|
| 268 | my $f=shift;
|
---|
| 269 | my $ptr=shift;
|
---|
| 270 | my %filter=%$ptr;
|
---|
| 271 | my $destfile=shift;
|
---|
[17] | 272 | my $pkg=shift;
|
---|
| 273 | my $dtype=shift;
|
---|
| 274 | my $dsuf=shift;
|
---|
[16] | 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 };
|
---|
[17] | 290 | # special case for ChangeLog
|
---|
| 291 | } elsif (($tmp =~ /^yes$/) && ($s =~ /^PBLOG$/)) {
|
---|
| 292 | $tmp = changelog($dtype, $pkg, $pbtag, $dsuf, \*DEST);
|
---|
[16] | 293 | }
|
---|
| 294 | $line =~ s|$s|$tmp|;
|
---|
| 295 | }
|
---|
| 296 | print DEST $line;
|
---|
[17] | 297 | }
|
---|
[16] | 298 | close(FILE);
|
---|
| 299 | close(DEST);
|
---|
| 300 | }
|
---|