1 | #!/usr/bin/perl -w
|
---|
2 | #
|
---|
3 | # Base subroutines for the Project-Builder project
|
---|
4 | #
|
---|
5 | # $Id$
|
---|
6 | #
|
---|
7 |
|
---|
8 | use strict;
|
---|
9 | use lib qw (lib);
|
---|
10 | use File::Basename;
|
---|
11 | use File::Path;
|
---|
12 | use File::Temp qw /tempdir/;
|
---|
13 | use AppConfig qw(ARGCOUNT_HASH);
|
---|
14 | use Data::Dumper;
|
---|
15 |
|
---|
16 | $ENV{'PBETC'} = "$ENV{'HOME'}/.pbrc";
|
---|
17 |
|
---|
18 | sub pb_env_init {
|
---|
19 |
|
---|
20 | my $proj=shift;
|
---|
21 | my $ver;
|
---|
22 | my $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 | #
|
---|
30 | if ((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 | #
|
---|
39 | my ($pbrc) = pb_conf_read("$ENV{'PBETC'}","pbrc");
|
---|
40 | print "DEBUG pbrc: ".Dumper($pbrc)."\n" if ($debug >= 1);
|
---|
41 |
|
---|
42 | %pbrc = %$pbrc;
|
---|
43 | if (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 | }
|
---|
48 | die "No project defined - use env var PBPROJ or -p proj" if (not (defined $proj));
|
---|
49 |
|
---|
50 | #
|
---|
51 | # Set delivery directory
|
---|
52 | #
|
---|
53 | my $topdir=dirname($pbrc{$proj});
|
---|
54 | chdir $topdir || die "Unable to change directory to $topdir";
|
---|
55 | $ENV{'PBDESTDIR'}=$topdir."/delivery";
|
---|
56 |
|
---|
57 | #
|
---|
58 | # Use project configuration file if needed
|
---|
59 | #
|
---|
60 | if (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";
|
---|
75 | die "Project $proj not Project-Builder compliant. Please populate $ENV{'PBCONF'}" if ( not -d "$ENV{'PBCONF'}");
|
---|
76 |
|
---|
77 | my %version = ();
|
---|
78 |
|
---|
79 | if (-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 | #
|
---|
107 | if (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/Tag
|
---|
114 | #
|
---|
115 | my ($pkgv, $pkgt) = pb_conf_read("$ENV{'PBCONF'}/$proj.pb","projver","projtag");
|
---|
116 |
|
---|
117 | if (not defined $ENV{'PBVER'}) {
|
---|
118 | if ((defined $pkgv) && (defined $pkgv->{$proj})) {
|
---|
119 | $ENV{'PBVER'}=$pkgv->{$proj};
|
---|
120 | } else {
|
---|
121 | die "No projver found in $ENV{'PBCONF'}/$proj.pb";
|
---|
122 | }
|
---|
123 | }
|
---|
124 | die "Invalid version name $ENV{'PBVER'} in $ENV{'PBCONF'}/$proj.pb" if (($ENV{'PBVER'} !~ /[0-9.]+/) && (not exists $version{$ENV{'PBVER'}}));
|
---|
125 |
|
---|
126 | if (not defined $ENV{'PBTAG'}) {
|
---|
127 | if ((defined $pkgt) && (defined $pkgt->{$proj})) {
|
---|
128 | $ENV{'PBTAG'}=$pkgt->{$proj};
|
---|
129 | } else {
|
---|
130 | die "No projtag found in $ENV{'PBCONF'}/$proj.pb";
|
---|
131 | }
|
---|
132 | }
|
---|
133 | die "Invalid tag name $ENV{'PBTAG'} in $ENV{'PBCONF'}/$proj.pb" if ($ENV{'PBTAG'} !~ /[0-9.]+/);
|
---|
134 |
|
---|
135 | #
|
---|
136 | # Removes all directory existing below the delivery dir
|
---|
137 | # as they are temp dir only
|
---|
138 | # Files stay and have to be cleaned up manually
|
---|
139 | #
|
---|
140 | if (-d $ENV{'PBDESTDIR'}) {
|
---|
141 | opendir(DIR,$ENV{'PBDESTDIR'}) || die "Unable to open directory $ENV{'PBDESTDIR'}: $!";
|
---|
142 | foreach my $d (readdir(DIR)) {
|
---|
143 | next if ($d =~ /^\./);
|
---|
144 | next if (-f "$ENV{'PBDESTDIR'}/$d");
|
---|
145 | pb_rm_rf("$ENV{'PBDESTDIR'}/$d") if (-d "$ENV{'PBDESTDIR'}/$d");
|
---|
146 | }
|
---|
147 | closedir(DIR);
|
---|
148 | }
|
---|
149 | if (! -d "$ENV{'PBDESTDIR'}") {
|
---|
150 | pb_mkdir_p($ENV{'PBDESTDIR'}) || die "Unable to recursively create $ENV{'PBDESTDIR'}";
|
---|
151 | }
|
---|
152 |
|
---|
153 | #
|
---|
154 | # Set build directory
|
---|
155 | #
|
---|
156 | $ENV{'PBBUILDDIR'}=$topdir."/build";
|
---|
157 | if (! -d "$ENV{'PBBUILDDIR'}") {
|
---|
158 | pb_mkdir_p($ENV{'PBBUILDDIR'}) || die "Unable to recursively create $ENV{'PBBUILDDIR'}";
|
---|
159 | }
|
---|
160 |
|
---|
161 | umask 0022;
|
---|
162 | return($proj);
|
---|
163 | }
|
---|
164 |
|
---|
165 | # Internal mkdir -p function
|
---|
166 | sub pb_mkdir_p {
|
---|
167 | my @dir = @_;
|
---|
168 | my $ret = mkpath(@dir, 0, 0755);
|
---|
169 | return($ret);
|
---|
170 | }
|
---|
171 |
|
---|
172 | # Internal rm -rf function
|
---|
173 | sub pb_rm_rf {
|
---|
174 | my @dir = @_;
|
---|
175 | my $ret = rmtree(@dir, 0, 0);
|
---|
176 | return($ret);
|
---|
177 | }
|
---|
178 |
|
---|
179 | # Internal system function
|
---|
180 | sub pb_system {
|
---|
181 |
|
---|
182 | my $cmd=shift;
|
---|
183 | my $cmt=shift || $cmd;
|
---|
184 |
|
---|
185 | print $LOG "$cmt... ";
|
---|
186 | system("$cmd");
|
---|
187 | if ($? == -1) {
|
---|
188 | print $LOG "failed to execute: $!\n" if ($debug >= 0);
|
---|
189 | } elsif ($? & 127) {
|
---|
190 | printf $LOG "child died with signal %d, %s coredump\n", ($? & 127), ($? & 128) ? 'with' : 'without' if ($debug >= 0);
|
---|
191 | } else {
|
---|
192 | print $LOG "OK\n" if ($debug >= 0);
|
---|
193 | }
|
---|
194 | }
|
---|
195 |
|
---|
196 | # Function which returns a pointer on a hash
|
---|
197 | # corresponding to a declaration (arg2) in the main conf file
|
---|
198 | # and test the returned vaue as they need to exist in that case
|
---|
199 | sub pb_conf_get {
|
---|
200 |
|
---|
201 | my @param = @_;
|
---|
202 |
|
---|
203 | my @ptr = pb_conf_read("$ENV{'PBETC'}", @param);
|
---|
204 |
|
---|
205 | foreach my $i (0..$#param) {
|
---|
206 | die "No $param[$i] defined for $ENV{'PBPROJ'}" if (not defined $ptr[$i]);
|
---|
207 | my $p = $ptr[$i];
|
---|
208 | $p->{$ENV{'PBPROJ'}} = $p->{'default'} if (not defined $p->{$ENV{'PBPROJ'}});
|
---|
209 | die "No $param[$i] defined for $ENV{'PBPROJ'}" if (not defined $p->{$ENV{'PBPROJ'}});
|
---|
210 | }
|
---|
211 | print "DEBUG: param: ".Dumper(@ptr)."\n" if ($debug >= 1);
|
---|
212 | return(@ptr);
|
---|
213 | }
|
---|
214 |
|
---|
215 | # Function which returns a pointer on a hash
|
---|
216 | # corresponding to a declaration (arg2) in a conf file (arg1)
|
---|
217 | sub pb_conf_read {
|
---|
218 |
|
---|
219 | my $conffile = shift;
|
---|
220 | my @param = @_;
|
---|
221 | my $trace;
|
---|
222 | my @ptr;
|
---|
223 |
|
---|
224 | if ($debug > 0) {
|
---|
225 | $trace = 1;
|
---|
226 | } else {
|
---|
227 | $trace = 0;
|
---|
228 | }
|
---|
229 |
|
---|
230 |
|
---|
231 | my $config = AppConfig->new({
|
---|
232 | # Auto Create variables mentioned in Conf file
|
---|
233 | CREATE => 1,
|
---|
234 | DEBUG => $trace,
|
---|
235 | GLOBAL => {
|
---|
236 | # Each conf item is a hash
|
---|
237 | ARGCOUNT => ARGCOUNT_HASH,
|
---|
238 | },
|
---|
239 | });
|
---|
240 | $config->file($conffile);
|
---|
241 | for my $param (@param) {
|
---|
242 | push @ptr,$config->get($param);
|
---|
243 | }
|
---|
244 | print "DEBUG: params: ".Dumper(@param)." ".Dumper(@ptr)."\n" if ($debug >= 1);
|
---|
245 | return(@ptr);
|
---|
246 | }
|
---|
247 |
|
---|
248 | # Setup environment for CMS system
|
---|
249 | sub pb_cms_init {
|
---|
250 |
|
---|
251 | my $proj = shift || undef;
|
---|
252 | my $ret;
|
---|
253 |
|
---|
254 | my ($cms) = pb_conf_get("cms");
|
---|
255 | # This one is optional
|
---|
256 | my ($cvsroot) = pb_conf_read($ENV{'PBETC'},"cvsroot");
|
---|
257 |
|
---|
258 | if ($cms->{$proj} eq "svn") {
|
---|
259 | $ENV{'PBREVISION'}=`(cd "$ENV{'PBROOT'}" ; svnversion .)`;
|
---|
260 | chomp($ENV{'PBREVISION'});
|
---|
261 | $ENV{'PBCMSLOG'}="svn log";
|
---|
262 | $ENV{'PBCMSLOGFILE'}="svn.log";
|
---|
263 | $ENV{'PBCMSEXP'}="svn export";
|
---|
264 | } elsif ($cms->{$proj} eq "cvs") {
|
---|
265 | $ENV{'PBREVISION'}=`(cd "$ENV{'PBROOT'}" ; cvs rannotate -f . 2>&1 | awk '{print \$1}' | grep -E '^[0-9]' | cut -d. -f2 |sort -nu | tail -1)`;
|
---|
266 | chomp($ENV{'PBREVISION'});
|
---|
267 | $ENV{'PBCMSLOG'}="cvs log";
|
---|
268 | $ENV{'PBCMSLOGFILE'}="cvs.log";
|
---|
269 | $ENV{'PBCMSEXP'}="cvs export";
|
---|
270 | #
|
---|
271 | # Export content if needed
|
---|
272 | #
|
---|
273 | $ENV{'CVSROOT'} = $cvsroot->{$proj} if (defined $cvsroot->{$proj});
|
---|
274 | } else {
|
---|
275 | die "cms $cms->{$proj} unknown";
|
---|
276 | }
|
---|
277 | }
|
---|
278 |
|
---|
279 | # Get all filters to apply
|
---|
280 | # They're cumulative from less specific to most specific
|
---|
281 | # suffix is .pbf
|
---|
282 |
|
---|
283 | sub pb_get_filters {
|
---|
284 |
|
---|
285 | my @ffiles;
|
---|
286 | my ($ffile0, $ffile1, $ffile2, $ffile3);
|
---|
287 | my $pbpkg = shift || die "No package specified";
|
---|
288 | my $dtype = shift || die "No dtype specified";
|
---|
289 | my $dfam = shift || die "No dfam specified";
|
---|
290 | my $ddir = shift || die "No ddir specified";
|
---|
291 | my $dver = shift || die "No dver specified";
|
---|
292 | my $ptr; # returned value pointer on the hash of filters
|
---|
293 | my %ptr;
|
---|
294 |
|
---|
295 | if (-d "$ENV{'PBCONF'}/$pbpkg/pbfilter") {
|
---|
296 | $ffile0 = "$ENV{'PBCONF'}/$pbpkg/pbfilter/$dtype.pbf" if (-f "$ENV{'PBCONF'}/$pbpkg/pbfilter/$dtype.pbf");
|
---|
297 | $ffile1 = "$ENV{'PBCONF'}/$pbpkg/pbfilter/$dfam.pbf" if (-f "$ENV{'PBCONF'}/$pbpkg/pbfilter/$dfam.pbf");
|
---|
298 | $ffile2 = "$ENV{'PBCONF'}/$pbpkg/pbfilter/$ddir.pbf" if (-f "$ENV{'PBCONF'}/$pbpkg/pbfilter/$ddir.pbf");
|
---|
299 | $ffile3 = "$ENV{'PBCONF'}/$pbpkg/pbfilter/$ddir-$dver.pbf" if (-f "$ENV{'PBCONF'}/$pbpkg/pbfilter/$ddir-$dver.pbf");
|
---|
300 |
|
---|
301 | push @ffiles,$ffile0 if (defined $ffile0);
|
---|
302 | push @ffiles,$ffile1 if (defined $ffile1);
|
---|
303 | push @ffiles,$ffile2 if (defined $ffile2);
|
---|
304 | push @ffiles,$ffile3 if (defined $ffile3);
|
---|
305 | }
|
---|
306 | if (@ffiles) {
|
---|
307 | print $LOG "DEBUG ffiles: ".Dumper(\@ffiles)."\n" if ($debug >= 1);
|
---|
308 |
|
---|
309 | my $config = AppConfig->new({
|
---|
310 | # Auto Create variables mentioned in Conf file
|
---|
311 | CREATE => 1,
|
---|
312 | DEBUG => 0,
|
---|
313 | GLOBAL => {
|
---|
314 | # Each conf item is a hash
|
---|
315 | ARGCOUNT => AppConfig::ARGCOUNT_HASH
|
---|
316 | }
|
---|
317 | });
|
---|
318 |
|
---|
319 | $config->file(@ffiles);
|
---|
320 | $ptr = $config->get("filter");
|
---|
321 | print $LOG "DEBUG f:".Dumper($ptr)."\n" if ($debug >= 1);
|
---|
322 | } else {
|
---|
323 | $ptr = { };
|
---|
324 | }
|
---|
325 | %ptr = %$ptr;
|
---|
326 | return(\%ptr);
|
---|
327 | }
|
---|
328 |
|
---|
329 | # Function which applies filter on files (only for pb)
|
---|
330 | sub pb_filter_file_pb {
|
---|
331 |
|
---|
332 | my $f=shift;
|
---|
333 | my $ptr=shift;
|
---|
334 | my %filter=%$ptr;
|
---|
335 | my $destfile=shift;
|
---|
336 | my $dtype=shift;
|
---|
337 | my $pbsuf=shift;
|
---|
338 | my $pbpkg=shift;
|
---|
339 | my $pbver=shift;
|
---|
340 | my $pbtag=shift;
|
---|
341 | my $pbrev=shift;
|
---|
342 | my $pbdate=shift;
|
---|
343 |
|
---|
344 | print $LOG "DEBUG: From $f to $destfile\n" if ($debug >= 1);
|
---|
345 | pb_mkdir_p(dirname($destfile)) if (! -d dirname($destfile));
|
---|
346 | open(DEST,"> $destfile") || die "Unable to create $destfile";
|
---|
347 | open(FILE,"$f") || die "Unable to open $f: $!";
|
---|
348 | while (<FILE>) {
|
---|
349 | my $line = $_;
|
---|
350 | foreach my $s (keys %filter) {
|
---|
351 | # Process single variables
|
---|
352 | print $LOG "DEBUG filter{$s}: $filter{$s}\n" if ($debug >= 1);
|
---|
353 | my $tmp = $filter{$s};
|
---|
354 | next if (not defined $tmp);
|
---|
355 | # Expand variables if any single one found
|
---|
356 | print $LOG "DEBUG tmp: $tmp\n" if ($debug >= 1);
|
---|
357 | if ($tmp =~ /\$/) {
|
---|
358 | eval { $tmp =~ s/(\$\w+)/$1/eeg };
|
---|
359 | # special case for ChangeLog only for pb
|
---|
360 | } elsif (($tmp =~ /^yes$/) && ($s =~ /^PBLOG$/) && ($line =~ /^PBLOG$/)) {
|
---|
361 | $tmp = "";
|
---|
362 | my $p = $defpkgdir{$pbpkg};
|
---|
363 | $p = $extpkgdir{$pbpkg} if (not defined $p);
|
---|
364 | pb_changelog($dtype, $pbpkg, $pbtag, $pbsuf, $p, \*DEST);
|
---|
365 | }
|
---|
366 | $line =~ s|$s|$tmp|;
|
---|
367 | }
|
---|
368 | print DEST $line;
|
---|
369 | }
|
---|
370 | close(FILE);
|
---|
371 | close(DEST);
|
---|
372 | }
|
---|
373 |
|
---|
374 | # Function which applies filter on files (external call)
|
---|
375 | sub pb_filter_file {
|
---|
376 |
|
---|
377 | my $f=shift;
|
---|
378 | my $ptr=shift;
|
---|
379 | my %filter=%$ptr;
|
---|
380 | my $destfile=shift;
|
---|
381 | my $pbsuf=shift;
|
---|
382 | my $pbpkg=shift;
|
---|
383 | my $pbver=shift;
|
---|
384 | my $pbtag=shift;
|
---|
385 | my $pbrev=shift;
|
---|
386 | my $pbdate=shift;
|
---|
387 |
|
---|
388 | print $LOG "DEBUG: From $f to $destfile\n" if ($debug >= 1);
|
---|
389 | pb_mkdir_p(dirname($destfile)) if (! -d dirname($destfile));
|
---|
390 | open(DEST,"> $destfile") || die "Unable to create $destfile";
|
---|
391 | open(FILE,"$f") || die "Unable to open $f: $!";
|
---|
392 | while (<FILE>) {
|
---|
393 | my $line = $_;
|
---|
394 | foreach my $s (keys %filter) {
|
---|
395 | # Process single variables
|
---|
396 | print $LOG "DEBUG filter{$s}: $filter{$s}\n" if ($debug > 1);
|
---|
397 | my $tmp = $filter{$s};
|
---|
398 | next if (not defined $tmp);
|
---|
399 | # Expand variables if any single one found
|
---|
400 | if ($tmp =~ /\$/) {
|
---|
401 | eval { $tmp =~ s/(\$\w+)/$1/eeg };
|
---|
402 | }
|
---|
403 | $line =~ s|$s|$tmp|;
|
---|
404 | }
|
---|
405 | print DEST $line;
|
---|
406 | }
|
---|
407 | close(FILE);
|
---|
408 | close(DEST);
|
---|
409 | }
|
---|
410 |
|
---|
411 |
|
---|
412 | 1;
|
---|