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

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

chomp forgotten in a system

File size: 15.2 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
16use ProjectBuilder::Changelog qw (pb_changelog);
17
18$ENV{'PBETC'} = "$ENV{'HOME'}/.pbrc";
19
20sub pb_env_init {
21
22my $proj=shift;
23my $ver;
24my $tag;
25
26# For the moment not dynamic
27my $debug = 0; # Debug level
28my $LOG = *STDOUT; # Where to log
29
30#
31# Check project name
32# Could be with env var PBPROJ
33# or option -p
34# if not define take the first in conf file
35#
36if ((defined $ENV{'PBPROJ'}) &&
37 (not (defined $proj))) {
38 $proj = $ENV{'PBPROJ'};
39}
40
41#
42# We get the pbrc file for that project
43# and use its content
44#
45my ($pbrc) = pb_conf_read("$ENV{'PBETC'}","pbrc");
46print "DEBUG pbrc: ".Dumper($pbrc)."\n" if ($debug >= 1);
47
48my %pbrc = %$pbrc;
49if (not defined $proj) {
50 # Take the first as the default project
51 $proj = (keys %pbrc)[0];
52 print $LOG "Using $proj as default project as none has been specified\n" if (($debug >= 0) and (defined $proj));
53}
54die "No project defined - use env var PBPROJ or -p proj" if (not (defined $proj));
55
56#
57# Set delivery directory
58#
59my $topdir=dirname($pbrc{$proj});
60# Expand potential env variable in it
61eval { $topdir =~ s/(\$ENV.+\})/$1/eeg };
62chdir $topdir || die "Unable to change directory to $topdir";
63$pbrc{$proj} = $topdir."/pbrc";
64$ENV{'PBDESTDIR'}=$topdir."/delivery";
65
66#
67# Use project configuration file if needed
68#
69if (not defined $ENV{'PBROOT'}) {
70 if (-f $pbrc{$proj}) {
71 my ($pbroot) = pb_conf_read($pbrc{$proj},"pbroot");
72 my %pbroot = %$pbroot;
73 # All lines should point to the same pbroot so take the first
74 $ENV{'PBROOT'} = (values %$pbroot)[0] if (defined $pbroot);
75 print $LOG "Using $ENV{'PBROOT'} as default pbroot from $pbrc{$proj}\n" if (($debug >= 0) and (defined $ENV{'PBROOT'}));
76 }
77 die "No pbroot defined - use env var PBROOT or -r pbroot " if (not defined $ENV{'PBROOT'});
78}
79
80#
81# Check pb conf compliance
82#
83$ENV{'PBCONF'} = "$ENV{'PBROOT'}/pbconf";
84die "Project $proj not Project-Builder compliant. Please populate $ENV{'PBCONF'}" if ( not -d "$ENV{'PBCONF'}");
85
86my %version = ();
87my %defpkgdir = ();
88my %extpkgdir = ();
89my %filteredfiles = ();
90
91if (-f "$ENV{'PBCONF'}/$proj.pb") {
92 # List of pkg to build by default (mandatory)
93 # List of additional pkg to build when all is called (optional)
94 # Valid version names (optional)
95 # List of files to filter (optional)
96 my ($defpkgdir, $extpkgdir, $version, $filteredfiles, $pkgv, $pkgt) = pb_conf_read("$ENV{'PBCONF'}/$proj.pb","defpkgdir","extpkgdir","version","filteredfiles","projver","projtag");
97 print "DEBUG: defpkgdir: ".Dumper($defpkgdir)."\n" if ($debug >= 1);
98 print "DEBUG: extpkgdir: ".Dumper($extpkgdir)."\n" if ($debug >= 1);
99 print "DEBUG: version: ".Dumper($version)."\n" if ($debug >= 1);
100 print "DEBUG: filteredfiles: ".Dumper($filteredfiles)."\n" if ($debug >= 1);
101 die "Unable to find defpkgdir in $ENV{'PBCONF'}/$proj.pb" if (not defined $defpkgdir);
102 # Global
103 %defpkgdir = %$defpkgdir;
104 # Global
105 %extpkgdir = %$extpkgdir if (defined $extpkgdir);
106 %version = %$version if (defined $version);
107 # Global
108 %filteredfiles = %$filteredfiles if (defined $filteredfiles);
109 #
110 # Get global Version/Tag
111 #
112
113 if (not defined $ENV{'PBVER'}) {
114 if ((defined $pkgv) && (defined $pkgv->{$proj})) {
115 $ENV{'PBVER'}=$pkgv->{$proj};
116 } else {
117 die "No projver found in $ENV{'PBCONF'}/$proj.pb";
118 }
119 }
120 die "Invalid version name $ENV{'PBVER'} in $ENV{'PBCONF'}/$proj.pb" if (($ENV{'PBVER'} !~ /[0-9.]+/) && (not exists $version{$ENV{'PBVER'}}));
121
122 if (not defined $ENV{'PBTAG'}) {
123 if ((defined $pkgt) && (defined $pkgt->{$proj})) {
124 $ENV{'PBTAG'}=$pkgt->{$proj};
125 } else {
126 die "No projtag found in $ENV{'PBCONF'}/$proj.pb";
127 }
128 }
129 die "Invalid tag name $ENV{'PBTAG'} in $ENV{'PBCONF'}/$proj.pb" if ($ENV{'PBTAG'} !~ /[0-9.]+/);
130} else {
131 die "Unable to open $ENV{'PBCONF'}/$proj.pb";
132}
133
134#
135# Set temp directory
136#
137if (not defined $ENV{'TMPDIR'}) {
138 $ENV{'TMPDIR'}="/tmp";
139}
140$ENV{'PBTMP'} = tempdir( "pb.XXXXXXXXXX", DIR => $ENV{'TMPDIR'}, CLEANUP => 1 );
141
142#
143# Removes all directory existing below the delivery dir
144# as they are temp dir only
145# Files stay and have to be cleaned up manually
146#
147if (-d $ENV{'PBDESTDIR'}) {
148 opendir(DIR,$ENV{'PBDESTDIR'}) || die "Unable to open directory $ENV{'PBDESTDIR'}: $!";
149 foreach my $d (readdir(DIR)) {
150 next if ($d =~ /^\./);
151 next if (-f "$ENV{'PBDESTDIR'}/$d");
152 pb_rm_rf("$ENV{'PBDESTDIR'}/$d") if (-d "$ENV{'PBDESTDIR'}/$d");
153 }
154 closedir(DIR);
155}
156if (! -d "$ENV{'PBDESTDIR'}") {
157 pb_mkdir_p($ENV{'PBDESTDIR'}) || die "Unable to recursively create $ENV{'PBDESTDIR'}";
158}
159
160#
161# Set build directory
162#
163$ENV{'PBBUILDDIR'}=$topdir."/build";
164if (! -d "$ENV{'PBBUILDDIR'}") {
165 pb_mkdir_p($ENV{'PBBUILDDIR'}) || die "Unable to recursively create $ENV{'PBBUILDDIR'}";
166}
167
168umask 0022;
169return($proj,$debug,$LOG,\%pbrc, \%filteredfiles, \%defpkgdir, \%extpkgdir);
170}
171
172# Internal mkdir -p function
173sub pb_mkdir_p {
174my @dir = @_;
175my $ret = mkpath(@dir, 0, 0755);
176return($ret);
177}
178
179# Internal rm -rf function
180sub pb_rm_rf {
181my @dir = @_;
182my $ret = rmtree(@dir, 0, 0);
183return($ret);
184}
185
186# Internal system function
187sub pb_system {
188
189my $cmd=shift;
190my $cmt=shift || $cmd;
191
192print "$cmt... ";
193#system("$cmd 2>&1 > $ENV{'PBTMP'}/system.log");
194system("$cmd");
195if ($? == -1) {
196 print "failed to execute ($cmd) : $!\n";
197 pb_display_file("$ENV{'PBTMP'}/system.log");
198} elsif ($? & 127) {
199 printf "child ($cmd) died with signal %d, %s coredump\n", ($? & 127), ($? & 128) ? 'with' : 'without';
200 pb_display_file("$ENV{'PBTMP'}/system.log");
201} elsif ($? == 0) {
202 print "OK\n";
203} else {
204 printf "child ($cmd) exited with value %d\n", $? >> 8;
205 pb_display_file("$ENV{'PBTMP'}/system.log");
206}
207}
208
209sub pb_display_file {
210
211my $file=shift;
212
213return if (not -f $file);
214open(FILE,"$file");
215while (<FILE>) {
216 print $_;
217}
218close(FILE);
219}
220
221# Function which returns a pointer on a hash
222# corresponding to a declaration (arg2) in the main conf file
223# and test the returned vaue as they need to exist in that case
224sub pb_conf_get {
225
226my @param = @_;
227
228my @ptr = pb_conf_read("$ENV{'PBETC'}", @param);
229
230foreach my $i (0..$#param) {
231 die "No $param[$i] defined for $ENV{'PBPROJ'}" if (not defined $ptr[$i]);
232 my $p = $ptr[$i];
233 $p->{$ENV{'PBPROJ'}} = $p->{'default'} if (not defined $p->{$ENV{'PBPROJ'}});
234 die "No $param[$i] defined for $ENV{'PBPROJ'}" if (not defined $p->{$ENV{'PBPROJ'}});
235}
236#print "DEBUG: param: ".Dumper(@ptr)."\n" if ($debug >= 1);
237return(@ptr);
238}
239
240# Function which returns a pointer on a hash
241# corresponding to a declaration (arg2) in a conf file (arg1)
242sub pb_conf_read {
243
244my $conffile = shift;
245my @param = @_;
246my $trace;
247my @ptr;
248
249my $debug = 0;
250
251if ($debug > 0) {
252 $trace = 1;
253} else {
254 $trace = 0;
255}
256
257
258my $config = AppConfig->new({
259 # Auto Create variables mentioned in Conf file
260 CREATE => 1,
261 DEBUG => $trace,
262 GLOBAL => {
263 # Each conf item is a hash
264 ARGCOUNT => ARGCOUNT_HASH,
265 },
266 });
267$config->file($conffile);
268for my $param (@param) {
269 push @ptr,$config->get($param);
270}
271print "DEBUG: params: ".Dumper(@param)." ".Dumper(@ptr)."\n" if ($debug >= 1);
272return(@ptr);
273}
274
275# Setup environment for CMS system
276sub pb_cms_init {
277
278my $proj = shift || undef;
279my $ret;
280
281my ($cms) = pb_conf_get("cms");
282# This one is optional
283my ($cvsroot,$cvsrsh) = pb_conf_read($ENV{'PBETC'},"cvsroot","cvsrsh");
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} elsif ($cms->{$proj} eq "cvs") {
291 # Way too slow
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{'PBREVISION'}="CVS";
295 $ENV{'PBCMSLOG'}="cvs log";
296 $ENV{'PBCMSLOGFILE'}="cvs.log";
297 #
298 # Export content if needed
299 #
300 $ENV{'CVSROOT'} = $cvsroot->{$proj} if (defined $cvsroot->{$proj});
301 $ENV{'CVSRSH'} = $cvsrsh->{$proj} if (defined $cvsrsh->{$proj});
302} else {
303 die "cms $cms->{$proj} unknown";
304}
305return($cms);
306}
307
308sub pb_cms_export {
309my $cms = shift;
310my $pbdate = shift || undef;
311my $source = shift;
312my $destdir = shift;
313my $tmp;
314my $tmp1;
315
316if ($cms->{$ENV{'PBPROJ'}} eq "svn") {
317 if (-d $source) {
318 $tmp = $destdir;
319 } else {
320 $tmp = $destdir."/".basename($source);
321 }
322 pb_system("svn export $source $tmp","Exporting $source from SVN to $tmp");
323} elsif ($cms->{$ENV{'PBPROJ'}} eq "cvs") {
324 my $dir=dirname($destdir);
325 my $base=basename($destdir);
326 if (-d $source) {
327 $tmp1 = $source;
328 $tmp1 =~ s|$ENV{'PBROOT'}/||;
329 } else {
330 $tmp1 = dirname($source);
331 $tmp1 =~ s|$ENV{'PBROOT'}/||;
332 $tmp1 = $tmp1."/".basename($source);
333 }
334 # CVS needs a relative path !
335 pb_system("cd $dir ; cvs export -D \"$pbdate\" -d $base $tmp1","Exporting $source from CVS to $destdir");
336} else {
337 die "cms $cms->{$ENV{'PBPROJ'}} unknown";
338}
339}
340
341sub pb_cms_log {
342my $cms = shift;
343my $pkgdir = shift;
344my $destfile = shift;
345
346if ($cms->{$ENV{'PBPROJ'}} eq "svn") {
347 pb_system("svn log -v $pkgdir > $destfile","Extracting log info from SVN");
348} elsif ($cms->{$ENV{'PBPROJ'}} eq "cvs") {
349 my $tmp=basename($pkgdir);
350 # CVS needs a relative path !
351 pb_system("cvs log $tmp > $destfile","Extracting log info from CVS");
352} else {
353 die "cms $cms->{$ENV{'PBPROJ'}} unknown";
354}
355}
356
357sub pb_cms_getinfo {
358my $cms = shift;
359my $url = "";
360my $void = "";
361
362if ($cms->{$ENV{'PBPROJ'}} eq "svn") {
363 open(PIPE,"LANGUAGE=C svn info $ENV{'PBROOT'} |") || die "Unable to get svn info from $ENV{'PBROOT'}";
364 while (<PIPE>) {
365 ($void,$url) = split(/^Repository Root:/) if (/^Repository Root:/);
366 }
367 close(PIPE);
368 chomp($url);
369} elsif ($cms->{$ENV{'PBPROJ'}} eq "cvs") {
370} else {
371 die "cms $cms->{$ENV{'PBPROJ'}} unknown";
372}
373return($url);
374}
375
376sub pb_cms_copy {
377my $cms = shift;
378my $oldurl = shift;
379my $newurl = shift;
380
381if ($cms->{$ENV{'PBPROJ'}} eq "svn") {
382 pb_system("svn copy $oldurl $newurl","Copying $oldurl to $newurl ");
383} elsif ($cms->{$ENV{'PBPROJ'}} eq "cvs") {
384} else {
385 die "cms $cms->{$ENV{'PBPROJ'}} unknown";
386}
387}
388
389sub pb_cms_checkout {
390my $cms = shift;
391my $url = shift;
392my $destination = shift;
393
394if ($cms->{$ENV{'PBPROJ'}} eq "svn") {
395 pb_system("svn co $url $destination","Checking $url to $destination ");
396} elsif ($cms->{$ENV{'PBPROJ'}} eq "cvs") {
397} else {
398 die "cms $cms->{$ENV{'PBPROJ'}} unknown";
399}
400}
401
402sub pb_cms_isdiff {
403my $cms = shift;
404
405if ($cms->{$ENV{'PBPROJ'}} eq "svn") {
406 open(PIPE,"svn diff $ENV{'PBROOT'} |") || die "Unable to get svn diff from $ENV{'PBROOT'}";
407 my $l = 0;
408 while (<PIPE>) {
409 $l++;
410 }
411 return($l);
412} elsif ($cms->{$ENV{'PBPROJ'}} eq "cvs") {
413} else {
414 die "cms $cms->{$ENV{'PBPROJ'}} unknown";
415}
416}
417
418# Get all filters to apply
419# They're cumulative from less specific to most specific
420# suffix is .pbf
421
422sub pb_get_filters {
423
424# For the moment not dynamic
425my $debug = 0; # Debug level
426my $LOG = *STDOUT; # Where to log
427
428my @ffiles;
429my ($ffile0, $ffile1, $ffile2, $ffile3);
430my ($mfile0, $mfile1, $mfile2, $mfile3);
431my $pbpkg = shift || die "No package specified";
432my $dtype = shift || die "No dtype specified";
433my $dfam = shift || die "No dfam specified";
434my $ddir = shift || die "No ddir specified";
435my $dver = shift || die "No dver specified";
436my $ptr; # returned value pointer on the hash of filters
437my %ptr;
438
439# Global filter files first, then package specificities
440if (-d "$ENV{'PBCONF'}/pbfilter") {
441 $mfile0 = "$ENV{'PBCONF'}/pbfilter/$dtype.pbf" if (-f "$ENV{'PBCONF'}/pbfilter/$dtype.pbf");
442 $mfile1 = "$ENV{'PBCONF'}/pbfilter/$dfam.pbf" if (-f "$ENV{'PBCONF'}/pbfilter/$dfam.pbf");
443 $mfile2 = "$ENV{'PBCONF'}/pbfilter/$ddir.pbf" if (-f "$ENV{'PBCONF'}/pbfilter/$ddir.pbf");
444 $mfile3 = "$ENV{'PBCONF'}/pbfilter/$ddir-$dver.pbf" if (-f "$ENV{'PBCONF'}/pbfilter/$ddir-$dver.pbf");
445
446 push @ffiles,$mfile0 if (defined $mfile0);
447 push @ffiles,$mfile1 if (defined $mfile1);
448 push @ffiles,$mfile2 if (defined $mfile2);
449 push @ffiles,$mfile3 if (defined $mfile3);
450}
451
452if (-d "$ENV{'PBCONF'}/$pbpkg/pbfilter") {
453 $ffile0 = "$ENV{'PBCONF'}/$pbpkg/pbfilter/$dtype.pbf" if (-f "$ENV{'PBCONF'}/$pbpkg/pbfilter/$dtype.pbf");
454 $ffile1 = "$ENV{'PBCONF'}/$pbpkg/pbfilter/$dfam.pbf" if (-f "$ENV{'PBCONF'}/$pbpkg/pbfilter/$dfam.pbf");
455 $ffile2 = "$ENV{'PBCONF'}/$pbpkg/pbfilter/$ddir.pbf" if (-f "$ENV{'PBCONF'}/$pbpkg/pbfilter/$ddir.pbf");
456 $ffile3 = "$ENV{'PBCONF'}/$pbpkg/pbfilter/$ddir-$dver.pbf" if (-f "$ENV{'PBCONF'}/$pbpkg/pbfilter/$ddir-$dver.pbf");
457
458 push @ffiles,$ffile0 if (defined $ffile0);
459 push @ffiles,$ffile1 if (defined $ffile1);
460 push @ffiles,$ffile2 if (defined $ffile2);
461 push @ffiles,$ffile3 if (defined $ffile3);
462}
463if (@ffiles) {
464 print $LOG "DEBUG ffiles: ".Dumper(\@ffiles)."\n" if ($debug >= 1);
465
466 my $config = AppConfig->new({
467 # Auto Create variables mentioned in Conf file
468 CREATE => 1,
469 DEBUG => 0,
470 GLOBAL => {
471 # Each conf item is a hash
472 ARGCOUNT => AppConfig::ARGCOUNT_HASH
473 }
474 });
475
476 $config->file(@ffiles);
477 $ptr = $config->get("filter");
478 print $LOG "DEBUG f:".Dumper($ptr)."\n" if ($debug >= 1);
479} else {
480 $ptr = { };
481}
482%ptr = %$ptr;
483return(\%ptr);
484}
485
486# Function which applies filter on files (only for pb)
487sub pb_filter_file_pb {
488
489my $f=shift;
490my $ptr=shift;
491my %filter=%$ptr;
492my $destfile=shift;
493my $dtype=shift;
494my $pbsuf=shift;
495my $pbpkg=shift;
496my $pbver=shift;
497my $pbtag=shift;
498my $pbrev=shift;
499my $pbdate=shift;
500my $defpkgdir = shift;
501my $extpkgdir = shift;
502my $pbpackager = shift;
503
504# For the moment not dynamic
505my $debug = 0; # Debug level
506my $LOG = *STDOUT; # Where to log
507
508print $LOG "DEBUG: From $f to $destfile\n" if ($debug >= 1);
509pb_mkdir_p(dirname($destfile)) if (! -d dirname($destfile));
510open(DEST,"> $destfile") || die "Unable to create $destfile";
511open(FILE,"$f") || die "Unable to open $f: $!";
512while (<FILE>) {
513 my $line = $_;
514 foreach my $s (keys %filter) {
515 # Process single variables
516 print $LOG "DEBUG filter{$s}: $filter{$s}\n" if ($debug >= 1);
517 my $tmp = $filter{$s};
518 next if (not defined $tmp);
519 # Expand variables if any single one found
520 print $LOG "DEBUG tmp: $tmp\n" if ($debug >= 1);
521 if ($tmp =~ /\$/) {
522 eval { $tmp =~ s/(\$\w+)/$1/eeg };
523 # special case for ChangeLog only for pb
524 } elsif (($tmp =~ /^yes$/) && ($s =~ /^PBLOG$/) && ($line =~ /^PBLOG$/)) {
525 $tmp = "";
526 my $p = $defpkgdir->{$pbpkg};
527 $p = $extpkgdir->{$pbpkg} if (not defined $p);
528 pb_changelog($dtype, $pbpkg, $pbtag, $pbsuf, $p, \*DEST);
529 }
530 $line =~ s|$s|$tmp|;
531 }
532 print DEST $line;
533}
534close(FILE);
535close(DEST);
536}
537
538# Function which applies filter on files (external call)
539sub pb_filter_file {
540
541my $f=shift;
542my $ptr=shift;
543my %filter=%$ptr;
544my $destfile=shift;
545my $pbsuf=shift;
546my $pbpkg=shift;
547my $pbver=shift;
548my $pbtag=shift;
549my $pbrev=shift;
550my $pbdate=shift;
551my $pbpackager=shift;
552
553# For the moment not dynamic
554my $debug = 0; # Debug level
555my $LOG = *STDOUT; # Where to log
556
557print $LOG "DEBUG: From $f to $destfile\n" if ($debug >= 1);
558pb_mkdir_p(dirname($destfile)) if (! -d dirname($destfile));
559open(DEST,"> $destfile") || die "Unable to create $destfile";
560open(FILE,"$f") || die "Unable to open $f: $!";
561while (<FILE>) {
562 my $line = $_;
563 foreach my $s (keys %filter) {
564 # Process single variables
565 print $LOG "DEBUG filter{$s}: $filter{$s}\n" if ($debug > 1);
566 my $tmp = $filter{$s};
567 next if (not defined $tmp);
568 # Expand variables if any single one found
569 if ($tmp =~ /\$/) {
570 eval { $tmp =~ s/(\$\w+)/$1/eeg };
571 }
572 $line =~ s|$s|$tmp|;
573 }
574 print DEST $line;
575}
576close(FILE);
577close(DEST);
578}
579
580
5811;
Note: See TracBrowser for help on using the repository browser.