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

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

code of build2pkg done. tests begin

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