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

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

Try to better organize the tree to be perl compliant

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