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

Last change on this file since 499 was 499, checked in by Bruno Cornec, 16 years ago
  • Modification of filter interface: use a single pb hash which contains the tag that will be handled during the filtering. Allow for easiest interface of functions, removal of a redundant function and evolution by simple addition of tags in pb.
  • Attempt to code patch management. Not tested yet.
File size: 6.8 KB
RevLine 
[5]1#!/usr/bin/perl -w
2#
[405]3# ProjectBuilder Filter module
4# Filtering subroutines brought by the the Project-Builder project
5# which can be easily used by pbinit
[5]6#
7# $Id$
8#
9# Copyright B. Cornec 2007
10# Provided under the GPL v2
11
[405]12package ProjectBuilder::Filter;
[9]13
[18]14use strict 'vars';
[9]15use Data::Dumper;
16use English;
[16]17use File::Basename;
[26]18use File::Copy;
[17]19use lib qw (lib);
[318]20use ProjectBuilder::Base;
[405]21use ProjectBuilder::Changelog;
[5]22
[405]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);
[5]32
[331]33=pod
34
35=head1 NAME
36
[409]37ProjectBuilder::Filter, part of the project-builder.org
[331]38
39=head1 DESCRIPTION
40
[409]41This module provides filtering functions suitable for pbinit calls.
[331]42
[427]43=over 4
44
[409]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
[331]59=cut
60
[395]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
[499]121=item B<pb_filter_file>
[409]122
[499]123This function applies all filters to files.
[409]124
[499]125It takes 4 parameters.
[409]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.
[499]130The fourth parameter is the pointer on the hash of variables to filter (tag, ver, ...)
[409]131
132=cut
133
[499]134sub pb_filter_file {
[395]135
136my $f=shift;
137my $ptr=shift;
138my %filter=%$ptr;
139my $destfile=shift;
[499]140my $pb=shift;
141my %pb = %$pb;
[395]142
143pb_log(2,"DEBUG: From $f to $destfile\n");
144pb_mkdir_p(dirname($destfile)) if (! -d dirname($destfile));
[473]145open(DEST,"> $destfile") || die "Unable to create $destfile: $!";
[395]146open(FILE,"$f") || die "Unable to open $f: $!";
147while (<FILE>) {
148 my $line = $_;
149 foreach my $s (keys %filter) {
150 # Process single variables
151 pb_log(2,"DEBUG filter{$s}: $filter{$s}\n");
152 my $tmp = $filter{$s};
153 next if (not defined $tmp);
154 # Expand variables if any single one found
155 pb_log(2,"DEBUG tmp: $tmp\n");
156 if ($tmp =~ /\$/) {
157 eval { $tmp =~ s/(\$\w+)/$1/eeg };
[462]158 eval { $tmp =~ s/(\$\/)/$1/eeg };
[395]159 # special case for ChangeLog only for pb
160 } elsif (($s =~ /^PBLOG$/) && ($line =~ /^PBLOG$/)) {
[499]161 my $p = $pb{'defpkgdir'}->{$pbpkg};
162 $p = $pb{'extpkgdir'}->{$pbpkg} if (not defined $p);
163 pb_changelog($pb{'dtype'}, $pb{'pkg'}, $pb{'ver'}, $pb{'tag'}, $pb{'suf'}, $p, \*DEST, $tmp, $pb{'chglog'});
[395]164 $tmp = "";
[499]165 } elsif (($s =~ /^PBPATCHSRC$/) && ($line =~ /^PBPATCHSRC$/)) {
166 my $i = 0;
167 foreach my $p (sort @$pb{'patches'}) {
168 print DEST "Patch$i: $p\n";
169 }
170 $tmp = "";
171 } elsif (($s =~ /^PBPATCHCMD$/) && ($line =~ /^PBPATCHCMD$/)) {
172 my $i = 0;
173 foreach my $p (sort @$pb{'patches'}) {
174 print DEST "%patch$i\n";
175 }
176 print DEST "\n";
177 $tmp = "";
[395]178 }
[475]179 $line =~ s|$s|$tmp|g;
[395]180 }
181 print DEST $line;
182}
183close(FILE);
184close(DEST);
185}
186
[409]187=item B<pb_filter_file_inplace>
188
189This function applies all filters to a file in place.
190
[499]191It takes 3 parameters.
[409]192
193The first parameter is the pointer on the hash of filters.
194The second parameter is the destination file after filtering.
[499]195The third parameter is the pointer on the hash of variables to filter (tag, ver, ...)
[409]196
197=cut
198
[395]199# Function which applies filter on files (external call)
200sub pb_filter_file_inplace {
201
202my $ptr=shift;
203my %filter=%$ptr;
204my $destfile=shift;
[499]205my $pb=shift;
206my %pb = %$pb;
[395]207
208my $cp = "$ENV{'PBTMP'}/".basename($destfile);
209copy($destfile,$cp) || die "Unable to create $cp";
210
[499]211pb_filter_file($cp,$ptr,$destfile,$pb);
[395]212unlink $cp;
213}
214
[409]215
216=back
217
218=head1 WEB SITES
219
220The 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/>.
221
222=head1 USER MAILING LIST
223
224None exists for the moment.
225
226=head1 AUTHORS
227
228The Project-Builder.org team L<http://trac.project-builder.org/> lead by Bruno Cornec L<mailto:bruno@project-builder.org>.
229
230=head1 COPYRIGHT
231
232Project-Builder.org is distributed under the GPL v2.0 license
233described in the file C<COPYING> included with the distribution.
234
235=cut
236
[395]2371;
Note: See TracBrowser for help on using the repository browser.