source: ProjectBuilder/devel/pb/lib/ProjectBuilder/Base.pm@ 116

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

Typo fixed

  • Property svn:executable set to *
File size: 13.1 KB
RevLine 
[2]1#!/usr/bin/perl -w
2#
[74]3# Base subroutines for the Project-Builder project
[2]4#
5# $Id$
6#
7
[18]8use strict;
[5]9use lib qw (lib);
[2]10use File::Basename;
[9]11use File::Path;
[2]12use File::Temp qw /tempdir/;
[74]13use AppConfig qw(ARGCOUNT_HASH);
[8]14use Data::Dumper;
[2]15
[49]16$ENV{'PBETC'} = "$ENV{'HOME'}/.pbrc";
[5]17
[74]18sub pb_env_init {
[2]19
[5]20my $proj=shift;
21my $ver;
22my $tag;
[2]23
[107]24# For the moment not dynamic
25my $debug = 0; # Debug level
26my $LOG = *STDOUT; # Where to log
27
[8]28#
[5]29# Check project name
[49]30# Could be with env var PBPROJ
31# or option -p
32# if not define take the first in conf file
[8]33#
[5]34if ((defined $ENV{'PBPROJ'}) &&
35 (not (defined $proj))) {
36 $proj = $ENV{'PBPROJ'};
37}
[69]38
[49]39#
[69]40# We get the pbrc file for that project
41# and use its content
42#
[89]43my ($pbrc) = pb_conf_read("$ENV{'PBETC'}","pbrc");
[74]44print "DEBUG pbrc: ".Dumper($pbrc)."\n" if ($debug >= 1);
[69]45
[108]46my %pbrc = %$pbrc;
[69]47if (not defined $proj) {
48 # Take the first as the default project
49 $proj = (keys %pbrc)[0];
50 print $LOG "Using $proj as default project as none has been specified\n" if (($debug >= 0) and (defined $proj));
51}
52die "No project defined - use env var PBPROJ or -p proj" if (not (defined $proj));
53
54#
55# Set delivery directory
56#
[71]57my $topdir=dirname($pbrc{$proj});
[108]58# Expand potential env variable in it
59eval { $topdir =~ s/(\$ENV.+\})/$1/eeg };
[69]60chdir $topdir || die "Unable to change directory to $topdir";
[108]61$pbrc{$proj} = $topdir."/pbrc";
[69]62$ENV{'PBDESTDIR'}=$topdir."/delivery";
63
64#
[67]65# Use project configuration file if needed
[49]66#
[67]67if (not defined $ENV{'PBROOT'}) {
[69]68 if (-f $pbrc{$proj}) {
[89]69 my ($pbroot) = pb_conf_read($pbrc{$proj},"pbroot");
[71]70 my %pbroot = %$pbroot;
[77]71 # All lines should point to the same pbroot so take the first
[69]72 $ENV{'PBROOT'} = (values %$pbroot)[0] if (defined $pbroot);
73 print $LOG "Using $ENV{'PBROOT'} as default pbroot from $pbrc{$proj}\n" if (($debug >= 0) and (defined $ENV{'PBROOT'}));
[67]74 }
[69]75 die "No pbroot defined - use env var PBROOT or -r pbroot " if (not defined $ENV{'PBROOT'});
[49]76}
[2]77
[8]78#
[49]79# Check pb conf compliance
[8]80#
[49]81$ENV{'PBCONF'} = "$ENV{'PBROOT'}/pbconf";
82die "Project $proj not Project-Builder compliant. Please populate $ENV{'PBCONF'}" if ( not -d "$ENV{'PBCONF'}");
[2]83
[74]84my %version = ();
[108]85my %defpkgdir = ();
86my %extpkgdir = ();
87my %filteredfiles = ();
[74]88
[49]89if (-f "$ENV{'PBCONF'}/$proj.pb") {
[74]90 # List of pkg to build by default (mandatory)
91 # List of additional pkg to build when all is called (optional)
92 # Valid version names (optional)
93 # List of files to filter (optional)
[105]94 my ($defpkgdir, $extpkgdir, $version, $filteredfiles, $pkgv, $pkgt) = pb_conf_read("$ENV{'PBCONF'}/$proj.pb","defpkgdir","extpkgdir","version","filteredfiles","projver","projtag");
[74]95 print "DEBUG: defpkgdir: ".Dumper($defpkgdir)."\n" if ($debug >= 1);
96 print "DEBUG: extpkgdir: ".Dumper($extpkgdir)."\n" if ($debug >= 1);
97 print "DEBUG: version: ".Dumper($version)."\n" if ($debug >= 1);
98 print "DEBUG: filteredfiles: ".Dumper($filteredfiles)."\n" if ($debug >= 1);
99 die "Unable to find defpkgdir in $ENV{'PBCONF'}/$proj.pb" if (not defined $defpkgdir);
100 # Global
101 %defpkgdir = %$defpkgdir;
102 # Global
[114]103 %extpkgdir = %$extpkgdir if (defined $extpkgdir);
[74]104 %version = %$version if (defined $version);
105 # Global
106 %filteredfiles = %$filteredfiles if (defined $filteredfiles);
[106]107 #
108 # Get global Version/Tag
109 #
[38]110
[106]111 if (not defined $ENV{'PBVER'}) {
112 if ((defined $pkgv) && (defined $pkgv->{$proj})) {
113 $ENV{'PBVER'}=$pkgv->{$proj};
114 } else {
115 die "No projver found in $ENV{'PBCONF'}/$proj.pb";
116 }
[98]117 }
[106]118 die "Invalid version name $ENV{'PBVER'} in $ENV{'PBCONF'}/$proj.pb" if (($ENV{'PBVER'} !~ /[0-9.]+/) && (not exists $version{$ENV{'PBVER'}}));
119
120 if (not defined $ENV{'PBTAG'}) {
121 if ((defined $pkgt) && (defined $pkgt->{$proj})) {
122 $ENV{'PBTAG'}=$pkgt->{$proj};
123 } else {
124 die "No projtag found in $ENV{'PBCONF'}/$proj.pb";
125 }
[98]126 }
[106]127 die "Invalid tag name $ENV{'PBTAG'} in $ENV{'PBCONF'}/$proj.pb" if ($ENV{'PBTAG'} !~ /[0-9.]+/);
128} else {
129 die "Unable to open $ENV{'PBCONF'}/$proj.pb";
[98]130}
131
[8]132#
[105]133# Set temp directory
134#
135if (not defined $ENV{'TMPDIR'}) {
136 $ENV{'TMPDIR'}="/tmp";
137}
138$ENV{'PBTMP'} = tempdir( "pb.XXXXXXXXXX", DIR => $ENV{'TMPDIR'}, CLEANUP => 1 );
139
140#
[69]141# Removes all directory existing below the delivery dir
142# as they are temp dir only
[68]143# Files stay and have to be cleaned up manually
[8]144#
[25]145if (-d $ENV{'PBDESTDIR'}) {
146 opendir(DIR,$ENV{'PBDESTDIR'}) || die "Unable to open directory $ENV{'PBDESTDIR'}: $!";
147 foreach my $d (readdir(DIR)) {
148 next if ($d =~ /^\./);
[68]149 next if (-f "$ENV{'PBDESTDIR'}/$d");
[74]150 pb_rm_rf("$ENV{'PBDESTDIR'}/$d") if (-d "$ENV{'PBDESTDIR'}/$d");
[25]151 }
152 closedir(DIR);
153}
154if (! -d "$ENV{'PBDESTDIR'}") {
[74]155 pb_mkdir_p($ENV{'PBDESTDIR'}) || die "Unable to recursively create $ENV{'PBDESTDIR'}";
[25]156}
[5]157
[25]158#
159# Set build directory
160#
[71]161$ENV{'PBBUILDDIR'}=$topdir."/build";
[101]162if (! -d "$ENV{'PBBUILDDIR'}") {
163 pb_mkdir_p($ENV{'PBBUILDDIR'}) || die "Unable to recursively create $ENV{'PBBUILDDIR'}";
164}
[25]165
[8]166umask 0022;
[107]167return($proj,$debug,$LOG,\%pbrc, \%filteredfiles, \%defpkgdir, \%extpkgdir);
[2]168}
[9]169
[74]170# Internal mkdir -p function
171sub pb_mkdir_p {
[29]172my @dir = @_;
173my $ret = mkpath(@dir, 0, 0755);
174return($ret);
[9]175}
176
[74]177# Internal rm -rf function
178sub pb_rm_rf {
[29]179my @dir = @_;
180my $ret = rmtree(@dir, 0, 0);
181return($ret);
[9]182}
183
[74]184# Internal system function
185sub pb_system {
[29]186
187my $cmd=shift;
[30]188my $cmt=shift || $cmd;
[29]189
[108]190print "$cmt... ";
[106]191system("$cmd 2>&1 > $ENV{'PBTMP'}/system.log");
[29]192if ($? == -1) {
[108]193 print "failed to execute ($cmd) : $!\n";
[106]194 pb_display_file("$ENV{'PBTMP'}/system.log");
[29]195} elsif ($? & 127) {
[108]196 printf "child ($cmd) died with signal %d, %s coredump\n", ($? & 127), ($? & 128) ? 'with' : 'without';
[106]197 pb_display_file("$ENV{'PBTMP'}/system.log");
198} elsif ($? == 0) {
[108]199 print "OK\n";
[29]200} else {
[106]201 printf "child ($cmd) exited with value %d\n", $? >> 8;
202 pb_display_file("$ENV{'PBTMP'}/system.log");
[29]203}
[30]204}
[74]205
[106]206sub pb_display_file {
207
208my $file=shift;
209
210open(FILE,"$file") || die "Unable to open $file";
211while (<FILE>) {
[108]212 print $_;
[106]213}
214close(FILE);
215}
216
[74]217# Function which returns a pointer on a hash
[88]218# corresponding to a declaration (arg2) in the main conf file
219# and test the returned vaue as they need to exist in that case
220sub pb_conf_get {
221
222my @param = @_;
223
[89]224my @ptr = pb_conf_read("$ENV{'PBETC'}", @param);
225
[88]226foreach my $i (0..$#param) {
227 die "No $param[$i] defined for $ENV{'PBPROJ'}" if (not defined $ptr[$i]);
228 my $p = $ptr[$i];
229 $p->{$ENV{'PBPROJ'}} = $p->{'default'} if (not defined $p->{$ENV{'PBPROJ'}});
230 die "No $param[$i] defined for $ENV{'PBPROJ'}" if (not defined $p->{$ENV{'PBPROJ'}});
231}
[108]232#print "DEBUG: param: ".Dumper(@ptr)."\n" if ($debug >= 1);
[89]233return(@ptr);
[88]234}
235
236# Function which returns a pointer on a hash
[74]237# corresponding to a declaration (arg2) in a conf file (arg1)
238sub pb_conf_read {
239
240my $conffile = shift;
241my @param = @_;
242my $trace;
243my @ptr;
244
[108]245my $debug = 0;
246
[74]247if ($debug > 0) {
248 $trace = 1;
249} else {
250 $trace = 0;
251}
252
253
254my $config = AppConfig->new({
255 # Auto Create variables mentioned in Conf file
256 CREATE => 1,
257 DEBUG => $trace,
258 GLOBAL => {
259 # Each conf item is a hash
260 ARGCOUNT => ARGCOUNT_HASH,
261 },
262 });
263$config->file($conffile);
264for my $param (@param) {
265 push @ptr,$config->get($param);
266}
[89]267print "DEBUG: params: ".Dumper(@param)." ".Dumper(@ptr)."\n" if ($debug >= 1);
268return(@ptr);
[74]269}
270
271# Setup environment for CMS system
272sub pb_cms_init {
273
274my $proj = shift || undef;
275my $ret;
276
[89]277my ($cms) = pb_conf_get("cms");
[88]278# This one is optional
[112]279my ($cvsroot,$cvsrsh) = pb_conf_read($ENV{'PBETC'},"cvsroot","cvsrsh");
[74]280
[88]281if ($cms->{$proj} eq "svn") {
[74]282 $ENV{'PBREVISION'}=`(cd "$ENV{'PBROOT'}" ; svnversion .)`;
283 chomp($ENV{'PBREVISION'});
284 $ENV{'PBCMSLOG'}="svn log";
285 $ENV{'PBCMSLOGFILE'}="svn.log";
[88]286} elsif ($cms->{$proj} eq "cvs") {
[106]287 # Way too slow
288 #$ENV{'PBREVISION'}=`(cd "$ENV{'PBROOT'}" ; cvs rannotate -f . 2>&1 | awk '{print \$1}' | grep -E '^[0-9]' | cut -d. -f2 |sort -nu | tail -1)`;
289 #chomp($ENV{'PBREVISION'});
290 $ENV{'PBREVISION'}="CVS";
[74]291 $ENV{'PBCMSLOG'}="cvs log";
292 $ENV{'PBCMSLOGFILE'}="cvs.log";
[87]293 #
294 # Export content if needed
295 #
[88]296 $ENV{'CVSROOT'} = $cvsroot->{$proj} if (defined $cvsroot->{$proj});
[112]297 $ENV{'CVSRSH'} = $cvsrsh->{$proj} if (defined $cvsrsh->{$proj});
[74]298} else {
[88]299 die "cms $cms->{$proj} unknown";
[74]300}
[106]301return($cms);
[74]302}
303
[106]304sub pb_cms_export {
305my $cms = shift;
306my $pbdate = shift || undef;
[108]307my $source = shift;
[106]308my $destdir = shift;
[108]309my $tmp;
[115]310my $tmp1;
[106]311
312if ($cms->{$ENV{'PBPROJ'}} eq "svn") {
[108]313 if (-d $source) {
314 $tmp = $destdir;
315 } else {
316 $tmp = $destdir."/".basename($source);
317 }
[116]318 pb_system("svn export $source $tmp","Exporting $source from SVN to $tmp");
[106]319} elsif ($cms->{$ENV{'PBPROJ'}} eq "cvs") {
320 my $dir=dirname($destdir);
321 my $base=basename($destdir);
[115]322 if (-d $source) {
323 $tmp1 = $source;
324 $tmp1 =~ s|$ENV{'PBROOT'}/||;
325 } else {
326 $tmp1 = dirname($source);
327 $tmp1 =~ s|$ENV{'PBROOT'}/||;
328 $tmp1 = $tmp1."/".basename($source);
329 }
[106]330 # CVS needs a relative path !
[115]331 pb_system("cd $dir ; cvs export -D \"$pbdate\" -d $base $tmp1","Exporting $source from CVS to $destdir");
[106]332} else {
333 die "cms $cms->{$ENV{'PBPROJ'}} unknown";
334}
335}
336
337sub pb_cms_log {
338my $cms = shift;
339my $pkgdir = shift;
340my $destfile = shift;
341
342if ($cms->{$ENV{'PBPROJ'}} eq "svn") {
[112]343 pb_system("svn log -v $pkgdir > $destfile","Extracting log info from SVN");
[106]344} elsif ($cms->{$ENV{'PBPROJ'}} eq "cvs") {
345 my $tmp=basename($pkgdir);
346 # CVS needs a relative path !
347 pb_system("cvs log $tmp > $destfile","Extracting log info from CVS");
348} else {
349 die "cms $cms->{$ENV{'PBPROJ'}} unknown";
350}
351}
352
353
354
[77]355# Get all filters to apply
356# They're cumulative from less specific to most specific
357# suffix is .pbf
358
359sub pb_get_filters {
360
[108]361# For the moment not dynamic
362my $debug = 0; # Debug level
363my $LOG = *STDOUT; # Where to log
364
[77]365my @ffiles;
366my ($ffile0, $ffile1, $ffile2, $ffile3);
367my $pbpkg = shift || die "No package specified";
368my $dtype = shift || die "No dtype specified";
369my $dfam = shift || die "No dfam specified";
370my $ddir = shift || die "No ddir specified";
371my $dver = shift || die "No dver specified";
372my $ptr; # returned value pointer on the hash of filters
[79]373my %ptr;
[77]374
375if (-d "$ENV{'PBCONF'}/$pbpkg/pbfilter") {
376 $ffile0 = "$ENV{'PBCONF'}/$pbpkg/pbfilter/$dtype.pbf" if (-f "$ENV{'PBCONF'}/$pbpkg/pbfilter/$dtype.pbf");
377 $ffile1 = "$ENV{'PBCONF'}/$pbpkg/pbfilter/$dfam.pbf" if (-f "$ENV{'PBCONF'}/$pbpkg/pbfilter/$dfam.pbf");
378 $ffile2 = "$ENV{'PBCONF'}/$pbpkg/pbfilter/$ddir.pbf" if (-f "$ENV{'PBCONF'}/$pbpkg/pbfilter/$ddir.pbf");
379 $ffile3 = "$ENV{'PBCONF'}/$pbpkg/pbfilter/$ddir-$dver.pbf" if (-f "$ENV{'PBCONF'}/$pbpkg/pbfilter/$ddir-$dver.pbf");
380
381 push @ffiles,$ffile0 if (defined $ffile0);
382 push @ffiles,$ffile1 if (defined $ffile1);
383 push @ffiles,$ffile2 if (defined $ffile2);
384 push @ffiles,$ffile3 if (defined $ffile3);
385}
386if (@ffiles) {
387 print $LOG "DEBUG ffiles: ".Dumper(\@ffiles)."\n" if ($debug >= 1);
[79]388
389 my $config = AppConfig->new({
390 # Auto Create variables mentioned in Conf file
391 CREATE => 1,
392 DEBUG => 0,
393 GLOBAL => {
394 # Each conf item is a hash
395 ARGCOUNT => AppConfig::ARGCOUNT_HASH
396 }
397 });
398
[77]399 $config->file(@ffiles);
400 $ptr = $config->get("filter");
401 print $LOG "DEBUG f:".Dumper($ptr)."\n" if ($debug >= 1);
402} else {
403 $ptr = { };
404}
[79]405%ptr = %$ptr;
406return(\%ptr);
[77]407}
408
409# Function which applies filter on files (only for pb)
410sub pb_filter_file_pb {
411
412my $f=shift;
413my $ptr=shift;
414my %filter=%$ptr;
415my $destfile=shift;
416my $dtype=shift;
[99]417my $pbsuf=shift;
[80]418my $pbpkg=shift;
419my $pbver=shift;
420my $pbtag=shift;
421my $pbrev=shift;
422my $pbdate=shift;
[108]423my $defpkgdir = shift;
424my $extpkgdir = shift;
[77]425
[108]426# For the moment not dynamic
427my $debug = 0; # Debug level
428my $LOG = *STDOUT; # Where to log
429
[77]430print $LOG "DEBUG: From $f to $destfile\n" if ($debug >= 1);
431pb_mkdir_p(dirname($destfile)) if (! -d dirname($destfile));
432open(DEST,"> $destfile") || die "Unable to create $destfile";
433open(FILE,"$f") || die "Unable to open $f: $!";
434while (<FILE>) {
435 my $line = $_;
436 foreach my $s (keys %filter) {
437 # Process single variables
[79]438 print $LOG "DEBUG filter{$s}: $filter{$s}\n" if ($debug >= 1);
[77]439 my $tmp = $filter{$s};
440 next if (not defined $tmp);
441 # Expand variables if any single one found
[79]442 print $LOG "DEBUG tmp: $tmp\n" if ($debug >= 1);
[77]443 if ($tmp =~ /\$/) {
444 eval { $tmp =~ s/(\$\w+)/$1/eeg };
445 # special case for ChangeLog only for pb
446 } elsif (($tmp =~ /^yes$/) && ($s =~ /^PBLOG$/) && ($line =~ /^PBLOG$/)) {
447 $tmp = "";
[108]448 my $p = $defpkgdir->{$pbpkg};
449 $p = $extpkgdir->{$pbpkg} if (not defined $p);
[99]450 pb_changelog($dtype, $pbpkg, $pbtag, $pbsuf, $p, \*DEST);
[77]451 }
452 $line =~ s|$s|$tmp|;
453 }
454 print DEST $line;
455}
456close(FILE);
457close(DEST);
458}
459
460# Function which applies filter on files (external call)
461sub pb_filter_file {
462
463my $f=shift;
464my $ptr=shift;
465my %filter=%$ptr;
466my $destfile=shift;
[99]467my $pbsuf=shift;
[80]468my $pbpkg=shift;
469my $pbver=shift;
470my $pbtag=shift;
471my $pbrev=shift;
472my $pbdate=shift;
[77]473
[108]474# For the moment not dynamic
475my $debug = 0; # Debug level
476my $LOG = *STDOUT; # Where to log
477
[77]478print $LOG "DEBUG: From $f to $destfile\n" if ($debug >= 1);
479pb_mkdir_p(dirname($destfile)) if (! -d dirname($destfile));
480open(DEST,"> $destfile") || die "Unable to create $destfile";
481open(FILE,"$f") || die "Unable to open $f: $!";
482while (<FILE>) {
483 my $line = $_;
484 foreach my $s (keys %filter) {
485 # Process single variables
486 print $LOG "DEBUG filter{$s}: $filter{$s}\n" if ($debug > 1);
487 my $tmp = $filter{$s};
488 next if (not defined $tmp);
489 # Expand variables if any single one found
490 if ($tmp =~ /\$/) {
491 eval { $tmp =~ s/(\$\w+)/$1/eeg };
492 }
493 $line =~ s|$s|$tmp|;
494 }
495 print DEST $line;
496}
497close(FILE);
498close(DEST);
499}
500
501
[2]5021;
Note: See TracBrowser for help on using the repository browser.