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

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

Prepare for new actions on delivery on SSH

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