CfgTie/Cfgfile.pm

filter: Statementstmt 45.8% 50 / 109 Branchbran 15.2% 7 / 46 Conditioncond 14.8% 4 / 27 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 100% 9 / 9 Podpod n/a totaltotal 33.8% 70 / 207 CC 36 SCAR 56.3
CC
36
Cov
34%
END
488.3
TIEHASH
41.3
BEGIN
1.0
CRAP 278.7
↑ Summary CfgTie/TieRCService.pm »
1 #!/usr/bin/perl -w
2 #Copyright 1998-2001, Randall Maas. All rights reserved. This program is free
3 #software; you can redistribute it and/or modify it under the same terms as
4 #PERL itself.
5  
6 package CfgTie::Cfgfile;
7 1 ▶use strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use CfgTie::filever;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Secure::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use vars qw($VERSION @ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use AutoLoader 'AUTOLOAD';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 @ISA=qw(AutoLoader);
13 1 $VERSION='0.41';
14  
15 =head1 NAME
16  
17 C<CfgTie::Cfgfile> -- An object to help interface to configuration files
18  
19 =head1 SYNOPSIS
20  
21 Helps interface to the text based configuration files commonly used in Unix
22  
23 =head1 DESCRIPTION
24  
25 This is a fairly generic interface to allow many configuration files to be
26 used as hash ties. I<Note: This is not intended to be called by user programs,
27 but by modules wishing to reuse a template structure!>
28  
29 package mytie;
30 require CfgTie::Cfgfile;
31  
32 @ISA = qw(CfgTie::Cfgfile);
33  
34  
35 The major methods:
36  
37 C<new(>I<$filepath>, I<@Extra stuff>C<)>
38  
39 or
40  
41 C<new(>I<$RCS-Object>, I<@Extra stuff>C<)>
42  
43 =over 1
44  
45 =item filepath
46  
47 If defined, this is the path to the configuration file to be opened.
48  
49 =item RCS-Object
50  
51 If defined, this is an RCS object (or similar) that will control how to check
52 in and check out the configuration file. See RCS Perl module for more details
53 on how to create this object (do not use the same instance for each
54 configuration file!). Files will be checked back in when the C<END> routine is
55 called. If a C<filepath> is specified, it will override the one previously
56 set with the RCS object. If no C<filepath> is specified, but the RCS has one
57 specified, it will be used instead.
58  
59 =back
60  
61 =head2 The derived object
62  
63 Your derived object may need to provide the following methods:
64  
65 =over 1
66  
67 =item C<scan>
68  
69 This is called when the file is first tied in. It should scan the file,
70 placing the associated contents into the C<{Contents}> key.
71  
72 =item C<format>(I<key>,I<contents>)
73  
74 This formats a single entry to be stored in a file. This is used for when a
75 value can be stored simply.
76  
77 =item C<cfg_begin>
78  
79 If this method is defined, it is called just before the configuration file
80 will be modified.
81  
82 =item C<cfg_end>
83  
84 If this method is defined, it is called after the configuration file has
85 changed. It can be used, for instances, to rebuild a binary database, restart
86 a service, or email Martians.
87  
88 =item C<makerewrites>
89  
90 This is called just before the configuration file will be rewritten. It
91 should return a reference to a function that is used to transform the current
92 control file into the new one. This transforming function is called
93 for each line in the configuration file while it is being rewritten.
94  
95 =back
96  
97 =head2 Object methods you can use
98  
99 C<Comment_Add> This appends the string to the list of comments that will be
100 logged for this revision (assuming that a Revision Control object was used).
101  
102 C<Queue_Store($key,$val)> This queues (for later) the transaction that
103 I<key> should be associated with I<val>. The queue is employed to synchronize
104 all of the settings with the stored settings on disk.
105  
106 C<Queue_Delete($key)> This queues (for later) the transaction that any value
107 associated with I<key> should be removed.
108  
109 C<RENAME(\%rules)> This method will move through the whole table and make a
110 series of changes. It may:
111  
112 =over 1
113  
114 =item Remove some entries, based upon their keys
115  
116 =item Rename the keys from some entries
117  
118 =item Change the contents of the keys, possibly removing portions
119  
120 =back
121  
122 C<\%rules> is the set of rules that governs will be changed in name and what
123 will be removed. It is an associative array (hash) of a form like:
124  
125 {
126 PATTERN1 => "",
127 PATTERN2 => REWRITE,
128 }
129  
130  
131 C<PATTERN1 = >
132 Two things will happen with a rule like this:
133  
134 =over 1
135  
136 =item Every key in the table that matches the pattern will be removed
137  
138 =item Any element (of an entry) that matches the pattern will be removed
139  
140 =back
141  
142 C<PATTERN2 = REWRITE>
143 In this case the rewrite indicates what should replace the pattern:
144  
145 =over 1
146  
147 =item *
148  
149 Every key that matches the pattern will be rewritten and replace with a
150 new key.
151  
152 =item *
153  
154 Any element (of an entry) that matches the pattern will be modified to
155 match the rewrite rule.
156  
157 =back
158  
159  
160 =head1 See Also
161  
162 L<CfgTie::TieAliases>, L<CfgTie::TieGeneric>, L<CfgTie::TieGroup>
163 L<CfgTie::TieHost>, L<CfgTie::TieMTab>, L<CfgTie::TieNamed>,
164 L<CfgTie::TieNet>, L<CfgTie::TiePh>, L<CfgTie::TieProto>,
165 L<CfgTie::TieRCService>, L<CfgTie::TieRsrc>, L<CfgTie::TieServ>,
166 L<CfgTie::TieShadow>, L<CfgTie::TieUser>
167  
168  
169 =head1 Caveats
170  
171 Additions that do not change any previously established values are reflected
172 immediately (and F<newaliases> is run as appropriate). Anything which changes
173 a previously established value, regardless of whether or not it has been
174 committed, are queue'd for later. This queue is used to rewrite the file when
175 C<END> is executed.
176  
177 =head1 Author
178  
179 Randall Maas (L<mailto:randym@acm.org>, L<http://www.hamline.edu/~rcmaas/>)
180  
181 =cut
182  
183 1 my $FNum=137;
184 1 my %Files2CheckIn;
185 1 local @Thingies=();
186 1 1;
187  
188 END
189 1 ▶{
Subroutine END: called CC=24 SCAR=61.9
190 #This takes the current set of outstanding ties and rewrites the files
191 1 foreach my $I (@Thingies)
192 {
193 #Skip it if there is nothing to do.
194 4 ▶ if (!exists $I->{Queue} ||
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $I->{"Queue"} and scalar keys %{$$I{"Queue"};}) 4 0 4
Condition33%1/3
exists $I->{"Queue"} and scalar keys %{$$I{"Queue"};}
!ll&&!rl&&r
400
MC/DC0%0/2
exists $I->{"Queue"} and scalar keys %{$$I{"Queue"};}
exists $I->{"Queue"} scalar keys %{$$I{"Queue"};}
Truth table33%1/3
A and B
A= exists $I->{"Queue"}B= scalar keys %{$$I{"Queue"};}
ABresult
0X0
100
111
195 0 !scalar keys %{$I->{Queue}}) {next;}
196  
197 0 my $rewrite;
198 #Build up rewrite rules:
199 0 eval '$rewrite = $I->makerewrites();';
200 0 ▶ if ($@)
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
201 {
202 #There was an error with the rewrite thing, skip this
203 0 warn $@;
204 0 next;
205 }
206  
207 0 ▶ if (!defined $I->{Path}) {next;}
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $I->{"Path"}) 0 0 0
208 0 ▶ if (!scalar keys %{$I->{Contents}}) {next;}
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar keys %{$$I{"Contents"};}) 0 0 0
209 0 ▶ if (-e $I->{Path} && !defined $I->{Queue}) {next;}
Branch: 1 branch
BranchTrueFalseTotal
if (-e $I->{"Path"} and not defined $I->{"Queue"}) 0 0 0
Condition0%0/3
-e $I->{"Path"} and not defined $I->{"Queue"}
!ll&&!rl&&r
000
MC/DC0%0/2
-e $I->{"Path"} and not defined $I->{"Queue"}
-e $I->{"Path"} not defined $I->{"Queue"}
Truth table0%0/3
A and not(B)
A= -e $I->{"Path"}B= not defined $I->{"Queue"}
ABresult
0X0
100
111
210  
211 #Announce that we will begin making changes
212 0 eval '$I->cfg_begin();';
213  
214 #Check with the revision control system
215 0 &RCS_Handshake($I);
216  
217 0 my($Base,$NewFileName);
218 0 ▶ if ($I->{Path}=~/^([^\s]+)/ && $1 && length $1)
Branch: 1 branch
BranchTrueFalseTotal
if ($I->{'Path'} =~ /^([^\s]+)/ and $1 and length $1) { } 0 0 0
Condition0%0/6
$I->{"Path"} =~ /^([^\s]+)/ and $1 and length $1
!ll&&!rl&&r
000
$I->{"Path"} =~ /^([^\s]+)/ and $1
!ll&&!rl&&r
000
MC/DC0%0/3
$I->{"Path"} =~ /^([^\s]+)/ and $1 and length $1
$I->{"Path"} =~ /^([^\s]+)/ $1 length $1
Truth table0%0/4
A and B and C
A= $I->{"Path"} =~ /^([^\s]+)/B= $1C= length $1
ABCresult
0XX0
10X0
1100
1111
219 0 {$Base=$1; $NewFileName = "$1,new";}
220 else
221 0 {next;}
222  
223 0 ▶ if (-e $I->{Path})
Branch: 1 branch
BranchTrueFalseTotal
if (-e $I->{'Path'}) { } 0 0 0
224 {
225 #Next rewrite file...
226 # Do the file rewrite
227 0 ▶ my $FIn = new Secure::File "<$Base" or die "Could not open file ($Base) for reading: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
my $FIn = "Secure::File"->new("<$Base") or die "Could not open file ($Base) for reading: $!\n" 0 0 0
228 0 ▶ my $FOut = new Secure::File ">$NewFileName" or die "Could not open the temporary output file ($NewFileName): $!\n";
Branch: 1 branch
BranchTrueFalseTotal
my $FOut = "Secure::File"->new(">$NewFileName") or die "Could not open the temporary output file ($NewFileName): $!\n" 0 0 0
229 0 my $S=[];
230 0 ▶ while (<$FIn>)
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $FIn)) 0 0 0
231 {
232 0 my $L = &$rewrite($_,$S);
233 0 ▶ if (defined $L) {print $FOut $L;}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $L) 0 0 0
234 }
235 0 $FIn->close();
236 0 $FOut->close();
237 }
238 else
239 {
240 0 ▶ if (defined $I->{RCS})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $I->{"RCS"}) 0 0 0
241 0 {$Files2CheckIn{$Base} = $I->{RCS};}
242 0 ▶ my $FOut = new Secure::File ">$NewFileName" or die "Could not open the output file ($Base): $!\n";
Branch: 1 branch
BranchTrueFalseTotal
my $FOut = "Secure::File"->new(">$NewFileName") or die "Could not open the output file ($Base): $!\n" 0 0 0
243 0 foreach my $k (keys %{$I->{Contents}})
244 {
245 0 my $x=$I->{Contents}->{$k};
246 0 print $FOut eval '$J->format($k,$x)';
247 0 print $FOut "\n";
248 }
249 0 $FOut->close;
250 }
251 #Swap the file
252 0 ▶ if (-e $NewFileName)
Branch: 1 branch
BranchTrueFalseTotal
if (-e $NewFileName) 0 0 0
253 {
254 #Rotate the file in
255 0 &CfgTie::filever'Rotate($Base,$NewFileName);
256 }
257  
258 #Announce that we are done making changes
259 0 eval '$I->cfg_end();';
260 }
261  
262 #Check in each of the files that need to be
263 1 foreach my $I (keys %Files2CheckIn)
264 {
265 0 my $Obj =$Files2CheckIn{$I};
266 0 my $Cmt;
267 0 ▶ if (exists $Obj->{Comments})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Obj->{'Comments'}) { } 0 0 0
268 {
269 0 $Cmt = $Obj->{Comments};
270 0 $Cmt =~ s/\n/ /g;
271 }
272 else
273 0 {$Cmt="Settings changed through the PERL-based envmgr";}
274 0 my @Args = ('-u',"-m$Cmt");
275 #Check to see if we need an initial description.
276 0 ▶ if (!defined $Obj->comments) {@Args=(@Args, '-t-');}
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $Obj->comments) 0 0 0
277 # print "Checkin' in\n";
278 0 $Obj->ci(@Args);
279 }
280 1 %Files2CheckIn=();
281 }
282  
283 __END__
↑ Summary CfgTie/TieRCService.pm »