source: ProjectBuilder/devel/pb/bin/pb.pl@ 28

Last change on this file since 28 was 28, checked in by Bruno Cornec, 17 years ago

build2pkg works for rpm. No we need a correct spec file ;-)

  • Property svn:executable set to *
File size: 15.0 KB
RevLine 
[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
[22]10# Syntax: see at end
[9]11
[18]12use strict 'vars';
[5]13use Getopt::Std;
[9]14use Data::Dumper;
15use English;
[16]16use AppConfig qw(:argcount :expand);
17use File::Basename;
[26]18use File::Copy;
[13]19use Time::localtime qw(localtime);
20use POSIX qw(strftime);
[5]21
[22]22use vars qw (%defpkgdir %extpkgdir %version %confparam %filteredfiles $debug $LOG);
[19]23%extpkgdir = ();
24%filteredfiles = ();
[21]25$debug = 0; # Debug level
[22]26$LOG = *STDOUT; # Where to log
[17]27use lib qw (lib);
[9]28use common qw (env_init);
[18]29use pb qw (pb_init);
[11]30use distro qw (distro_init);
[5]31use cms;
[17]32use changelog qw (changelog);
[5]33
34my %opts; # CLI Options
[9]35my $action; # action to realize
36my $test = "FALSE";
37my $option = "";
38my @pkgs;
[16]39my $pbtag; # Global TAG variable
40my $pbver; # Global VERSION variable
41my $pbrev; # GLOBAL REVISION variable
42my @date=(localtime->sec(), localtime->min(), localtime->hour(), localtime->mday(), localtime->mon(), localtime->year(), localtime->wday(), localtime->yday(), localtime->isdst());
43my $pbdate = strftime("%Y-%m-%d", @date);
[5]44
[22]45getopts('hl:p:qtv',\%opts);
[5]46
[21]47if (defined $opts{'h'}) {
48 syntax();
49 exit(0);
50}
51if (defined $opts{'v'}) {
52 $debug++;
53}
54if (defined $opts{'q'}) {
55 $debug=-1;
56}
[22]57if (defined $opts{'l'}) {
58 open(LOG,"> $opts{'l'}") || die "Unable to log to $opts{'l'}: $!";
59 $LOG = *LOG;
60 $debug = 0 if ($debug == -1);
61 }
[5]62# Handles project name if any
63if (defined $opts{'p'}) {
[9]64 $ENV{'PBPROJ'} = env_init($opts{'p'});
[5]65} else {
[9]66 $ENV{'PBPROJ'} = env_init();
[5]67}
[9]68# Handles test option
69if (defined $opts{'t'}) {
70 $test = "TRUE";
71 # Works only for SVN
72 $option = "-r BASE";
73}
[5]74
[9]75# Get Action
76$action = shift @ARGV;
[21]77die syntax() if (not defined $action);
[6]78
[22]79print $LOG "Project $ENV{'PBPROJ'}\n" if ($debug >= 0);
80print $LOG "Action: $action\n" if ($debug >= 0);
[9]81
82# Act depending on action
83if ($action =~ /^cms2build$/) {
[22]84 my $ptr = get_pkg();
85 @pkgs = @$ptr;
[9]86 cms_init();
87
[27]88 foreach my $pbpkg (@pkgs) {
89 if (-f "$ENV{'PBROOT'}/$pbpkg/VERSION") {
90 open(V,"$ENV{'PBROOT'}/$pbpkg/VERSION") || die "Unable to open $ENV{'PBROOT'}/$pbpkg/VERSION";
[16]91 $pbver = <V>;
92 chomp($pbver);
[9]93 close(V);
94 } else {
[16]95 $pbver = $ENV{'PBVER'};
[9]96 }
97
[27]98 if (-f "$ENV{'PBROOT'}/$pbpkg/TAG") {
99 open(T,"$ENV{'PBROOT'}/$pbpkg/TAG") || die "Unable to open $ENV{'PBROOT'}/$pbpkg/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'};
[22]107 print $LOG "\n" if ($debug >= 0);
[27]108 print $LOG "Management of $pbpkg $pbver-$pbtag (rev $pbrev)\n" if ($debug >= 0);
[9]109 die "Unable to get env var PBDESTDIR" if (not defined $ENV{'PBDESTDIR'});
[16]110 # Clean up dest if necessary. The export will recreate it
[27]111 my $dest = "$ENV{'PBDESTDIR'}/$pbpkg-$pbver";
[9]112 pbrm_rf($dest) if (-d $dest);
113
114 # Export CMS tree for the concerned package to dest
115 # And generate some additional files
116 $OUTPUT_AUTOFLUSH=1;
[27]117 print $LOG "$ENV{'PBCMSEXP'} of $pbpkg..." if ($debug >= 0);
[9]118 # computes in which dir we have to work
[27]119 my $dir = $defpkgdir{$pbpkg};
120 $dir = $extpkgdir{$pbpkg} if (not defined $dir);
[9]121 system("$ENV{'PBCMSEXP'} $option $ENV{'PBROOT'}/$dir $dest 1>/dev/null");
122 if ($? == -1) {
[22]123 print $LOG "failed to execute: $!\n" if ($debug >= 0);
[9]124 } elsif ($? & 127) {
[22]125 printf $LOG "child died with signal %d, %s coredump\n", ($? & 127), ($? & 128) ? 'with' : 'without' if ($debug >= 0);
[9]126 } else {
[22]127 print $LOG " OK\n" if ($debug >= 0);
[9]128 }
129
130 # Creates a REVISION file
131 open(R,"> $dest/REVISION") || die "Unable to create $dest/REVISION";
[16]132 print R "$pbrev\n";
[9]133 close(R);
134
135 # Extract cms log history and store it
136 system("$ENV{'PBCMSLOG'} $option $ENV{'PBROOT'}/$dir > $dest/$ENV{'PBCMSLOGFILE'}");
[27]137 print $LOG "$ENV{'PBCMSLOG'} of $pbpkg..." if ($debug >= 0);
[9]138 if ($? == -1) {
[22]139 print $LOG "failed to execute: $!\n" if ($debug >= 0);
[9]140 } elsif ($? & 127) {
[22]141 printf $LOG "child died with signal %d, %s coredump\n", ($? & 127), ($? & 128) ? 'with' : 'without' if ($debug >= 0);
[9]142 } else {
[22]143 print $LOG " OK\n" if ($debug >= 0);
[9]144 }
[21]145 my %build;
[11]146 open(D,"$ENV{'PBCONF'}/DISTROS") || die "Unable to find $ENV{'PBCONF'}/DISTROS\n";
147 while (<D>) {
148 my $d = $_;
149 my ($dir,$ver) = split(/_/,$d);
150 chomp($ver);
151 my ($ddir, $dver, $dfam, $dtype, $dsuf) = distro_init($dir,$ver);
[22]152 print $LOG "DEBUG: distro tuple: ".Dumper($ddir, $dver, $dfam, $dtype, $dsuf)."\n" if ($debug >= 1);
153 print $LOG "DEBUG Filtering PBDATE => $pbdate, PBTAG => $pbtag, PBVER => $pbver\n" if ($debug >= 1);
[13]154
[16]155 # Filter build files from the less precise up to the most with overloading
[13]156 # Filter all files found, keeping the name, and generating in dest
[16]157
158 # Find all build files first relatively to PBROOT
159 my %bfiles;
[27]160 print $LOG "DEBUG dir: $ENV{'PBCONF'}/$pbpkg\n" if ($debug >= 1);
[21]161 $build{"$ddir-$dver"} = "yes";
[27]162 if (-d "$ENV{'PBCONF'}/$pbpkg/$dtype") {
163 opendir(BDIR,"$ENV{'PBCONF'}/$pbpkg/$dtype") || die "Unable to open dir $ENV{'PBCONF'}/$pbpkg/$dtype: $!";
[16]164 foreach my $f (readdir(BDIR)) {
165 next if ($f =~ /^\./);
[27]166 $bfiles{$f} = "$ENV{'PBCONF'}/$pbpkg/$dtype/$f";
[16]167 $bfiles{$f} =~ s~$ENV{'PBROOT'}~~;
168 }
[13]169 closedir(BDIR);
[27]170 } elsif (-d "$ENV{'PBCONF'}/$pbpkg/$dfam") {
171 opendir(BDIR,"$ENV{'PBCONF'}/$pbpkg/$dfam") || die "Unable to open dir $ENV{'PBCONF'}/$pbpkg/$dfam: $!";
[16]172 foreach my $f (readdir(BDIR)) {
173 next if ($f =~ /^\./);
[27]174 $bfiles{$f} = "$ENV{'PBCONF'}/$pbpkg/$dfam/$f";
[16]175 $bfiles{$f} =~ s~$ENV{'PBROOT'}~~;
176 }
177 closedir(BDIR);
[27]178 } elsif (-d "$ENV{'PBCONF'}/$pbpkg/$ddir") {
179 opendir(BDIR,"$ENV{'PBCONF'}/$pbpkg/$ddir") || die "Unable to open dir $ENV{'PBCONF'}/$pbpkg/$ddir: $!";
[16]180 foreach my $f (readdir(BDIR)) {
181 next if ($f =~ /^\./);
[27]182 $bfiles{$f} = "$ENV{'PBCONF'}/$pbpkg/$ddir/$f";
[16]183 $bfiles{$f} =~ s~$ENV{'PBROOT'}~~;
184 }
[13]185 closedir(BDIR);
[27]186 } elsif (-d "$ENV{'PBCONF'}/$pbpkg/$ddir-$dver") {
187 opendir(BDIR,"$ENV{'PBCONF'}/$pbpkg/$ddir-$dver") || die "Unable to open dir $ENV{'PBCONF'}/$pbpkg/$ddir-$dver: $!";
[16]188 foreach my $f (readdir(BDIR)) {
189 next if ($f =~ /^\./);
[27]190 $bfiles{$f} = "$ENV{'PBCONF'}/$pbpkg/$ddir-$dver/$f";
[16]191 $bfiles{$f} =~ s~$ENV{'PBROOT'}~~;
192 }
[13]193 closedir(BDIR);
194 } else {
[21]195 $build{"$ddir-$dver"} = "no";
[13]196 next;
197 }
[22]198 print $LOG "DEBUG bfiles: ".Dumper(\%bfiles)."\n" if ($debug >= 1);
[13]199
[15]200 # Get all filters to apply
201 # They're cumulative from less specific to most specific
202 # suffix is .pbf
[16]203 my @ffiles;
204 my ($ffile0, $ffile1, $ffile2, $ffile3);
[27]205 if (-d "$ENV{'PBCONF'}/$pbpkg/pbfilter") {
206 $ffile0 = "$ENV{'PBCONF'}/$pbpkg/pbfilter/$dtype.pbf" if (-f "$ENV{'PBCONF'}/$pbpkg/pbfilter/$dtype.pbf");
207 $ffile1 = "$ENV{'PBCONF'}/$pbpkg/pbfilter/$dfam.pbf" if (-f "$ENV{'PBCONF'}/$pbpkg/pbfilter/$dfam.pbf");
208 $ffile2 = "$ENV{'PBCONF'}/$pbpkg/pbfilter/$ddir.pbf" if (-f "$ENV{'PBCONF'}/$pbpkg/pbfilter/$ddir.pbf");
209 $ffile3 = "$ENV{'PBCONF'}/$pbpkg/pbfilter/$ddir-$dver.pbf" if (-f "$ENV{'PBCONF'}/$pbpkg/pbfilter/$ddir-$dver.pbf");
[16]210 push @ffiles,$ffile0 if (defined $ffile0);
211 push @ffiles,$ffile1 if (defined $ffile1);
212 push @ffiles,$ffile2 if (defined $ffile2);
213 push @ffiles,$ffile3 if (defined $ffile3);
214 }
215 my $config = AppConfig->new({
216 # Auto Create variables mentioned in Conf file
217 CREATE => 1,
218 DEBUG => 0,
219 GLOBAL => {
220 # Each conf item is a hash
221 ARGCOUNT => AppConfig::ARGCOUNT_HASH
222 }
223 });
[19]224 my $ptr;
[16]225 if (@ffiles) {
[22]226 print $LOG "DEBUG ffiles: ".Dumper(\@ffiles)."\n" if ($debug >= 1);
[16]227 $config->file(@ffiles);
[19]228 $ptr = $config->get("filter");
[22]229 print $LOG "DEBUG f:".Dumper($ptr)."\n" if ($debug >= 1);
[19]230 } else {
231 $ptr = { };
232 }
[15]233
[19]234 # Apply now all the filters on all the files concerned
235 # All files are relative to PBROOT
236 # destination dir depends on the type of file
237 if (defined $ptr) {
238 foreach my $f (values %bfiles) {
[27]239 filter_file($f,$ptr,"$dest/pbconf/$ddir-$dver/".basename($f),$pbpkg,$dtype,$dsuf);
[16]240 }
[19]241 foreach my $f (keys %filteredfiles) {
[27]242 filter_file($f,$ptr,"$dest/$f",$pbpkg,$dtype,$dsuf);
[19]243 }
[15]244 }
[18]245 }
[21]246 if ($debug >= 0) {
247 my @found;
248 my @notfound;
249 foreach my $b (keys %build) {
250 push @found,$b if ($build{$b} =~ /yes/);
251 push @notfound,$b if ($build{$b} =~ /no/);
252 }
[22]253 print $LOG "Build files generated for ".join(',',@found)."\n";
254 print $LOG "No Build files found for ".join(',',@notfound)."\n";
[21]255 }
[18]256 close(D);
257 # Prepare the dest directory for archive
[27]258 if (-x "$ENV{'PBCONF'}/$pbpkg/pbpkginit") {
259 print $LOG " Executing $ENV{'PBCONF'}/$pbpkg/pbinit...\n" if ($debug >= 0);
260 system("cd $dest ; $ENV{'PBCONF'}/$pbpkg/pbinit");
[17]261 if ($? == -1) {
[22]262 print $LOG "failed to execute: $!\n" if ($debug >= 0);
[17]263 } elsif ($? & 127) {
[22]264 printf $LOG "child died with signal %d, %s coredump\n", ($? & 127), ($? & 128) ? 'with' : 'without' if ($debug >= 0);
[17]265 } else {
[22]266 print $LOG " OK\n" if ($debug >= 0);
[17]267 }
[11]268 }
[18]269 # Archive dest dir
[22]270 chdir "$ENV{'PBDESTDIR'}";
[27]271 print $LOG "Creating $pbpkg tar files (gzip... " if ($debug >= 0);
[25]272 # Possibility to look at PBSRC to guess more the filename
[27]273 system("tar cfphz $pbpkg-$pbver.tar.gz $pbpkg-$pbver");
[18]274 if ($? == -1) {
[22]275 print $LOG "failed to execute: $!\n" if ($debug >= 0);
[18]276 } elsif ($? & 127) {
[22]277 printf $LOG "child died with signal %d, %s coredump\n", ($? & 127), ($? & 128) ? 'with' : 'without' if ($debug >= 0);
[18]278 } else {
[22]279 print $LOG " OK)\n" if ($debug >= 0);
[27]280 print $LOG "Under $ENV{'PBDESTDIR'}/$pbpkg-$pbver.tar.gz\n" if ($debug >= 0);
[22]281 # Keep track of what is generated for build2pkg default
282 open(LAST,"> $ENV{'PBDESTDIR'}/LAST") || die "Unable to create $ENV{'PBDESTDIR'}/LAST";
283 print LAST "$pbver-$pbtag\n";
284 close(LAST);
[18]285 }
[9]286 }
[22]287} elsif ($action =~ /^build2pkg$/) {
288 # Check whether we have a specific version to build
289 my $vertag = shift @ARGV;
290 if (not defined $vertag) {
291 open(LAST,"$ENV{'PBDESTDIR'}/LAST") || die "Unable to open $ENV{'PBDESTDIR'}/LAST\nYou may want to precise as parameter version-tag";
292 $vertag = <LAST>;
293 chomp($vertag);
294 close(LAST);
295 }
296 ($pbver,$pbtag) = split(/-/,$vertag);
297
298 # Get list of packages to build
299 my $ptr = get_pkg();
300 @pkgs = @$ptr;
301
302 # Get the running distro to build on
[23]303 my ($ddir, $dver, $dfam, $dtype, $dsuf) = distro_init();
[25]304 print $LOG "DEBUG: distro tuple: ".join(',',($ddir, $dver, $dfam, $dtype, $dsuf))."\n" if ($debug >= 1);
[22]305
[25]306 chdir "$ENV{'PBBUILDDIR'}";
[27]307 foreach my $pbpkg (@pkgs) {
308 my $src="$ENV{'PBDESTDIR'}/$pbpkg-$pbver.tar.gz";
[25]309 print $LOG "Handling source file $src\n" if ($debug >= 0);
310
311 if ($dtype eq "rpm") {
312 # rpm has its own standard build directory
[28]313 my $tmp=`rpmquery --eval '%{_topdir}' 2> /dev/null`;
314 chomp($tmp);
315 $ENV{'PBBUILDDIR'}=$tmp;
316 print $LOG "Working under $ENV{'PBBUILDDIR'}\n" if ($debug >= 0);
[25]317 foreach my $d ('RPMS','SRPMS','SPECS','SOURCES','BUILD') {
[28]318 if (! -d "$ENV{'PBBUILDDIR'}/$d") {
319 pbmkdir_p("$ENV{'PBBUILDDIR'}/$d") || die "Please ensure that you can write into $ENV{'PBBUILDDIR'} to create $d\nSolution: setup _topdir in your ~/.rpmmacros or\nchown the $ENV{'PBBUILDDIR'} directory to your uid";
320 }
[25]321 }
322
323 # We need to first extract the spec file
[28]324 symlink "$src","$ENV{'PBBUILDDIR'}/SOURCES/".basename($src) || die "Unable to symlink $src in $ENV{'PBBUILDDIR'}/SOURCES";
325 my @specfile;
326 @specfile = extract_build_files($src,"$pbpkg-$pbver/pbconf/$ddir-$dver/","$ENV{'PBBUILDDIR'}/SPECS");
[25]327
[28]328 print $LOG "specfile: ".Dumper(\@specfile)."\n" if ($debug >= 1);
[25]329 # set LANGUAGE to check for correct log messages
330 $ENV{'LANGUAGE'}="C";
[28]331 #system("ls -R $ENV{'PBBUILDDIR'}") if ($debug >= 1);
332 foreach my $f (@specfile) {
333 if ($f =~ /\.spec$/) {
334 print $LOG "rpmbuild -ba $f ...\n";
335 system("rpmbuild -ba $f");
336 last;
337 }
338 }
[25]339 } elsif ($dtype eq "tgz") {
340 pbmkdir_p("$ENV{'PBBUILDDIR'}/install") if (! -d "$ENV{'PBBUILDDIR'}/install");
341 } elsif ($dtype eq "ebuild") {
342 pbmkdir_p("$ENV{'PBBUILDDIR'}/portage") if (! -d "$ENV{'PBBUILDDIR'}/portage");
343 } else {
344 }
[22]345 }
[9]346} else {
[22]347 print $LOG "'$action' is not available\n";
[21]348 syntax();
[9]349}
[16]350
[17]351# Function which applies filter on files
[16]352sub filter_file {
353
354my $f=shift;
355my $ptr=shift;
356my %filter=%$ptr;
357my $destfile=shift;
[27]358my $pbpkg=shift;
[17]359my $dtype=shift;
360my $dsuf=shift;
[16]361
[22]362print $LOG "DEBUG: From $f to $destfile\n" if ($debug >= 1);
[16]363pbmkdir_p(dirname($destfile)) if (! -d dirname($destfile));
364open(DEST,"> $destfile") || die "Unable to create $destfile";
365open(FILE,"$ENV{'PBROOT'}/$f") || die "Unable to open $f: $!";
366while (<FILE>) {
367 my $line = $_;
368 foreach my $s (keys %filter) {
369 # Process single variables
[22]370 print $LOG "DEBUG filter{$s}: $filter{$s}\n" if ($debug > 1);
[16]371 my $tmp = $filter{$s};
372 next if (not defined $tmp);
373 # Expand variables if any single one found
374 if ($tmp =~ /\$/) {
375 eval { $tmp =~ s/(\$\w+)/$1/eeg };
[17]376 # special case for ChangeLog
[20]377 } elsif (($tmp =~ /^yes$/) && ($s =~ /^PBLOG$/) && ($line =~ /^PBLOG$/)) {
[27]378 my $p = $defpkgdir{$pbpkg};
379 $p = $extpkgdir{$pbpkg} if (not defined $p);
380 changelog($dtype, $pbpkg, $pbtag, $dsuf, $p, \*DEST);
[20]381 $tmp = "";
[16]382 }
383 $line =~ s|$s|$tmp|;
384 }
385 print DEST $line;
[17]386}
[16]387close(FILE);
388close(DEST);
389}
[21]390
[22]391sub get_pkg {
392
393my @pkgs;
394
395# Get packages list
396if (not defined $ARGV[0]) {
397 @pkgs = keys %defpkgdir;
398} elsif ($ARGV[0] =~ /^all$/) {
399 @pkgs = keys %defpkgdir;
400 if (defined %extpkgdir) {
401 my $k = keys %extpkgdir;
402 if (defined $k) {
403 push(@pkgs, keys %extpkgdir);
404 }
405 }
406} else {
407 @pkgs = @ARGV;
408}
409print $LOG "Packages: ".join(',',@pkgs)."\n" if ($debug >= 0);
410return(\@pkgs);
411}
412
[25]413sub extract_build_files {
414
415my $src=shift;
416my $dir=shift;
[26]417my $ddir=shift;
[28]418my @files;
[25]419
[26]420print $LOG "Extracting build files... " if ($debug >= 0);
[25]421system("tar xfz $src $dir >/dev/null");
422if ($? == -1) {
423 print $LOG "failed to execute: $!\n" if ($debug >= 0);
424} elsif ($? & 127) {
425 printf $LOG "child died with signal %d, %s coredump\n", ($? & 127), ($? & 128) ? 'with' : 'without' if ($debug >= 0);
426} else {
427 print $LOG " OK\n" if ($debug >= 0);
428}
429opendir(DIR,"$dir") || die "Unable to open directory $dir";
430foreach my $f (readdir(DIR)) {
431 next if ($f =~ /^\./);
[26]432 move("$dir/$f","$ddir") || die "Unable to move $dir/$f to $ddir";
[28]433 print $LOG "mv $dir/$f $ddir\n" if ($debug >= 1);
434 push @files,"$ddir/$f";
[25]435}
436closedir(DIR);
[26]437# Not enough but still a first cleanup
[25]438pbrm_rf("$dir");
[28]439return(@files);
[25]440}
441
[21]442sub syntax {
443
444 print "Syntax: pb [-vhqt][-p project] <action> [<params>...]\n";
445 print "\n";
446 print "-h : This help file\n";
447 print "-q : Quiet mode\n";
448 print "-t : Test mode (not done yet)\n";
449 print "-v : Verbose mode\n";
450 print "\n";
451 print "-p project : Name of the project you're working on\n";
452 print " (or use the env variable PBPROJ) \n";
453 print "\n";
454 print "<action> can be:\n";
455 print "\n";
456 print "\tcms2build: Create a tar file of the project under your CMS\n";
457 print "\t CMS supported are SVN and CVS\n";
[22]458 print "\t parameters are packages to build\n";
459 print "\t if not using default list\n";
[21]460 print "\n";
461 print "\tbuild2pkg: Create packages for your running distribution \n";
[22]462 print "\t first parameter is version-tag to build\n";
463 print "\t if not using default version-tag\n";
464 print "\t following parameters are packages to build\n";
465 print "\t if not using default list\n";
[21]466 print "\n";
467 print "\n";
468}
Note: See TracBrowser for help on using the repository browser.