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

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

Modifications in progress...

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