source: ProjectBuilder/devel/pb/bin/pb@ 79

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

Use more nv variables to deal with filters easily even outside ou pb

  • Property svn:executable set to *
File size: 12.9 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
[73]22# Global variables
[74]23use vars qw (%defpkgdir %extpkgdir %filteredfiles %pbrc $debug $LOG);
[73]24
[21]25$debug = 0; # Debug level
[22]26$LOG = *STDOUT; # Where to log
[17]27use lib qw (lib);
[74]28use ProjectBuilder::Distribution qw (pb_distro_init);
29use ProjectBuilder::Changelog qw (pb_changelog);
30use ProjectBuilder::Version qw (pb_version_init);
[77]31use ProjectBuilder::Base qw (pb_conf_read pb_cms_init pb_mkdir_p pb_system pb_rm_rf pb_get_filters pb_filter_file pb_filter_file_pb);
[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
[77]40my %pbver; # per package
41my %pbtag; # per package
[53]42my $pbrev; # Global REVISION variable
[16]43my @date=(localtime->sec(), localtime->min(), localtime->hour(), localtime->mday(), localtime->mon(), localtime->year(), localtime->wday(), localtime->yday(), localtime->isdst());
44my $pbdate = strftime("%Y-%m-%d", @date);
[79]45$ENV{'PBDATE'}=$pbdate;
[5]46
[67]47getopts('hl:p:qr:tv',\%opts);
[5]48
[75]49my ($projectbuilderver,$projectbuilderrev) = pb_version_init();
[21]50if (defined $opts{'h'}) {
[74]51 pb_syntax();
[21]52 exit(0);
53}
54if (defined $opts{'v'}) {
55 $debug++;
56}
57if (defined $opts{'q'}) {
58 $debug=-1;
59}
[22]60if (defined $opts{'l'}) {
61 open(LOG,"> $opts{'l'}") || die "Unable to log to $opts{'l'}: $!";
62 $LOG = *LOG;
63 $debug = 0 if ($debug == -1);
64 }
[9]65# Handles test option
66if (defined $opts{'t'}) {
67 $test = "TRUE";
68 # Works only for SVN
69 $option = "-r BASE";
70}
[5]71
[9]72# Get Action
73$action = shift @ARGV;
[74]74die pb_syntax() if (not defined $action);
[6]75
[67]76# Handle root of the project if defined
77if (defined $opts{'r'}) {
78 $ENV{'PBROOT'} = $opts{'r'};
79}
[59]80# Handles project name if any
81if (defined $opts{'p'}) {
[74]82 $ENV{'PBPROJ'} = pb_env_init($opts{'p'});
[59]83} else {
[74]84 $ENV{'PBPROJ'} = pb_env_init();
[59]85}
86
[22]87print $LOG "Project $ENV{'PBPROJ'}\n" if ($debug >= 0);
88print $LOG "Action: $action\n" if ($debug >= 0);
[9]89
90# Act depending on action
91if ($action =~ /^cms2build$/) {
[77]92 pb_cms2build();
93} elsif ($action =~ /^build2pkg$/) {
94 pb_build2pkg();
95} elsif ($action =~ /^cms2pkg$/) {
96 pb_cms2build();
97 pb_build2pkg();
98} else {
99 print $LOG "'$action' is not available\n";
100 pb_syntax();
101}
102
103sub pb_cms2build {
104
105 my $ptr = pb_get_pkg();
[22]106 @pkgs = @$ptr;
[74]107 pb_cms_init($ENV{'PBPROJ'});
[9]108
[27]109 foreach my $pbpkg (@pkgs) {
110 if (-f "$ENV{'PBROOT'}/$pbpkg/VERSION") {
111 open(V,"$ENV{'PBROOT'}/$pbpkg/VERSION") || die "Unable to open $ENV{'PBROOT'}/$pbpkg/VERSION";
[16]112 $pbver = <V>;
113 chomp($pbver);
[9]114 close(V);
115 } else {
[16]116 $pbver = $ENV{'PBVER'};
[9]117 }
118
[27]119 if (-f "$ENV{'PBROOT'}/$pbpkg/TAG") {
120 open(T,"$ENV{'PBROOT'}/$pbpkg/TAG") || die "Unable to open $ENV{'PBROOT'}/$pbpkg/TAG";
[16]121 $pbtag = <T>;
122 chomp($pbtag);
[9]123 close(T);
124 } else {
[16]125 $pbtag = $ENV{'PBTAG'};
[9]126 }
[16]127 $pbrev = $ENV{'PBREVISION'};
[22]128 print $LOG "\n" if ($debug >= 0);
[27]129 print $LOG "Management of $pbpkg $pbver-$pbtag (rev $pbrev)\n" if ($debug >= 0);
[9]130 die "Unable to get env var PBDESTDIR" if (not defined $ENV{'PBDESTDIR'});
[79]131 # Put some var in env to allow filtering
132 $ENV{'PBVER'}=$pbver;
133 $ENV{'PBTAG'}=$pbtag;
134 $ENV{'PBPKG'}=$pbpkg;
135 $ENV{'PBREV'}=$pbrev;
[16]136 # Clean up dest if necessary. The export will recreate it
[27]137 my $dest = "$ENV{'PBDESTDIR'}/$pbpkg-$pbver";
[74]138 pb_rm_rf($dest) if (-d $dest);
[9]139
140 # Export CMS tree for the concerned package to dest
141 # And generate some additional files
142 $OUTPUT_AUTOFLUSH=1;
[29]143
[9]144 # computes in which dir we have to work
[27]145 my $dir = $defpkgdir{$pbpkg};
146 $dir = $extpkgdir{$pbpkg} if (not defined $dir);
[74]147 pb_system("$ENV{'PBCMSEXP'} $option $ENV{'PBROOT'}/$dir $dest 1>/dev/null", "Exporting $ENV{'PBROOT'}/$dir");
[9]148
149 # Creates a REVISION file
150 open(R,"> $dest/REVISION") || die "Unable to create $dest/REVISION";
[16]151 print R "$pbrev\n";
[9]152 close(R);
153
154 # Extract cms log history and store it
[74]155 pb_system("$ENV{'PBCMSLOG'} $option $ENV{'PBROOT'}/$dir > $dest/$ENV{'PBCMSLOGFILE'}", "Extracting log info");
[29]156
[21]157 my %build;
[11]158 open(D,"$ENV{'PBCONF'}/DISTROS") || die "Unable to find $ENV{'PBCONF'}/DISTROS\n";
159 while (<D>) {
160 my $d = $_;
[55]161 my ($ndir,$ver) = split(/_/,$d);
[11]162 chomp($ver);
[74]163 my ($ddir, $dver, $dfam, $dtype, $dsuf) = pb_distro_init($ndir,$ver);
[22]164 print $LOG "DEBUG: distro tuple: ".Dumper($ddir, $dver, $dfam, $dtype, $dsuf)."\n" if ($debug >= 1);
165 print $LOG "DEBUG Filtering PBDATE => $pbdate, PBTAG => $pbtag, PBVER => $pbver\n" if ($debug >= 1);
[13]166
[16]167 # Filter build files from the less precise up to the most with overloading
[13]168 # Filter all files found, keeping the name, and generating in dest
[16]169
170 # Find all build files first relatively to PBROOT
171 my %bfiles;
[27]172 print $LOG "DEBUG dir: $ENV{'PBCONF'}/$pbpkg\n" if ($debug >= 1);
[21]173 $build{"$ddir-$dver"} = "yes";
[27]174 if (-d "$ENV{'PBCONF'}/$pbpkg/$dtype") {
175 opendir(BDIR,"$ENV{'PBCONF'}/$pbpkg/$dtype") || die "Unable to open dir $ENV{'PBCONF'}/$pbpkg/$dtype: $!";
[16]176 foreach my $f (readdir(BDIR)) {
177 next if ($f =~ /^\./);
[27]178 $bfiles{$f} = "$ENV{'PBCONF'}/$pbpkg/$dtype/$f";
[16]179 $bfiles{$f} =~ s~$ENV{'PBROOT'}~~;
180 }
[13]181 closedir(BDIR);
[27]182 } elsif (-d "$ENV{'PBCONF'}/$pbpkg/$dfam") {
183 opendir(BDIR,"$ENV{'PBCONF'}/$pbpkg/$dfam") || die "Unable to open dir $ENV{'PBCONF'}/$pbpkg/$dfam: $!";
[16]184 foreach my $f (readdir(BDIR)) {
185 next if ($f =~ /^\./);
[27]186 $bfiles{$f} = "$ENV{'PBCONF'}/$pbpkg/$dfam/$f";
[16]187 $bfiles{$f} =~ s~$ENV{'PBROOT'}~~;
188 }
189 closedir(BDIR);
[27]190 } elsif (-d "$ENV{'PBCONF'}/$pbpkg/$ddir") {
191 opendir(BDIR,"$ENV{'PBCONF'}/$pbpkg/$ddir") || die "Unable to open dir $ENV{'PBCONF'}/$pbpkg/$ddir: $!";
[16]192 foreach my $f (readdir(BDIR)) {
193 next if ($f =~ /^\./);
[27]194 $bfiles{$f} = "$ENV{'PBCONF'}/$pbpkg/$ddir/$f";
[16]195 $bfiles{$f} =~ s~$ENV{'PBROOT'}~~;
196 }
[13]197 closedir(BDIR);
[27]198 } elsif (-d "$ENV{'PBCONF'}/$pbpkg/$ddir-$dver") {
199 opendir(BDIR,"$ENV{'PBCONF'}/$pbpkg/$ddir-$dver") || die "Unable to open dir $ENV{'PBCONF'}/$pbpkg/$ddir-$dver: $!";
[16]200 foreach my $f (readdir(BDIR)) {
201 next if ($f =~ /^\./);
[27]202 $bfiles{$f} = "$ENV{'PBCONF'}/$pbpkg/$ddir-$dver/$f";
[16]203 $bfiles{$f} =~ s~$ENV{'PBROOT'}~~;
204 }
[13]205 closedir(BDIR);
206 } else {
[21]207 $build{"$ddir-$dver"} = "no";
[13]208 next;
209 }
[22]210 print $LOG "DEBUG bfiles: ".Dumper(\%bfiles)."\n" if ($debug >= 1);
[13]211
[15]212 # Get all filters to apply
[77]213 my $ptr = pb_get_filters($pbpkg, $dtype, $dfam, $ddir, $dver);
[15]214
[19]215 # Apply now all the filters on all the files concerned
216 # destination dir depends on the type of file
217 if (defined $ptr) {
218 foreach my $f (values %bfiles) {
[79]219 pb_filter_file_pb("$ENV{'PBROOT'}/$f",$ptr,"$dest/pbconf/$ddir-$dver/".basename($f),$dtype,$dsuf);
[16]220 }
[61]221 if (defined $filteredfiles{$dir}) {
222 foreach my $f (split(/,/,$filteredfiles{$dir})) {
[77]223 pb_filter_file("$ENV{'PBROOT'}/$dir/$f",$ptr,"$dest/$f");
[61]224 }
[19]225 }
[15]226 }
[18]227 }
[21]228 if ($debug >= 0) {
229 my @found;
230 my @notfound;
231 foreach my $b (keys %build) {
232 push @found,$b if ($build{$b} =~ /yes/);
233 push @notfound,$b if ($build{$b} =~ /no/);
234 }
[22]235 print $LOG "Build files generated for ".join(',',@found)."\n";
236 print $LOG "No Build files found for ".join(',',@notfound)."\n";
[21]237 }
[18]238 close(D);
239 # Prepare the dest directory for archive
[60]240 if (-x "$ENV{'PBCONF'}/$pbpkg/pbinit") {
[74]241 pb_system("cd $dest ; $ENV{'PBCONF'}/$pbpkg/pbinit","Executing init script $ENV{'PBCONF'}/$pbpkg/pbinit");
[11]242 }
[29]243
[18]244 # Archive dest dir
[69]245 chdir "$ENV{'PBDESTDIR'}" || die "Unable to change dir to $ENV{'PBDESTDIR'}";
[25]246 # Possibility to look at PBSRC to guess more the filename
[74]247 pb_system("tar cfpz $pbpkg-$pbver.tar.gz $pbpkg-$pbver","Creating $pbpkg tar files compressed");
[31]248 print $LOG "Under $ENV{'PBDESTDIR'}/$pbpkg-$pbver.tar.gz\n" if ($debug >= 0);
[70]249 # Keep track of what is generated for default
[77]250 $pbver{$pbpkg} = $pbver;
251 $pbtag{$pbpkg} = $pbtag;
[9]252 }
[77]253 open(LAST,"> $pbrc{$ENV{'PBPROJ'}}") || die "Unable to create $pbrc{$ENV{'PBPROJ'}}";
254 foreach my $v (keys %pbver) {
[79]255 print LAST "pbroot $v-_-$pbver{$v}-$pbtag{$v} = $ENV{'PBROOT'}\n";
[22]256 }
[77]257 close(LAST);
258}
[22]259
[77]260sub pb_build2pkg {
261
[22]262 # Get list of packages to build
[77]263 my $ptr = pb_get_pkg();
[22]264 @pkgs = @$ptr;
265
[77]266 # Check whether we have a specific version to build
267 my $vertag = shift @ARGV;
268
[22]269 # Get the running distro to build on
[74]270 my ($ddir, $dver, $dfam, $dtype, $dsuf) = pb_distro_init();
[25]271 print $LOG "DEBUG: distro tuple: ".join(',',($ddir, $dver, $dfam, $dtype, $dsuf))."\n" if ($debug >= 1);
[22]272
[25]273 chdir "$ENV{'PBBUILDDIR'}";
[27]274 foreach my $pbpkg (@pkgs) {
[77]275 # get the version of the current package - maybe different
276 if (not defined $vertag) {
277 if (-f $pbrc{$ENV{'PBPROJ'}}) {
278 my $pbroot = pb_conf_read($pbrc{$ENV{'PBPROJ'}},"pbroot");
279 # All lines should point to the same pbroot so take the first
280 $ENV{'PBROOT'} = (values %$pbroot)[0];
281 foreach my $k (keys %$pbroot) {
282 if ($k =~ /^$pbpkg-_-/) {
283 ($ptr,$vertag) = split(/-_-/,$k);
284 last;
285 }
286 }
287 die "Unable to find $pbpkg in $pbrc{$ENV{'PBPROJ'}}\nYou may want to precise as parameter version-tag" if (not defined $vertag);
288 } else {
289 die "Unable to open $pbrc{$ENV{'PBPROJ'}}\nYou may want to precise as parameter version-tag";
290 }
291 }
292 ($pbver,$pbtag) = split(/-/,$vertag);
293
[27]294 my $src="$ENV{'PBDESTDIR'}/$pbpkg-$pbver.tar.gz";
[67]295 print $LOG "Source file: $src\n" if ($debug >= 0);
[25]296
297 if ($dtype eq "rpm") {
298 # rpm has its own standard build directory
[28]299 my $tmp=`rpmquery --eval '%{_topdir}' 2> /dev/null`;
300 chomp($tmp);
301 $ENV{'PBBUILDDIR'}=$tmp;
302 print $LOG "Working under $ENV{'PBBUILDDIR'}\n" if ($debug >= 0);
[25]303 foreach my $d ('RPMS','SRPMS','SPECS','SOURCES','BUILD') {
[28]304 if (! -d "$ENV{'PBBUILDDIR'}/$d") {
[74]305 pb_mkdir_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";
[28]306 }
[25]307 }
308
309 # We need to first extract the spec file
[28]310 symlink "$src","$ENV{'PBBUILDDIR'}/SOURCES/".basename($src) || die "Unable to symlink $src in $ENV{'PBBUILDDIR'}/SOURCES";
311 my @specfile;
[77]312 @specfile = pb_extract_build_files($src,"$pbpkg-$pbver/pbconf/$ddir-$dver/","$ENV{'PBBUILDDIR'}/SPECS");
[25]313
[28]314 print $LOG "specfile: ".Dumper(\@specfile)."\n" if ($debug >= 1);
[25]315 # set LANGUAGE to check for correct log messages
316 $ENV{'LANGUAGE'}="C";
[28]317 #system("ls -R $ENV{'PBBUILDDIR'}") if ($debug >= 1);
318 foreach my $f (@specfile) {
319 if ($f =~ /\.spec$/) {
[74]320 pb_system("rpmbuild -ba $f","Building package with $f");
[28]321 last;
322 }
323 }
[25]324 } elsif ($dtype eq "tgz") {
[74]325 pb_mkdir_p("$ENV{'PBBUILDDIR'}/install") if (! -d "$ENV{'PBBUILDDIR'}/install");
[25]326 } elsif ($dtype eq "ebuild") {
[74]327 pb_mkdir_p("$ENV{'PBBUILDDIR'}/portage") if (! -d "$ENV{'PBBUILDDIR'}/portage");
[25]328 } else {
329 }
[22]330 }
[9]331}
[16]332
[77]333sub pb_get_pkg {
[16]334
[22]335my @pkgs;
336
337# Get packages list
338if (not defined $ARGV[0]) {
339 @pkgs = keys %defpkgdir;
340} elsif ($ARGV[0] =~ /^all$/) {
341 @pkgs = keys %defpkgdir;
342 if (defined %extpkgdir) {
343 my $k = keys %extpkgdir;
344 if (defined $k) {
345 push(@pkgs, keys %extpkgdir);
346 }
347 }
348} else {
349 @pkgs = @ARGV;
350}
351print $LOG "Packages: ".join(',',@pkgs)."\n" if ($debug >= 0);
352return(\@pkgs);
353}
354
[77]355sub pb_extract_build_files {
[25]356
357my $src=shift;
358my $dir=shift;
[26]359my $ddir=shift;
[28]360my @files;
[25]361
[74]362pb_system("tar xfpz $src $dir >/dev/null","Extracting build files");
[25]363opendir(DIR,"$dir") || die "Unable to open directory $dir";
364foreach my $f (readdir(DIR)) {
365 next if ($f =~ /^\./);
[26]366 move("$dir/$f","$ddir") || die "Unable to move $dir/$f to $ddir";
[28]367 print $LOG "mv $dir/$f $ddir\n" if ($debug >= 1);
368 push @files,"$ddir/$f";
[25]369}
370closedir(DIR);
[26]371# Not enough but still a first cleanup
[74]372pb_rm_rf("$dir");
[28]373return(@files);
[25]374}
375
[74]376sub pb_syntax {
[21]377
[53]378 print "pb (aka project-builder) Version $projectbuilderver-$projectbuilderrev\n";
379 print "\n";
[69]380 print "Syntax: pb [-vhqt][-r pbroot][-p project] <action> [<params>...]\n";
[21]381 print "\n";
382 print "-h : This help file\n";
383 print "-q : Quiet mode\n";
384 print "-t : Test mode (not done yet)\n";
385 print "-v : Verbose mode\n";
386 print "\n";
[69]387 print "-r pbroot : Path Name of project under the CMS \n";
388 print " (or use the env variable PBROOT) \n";
389 print "\n";
[21]390 print "-p project : Name of the project you're working on\n";
391 print " (or use the env variable PBPROJ) \n";
392 print "\n";
393 print "<action> can be:\n";
394 print "\n";
395 print "\tcms2build: Create a tar file of the project under your CMS\n";
396 print "\t CMS supported are SVN and CVS\n";
[22]397 print "\t parameters are packages to build\n";
398 print "\t if not using default list\n";
[21]399 print "\n";
400 print "\tbuild2pkg: Create packages for your running distribution \n";
[22]401 print "\t first parameter is version-tag to build\n";
402 print "\t if not using default version-tag\n";
403 print "\t following parameters are packages to build\n";
404 print "\t if not using default list\n";
[21]405 print "\n";
[77]406 print "\tcms2pkg: cms2build + build2pkg\n";
[21]407 print "\n";
408}
Note: See TracBrowser for help on using the repository browser.