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

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

Additional version allow now for optimal reuse of parameters

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