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

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

Should not clean PBBUILDDIR at first !

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