source: ProjectBuilder/devel/pb/lib/ProjectBuilder/Filter.pm@ 409

Last change on this file since 409 was 409, checked in by Bruno Cornec, 16 years ago
  • Document all reusable functions in pb
  • remove the useless pbproj parameter from pb_filter functions
  • Addition and use of pb_conf_init and pb_conf_add in pb
  • Addition and use of pb_conf_fromfile_if in Conf.pm
  • preparation for 0.9.1
  • Update of pbinit files for mondo to support the new interface of pb_filter functions
  • Property svn:executable set to *
File size: 8.8 KB
Line 
1#!/usr/bin/perl -w
2#
3# ProjectBuilder Filter module
4# Filtering subroutines brought by the the Project-Builder project
5# which can be easily used by pbinit
6#
7# $Id$
8#
9# Copyright B. Cornec 2007
10# Provided under the GPL v2
11
12package ProjectBuilder::Filter;
13
14use strict 'vars';
15use Data::Dumper;
16use English;
17use File::Basename;
18use File::Copy;
19use lib qw (lib);
20use ProjectBuilder::Base;
21use ProjectBuilder::Changelog;
22
23# Inherit from the "Exporter" module which handles exporting functions.
24
25use Exporter;
26
27# Export, by default, all the functions into the namespace of
28# any code which uses this module.
29
30our @ISA = qw(Exporter);
31our @EXPORT = qw(pb_get_filters pb_filter_file_pb pb_filter_file_inplace pb_filter_file);
32
33=pod
34
35=head1 NAME
36
37ProjectBuilder::Filter, part of the project-builder.org
38
39=head1 DESCRIPTION
40
41This module provides filtering functions suitable for pbinit calls.
42
43=item B<pb_get_filters>
44
45This function gets all filters to apply. They're cumulative from the less specific to the most specific.
46
47Suffix of those filters is .pbf. Filter all.pbf applies to whatever distribution. The pbfilter directory may be global under pbconf or per package, for overloading values. Then in order filters are loaded for distribution type, distribution family, distribution name, distribution name-version.
48
49The first parameter is the package name.
50The second parameter is the distribution type.
51The third parameter is the distribution family.
52The fourth parameter is the distribution name.
53The fifth parameter is the distribution version.
54
55The function returns a pointer on a hash of filters.
56
57=cut
58
59sub pb_get_filters {
60
61my @ffiles;
62my ($ffile00, $ffile0, $ffile1, $ffile2, $ffile3);
63my ($mfile00, $mfile0, $mfile1, $mfile2, $mfile3);
64my $pbpkg = shift || die "No package specified";
65my $dtype = shift || "";
66my $dfam = shift || "";
67my $ddir = shift || "";
68my $dver = shift || "";
69my $ptr = undef; # returned value pointer on the hash of filters
70my %h;
71
72# Global filter files first, then package specificities
73if (-d "$ENV{'PBROOTDIR'}/pbfilter") {
74 $mfile00 = "$ENV{'PBROOTDIR'}/pbfilter/all.pbf" if (-f "$ENV{'PBROOTDIR'}/pbfilter/all.pbf");
75 $mfile0 = "$ENV{'PBROOTDIR'}/pbfilter/$dtype.pbf" if (-f "$ENV{'PBROOTDIR'}/pbfilter/$dtype.pbf");
76 $mfile1 = "$ENV{'PBROOTDIR'}/pbfilter/$dfam.pbf" if (-f "$ENV{'PBROOTDIR'}/pbfilter/$dfam.pbf");
77 $mfile2 = "$ENV{'PBROOTDIR'}/pbfilter/$ddir.pbf" if (-f "$ENV{'PBROOTDIR'}/pbfilter/$ddir.pbf");
78 $mfile3 = "$ENV{'PBROOTDIR'}/pbfilter/$ddir-$dver.pbf" if (-f "$ENV{'PBROOTDIR'}/pbfilter/$ddir-$dver.pbf");
79
80 push @ffiles,$mfile00 if (defined $mfile00);
81 push @ffiles,$mfile0 if (defined $mfile0);
82 push @ffiles,$mfile1 if (defined $mfile1);
83 push @ffiles,$mfile2 if (defined $mfile2);
84 push @ffiles,$mfile3 if (defined $mfile3);
85}
86
87if (-d "$ENV{'PBROOTDIR'}/$pbpkg/pbfilter") {
88 $ffile00 = "$ENV{'PBROOTDIR'}/$pbpkg/pbfilter/all.pbf" if (-f "$ENV{'PBROOTDIR'}/$pbpkg/pbfilter/all.pbf");
89 $ffile0 = "$ENV{'PBROOTDIR'}/$pbpkg/pbfilter/$dtype.pbf" if (-f "$ENV{'PBROOTDIR'}/$pbpkg/pbfilter/$dtype.pbf");
90 $ffile1 = "$ENV{'PBROOTDIR'}/$pbpkg/pbfilter/$dfam.pbf" if (-f "$ENV{'PBROOTDIR'}/$pbpkg/pbfilter/$dfam.pbf");
91 $ffile2 = "$ENV{'PBROOTDIR'}/$pbpkg/pbfilter/$ddir.pbf" if (-f "$ENV{'PBROOTDIR'}/$pbpkg/pbfilter/$ddir.pbf");
92 $ffile3 = "$ENV{'PBROOTDIR'}/$pbpkg/pbfilter/$ddir-$dver.pbf" if (-f "$ENV{'PBROOTDIR'}/$pbpkg/pbfilter/$ddir-$dver.pbf");
93
94 push @ffiles,$ffile00 if (defined $ffile00);
95 push @ffiles,$ffile0 if (defined $ffile0);
96 push @ffiles,$ffile1 if (defined $ffile1);
97 push @ffiles,$ffile2 if (defined $ffile2);
98 push @ffiles,$ffile3 if (defined $ffile3);
99}
100if (@ffiles) {
101 pb_log(2,"DEBUG ffiles: ".Dumper(\@ffiles)."\n");
102
103 foreach my $f (@ffiles) {
104 open(CONF,$f) || next;
105 while(<CONF>) {
106 if (/^\s*([A-z0-9-_]+)\s+([[A-z0-9-_]+)\s*=\s*(.+)$/) {
107 $h{$1}{$2}=$3;
108 }
109 }
110 close(CONF);
111
112 $ptr = $h{"filter"};
113 pb_log(2,"DEBUG f:".Dumper($ptr)."\n");
114 }
115}
116return($ptr);
117}
118
119=item B<pb_filter_file_pb>
120
121This function applies all filters to pb build files.
122
123It takes 15 parameters.
124
125The first parameter is the file to filter.
126The second parameter is the pointer on the hash of filters.
127The third parameter is the destination file after filtering.
128The fourth parameter is the distribution type.
129The fifth parameter is the suffix of the distribution.
130The sixth parameter is the package name.
131The seventh parameter is the version of the package.
132The eighth parameter is the tag of the package.
133The nineth parameter is the revision of the package.
134The tenth parameter is the current date.
135The eleventh parameter is the list of required packages.
136The twelveth parameter is the list of optional packages.
137The thirteenth parameter is the packager name.
138The fourteenth parameter is the changelog.
139
140=cut
141
142sub pb_filter_file_pb {
143
144my $f=shift;
145my $ptr=shift;
146my %filter=%$ptr;
147my $destfile=shift;
148my $dtype=shift;
149my $pbsuf=shift;
150my $pbpkg=shift;
151my $pbver=shift;
152my $pbtag=shift;
153my $pbrev=shift;
154my $pbdate=shift;
155my $defpkgdir = shift;
156my $extpkgdir = shift;
157my $pbpackager = shift;
158my $chglog = shift || undef;
159
160pb_log(2,"DEBUG: From $f to $destfile\n");
161pb_mkdir_p(dirname($destfile)) if (! -d dirname($destfile));
162open(DEST,"> $destfile") || die "Unable to create $destfile";
163open(FILE,"$f") || die "Unable to open $f: $!";
164while (<FILE>) {
165 my $line = $_;
166 foreach my $s (keys %filter) {
167 # Process single variables
168 pb_log(2,"DEBUG filter{$s}: $filter{$s}\n");
169 my $tmp = $filter{$s};
170 next if (not defined $tmp);
171 # Expand variables if any single one found
172 pb_log(2,"DEBUG tmp: $tmp\n");
173 if ($tmp =~ /\$/) {
174 eval { $tmp =~ s/(\$\w+)/$1/eeg };
175 # special case for ChangeLog only for pb
176 } elsif (($s =~ /^PBLOG$/) && ($line =~ /^PBLOG$/)) {
177 my $p = $defpkgdir->{$pbpkg};
178 $p = $extpkgdir->{$pbpkg} if (not defined $p);
179 pb_changelog($dtype, $pbpkg, $pbver, $pbtag, $pbsuf, $p, \*DEST, $tmp, $chglog);
180 $tmp = "";
181 }
182 $line =~ s|$s|$tmp|;
183 }
184 print DEST $line;
185}
186close(FILE);
187close(DEST);
188}
189
190=item B<pb_filter_file_inplace>
191
192This function applies all filters to a file in place.
193
194It takes 8 parameters.
195
196The first parameter is the pointer on the hash of filters.
197The second parameter is the destination file after filtering.
198The third parameter is the package name.
199The fourth parameter is the version of the package.
200The fifth parameter is the tag of the package.
201The sixth parameter is the revision of the package.
202The seventh parameter is the current date.
203The eighth parameter is the packager name.
204
205=cut
206
207# Function which applies filter on files (external call)
208sub pb_filter_file_inplace {
209
210my $ptr=shift;
211my %filter=%$ptr;
212my $destfile=shift;
213my $pbpkg=shift;
214my $pbver=shift;
215my $pbtag=shift;
216my $pbrev=shift;
217my $pbdate=shift;
218my $pbpackager=shift;
219
220my $cp = "$ENV{'PBTMP'}/".basename($destfile);
221copy($destfile,$cp) || die "Unable to create $cp";
222
223pb_filter_file($cp,$ptr,$destfile,$pbpkg,$pbver,$pbtag,$pbrev,$pbdate,$pbpackager);
224unlink $cp;
225}
226
227=item B<pb_filter_file>
228
229This function applies all filters on a file to generate a new filtered one.
230
231It takes 9 parameters.
232
233The first parameter is the original file to filter.
234The second parameter is the pointer on the hash of filters.
235The third parameter is the destination file after filtering.
236The fourth parameter is the package name.
237The fifth parameter is the version of the package.
238The sixth parameter is the tag of the package.
239The seventh parameter is the revision of the package.
240The eighth parameter is the current date.
241The nineth parameter is the packager name.
242
243=cut
244
245
246# Function which applies filter on files (external call)
247sub pb_filter_file {
248
249my $f=shift;
250my $ptr=shift;
251my %filter=%$ptr;
252my $destfile=shift;
253my $pbpkg=shift;
254my $pbver=shift;
255my $pbtag=shift;
256my $pbrev=shift;
257my $pbdate=shift;
258my $pbpackager=shift;
259
260pb_log(2,"DEBUG: From $f to $destfile\n");
261pb_mkdir_p(dirname($destfile)) if (! -d dirname($destfile));
262open(DEST,"> $destfile") || die "Unable to create $destfile";
263open(FILE,"$f") || die "Unable to open $f: $!";
264while (<FILE>) {
265 my $line = $_;
266 foreach my $s (keys %filter) {
267 # Process single variables
268 pb_log(2,"DEBUG filter{$s}: $filter{$s}\n");
269 my $tmp = $filter{$s};
270 next if (not defined $tmp);
271 # Expand variables if any single one found
272 if ($tmp =~ /\$/) {
273 eval { $tmp =~ s/(\$\w+)/$1/eeg };
274 }
275 $line =~ s|$s|$tmp|;
276 }
277 print DEST $line;
278}
279close(FILE);
280close(DEST);
281}
282
283=back
284
285=head1 WEB SITES
286
287The main Web site of the project is available at L<http://www.project-builder.org/>. Bug reports should be filled using the trac instance of the project at L<http://trac.project-builder.org/>.
288
289=head1 USER MAILING LIST
290
291None exists for the moment.
292
293=head1 AUTHORS
294
295The Project-Builder.org team L<http://trac.project-builder.org/> lead by Bruno Cornec L<mailto:bruno@project-builder.org>.
296
297=head1 COPYRIGHT
298
299Project-Builder.org is distributed under the GPL v2.0 license
300described in the file C<COPYING> included with the distribution.
301
302=cut
303
3041;
Note: See TracBrowser for help on using the repository browser.