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

Last change on this file since 405 was 405, checked in by Bruno Cornec, 16 years ago

Split again function in modules to allow for usage with pbinit and easier reuse.

  • Property svn:executable set to *
File size: 6.3 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::Base, part of the project-builder.org - module dealing with generic functions suitable for perl project development
38
39=head1 DESCRIPTION
40
41pb helps you build various packages directly from your project sources.
42Those sources could be handled by a CMS (Configuration Management System)
43such as Subversion, CVS, ... or being a simple reference to a compressed tar file.
44It's based on a set of configuration files, a set of provided macros to help
45you keeping build files as generic as possible. For example, a single .spec
46file should be required to generate for all rpm based distributions, even
47if you could also have multiple .spec files if required.
48
49=cut
50
51# Get all filters to apply
52# They're cumulative from less specific to most specific
53# suffix is .pbf
54
55sub pb_get_filters {
56
57my @ffiles;
58my ($ffile00, $ffile0, $ffile1, $ffile2, $ffile3);
59my ($mfile00, $mfile0, $mfile1, $mfile2, $mfile3);
60my $pbpkg = shift || die "No package specified";
61my $dtype = shift || "";
62my $dfam = shift || "";
63my $ddir = shift || "";
64my $dver = shift || "";
65my $ptr = undef; # returned value pointer on the hash of filters
66my %h;
67
68# Global filter files first, then package specificities
69if (-d "$ENV{'PBROOTDIR'}/pbfilter") {
70 $mfile00 = "$ENV{'PBROOTDIR'}/pbfilter/all.pbf" if (-f "$ENV{'PBROOTDIR'}/pbfilter/all.pbf");
71 $mfile0 = "$ENV{'PBROOTDIR'}/pbfilter/$dtype.pbf" if (-f "$ENV{'PBROOTDIR'}/pbfilter/$dtype.pbf");
72 $mfile1 = "$ENV{'PBROOTDIR'}/pbfilter/$dfam.pbf" if (-f "$ENV{'PBROOTDIR'}/pbfilter/$dfam.pbf");
73 $mfile2 = "$ENV{'PBROOTDIR'}/pbfilter/$ddir.pbf" if (-f "$ENV{'PBROOTDIR'}/pbfilter/$ddir.pbf");
74 $mfile3 = "$ENV{'PBROOTDIR'}/pbfilter/$ddir-$dver.pbf" if (-f "$ENV{'PBROOTDIR'}/pbfilter/$ddir-$dver.pbf");
75
76 push @ffiles,$mfile00 if (defined $mfile00);
77 push @ffiles,$mfile0 if (defined $mfile0);
78 push @ffiles,$mfile1 if (defined $mfile1);
79 push @ffiles,$mfile2 if (defined $mfile2);
80 push @ffiles,$mfile3 if (defined $mfile3);
81}
82
83if (-d "$ENV{'PBROOTDIR'}/$pbpkg/pbfilter") {
84 $ffile00 = "$ENV{'PBROOTDIR'}/$pbpkg/pbfilter/all.pbf" if (-f "$ENV{'PBROOTDIR'}/$pbpkg/pbfilter/all.pbf");
85 $ffile0 = "$ENV{'PBROOTDIR'}/$pbpkg/pbfilter/$dtype.pbf" if (-f "$ENV{'PBROOTDIR'}/$pbpkg/pbfilter/$dtype.pbf");
86 $ffile1 = "$ENV{'PBROOTDIR'}/$pbpkg/pbfilter/$dfam.pbf" if (-f "$ENV{'PBROOTDIR'}/$pbpkg/pbfilter/$dfam.pbf");
87 $ffile2 = "$ENV{'PBROOTDIR'}/$pbpkg/pbfilter/$ddir.pbf" if (-f "$ENV{'PBROOTDIR'}/$pbpkg/pbfilter/$ddir.pbf");
88 $ffile3 = "$ENV{'PBROOTDIR'}/$pbpkg/pbfilter/$ddir-$dver.pbf" if (-f "$ENV{'PBROOTDIR'}/$pbpkg/pbfilter/$ddir-$dver.pbf");
89
90 push @ffiles,$ffile00 if (defined $ffile00);
91 push @ffiles,$ffile0 if (defined $ffile0);
92 push @ffiles,$ffile1 if (defined $ffile1);
93 push @ffiles,$ffile2 if (defined $ffile2);
94 push @ffiles,$ffile3 if (defined $ffile3);
95}
96if (@ffiles) {
97 pb_log(2,"DEBUG ffiles: ".Dumper(\@ffiles)."\n");
98
99 foreach my $f (@ffiles) {
100 open(CONF,$f) || next;
101 while(<CONF>) {
102 if (/^\s*([A-z0-9-_]+)\s+([[A-z0-9-_]+)\s*=\s*(.+)$/) {
103 $h{$1}{$2}=$3;
104 }
105 }
106 close(CONF);
107
108 $ptr = $h{"filter"};
109 pb_log(2,"DEBUG f:".Dumper($ptr)."\n");
110 }
111}
112return($ptr);
113}
114
115# Function which applies filter on pb build files
116sub pb_filter_file_pb {
117
118my $f=shift;
119my $ptr=shift;
120my %filter=%$ptr;
121my $destfile=shift;
122my $dtype=shift;
123my $pbsuf=shift;
124my $pbproj=shift;
125my $pbpkg=shift;
126my $pbver=shift;
127my $pbtag=shift;
128my $pbrev=shift;
129my $pbdate=shift;
130my $defpkgdir = shift;
131my $extpkgdir = shift;
132my $pbpackager = shift;
133my $chglog = shift || undef;
134
135pb_log(2,"DEBUG: From $f to $destfile\n");
136pb_mkdir_p(dirname($destfile)) if (! -d dirname($destfile));
137open(DEST,"> $destfile") || die "Unable to create $destfile";
138open(FILE,"$f") || die "Unable to open $f: $!";
139while (<FILE>) {
140 my $line = $_;
141 foreach my $s (keys %filter) {
142 # Process single variables
143 pb_log(2,"DEBUG filter{$s}: $filter{$s}\n");
144 my $tmp = $filter{$s};
145 next if (not defined $tmp);
146 # Expand variables if any single one found
147 pb_log(2,"DEBUG tmp: $tmp\n");
148 if ($tmp =~ /\$/) {
149 eval { $tmp =~ s/(\$\w+)/$1/eeg };
150 # special case for ChangeLog only for pb
151 } elsif (($s =~ /^PBLOG$/) && ($line =~ /^PBLOG$/)) {
152 my $p = $defpkgdir->{$pbpkg};
153 $p = $extpkgdir->{$pbpkg} if (not defined $p);
154 pb_changelog($dtype, $pbpkg, $pbver, $pbtag, $pbsuf, $p, \*DEST, $tmp, $chglog);
155 $tmp = "";
156 }
157 $line =~ s|$s|$tmp|;
158 }
159 print DEST $line;
160}
161close(FILE);
162close(DEST);
163}
164
165# Function which applies filter on files (external call)
166sub pb_filter_file_inplace {
167
168my $ptr=shift;
169my %filter=%$ptr;
170my $destfile=shift;
171my $pbproj=shift;
172my $pbpkg=shift;
173my $pbver=shift;
174my $pbtag=shift;
175my $pbrev=shift;
176my $pbdate=shift;
177my $pbpackager=shift;
178
179my $cp = "$ENV{'PBTMP'}/".basename($destfile);
180copy($destfile,$cp) || die "Unable to create $cp";
181
182pb_filter_file($cp,$ptr,$destfile,$pbproj,$pbpkg,$pbver,$pbtag,$pbrev,$pbdate,$pbpackager);
183unlink $cp;
184}
185
186# Function which applies filter on files (external call)
187sub pb_filter_file {
188
189my $f=shift;
190my $ptr=shift;
191my %filter=%$ptr;
192my $destfile=shift;
193my $pbproj=shift;
194my $pbpkg=shift;
195my $pbver=shift;
196my $pbtag=shift;
197my $pbrev=shift;
198my $pbdate=shift;
199my $pbpackager=shift;
200
201pb_log(2,"DEBUG: From $f to $destfile\n");
202pb_mkdir_p(dirname($destfile)) if (! -d dirname($destfile));
203open(DEST,"> $destfile") || die "Unable to create $destfile";
204open(FILE,"$f") || die "Unable to open $f: $!";
205while (<FILE>) {
206 my $line = $_;
207 foreach my $s (keys %filter) {
208 # Process single variables
209 pb_log(2,"DEBUG filter{$s}: $filter{$s}\n");
210 my $tmp = $filter{$s};
211 next if (not defined $tmp);
212 # Expand variables if any single one found
213 if ($tmp =~ /\$/) {
214 eval { $tmp =~ s/(\$\w+)/$1/eeg };
215 }
216 $line =~ s|$s|$tmp|;
217 }
218 print DEST $line;
219}
220close(FILE);
221close(DEST);
222}
223
2241;
Note: See TracBrowser for help on using the repository browser.