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

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

Preparing work on VM

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