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

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

Lots of various fixes for CVS support with LinuxCOE

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