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

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

Fix typo

  • Property svn:executable set to *
File size: 10.7 KB
Line 
1#!/usr/bin/perl -w
2#
3# Base subroutines for the Project-Builder project
4#
5# $Id$
6#
7
8use strict;
9use lib qw (lib);
10use File::Basename;
11use File::Path;
12use File::Temp qw /tempdir/;
13use AppConfig qw(ARGCOUNT_HASH);
14use Data::Dumper;
15
16$ENV{'PBETC'} = "$ENV{'HOME'}/.pbrc";
17
18sub pb_env_init {
19
20my $proj=shift;
21my $ver;
22my $tag;
23
24#
25# Check project name
26# Could be with env var PBPROJ
27# or option -p
28# if not define take the first in conf file
29#
30if ((defined $ENV{'PBPROJ'}) &&
31 (not (defined $proj))) {
32 $proj = $ENV{'PBPROJ'};
33}
34
35#
36# We get the pbrc file for that project
37# and use its content
38#
39my ($pbrc) = pb_conf_read("$ENV{'PBETC'}","pbrc");
40print "DEBUG pbrc: ".Dumper($pbrc)."\n" if ($debug >= 1);
41
42%pbrc = %$pbrc;
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#
53my $topdir=dirname($pbrc{$proj});
54chdir $topdir || die "Unable to change directory to $topdir";
55$ENV{'PBDESTDIR'}=$topdir."/delivery";
56
57#
58# Use project configuration file if needed
59#
60if (not defined $ENV{'PBROOT'}) {
61 if (-f $pbrc{$proj}) {
62 my ($pbroot) = pb_conf_read($pbrc{$proj},"pbroot");
63 my %pbroot = %$pbroot;
64 # All lines should point to the same pbroot so take the first
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 }
68 die "No pbroot defined - use env var PBROOT or -r pbroot " if (not defined $ENV{'PBROOT'});
69}
70
71#
72# Check pb conf compliance
73#
74$ENV{'PBCONF'} = "$ENV{'PBROOT'}/pbconf";
75die "Project $proj not Project-Builder compliant. Please populate $ENV{'PBCONF'}" if ( not -d "$ENV{'PBCONF'}");
76
77my %version = ();
78
79if (-f "$ENV{'PBCONF'}/$proj.pb") {
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)
84 my ($defpkgdir, $extpkgdir, $version, $filteredfiles) = pb_conf_read("$ENV{'PBCONF'}/$proj.pb","defpkgdir","extpkgdir","version","filteredfiles");
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);
100} else {
101 die "Unable to open $ENV{'PBCONF'}/$proj.pb";
102}
103
104#
105# Set temp directory
106#
107if (not defined $ENV{'TMPDIR'}) {
108 $ENV{'TMPDIR'}="/tmp";
109}
110$ENV{'PBTMP'} = tempdir( "pb.XXXXXXXXXX", DIR => $ENV{'TMPDIR'}, CLEANUP => 1 );
111
112#
113# Get global VERSION
114#
115open(VER, "$ENV{'PBCONF'}/VERSION") || die "Unable to open $ENV{'PBCONF'}/VERSION: $?";
116$ver = <VER>;
117chomp($ver);
118#print Dumper(%version);
119die "Invalid version name $ver in $ENV{'PBROOT'}/VERSION" if ($ver !~ /[0-9.]+/) && (not exists $version{$ver});
120$ENV{'PBVER'}=$ver;
121close(VER);
122
123#
124# Get global TAG
125#
126open(TAG, "$ENV{'PBCONF'}/TAG") || die "Unable to open $ENV{'PBCONF'}/TAG: $?";
127$tag = <TAG>;
128chomp($tag);
129die "Invalid tag name $tag in $ENV{'PBROOT'}/TAG" if ($tag !~ /[0-9]+/);
130$ENV{'PBTAG'}=$tag;
131close(TAG);
132
133#
134# Removes all directory existing below the delivery dir
135# as they are temp dir only
136# Files stay and have to be cleaned up manually
137#
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 =~ /^\./);
142 next if (-f "$ENV{'PBDESTDIR'}/$d");
143 pb_rm_rf("$ENV{'PBDESTDIR'}/$d") if (-d "$ENV{'PBDESTDIR'}/$d");
144 }
145 closedir(DIR);
146}
147if (! -d "$ENV{'PBDESTDIR'}") {
148 pb_mkdir_p($ENV{'PBDESTDIR'}) || die "Unable to recursively create $ENV{'PBDESTDIR'}";
149}
150
151#
152# Set build directory
153#
154$ENV{'PBBUILDDIR'}=$topdir."/build";
155pb_rm_rf($ENV{'PBBUILDDIR'}) if (-d "$ENV{'PBBUILDDIR'}");
156pb_mkdir_p($ENV{'PBBUILDDIR'}) || die "Unable to recursively create $ENV{'PBBUILDDIR'}";
157
158umask 0022;
159return($proj);
160}
161
162# Internal mkdir -p function
163sub pb_mkdir_p {
164my @dir = @_;
165my $ret = mkpath(@dir, 0, 0755);
166return($ret);
167}
168
169# Internal rm -rf function
170sub pb_rm_rf {
171my @dir = @_;
172my $ret = rmtree(@dir, 0, 0);
173return($ret);
174}
175
176# Internal system function
177sub pb_system {
178
179my $cmd=shift;
180my $cmt=shift || $cmd;
181
182print $LOG "$cmt... ";
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 {
189 print $LOG "OK\n" if ($debug >= 0);
190}
191}
192
193# Function which returns a pointer on a hash
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
200my @ptr = pb_conf_read("$ENV{'PBETC'}", @param);
201
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}
208print "DEBUG: param: ".Dumper(@ptr)."\n" if ($debug >= 1);
209return(@ptr);
210}
211
212# Function which returns a pointer on a hash
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}
241print "DEBUG: params: ".Dumper(@param)." ".Dumper(@ptr)."\n" if ($debug >= 1);
242return(@ptr);
243}
244
245# Setup environment for CMS system
246sub pb_cms_init {
247
248my $proj = shift || undef;
249my $ret;
250
251my ($cms) = pb_conf_get("cms");
252# This one is optional
253my ($cvsroot) = pb_conf_read($ENV{'PBETC'},"cvsroot");
254
255if ($cms->{$proj} eq "svn") {
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";
261} elsif ($cms->{$proj} eq "cvs") {
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";
266 $ENV{'PBCMSEXP'}="cvs export";
267 #
268 # Export content if needed
269 #
270 $ENV{'CVSROOT'} = $cvsroot->{$proj} if (defined $cvsroot->{$proj});
271} else {
272 die "cms $cms->{$proj} unknown";
273}
274}
275
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
290my %ptr;
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);
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
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}
322%ptr = %$ptr;
323return(\%ptr);
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;
335my $pbpkg=shift;
336my $pbver=shift;
337my $pbtag=shift;
338my $pbrev=shift;
339my $pbdate=shift;
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
349 print $LOG "DEBUG filter{$s}: $filter{$s}\n" if ($debug >= 1);
350 my $tmp = $filter{$s};
351 next if (not defined $tmp);
352 # Expand variables if any single one found
353 print $LOG "DEBUG tmp: $tmp\n" if ($debug >= 1);
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 = "";
359 my $p = $defpkgdir{$pbpkg};
360 $p = $extpkgdir{$pbpkg} if (not defined $p);
361 pb_changelog($dtype, $pbpkg, $pbtag, $dsuf, $p, \*DEST);
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;
378my $pbpkg=shift;
379my $pbver=shift;
380my $pbtag=shift;
381my $pbrev=shift;
382my $pbdate=shift;
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
4081;
Note: See TracBrowser for help on using the repository browser.