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

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

Lots of various fixes for CVS support with LinuxCOE

  • Property svn:executable set to *
File size: 12.1 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, $pkgv, $pkgt) = pb_conf_read("$ENV{'PBCONF'}/$proj.pb","defpkgdir","extpkgdir","version","filteredfiles","projver","projtag");
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 #
101 # Get global Version/Tag
102 #
103
104 if (not defined $ENV{'PBVER'}) {
105 if ((defined $pkgv) && (defined $pkgv->{$proj})) {
106 $ENV{'PBVER'}=$pkgv->{$proj};
107 } else {
108 die "No projver found in $ENV{'PBCONF'}/$proj.pb";
109 }
110 }
111 die "Invalid version name $ENV{'PBVER'} in $ENV{'PBCONF'}/$proj.pb" if (($ENV{'PBVER'} !~ /[0-9.]+/) && (not exists $version{$ENV{'PBVER'}}));
112
113 if (not defined $ENV{'PBTAG'}) {
114 if ((defined $pkgt) && (defined $pkgt->{$proj})) {
115 $ENV{'PBTAG'}=$pkgt->{$proj};
116 } else {
117 die "No projtag found in $ENV{'PBCONF'}/$proj.pb";
118 }
119 }
120 die "Invalid tag name $ENV{'PBTAG'} in $ENV{'PBCONF'}/$proj.pb" if ($ENV{'PBTAG'} !~ /[0-9.]+/);
121} else {
122 die "Unable to open $ENV{'PBCONF'}/$proj.pb";
123}
124
125#
126# Set temp directory
127#
128if (not defined $ENV{'TMPDIR'}) {
129 $ENV{'TMPDIR'}="/tmp";
130}
131$ENV{'PBTMP'} = tempdir( "pb.XXXXXXXXXX", DIR => $ENV{'TMPDIR'}, CLEANUP => 1 );
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";
155if (! -d "$ENV{'PBBUILDDIR'}") {
156 pb_mkdir_p($ENV{'PBBUILDDIR'}) || die "Unable to recursively create $ENV{'PBBUILDDIR'}";
157}
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 2>&1 > $ENV{'PBTMP'}/system.log");
185if ($? == -1) {
186 print $LOG "failed to execute ($cmd) : $!\n" if ($debug >= 0);
187 pb_display_file("$ENV{'PBTMP'}/system.log");
188} elsif ($? & 127) {
189 printf $LOG "child ($cmd) died with signal %d, %s coredump\n", ($? & 127), ($? & 128) ? 'with' : 'without' if ($debug >= 0);
190 pb_display_file("$ENV{'PBTMP'}/system.log");
191} elsif ($? == 0) {
192 print $LOG "OK\n" if ($debug >= 0);
193} else {
194 printf "child ($cmd) exited with value %d\n", $? >> 8;
195 pb_display_file("$ENV{'PBTMP'}/system.log");
196}
197}
198
199sub pb_display_file {
200
201my $file=shift;
202
203open(FILE,"$file") || die "Unable to open $file";
204while (<FILE>) {
205 print $LOG;
206}
207close(FILE);
208}
209
210# Function which returns a pointer on a hash
211# corresponding to a declaration (arg2) in the main conf file
212# and test the returned vaue as they need to exist in that case
213sub pb_conf_get {
214
215my @param = @_;
216
217my @ptr = pb_conf_read("$ENV{'PBETC'}", @param);
218
219foreach my $i (0..$#param) {
220 die "No $param[$i] defined for $ENV{'PBPROJ'}" if (not defined $ptr[$i]);
221 my $p = $ptr[$i];
222 $p->{$ENV{'PBPROJ'}} = $p->{'default'} if (not defined $p->{$ENV{'PBPROJ'}});
223 die "No $param[$i] defined for $ENV{'PBPROJ'}" if (not defined $p->{$ENV{'PBPROJ'}});
224}
225print "DEBUG: param: ".Dumper(@ptr)."\n" if ($debug >= 1);
226return(@ptr);
227}
228
229# Function which returns a pointer on a hash
230# corresponding to a declaration (arg2) in a conf file (arg1)
231sub pb_conf_read {
232
233my $conffile = shift;
234my @param = @_;
235my $trace;
236my @ptr;
237
238if ($debug > 0) {
239 $trace = 1;
240} else {
241 $trace = 0;
242}
243
244
245my $config = AppConfig->new({
246 # Auto Create variables mentioned in Conf file
247 CREATE => 1,
248 DEBUG => $trace,
249 GLOBAL => {
250 # Each conf item is a hash
251 ARGCOUNT => ARGCOUNT_HASH,
252 },
253 });
254$config->file($conffile);
255for my $param (@param) {
256 push @ptr,$config->get($param);
257}
258print "DEBUG: params: ".Dumper(@param)." ".Dumper(@ptr)."\n" if ($debug >= 1);
259return(@ptr);
260}
261
262# Setup environment for CMS system
263sub pb_cms_init {
264
265my $proj = shift || undef;
266my $ret;
267
268my ($cms) = pb_conf_get("cms");
269# This one is optional
270my ($cvsroot) = pb_conf_read($ENV{'PBETC'},"cvsroot");
271
272if ($cms->{$proj} eq "svn") {
273 $ENV{'PBREVISION'}=`(cd "$ENV{'PBROOT'}" ; svnversion .)`;
274 chomp($ENV{'PBREVISION'});
275 $ENV{'PBCMSLOG'}="svn log";
276 $ENV{'PBCMSLOGFILE'}="svn.log";
277} elsif ($cms->{$proj} eq "cvs") {
278 # Way too slow
279 #$ENV{'PBREVISION'}=`(cd "$ENV{'PBROOT'}" ; cvs rannotate -f . 2>&1 | awk '{print \$1}' | grep -E '^[0-9]' | cut -d. -f2 |sort -nu | tail -1)`;
280 #chomp($ENV{'PBREVISION'});
281 $ENV{'PBREVISION'}="CVS";
282 $ENV{'PBCMSLOG'}="cvs log";
283 $ENV{'PBCMSLOGFILE'}="cvs.log";
284 #
285 # Export content if needed
286 #
287 $ENV{'CVSROOT'} = $cvsroot->{$proj} if (defined $cvsroot->{$proj});
288} else {
289 die "cms $cms->{$proj} unknown";
290}
291return($cms);
292}
293
294sub pb_cms_export {
295my $cms = shift;
296my $pbdate = shift || undef;
297my $pkgdir = shift;
298my $destdir = shift;
299
300if ($cms->{$ENV{'PBPROJ'}} eq "svn") {
301 pb_system("svn export $pkgdir $destdir","Exporting $pkgdir from SVN");
302} elsif ($cms->{$ENV{'PBPROJ'}} eq "cvs") {
303 my $dir=dirname($destdir);
304 my $base=basename($destdir);
305 my $tmp=basename($pkgdir);
306 # CVS needs a relative path !
307 pb_system("cd $dir ; cvs export -D $pbdate -d $base $tmp","Exporting $pkgdir from CVS");
308} else {
309 die "cms $cms->{$ENV{'PBPROJ'}} unknown";
310}
311}
312
313sub pb_cms_log {
314my $cms = shift;
315my $pkgdir = shift;
316my $destfile = shift;
317
318if ($cms->{$ENV{'PBPROJ'}} eq "svn") {
319 pb_system("svn log $pkgdir > $destfile","Extracting log info from SVN");
320} elsif ($cms->{$ENV{'PBPROJ'}} eq "cvs") {
321 my $tmp=basename($pkgdir);
322 # CVS needs a relative path !
323 pb_system("cvs log $tmp > $destfile","Extracting log info from CVS");
324} else {
325 die "cms $cms->{$ENV{'PBPROJ'}} unknown";
326}
327}
328
329
330
331# Get all filters to apply
332# They're cumulative from less specific to most specific
333# suffix is .pbf
334
335sub pb_get_filters {
336
337my @ffiles;
338my ($ffile0, $ffile1, $ffile2, $ffile3);
339my $pbpkg = shift || die "No package specified";
340my $dtype = shift || die "No dtype specified";
341my $dfam = shift || die "No dfam specified";
342my $ddir = shift || die "No ddir specified";
343my $dver = shift || die "No dver specified";
344my $ptr; # returned value pointer on the hash of filters
345my %ptr;
346
347if (-d "$ENV{'PBCONF'}/$pbpkg/pbfilter") {
348 $ffile0 = "$ENV{'PBCONF'}/$pbpkg/pbfilter/$dtype.pbf" if (-f "$ENV{'PBCONF'}/$pbpkg/pbfilter/$dtype.pbf");
349 $ffile1 = "$ENV{'PBCONF'}/$pbpkg/pbfilter/$dfam.pbf" if (-f "$ENV{'PBCONF'}/$pbpkg/pbfilter/$dfam.pbf");
350 $ffile2 = "$ENV{'PBCONF'}/$pbpkg/pbfilter/$ddir.pbf" if (-f "$ENV{'PBCONF'}/$pbpkg/pbfilter/$ddir.pbf");
351 $ffile3 = "$ENV{'PBCONF'}/$pbpkg/pbfilter/$ddir-$dver.pbf" if (-f "$ENV{'PBCONF'}/$pbpkg/pbfilter/$ddir-$dver.pbf");
352
353 push @ffiles,$ffile0 if (defined $ffile0);
354 push @ffiles,$ffile1 if (defined $ffile1);
355 push @ffiles,$ffile2 if (defined $ffile2);
356 push @ffiles,$ffile3 if (defined $ffile3);
357}
358if (@ffiles) {
359 print $LOG "DEBUG ffiles: ".Dumper(\@ffiles)."\n" if ($debug >= 1);
360
361 my $config = AppConfig->new({
362 # Auto Create variables mentioned in Conf file
363 CREATE => 1,
364 DEBUG => 0,
365 GLOBAL => {
366 # Each conf item is a hash
367 ARGCOUNT => AppConfig::ARGCOUNT_HASH
368 }
369 });
370
371 $config->file(@ffiles);
372 $ptr = $config->get("filter");
373 print $LOG "DEBUG f:".Dumper($ptr)."\n" if ($debug >= 1);
374} else {
375 $ptr = { };
376}
377%ptr = %$ptr;
378return(\%ptr);
379}
380
381# Function which applies filter on files (only for pb)
382sub pb_filter_file_pb {
383
384my $f=shift;
385my $ptr=shift;
386my %filter=%$ptr;
387my $destfile=shift;
388my $dtype=shift;
389my $pbsuf=shift;
390my $pbpkg=shift;
391my $pbver=shift;
392my $pbtag=shift;
393my $pbrev=shift;
394my $pbdate=shift;
395
396print $LOG "DEBUG: From $f to $destfile\n" if ($debug >= 1);
397pb_mkdir_p(dirname($destfile)) if (! -d dirname($destfile));
398open(DEST,"> $destfile") || die "Unable to create $destfile";
399open(FILE,"$f") || die "Unable to open $f: $!";
400while (<FILE>) {
401 my $line = $_;
402 foreach my $s (keys %filter) {
403 # Process single variables
404 print $LOG "DEBUG filter{$s}: $filter{$s}\n" if ($debug >= 1);
405 my $tmp = $filter{$s};
406 next if (not defined $tmp);
407 # Expand variables if any single one found
408 print $LOG "DEBUG tmp: $tmp\n" if ($debug >= 1);
409 if ($tmp =~ /\$/) {
410 eval { $tmp =~ s/(\$\w+)/$1/eeg };
411 # special case for ChangeLog only for pb
412 } elsif (($tmp =~ /^yes$/) && ($s =~ /^PBLOG$/) && ($line =~ /^PBLOG$/)) {
413 $tmp = "";
414 my $p = $defpkgdir{$pbpkg};
415 $p = $extpkgdir{$pbpkg} if (not defined $p);
416 pb_changelog($dtype, $pbpkg, $pbtag, $pbsuf, $p, \*DEST);
417 }
418 $line =~ s|$s|$tmp|;
419 }
420 print DEST $line;
421}
422close(FILE);
423close(DEST);
424}
425
426# Function which applies filter on files (external call)
427sub pb_filter_file {
428
429my $f=shift;
430my $ptr=shift;
431my %filter=%$ptr;
432my $destfile=shift;
433my $pbsuf=shift;
434my $pbpkg=shift;
435my $pbver=shift;
436my $pbtag=shift;
437my $pbrev=shift;
438my $pbdate=shift;
439
440print $LOG "DEBUG: From $f to $destfile\n" if ($debug >= 1);
441pb_mkdir_p(dirname($destfile)) if (! -d dirname($destfile));
442open(DEST,"> $destfile") || die "Unable to create $destfile";
443open(FILE,"$f") || die "Unable to open $f: $!";
444while (<FILE>) {
445 my $line = $_;
446 foreach my $s (keys %filter) {
447 # Process single variables
448 print $LOG "DEBUG filter{$s}: $filter{$s}\n" if ($debug > 1);
449 my $tmp = $filter{$s};
450 next if (not defined $tmp);
451 # Expand variables if any single one found
452 if ($tmp =~ /\$/) {
453 eval { $tmp =~ s/(\$\w+)/$1/eeg };
454 }
455 $line =~ s|$s|$tmp|;
456 }
457 print DEST $line;
458}
459close(FILE);
460close(DEST);
461}
462
463
4641;
Note: See TracBrowser for help on using the repository browser.