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

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

Try to fix a difference between generated script and mine

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