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

Last change on this file since 1183 was 1183, checked in by Bruno Cornec, 13 years ago
  • Allow pb_filter_file to manage undefined filter hash
File size: 8.2 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);
[1148]20use ProjectBuilder::Version;
[318]21use ProjectBuilder::Base;
[405]22use ProjectBuilder::Changelog;
[5]23
[405]24# Inherit from the "Exporter" module which handles exporting functions.
25
[1156]26use vars qw($VERSION $REVISION @ISA @EXPORT);
[405]27use Exporter;
28
29# Export, by default, all the functions into the namespace of
30# any code which uses this module.
31
32our @ISA = qw(Exporter);
33our @EXPORT = qw(pb_get_filters pb_filter_file_pb pb_filter_file_inplace pb_filter_file);
[1156]34($VERSION,$REVISION) = pb_version_init();
[5]35
[331]36=pod
37
38=head1 NAME
39
[409]40ProjectBuilder::Filter, part of the project-builder.org
[331]41
42=head1 DESCRIPTION
43
[409]44This module provides filtering functions suitable for pbinit calls.
[331]45
[427]46=over 4
47
[409]48=item B<pb_get_filters>
49
50This function gets all filters to apply. They're cumulative from the less specific to the most specific.
51
52Suffix 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.
53
54The first parameter is the package name.
[1177]55The second parameter is OS hash
[409]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";
[1177]67my $pbos = shift;
[395]68my $ptr = undef; # returned value pointer on the hash of filters
69my %h;
70
71# Global filter files first, then package specificities
72if (-d "$ENV{'PBROOTDIR'}/pbfilter") {
73 $mfile00 = "$ENV{'PBROOTDIR'}/pbfilter/all.pbf" if (-f "$ENV{'PBROOTDIR'}/pbfilter/all.pbf");
[1177]74 if (defined $pbos) {
75 $mfile0 = "$ENV{'PBROOTDIR'}/pbfilter/$pbos->{'type'}.pbf" if ((defined $pbos->{'type'}) && (-f "$ENV{'PBROOTDIR'}/pbfilter/$pbos->{'type'}.pbf"));
76 $mfile1 = "$ENV{'PBROOTDIR'}/pbfilter/$pbos->{'family'}.pbf" if ((defined $pbos->{'type'}) && (-f "$ENV{'PBROOTDIR'}/pbfilter/$pbos->{'family'}.pbf"));
77 $mfile2 = "$ENV{'PBROOTDIR'}/pbfilter/$pbos->{'name'}.pbf" if ((defined $pbos->{'type'}) && (-f "$ENV{'PBROOTDIR'}/pbfilter/$pbos->{'name'}.pbf"));
78 $mfile3 = "$ENV{'PBROOTDIR'}/pbfilter/$pbos->{'name'}-$pbos->{'version'}.pbf" if ((defined $pbos->{'type'}) && (-f "$ENV{'PBROOTDIR'}/pbfilter/$pbos->{'name'}-$pbos->{'version'}.pbf"));
79 }
[395]80
81 push @ffiles,$mfile00 if (defined $mfile00);
82 push @ffiles,$mfile0 if (defined $mfile0);
83 push @ffiles,$mfile1 if (defined $mfile1);
84 push @ffiles,$mfile2 if (defined $mfile2);
85 push @ffiles,$mfile3 if (defined $mfile3);
86}
87
88if (-d "$ENV{'PBROOTDIR'}/$pbpkg/pbfilter") {
89 $ffile00 = "$ENV{'PBROOTDIR'}/$pbpkg/pbfilter/all.pbf" if (-f "$ENV{'PBROOTDIR'}/$pbpkg/pbfilter/all.pbf");
[1177]90 if (defined $pbos) {
91 $ffile0 = "$ENV{'PBROOTDIR'}/$pbpkg/pbfilter/$pbos->{'type'}.pbf" if ((defined $pbos->{'type'}) && (-f "$ENV{'PBROOTDIR'}/$pbpkg/pbfilter/$pbos->{'type'}.pbf"));
92 $ffile1 = "$ENV{'PBROOTDIR'}/$pbpkg/pbfilter/$pbos->{'family'}.pbf" if ((defined $pbos->{'type'}) && (-f "$ENV{'PBROOTDIR'}/$pbpkg/pbfilter/$pbos->{'family'}.pbf"));
93 $ffile2 = "$ENV{'PBROOTDIR'}/$pbpkg/pbfilter/$pbos->{'name'}.pbf" if ((defined $pbos->{'type'}) && (-f "$ENV{'PBROOTDIR'}/$pbpkg/pbfilter/$pbos->{'name'}.pbf"));
94 $ffile3 = "$ENV{'PBROOTDIR'}/$pbpkg/pbfilter/$pbos->{'name'}-$pbos->{'version'}.pbf" if ((defined $pbos->{'type'}) && (-f "$ENV{'PBROOTDIR'}/$pbpkg/pbfilter/$pbos->{'name'}-$pbos->{'version'}.pbf"));
95 }
[395]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.
[1183]128The second parameter is the pointer on the hash of filters. If undefined no filtering will occur.
[409]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;
[1183]138my %filter;
139if (defined $ptr) {
140 %filter=%$ptr;
141} else {
142 %filter = ();
143}
[395]144my $destfile=shift;
[499]145my $pb=shift;
[395]146
147pb_log(2,"DEBUG: From $f to $destfile\n");
148pb_mkdir_p(dirname($destfile)) if (! -d dirname($destfile));
[473]149open(DEST,"> $destfile") || die "Unable to create $destfile: $!";
[395]150open(FILE,"$f") || die "Unable to open $f: $!";
151while (<FILE>) {
152 my $line = $_;
153 foreach my $s (keys %filter) {
154 # Process single variables
155 my $tmp = $filter{$s};
156 next if (not defined $tmp);
[500]157 pb_log(3,"DEBUG filter{$s}: $filter{$s}\n");
[395]158 # Expand variables if any single one found
159 if ($tmp =~ /\$/) {
[1044]160 pb_log(3,"*** Filtering variable in $tmp ***\n");
[500]161 # Order is important as we need to handle hashes refs before simple vars
162 eval { $tmp =~ s/(\$\w+-\>\{\'\w+\'\})/$1/eeg };
[395]163 eval { $tmp =~ s/(\$\w+)/$1/eeg };
[916]164 if (($s =~ /^PBDESC$/) && ($line =~ /^ PBDESC/)) {
165 # if on debian, we need to preserve the space before each desc line
166 pb_log(3,"*** DEBIAN CASE ADDING SPACE ***\n");
167 $tmp =~ s/\$\//\$\/ /g;
168 pb_log(3,"*** tmp:$tmp ***\n");
169 }
[462]170 eval { $tmp =~ s/(\$\/)/$1/eeg };
[395]171 } elsif (($s =~ /^PBLOG$/) && ($line =~ /^PBLOG$/)) {
[916]172 # special case for ChangeLog only for pb
[500]173 pb_log(3,"DEBUG filtering PBLOG\n");
[585]174 pb_changelog($pb, \*DEST, $tmp);
[395]175 $tmp = "";
[499]176 } elsif (($s =~ /^PBPATCHSRC$/) && ($line =~ /^PBPATCHSRC$/)) {
[500]177 pb_log(3,"DEBUG filtering PBPATCHSRC\n");
[499]178 my $i = 0;
[1180]179 if (defined $pb->{'patches'}->{$pb->{'tuple'}}) {
180 foreach my $p (split(/,/,$pb->{'patches'}->{$pb->{'tuple'}})) {
181 print DEST "Patch$i: ".basename($p).".gz\n";
182 $i++;
183 }
[499]184 }
185 $tmp = "";
[1130]186 } elsif (($s =~ /^PBMULTISRC$/) && ($line =~ /^PBMULTISRC$/)) {
187 pb_log(3,"DEBUG filtering PBMULTISRC\n");
188 my $i = 1;
[1180]189 if (defined $pb->{'patches'}->{$pb->{'tuple'}}) {
190 foreach my $p (split(/,/,$pb->{'sources'}->{$pb->{'tuple'}})) {
191 print DEST "Source$i: ".basename($p)."\n";
192 $i++;
193 }
[1130]194 }
195 $tmp = "";
[499]196 } elsif (($s =~ /^PBPATCHCMD$/) && ($line =~ /^PBPATCHCMD$/)) {
[500]197 pb_log(3,"DEBUG filtering PBPATCHCMD\n");
[499]198 my $i = 0;
[1180]199 if (defined $pb->{'patches'}->{$pb->{'tuple'}}) {
200 foreach my $p (split(/,/,$pb->{'patches'}->{$pb->{'tuple'}})) {
201 print DEST "%patch$i -p1\n";
202 $i++;
203 }
[499]204 }
205 print DEST "\n";
206 $tmp = "";
[395]207 }
[475]208 $line =~ s|$s|$tmp|g;
[395]209 }
210 print DEST $line;
211}
212close(FILE);
213close(DEST);
214}
215
[409]216=item B<pb_filter_file_inplace>
217
218This function applies all filters to a file in place.
219
[499]220It takes 3 parameters.
[409]221
222The first parameter is the pointer on the hash of filters.
223The second parameter is the destination file after filtering.
[499]224The third parameter is the pointer on the hash of variables to filter (tag, ver, ...)
[409]225
226=cut
227
[395]228# Function which applies filter on files (external call)
229sub pb_filter_file_inplace {
230
231my $ptr=shift;
232my $destfile=shift;
[499]233my $pb=shift;
[395]234
235my $cp = "$ENV{'PBTMP'}/".basename($destfile);
[500]236copy($destfile,$cp) || die "Unable to copy $destfile to $cp";
[395]237
[499]238pb_filter_file($cp,$ptr,$destfile,$pb);
[395]239unlink $cp;
240}
241
[409]242
243=back
244
245=head1 WEB SITES
246
247The 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/>.
248
249=head1 USER MAILING LIST
250
251None exists for the moment.
252
253=head1 AUTHORS
254
255The Project-Builder.org team L<http://trac.project-builder.org/> lead by Bruno Cornec L<mailto:bruno@project-builder.org>.
256
257=head1 COPYRIGHT
258
259Project-Builder.org is distributed under the GPL v2.0 license
260described in the file C<COPYING> included with the distribution.
261
262=cut
263
[395]2641;
Note: See TracBrowser for help on using the repository browser.