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

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

transfer pb_fiter_file to Base and duplicate :-( for external call
Create sub functions per action

  • 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#
[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 = ();
78my %confparam = ();
79
[49]80if (-f "$ENV{'PBCONF'}/$proj.pb") {
[74]81 # main parameter confparam (mandatory)
82 # List of pkg to build by default (mandatory)
83 # List of additional pkg to build when all is called (optional)
84 # Valid version names (optional)
85 # List of files to filter (optional)
86 my $ptr = pb_conf_read("$ENV{'PBCONF'}/$proj.pb","confparam","defpkgdir","extpkgdir","version","filteredfiles");
87 my ($confparam, $defpkgdir, $extpkgdir, $version, $filteredfiles) = @$ptr;
88 print "DEBUG: confparam: ".Dumper($confparam)."\n" if ($debug >= 1);
89 print "DEBUG: defpkgdir: ".Dumper($defpkgdir)."\n" if ($debug >= 1);
90 print "DEBUG: extpkgdir: ".Dumper($extpkgdir)."\n" if ($debug >= 1);
91 print "DEBUG: version: ".Dumper($version)."\n" if ($debug >= 1);
92 print "DEBUG: filteredfiles: ".Dumper($filteredfiles)."\n" if ($debug >= 1);
93 die "Unable to find confparam in $ENV{'PBCONF'}/$proj.pb" if (not defined $confparam);
94 die "Unable to find defpkgdir in $ENV{'PBCONF'}/$proj.pb" if (not defined $defpkgdir);
95 %confparam = %$confparam;
96 # Global
97 %defpkgdir = %$defpkgdir;
98 # Global
99 %extpkgdir = ();
100 %extpkgdir = %$defpkgdir if (defined $defpkgdir);
101 %version = ();
102 %version = %$version if (defined $version);
103 # Global
104 %filteredfiles = ();
105 %filteredfiles = %$filteredfiles if (defined $filteredfiles);
[38]106} else {
[49]107 die "Unable to open $ENV{'PBCONF'}/$proj.pb";
[38]108}
109
[8]110#
[74]111# Export content if needed
[8]112#
[18]113if (defined $confparam{"cvsroot"}) {
114 $ENV{'CVSROOT'} = $confparam{"cvsroot"};
[17]115}
116
[8]117#
[5]118# Set temp directory
[8]119#
[7]120if (not defined $ENV{'TMPDIR'}) {
[2]121 $ENV{'TMPDIR'}="/tmp";
122}
[5]123$ENV{'PBTMP'} = tempdir( "pb.XXXXXXXXXX", DIR => $ENV{'TMPDIR'}, CLEANUP => 1 );
[2]124
[8]125#
[5]126# Get global VERSION
[8]127#
[11]128open(VER, "$ENV{'PBCONF'}/VERSION") || die "Unable to open $ENV{'PBCONF'}/VERSION: $?";
[5]129$ver = <VER>;
130chomp($ver);
[8]131#print Dumper(%version);
132die "Invalid version name $ver in $ENV{'PBROOT'}/VERSION" if ($ver !~ /[0-9.]+/) && (not exists $version{$ver});
[5]133$ENV{'PBVER'}=$ver;
134close(VER);
135
[8]136#
[74]137# Get global TAG
[8]138#
[11]139open(TAG, "$ENV{'PBCONF'}/TAG") || die "Unable to open $ENV{'PBCONF'}/TAG: $?";
[5]140$tag = <TAG>;
141chomp($tag);
[8]142die "Invalid tag name $tag in $ENV{'PBROOT'}/TAG" if ($tag !~ /[0-9]+/);
[5]143$ENV{'PBTAG'}=$tag;
144close(TAG);
145
[8]146#
[69]147# Removes all directory existing below the delivery dir
148# as they are temp dir only
[68]149# Files stay and have to be cleaned up manually
[8]150#
[25]151if (-d $ENV{'PBDESTDIR'}) {
152 opendir(DIR,$ENV{'PBDESTDIR'}) || die "Unable to open directory $ENV{'PBDESTDIR'}: $!";
153 foreach my $d (readdir(DIR)) {
154 next if ($d =~ /^\./);
[68]155 next if (-f "$ENV{'PBDESTDIR'}/$d");
[74]156 pb_rm_rf("$ENV{'PBDESTDIR'}/$d") if (-d "$ENV{'PBDESTDIR'}/$d");
[25]157 }
158 closedir(DIR);
159}
160if (! -d "$ENV{'PBDESTDIR'}") {
[74]161 pb_mkdir_p($ENV{'PBDESTDIR'}) || die "Unable to recursively create $ENV{'PBDESTDIR'}";
[25]162}
[5]163
[25]164#
165# Set build directory
166#
[71]167$ENV{'PBBUILDDIR'}=$topdir."/build";
[74]168pb_rm_rf($ENV{'PBBUILDDIR'}) if (-d "$ENV{'PBBUILDDIR'}");
169pb_mkdir_p($ENV{'PBBUILDDIR'}) || die "Unable to recursively create $ENV{'PBBUILDDIR'}";
[25]170
[8]171umask 0022;
172return($proj);
[2]173}
[9]174
[74]175# Internal mkdir -p function
176sub pb_mkdir_p {
[29]177my @dir = @_;
178my $ret = mkpath(@dir, 0, 0755);
179return($ret);
[9]180}
181
[74]182# Internal rm -rf function
183sub pb_rm_rf {
[29]184my @dir = @_;
185my $ret = rmtree(@dir, 0, 0);
186return($ret);
[9]187}
188
[74]189# Internal system function
190sub pb_system {
[29]191
192my $cmd=shift;
[30]193my $cmt=shift || $cmd;
[29]194
[30]195print $LOG "$cmt... ";
[29]196system("$cmd");
197if ($? == -1) {
198 print $LOG "failed to execute: $!\n" if ($debug >= 0);
199} elsif ($? & 127) {
200 printf $LOG "child died with signal %d, %s coredump\n", ($? & 127), ($? & 128) ? 'with' : 'without' if ($debug >= 0);
201} else {
[30]202 print $LOG "OK\n" if ($debug >= 0);
[29]203}
[30]204}
[74]205
206# Function which returns a pointer on a hash
207# corresponding to a declaration (arg2) in a conf file (arg1)
208sub pb_conf_read {
209
210my $conffile = shift;
211my @param = @_;
212my $trace;
213my @ptr;
214
215if ($debug > 0) {
216 $trace = 1;
217} else {
218 $trace = 0;
219}
220
221
222my $config = AppConfig->new({
223 # Auto Create variables mentioned in Conf file
224 CREATE => 1,
225 DEBUG => $trace,
226 GLOBAL => {
227 # Each conf item is a hash
228 ARGCOUNT => ARGCOUNT_HASH,
229 },
230 });
231$config->file($conffile);
232for my $param (@param) {
233 push @ptr,$config->get($param);
234}
235if ($#param == 0) {
236 print "DEBUG: param: ".Dumper($ptr[0])."\n" if ($debug >= 1);
237 return($ptr[0]);
238} else {
239 my $ptr = \@ptr;
240 print "DEBUG: params: ".Dumper($ptr)."\n" if ($debug >= 1);
241 return($ptr);
242}
243}
244
245sub pb_conf_init {
246
247my $conffile = shift;
248my $ptr;
249my $trace;
250
251if ($debug > 0) {
252 $trace = 1;
253} else {
254 $trace = 0;
255}
256
257my $config = AppConfig->new({
258 # Auto Create variables mentioned in Conf file
259 DEBUG => $trace,
260 CREATE => '1',
261 GLOBAL => {
262 # Each conf item is a hash
263 ARGCOUNT => ARGCOUNT_HASH,
264 },
265 });
266$config->file($conffile);
267
268# Root of the project to build
269# needs at least 2 levels of dir as in the upper
270# other dirs will be created and used
271
272}
273
274# Setup environment for CMS system
275sub pb_cms_init {
276
277my $proj = shift || undef;
278my $ret;
279
280my $cms = pb_conf_read("$ENV{'PBETC'}","cms");
281die "No CMS defined for $proj" if (not defined $cms);
282my %cms = %$cms;
283die "No CMS defined for $proj" if (not defined $cms{$proj});
284
285if ($cms{$proj} eq "svn") {
286 $ENV{'PBREVISION'}=`(cd "$ENV{'PBROOT'}" ; svnversion .)`;
287 chomp($ENV{'PBREVISION'});
288 $ENV{'PBCMSLOG'}="svn log";
289 $ENV{'PBCMSLOGFILE'}="svn.log";
290 $ENV{'PBCMSEXP'}="svn export";
291} elsif ($cms{$proj} eq "cvs") {
292 $ENV{'PBREVISION'}=`(cd "$ENV{'PBROOT'}" ; cvs rannotate -f . 2>&1 | awk '{print \$1}' | grep -E '^[0-9]' | cut -d. -f2 |sort -nu | tail -1)`;
293 chomp($ENV{'PBREVISION'});
294 $ENV{'PBCMSLOG'}="cvs log";
295 $ENV{'PBCMSLOGFILE'}="cvs.log";
296 $ENV{'PBCMSEXP'}="cvs export"
297} else {
298 die "CMS $cms{$proj} unknown";
299}
300}
301
[77]302# Get all filters to apply
303# They're cumulative from less specific to most specific
304# suffix is .pbf
305
306sub pb_get_filters {
307
308my @ffiles;
309my ($ffile0, $ffile1, $ffile2, $ffile3);
310my $pbpkg = shift || die "No package specified";
311my $dtype = shift || die "No dtype specified";
312my $dfam = shift || die "No dfam specified";
313my $ddir = shift || die "No ddir specified";
314my $dver = shift || die "No dver specified";
315my $ptr; # returned value pointer on the hash of filters
316
317if (-d "$ENV{'PBCONF'}/$pbpkg/pbfilter") {
318 $ffile0 = "$ENV{'PBCONF'}/$pbpkg/pbfilter/$dtype.pbf" if (-f "$ENV{'PBCONF'}/$pbpkg/pbfilter/$dtype.pbf");
319 $ffile1 = "$ENV{'PBCONF'}/$pbpkg/pbfilter/$dfam.pbf" if (-f "$ENV{'PBCONF'}/$pbpkg/pbfilter/$dfam.pbf");
320 $ffile2 = "$ENV{'PBCONF'}/$pbpkg/pbfilter/$ddir.pbf" if (-f "$ENV{'PBCONF'}/$pbpkg/pbfilter/$ddir.pbf");
321 $ffile3 = "$ENV{'PBCONF'}/$pbpkg/pbfilter/$ddir-$dver.pbf" if (-f "$ENV{'PBCONF'}/$pbpkg/pbfilter/$ddir-$dver.pbf");
322
323 push @ffiles,$ffile0 if (defined $ffile0);
324 push @ffiles,$ffile1 if (defined $ffile1);
325 push @ffiles,$ffile2 if (defined $ffile2);
326 push @ffiles,$ffile3 if (defined $ffile3);
327}
328my $config = AppConfig->new({
329 # Auto Create variables mentioned in Conf file
330 CREATE => 1,
331 DEBUG => 0,
332 GLOBAL => {
333 # Each conf item is a hash
334 ARGCOUNT => AppConfig::ARGCOUNT_HASH
335 }
336});
337
338if (@ffiles) {
339 print $LOG "DEBUG ffiles: ".Dumper(\@ffiles)."\n" if ($debug >= 1);
340 $config->file(@ffiles);
341 $ptr = $config->get("filter");
342 print $LOG "DEBUG f:".Dumper($ptr)."\n" if ($debug >= 1);
343} else {
344 $ptr = { };
345}
346return($ptr);
347}
348
349# Function which applies filter on files (only for pb)
350sub pb_filter_file_pb {
351
352my $f=shift;
353my $ptr=shift;
354my %filter=%$ptr;
355my $destfile=shift;
356my $pbpkg=shift;
357my $dtype=shift;
358my $dsuf=shift;
359
360print $LOG "DEBUG: From $f to $destfile\n" if ($debug >= 1);
361pb_mkdir_p(dirname($destfile)) if (! -d dirname($destfile));
362open(DEST,"> $destfile") || die "Unable to create $destfile";
363open(FILE,"$f") || die "Unable to open $f: $!";
364while (<FILE>) {
365 my $line = $_;
366 foreach my $s (keys %filter) {
367 # Process single variables
368 print $LOG "DEBUG filter{$s}: $filter{$s}\n" if ($debug > 1);
369 my $tmp = $filter{$s};
370 next if (not defined $tmp);
371 # Expand variables if any single one found
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 = "";
377 my $p = $defpkgdir{$pbpkg};
378 $p = $extpkgdir{$pbpkg} if (not defined $p);
379 pb_changelog($dtype, $pbpkg, $pbtag, $dsuf, $p, \*DEST);
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;
396
397print $LOG "DEBUG: From $f to $destfile\n" if ($debug >= 1);
398pb_mkdir_p(dirname($destfile)) if (! -d dirname($destfile));
399open(DEST,"> $destfile") || die "Unable to create $destfile";
400open(FILE,"$f") || die "Unable to open $f: $!";
401while (<FILE>) {
402 my $line = $_;
403 foreach my $s (keys %filter) {
404 # Process single variables
405 print $LOG "DEBUG filter{$s}: $filter{$s}\n" if ($debug > 1);
406 my $tmp = $filter{$s};
407 next if (not defined $tmp);
408 # Expand variables if any single one found
409 if ($tmp =~ /\$/) {
410 eval { $tmp =~ s/(\$\w+)/$1/eeg };
411 }
412 $line =~ s|$s|$tmp|;
413 }
414 print DEST $line;
415}
416close(FILE);
417close(DEST);
418}
419
420
[2]4211;
Note: See TracBrowser for help on using the repository browser.