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

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

Better interface for pb_get functions

  • Property svn:executable set to *
File size: 10.7 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)
[89]84 my ($defpkgdir, $extpkgdir, $version, $filteredfiles) = pb_conf_read("$ENV{'PBCONF'}/$proj.pb","defpkgdir","extpkgdir","version","filteredfiles");
[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);
[38]100} else {
[49]101 die "Unable to open $ENV{'PBCONF'}/$proj.pb";
[38]102}
103
[8]104#
[5]105# Set temp directory
[8]106#
[7]107if (not defined $ENV{'TMPDIR'}) {
[2]108 $ENV{'TMPDIR'}="/tmp";
109}
[5]110$ENV{'PBTMP'} = tempdir( "pb.XXXXXXXXXX", DIR => $ENV{'TMPDIR'}, CLEANUP => 1 );
[2]111
[8]112#
[5]113# Get global VERSION
[8]114#
[11]115open(VER, "$ENV{'PBCONF'}/VERSION") || die "Unable to open $ENV{'PBCONF'}/VERSION: $?";
[5]116$ver = <VER>;
117chomp($ver);
[8]118#print Dumper(%version);
119die "Invalid version name $ver in $ENV{'PBROOT'}/VERSION" if ($ver !~ /[0-9.]+/) && (not exists $version{$ver});
[5]120$ENV{'PBVER'}=$ver;
121close(VER);
122
[8]123#
[74]124# Get global TAG
[8]125#
[11]126open(TAG, "$ENV{'PBCONF'}/TAG") || die "Unable to open $ENV{'PBCONF'}/TAG: $?";
[5]127$tag = <TAG>;
128chomp($tag);
[8]129die "Invalid tag name $tag in $ENV{'PBROOT'}/TAG" if ($tag !~ /[0-9]+/);
[5]130$ENV{'PBTAG'}=$tag;
131close(TAG);
132
[8]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";
[74]155pb_rm_rf($ENV{'PBBUILDDIR'}) if (-d "$ENV{'PBBUILDDIR'}");
156pb_mkdir_p($ENV{'PBBUILDDIR'}) || die "Unable to recursively create $ENV{'PBBUILDDIR'}";
[25]157
[8]158umask 0022;
159return($proj);
[2]160}
[9]161
[74]162# Internal mkdir -p function
163sub pb_mkdir_p {
[29]164my @dir = @_;
165my $ret = mkpath(@dir, 0, 0755);
166return($ret);
[9]167}
168
[74]169# Internal rm -rf function
170sub pb_rm_rf {
[29]171my @dir = @_;
172my $ret = rmtree(@dir, 0, 0);
173return($ret);
[9]174}
175
[74]176# Internal system function
177sub pb_system {
[29]178
179my $cmd=shift;
[30]180my $cmt=shift || $cmd;
[29]181
[30]182print $LOG "$cmt... ";
[29]183system("$cmd");
184if ($? == -1) {
185 print $LOG "failed to execute: $!\n" if ($debug >= 0);
186} elsif ($? & 127) {
187 printf $LOG "child died with signal %d, %s coredump\n", ($? & 127), ($? & 128) ? 'with' : 'without' if ($debug >= 0);
188} else {
[30]189 print $LOG "OK\n" if ($debug >= 0);
[29]190}
[30]191}
[74]192
193# Function which returns a pointer on a hash
[88]194# corresponding to a declaration (arg2) in the main conf file
195# and test the returned vaue as they need to exist in that case
196sub pb_conf_get {
197
198my @param = @_;
199
[89]200my @ptr = pb_conf_read("$ENV{'PBETC'}", @param);
201
[88]202foreach my $i (0..$#param) {
203 die "No $param[$i] defined for $ENV{'PBPROJ'}" if (not defined $ptr[$i]);
204 my $p = $ptr[$i];
205 $p->{$ENV{'PBPROJ'}} = $p->{'default'} if (not defined $p->{$ENV{'PBPROJ'}});
206 die "No $param[$i] defined for $ENV{'PBPROJ'}" if (not defined $p->{$ENV{'PBPROJ'}});
207}
[89]208print "DEBUG: param: ".Dumper($ptr)."\n" if ($debug >= 1);
209return(@ptr);
[88]210}
211
212# Function which returns a pointer on a hash
[74]213# corresponding to a declaration (arg2) in a conf file (arg1)
214sub pb_conf_read {
215
216my $conffile = shift;
217my @param = @_;
218my $trace;
219my @ptr;
220
221if ($debug > 0) {
222 $trace = 1;
223} else {
224 $trace = 0;
225}
226
227
228my $config = AppConfig->new({
229 # Auto Create variables mentioned in Conf file
230 CREATE => 1,
231 DEBUG => $trace,
232 GLOBAL => {
233 # Each conf item is a hash
234 ARGCOUNT => ARGCOUNT_HASH,
235 },
236 });
237$config->file($conffile);
238for my $param (@param) {
239 push @ptr,$config->get($param);
240}
[89]241print "DEBUG: params: ".Dumper(@param)." ".Dumper(@ptr)."\n" if ($debug >= 1);
242return(@ptr);
[74]243}
244
245# Setup environment for CMS system
246sub pb_cms_init {
247
248my $proj = shift || undef;
249my $ret;
250
[89]251my ($cms) = pb_conf_get("cms");
[88]252# This one is optional
[89]253my ($cvsroot) = pb_conf_read($ENV{'PBETC'},"cvsroot");
[74]254
[88]255if ($cms->{$proj} eq "svn") {
[74]256 $ENV{'PBREVISION'}=`(cd "$ENV{'PBROOT'}" ; svnversion .)`;
257 chomp($ENV{'PBREVISION'});
258 $ENV{'PBCMSLOG'}="svn log";
259 $ENV{'PBCMSLOGFILE'}="svn.log";
260 $ENV{'PBCMSEXP'}="svn export";
[88]261} elsif ($cms->{$proj} eq "cvs") {
[74]262 $ENV{'PBREVISION'}=`(cd "$ENV{'PBROOT'}" ; cvs rannotate -f . 2>&1 | awk '{print \$1}' | grep -E '^[0-9]' | cut -d. -f2 |sort -nu | tail -1)`;
263 chomp($ENV{'PBREVISION'});
264 $ENV{'PBCMSLOG'}="cvs log";
265 $ENV{'PBCMSLOGFILE'}="cvs.log";
[88]266 $ENV{'PBCMSEXP'}="cvs export";
[87]267 #
268 # Export content if needed
269 #
[88]270 $ENV{'CVSROOT'} = $cvsroot->{$proj} if (defined $cvsroot->{$proj});
[74]271} else {
[88]272 die "cms $cms->{$proj} unknown";
[74]273}
274}
275
[77]276# Get all filters to apply
277# They're cumulative from less specific to most specific
278# suffix is .pbf
279
280sub pb_get_filters {
281
282my @ffiles;
283my ($ffile0, $ffile1, $ffile2, $ffile3);
284my $pbpkg = shift || die "No package specified";
285my $dtype = shift || die "No dtype specified";
286my $dfam = shift || die "No dfam specified";
287my $ddir = shift || die "No ddir specified";
288my $dver = shift || die "No dver specified";
289my $ptr; # returned value pointer on the hash of filters
[79]290my %ptr;
[77]291
292if (-d "$ENV{'PBCONF'}/$pbpkg/pbfilter") {
293 $ffile0 = "$ENV{'PBCONF'}/$pbpkg/pbfilter/$dtype.pbf" if (-f "$ENV{'PBCONF'}/$pbpkg/pbfilter/$dtype.pbf");
294 $ffile1 = "$ENV{'PBCONF'}/$pbpkg/pbfilter/$dfam.pbf" if (-f "$ENV{'PBCONF'}/$pbpkg/pbfilter/$dfam.pbf");
295 $ffile2 = "$ENV{'PBCONF'}/$pbpkg/pbfilter/$ddir.pbf" if (-f "$ENV{'PBCONF'}/$pbpkg/pbfilter/$ddir.pbf");
296 $ffile3 = "$ENV{'PBCONF'}/$pbpkg/pbfilter/$ddir-$dver.pbf" if (-f "$ENV{'PBCONF'}/$pbpkg/pbfilter/$ddir-$dver.pbf");
297
298 push @ffiles,$ffile0 if (defined $ffile0);
299 push @ffiles,$ffile1 if (defined $ffile1);
300 push @ffiles,$ffile2 if (defined $ffile2);
301 push @ffiles,$ffile3 if (defined $ffile3);
302}
303if (@ffiles) {
304 print $LOG "DEBUG ffiles: ".Dumper(\@ffiles)."\n" if ($debug >= 1);
[79]305
306 my $config = AppConfig->new({
307 # Auto Create variables mentioned in Conf file
308 CREATE => 1,
309 DEBUG => 0,
310 GLOBAL => {
311 # Each conf item is a hash
312 ARGCOUNT => AppConfig::ARGCOUNT_HASH
313 }
314 });
315
[77]316 $config->file(@ffiles);
317 $ptr = $config->get("filter");
318 print $LOG "DEBUG f:".Dumper($ptr)."\n" if ($debug >= 1);
319} else {
320 $ptr = { };
321}
[79]322%ptr = %$ptr;
323return(\%ptr);
[77]324}
325
326# Function which applies filter on files (only for pb)
327sub pb_filter_file_pb {
328
329my $f=shift;
330my $ptr=shift;
331my %filter=%$ptr;
332my $destfile=shift;
333my $dtype=shift;
334my $dsuf=shift;
[80]335my $pbpkg=shift;
336my $pbver=shift;
337my $pbtag=shift;
338my $pbrev=shift;
339my $pbdate=shift;
[77]340
341print $LOG "DEBUG: From $f to $destfile\n" if ($debug >= 1);
342pb_mkdir_p(dirname($destfile)) if (! -d dirname($destfile));
343open(DEST,"> $destfile") || die "Unable to create $destfile";
344open(FILE,"$f") || die "Unable to open $f: $!";
345while (<FILE>) {
346 my $line = $_;
347 foreach my $s (keys %filter) {
348 # Process single variables
[79]349 print $LOG "DEBUG filter{$s}: $filter{$s}\n" if ($debug >= 1);
[77]350 my $tmp = $filter{$s};
351 next if (not defined $tmp);
352 # Expand variables if any single one found
[79]353 print $LOG "DEBUG tmp: $tmp\n" if ($debug >= 1);
[77]354 if ($tmp =~ /\$/) {
355 eval { $tmp =~ s/(\$\w+)/$1/eeg };
356 # special case for ChangeLog only for pb
357 } elsif (($tmp =~ /^yes$/) && ($s =~ /^PBLOG$/) && ($line =~ /^PBLOG$/)) {
358 $tmp = "";
[82]359 my $p = $defpkgdir{$pbpkg};
360 $p = $extpkgdir{$pbpkg} if (not defined $p);
361 pb_changelog($dtype, $pbpkg, $pbtag, $dsuf, $p, \*DEST);
[77]362 }
363 $line =~ s|$s|$tmp|;
364 }
365 print DEST $line;
366}
367close(FILE);
368close(DEST);
369}
370
371# Function which applies filter on files (external call)
372sub pb_filter_file {
373
374my $f=shift;
375my $ptr=shift;
376my %filter=%$ptr;
377my $destfile=shift;
[80]378my $pbpkg=shift;
379my $pbver=shift;
380my $pbtag=shift;
381my $pbrev=shift;
382my $pbdate=shift;
[77]383
384print $LOG "DEBUG: From $f to $destfile\n" if ($debug >= 1);
385pb_mkdir_p(dirname($destfile)) if (! -d dirname($destfile));
386open(DEST,"> $destfile") || die "Unable to create $destfile";
387open(FILE,"$f") || die "Unable to open $f: $!";
388while (<FILE>) {
389 my $line = $_;
390 foreach my $s (keys %filter) {
391 # Process single variables
392 print $LOG "DEBUG filter{$s}: $filter{$s}\n" if ($debug > 1);
393 my $tmp = $filter{$s};
394 next if (not defined $tmp);
395 # Expand variables if any single one found
396 if ($tmp =~ /\$/) {
397 eval { $tmp =~ s/(\$\w+)/$1/eeg };
398 }
399 $line =~ s|$s|$tmp|;
400 }
401 print DEST $line;
402}
403close(FILE);
404close(DEST);
405}
406
407
[2]4081;
Note: See TracBrowser for help on using the repository browser.