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
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 $ptr = pb_conf_read("$ENV{'PBCONF'}/$proj.pb","defpkgdir","extpkgdir","version","filteredfiles");
85 my ($defpkgdir, $extpkgdir, $version, $filteredfiles) = @$ptr;
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);
101} else {
102 die "Unable to open $ENV{'PBCONF'}/$proj.pb";
103}
104
105#
106# Set temp directory
107#
108if (not defined $ENV{'TMPDIR'}) {
109 $ENV{'TMPDIR'}="/tmp";
110}
111$ENV{'PBTMP'} = tempdir( "pb.XXXXXXXXXX", DIR => $ENV{'TMPDIR'}, CLEANUP => 1 );
112
113#
114# Get global VERSION
115#
116open(VER, "$ENV{'PBCONF'}/VERSION") || die "Unable to open $ENV{'PBCONF'}/VERSION: $?";
117$ver = <VER>;
118chomp($ver);
119#print Dumper(%version);
120die "Invalid version name $ver in $ENV{'PBROOT'}/VERSION" if ($ver !~ /[0-9.]+/) && (not exists $version{$ver});
121$ENV{'PBVER'}=$ver;
122close(VER);
123
124#
125# Get global TAG
126#
127open(TAG, "$ENV{'PBCONF'}/TAG") || die "Unable to open $ENV{'PBCONF'}/TAG: $?";
128$tag = <TAG>;
129chomp($tag);
130die "Invalid tag name $tag in $ENV{'PBROOT'}/TAG" if ($tag !~ /[0-9]+/);
131$ENV{'PBTAG'}=$tag;
132close(TAG);
133
134#
135# Removes all directory existing below the delivery dir
136# as they are temp dir only
137# Files stay and have to be cleaned up manually
138#
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 =~ /^\./);
143 next if (-f "$ENV{'PBDESTDIR'}/$d");
144 pb_rm_rf("$ENV{'PBDESTDIR'}/$d") if (-d "$ENV{'PBDESTDIR'}/$d");
145 }
146 closedir(DIR);
147}
148if (! -d "$ENV{'PBDESTDIR'}") {
149 pb_mkdir_p($ENV{'PBDESTDIR'}) || die "Unable to recursively create $ENV{'PBDESTDIR'}";
150}
151
152#
153# Set build directory
154#
155$ENV{'PBBUILDDIR'}=$topdir."/build";
156pb_rm_rf($ENV{'PBBUILDDIR'}) if (-d "$ENV{'PBBUILDDIR'}");
157pb_mkdir_p($ENV{'PBBUILDDIR'}) || die "Unable to recursively create $ENV{'PBBUILDDIR'}";
158
159umask 0022;
160return($proj);
161}
162
163# Internal mkdir -p function
164sub pb_mkdir_p {
165my @dir = @_;
166my $ret = mkpath(@dir, 0, 0755);
167return($ret);
168}
169
170# Internal rm -rf function
171sub pb_rm_rf {
172my @dir = @_;
173my $ret = rmtree(@dir, 0, 0);
174return($ret);
175}
176
177# Internal system function
178sub pb_system {
179
180my $cmd=shift;
181my $cmt=shift || $cmd;
182
183print $LOG "$cmt... ";
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 {
190 print $LOG "OK\n" if ($debug >= 0);
191}
192}
193
194# Function which returns a pointer on a hash
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
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) {
254 print "DEBUG: param: $param[0] ".Dumper($ptr[0])."\n" if ($debug >= 1);
255 return($ptr[0]);
256} else {
257 my $ptr = \@ptr;
258 print "DEBUG: params: ".Dumper(@param)." ".Dumper($ptr)."\n" if ($debug >= 1);
259 return($ptr);
260}
261}
262
263# Setup environment for CMS system
264sub pb_cms_init {
265
266my $proj = shift || undef;
267my $ret;
268
269my $cms = pb_conf_get("cms");
270# This one is optional
271my $cvsroot = pb_conf_read($ENV{'PBETC'},"cvsroot");
272
273if ($cms->{$proj} eq "svn") {
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";
279} elsif ($cms->{$proj} eq "cvs") {
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";
284 $ENV{'PBCMSEXP'}="cvs export";
285 #
286 # Export content if needed
287 #
288 $ENV{'CVSROOT'} = $cvsroot->{$proj} if (defined $cvsroot->{$proj});
289} else {
290 die "cms $cms->{$proj} unknown";
291}
292}
293
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
308my %ptr;
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);
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
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}
340%ptr = %$ptr;
341return(\%ptr);
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;
353my $pbpkg=shift;
354my $pbver=shift;
355my $pbtag=shift;
356my $pbrev=shift;
357my $pbdate=shift;
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
367 print $LOG "DEBUG filter{$s}: $filter{$s}\n" if ($debug >= 1);
368 my $tmp = $filter{$s};
369 next if (not defined $tmp);
370 # Expand variables if any single one found
371 print $LOG "DEBUG tmp: $tmp\n" if ($debug >= 1);
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;
396my $pbpkg=shift;
397my $pbver=shift;
398my $pbtag=shift;
399my $pbrev=shift;
400my $pbdate=shift;
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
4261;
Note: See TracBrowser for help on using the repository browser.