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

Last change on this file since 429 was 429, checked in by bruno, 11 years ago
  • Change filtering interface to add pbrepo keyword support and PBREPO macro
  • Add repository generation support + conf files to pb for rpm
  • Adapt pb and mondorecue spec files to new interfaces
File size: 9.2 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=over 4
44
45=item B<pb_get_filters>
46
47This function gets all filters to apply. They're cumulative from the less specific to the most specific.
48
49Suffix 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.
50
51The first parameter is the package name.
52The second parameter is the distribution type.
53The third parameter is the distribution family.
54The fourth parameter is the distribution name.
55The fifth parameter is the distribution version.
56
57The function returns a pointer on a hash of filters.
58
59=cut
60
61sub pb_get_filters {
62
63my @ffiles;
64my ($ffile00, $ffile0, $ffile1, $ffile2, $ffile3);
65my ($mfile00, $mfile0, $mfile1, $mfile2, $mfile3);
66my $pbpkg = shift || die "No package specified";
67my $dtype = shift || "";
68my $dfam = shift || "";
69my $ddir = shift || "";
70my $dver = shift || "";
71my $ptr = undef; # returned value pointer on the hash of filters
72my %h;
73
74# Global filter files first, then package specificities
75if (-d "$ENV{'PBROOTDIR'}/pbfilter") {
76    $mfile00 = "$ENV{'PBROOTDIR'}/pbfilter/all.pbf" if (-f "$ENV{'PBROOTDIR'}/pbfilter/all.pbf");
77    $mfile0 = "$ENV{'PBROOTDIR'}/pbfilter/$dtype.pbf" if (-f "$ENV{'PBROOTDIR'}/pbfilter/$dtype.pbf");
78    $mfile1 = "$ENV{'PBROOTDIR'}/pbfilter/$dfam.pbf" if (-f "$ENV{'PBROOTDIR'}/pbfilter/$dfam.pbf");
79    $mfile2 = "$ENV{'PBROOTDIR'}/pbfilter/$ddir.pbf" if (-f "$ENV{'PBROOTDIR'}/pbfilter/$ddir.pbf");
80    $mfile3 = "$ENV{'PBROOTDIR'}/pbfilter/$ddir-$dver.pbf" if (-f "$ENV{'PBROOTDIR'}/pbfilter/$ddir-$dver.pbf");
81
82    push @ffiles,$mfile00 if (defined $mfile00);
83    push @ffiles,$mfile0 if (defined $mfile0);
84    push @ffiles,$mfile1 if (defined $mfile1);
85    push @ffiles,$mfile2 if (defined $mfile2);
86    push @ffiles,$mfile3 if (defined $mfile3);
87}
88
89if (-d "$ENV{'PBROOTDIR'}/$pbpkg/pbfilter") {
90    $ffile00 = "$ENV{'PBROOTDIR'}/$pbpkg/pbfilter/all.pbf" if (-f "$ENV{'PBROOTDIR'}/$pbpkg/pbfilter/all.pbf");
91    $ffile0 = "$ENV{'PBROOTDIR'}/$pbpkg/pbfilter/$dtype.pbf" if (-f "$ENV{'PBROOTDIR'}/$pbpkg/pbfilter/$dtype.pbf");
92    $ffile1 = "$ENV{'PBROOTDIR'}/$pbpkg/pbfilter/$dfam.pbf" if (-f "$ENV{'PBROOTDIR'}/$pbpkg/pbfilter/$dfam.pbf");
93    $ffile2 = "$ENV{'PBROOTDIR'}/$pbpkg/pbfilter/$ddir.pbf" if (-f "$ENV{'PBROOTDIR'}/$pbpkg/pbfilter/$ddir.pbf");
94    $ffile3 = "$ENV{'PBROOTDIR'}/$pbpkg/pbfilter/$ddir-$dver.pbf" if (-f "$ENV{'PBROOTDIR'}/$pbpkg/pbfilter/$ddir-$dver.pbf");
95
96    push @ffiles,$ffile00 if (defined $ffile00);
97    push @ffiles,$ffile0 if (defined $ffile0);
98    push @ffiles,$ffile1 if (defined $ffile1);
99    push @ffiles,$ffile2 if (defined $ffile2);
100    push @ffiles,$ffile3 if (defined $ffile3);
101}
102if (@ffiles) {
103    pb_log(2,"DEBUG ffiles: ".Dumper(\@ffiles)."\n");
104
105    foreach my $f (@ffiles) {
106        open(CONF,$f) || next;
107        while(<CONF>)  {
108            if (/^\s*([A-z0-9-_]+)\s+([[A-z0-9-_]+)\s*=\s*(.+)$/) {
109                $h{$1}{$2}=$3;
110            }
111        }
112        close(CONF);
113
114        $ptr = $h{"filter"};
115        pb_log(2,"DEBUG f:".Dumper($ptr)."\n");
116    }
117}
118return($ptr);
119}
120
121=item B<pb_filter_file_pb>
122
123This function applies all filters to pb build files.
124
125It takes 15 parameters. To be filtered a variable has to be passed to that function.
126
127The first parameter is the file to filter.
128The second parameter is the pointer on the hash of filters.
129The third parameter is the destination file after filtering.
130The fourth parameter is the distribution type.
131The fifth parameter is the suffix of the distribution.
132The sixth parameter is the package name.
133The seventh parameter is the version of the package.
134The eighth parameter is the tag of the package.
135The nineth parameter is the revision of the package.
136The tenth parameter is the current date.
137The eleventh parameter is the list of required packages.
138The twelveth parameter is the list of optional packages.
139The thirteenth parameter is the packager name.
140The fourteenth parameter is the changelog.
141The fifteenth parameter is the project.
142The sixteenth parameter is the repo.
143
144=cut
145
146sub pb_filter_file_pb {
147
148my $f=shift;
149my $ptr=shift;
150my %filter=%$ptr;
151my $destfile=shift;
152my $dtype=shift;
153my $pbsuf=shift;
154my $pbpkg=shift;
155my $pbver=shift;
156my $pbtag=shift;
157my $pbrev=shift;
158my $pbdate=shift;
159my $defpkgdir = shift;
160my $extpkgdir = shift;
161my $pbpackager = shift;
162my $chglog = shift || undef;
163my $pbproj = shift;
164my $pbrepo = shift;
165
166pb_log(2,"DEBUG: From $f to $destfile\n");
167pb_mkdir_p(dirname($destfile)) if (! -d dirname($destfile));
168open(DEST,"> $destfile") || die "Unable to create $destfile";
169open(FILE,"$f") || die "Unable to open $f: $!";
170while (<FILE>) {
171    my $line = $_;
172    foreach my $s (keys %filter) {
173        # Process single variables
174        pb_log(2,"DEBUG filter{$s}: $filter{$s}\n");
175        my $tmp = $filter{$s};
176        next if (not defined $tmp);
177        # Expand variables if any single one found
178        pb_log(2,"DEBUG tmp: $tmp\n");
179        if ($tmp =~ /\$/) {
180            eval { $tmp =~ s/(\$\w+)/$1/eeg };
181        # special case for ChangeLog only for pb
182        } elsif (($s =~ /^PBLOG$/) && ($line =~ /^PBLOG$/)) {
183            my $p = $defpkgdir->{$pbpkg};
184            $p = $extpkgdir->{$pbpkg} if (not defined $p);
185            pb_changelog($dtype, $pbpkg, $pbver, $pbtag, $pbsuf, $p, \*DEST, $tmp, $chglog);
186            $tmp = "";
187        }
188        $line =~ s|$s|$tmp|;
189    }
190    print DEST $line;
191}
192close(FILE);
193close(DEST);
194}
195
196=item B<pb_filter_file_inplace>
197
198This function applies all filters to a file in place.
199
200It takes 9 parameters.
201
202The first parameter is the pointer on the hash of filters.
203The second parameter is the destination file after filtering.
204The third parameter is the package name.
205The fourth parameter is the version of the package.
206The fifth parameter is the tag of the package.
207The sixth parameter is the revision of the package.
208The seventh parameter is the current date.
209The eighth parameter is the packager name.
210The nineth parameter is the project name.
211The tenth parameter is the repo.
212
213=cut
214
215# Function which applies filter on files (external call)
216sub pb_filter_file_inplace {
217
218my $ptr=shift;
219my %filter=%$ptr;
220my $destfile=shift;
221my $pbpkg=shift;
222my $pbver=shift;
223my $pbtag=shift;
224my $pbrev=shift;
225my $pbdate=shift;
226my $pbpackager=shift;
227my $pbproj=shift;
228my $pbrepo = shift;
229
230my $cp = "$ENV{'PBTMP'}/".basename($destfile);
231copy($destfile,$cp) || die "Unable to create $cp";
232
233pb_filter_file($cp,$ptr,$destfile,$pbpkg,$pbver,$pbtag,$pbrev,$pbdate,$pbpackager,$pbproj,$pbrepo);
234unlink $cp;
235}
236
237=item B<pb_filter_file>
238
239This function applies all filters on a file to generate a new filtered one.
240
241It takes 10 parameters. To be filtered a variable has to be passed to that function.
242
243The first parameter is the original file to filter.
244The second parameter is the pointer on the hash of filters.
245The third parameter is the destination file after filtering.
246The fourth parameter is the package name.
247The fifth parameter is the version of the package.
248The sixth parameter is the tag of the package.
249The seventh parameter is the revision of the package.
250The eighth parameter is the current date.
251The nineth parameter is the packager name.
252The tenth parameter is the project name.
253The eleventh parameter is the repo.
254
255=cut
256
257
258# Function which applies filter on files (external call)
259sub pb_filter_file {
260
261my $f=shift;
262my $ptr=shift;
263my %filter=%$ptr;
264my $destfile=shift;
265my $pbpkg=shift;
266my $pbver=shift;
267my $pbtag=shift;
268my $pbrev=shift;
269my $pbdate=shift;
270my $pbpackager=shift;
271my $pbproj=shift;
272my $pbrepo = shift;
273
274pb_log(2,"DEBUG: From $f to $destfile\n");
275pb_mkdir_p(dirname($destfile)) if (! -d dirname($destfile));
276open(DEST,"> $destfile") || die "Unable to create $destfile";
277open(FILE,"$f") || die "Unable to open $f: $!";
278while (<FILE>) {
279    my $line = $_;
280    foreach my $s (keys %filter) {
281        # Process single variables
282        pb_log(2,"DEBUG filter{$s}: $filter{$s}\n");
283        my $tmp = $filter{$s};
284        next if (not defined $tmp);
285        # Expand variables if any single one found
286        if ($tmp =~ /\$/) {
287            eval { $tmp =~ s/(\$\w+)/$1/eeg };
288        }
289        $line =~ s|$s|$tmp|;
290    }
291    print DEST $line;
292}
293close(FILE);
294close(DEST);
295}
296
297=back
298
299=head1 WEB SITES
300
301The 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/>.
302
303=head1 USER MAILING LIST
304
305None exists for the moment.
306
307=head1 AUTHORS
308
309The Project-Builder.org team L<http://trac.project-builder.org/> lead by Bruno Cornec L<mailto:bruno@project-builder.org>.
310
311=head1 COPYRIGHT
312
313Project-Builder.org is distributed under the GPL v2.0 license
314described in the file C<COPYING> included with the distribution.
315
316=cut
317
3181;
Note: See TracBrowser for help on using the repository browser.