Modify.pm

filter: Statementstmt 57.8% 111 / 192 Branchbran 44.0% 22 / 50 Conditioncond 23.8% 5 / 21 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 52.7% 19 / 36 Podpod 100% 24 / 24 totaltotal 53.8% 181 / 336 CC 35 SCAR 20.8
CC
35
Cov
52%
_getBackupFilename
30.0
createBackup
20.0
process
11.8
CRAP 8.0
« Buffer.pm ↑ Summary
1 package Text::Modify;
2 #================================================================
3 # (C)2004-2005, lammel@cpan.org
4 #================================================================
5 # - Multiline replace is NOT supported currently
6 # - only simple regex and string replacement probably works the
7 # way it should
8 #================================================================
9
10 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use File::Temp qw(tempfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use File::Copy;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Text::Modify::Rule;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use Text::Buffer;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
16
17 3 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 $VERSION="0.5";
19 }
20
21 2 ▶sub new {
Subroutine new: called CC=9 SCAR=22.8
Pod documented
22 2 my $class = shift;
23 2 my %default = (
24 backup => 1, # The node id if available (used only for correlation with OMC db)
25 backupExt => '.bak', # The ip address of the core network interface
26 dryrun => 0,
27 writeto => undef, # Output file to use, by default a temp. file is created and input file is overwritten
28 _debug => 0
29 );
30 2 my $self = bless {%default}, $class;
31 # Processing of arguments, either ->new("filename")
32 # or ->new(file => "test.txt", writeto => "test.out")
33 2 my %opts;
34 2 ▶ if (scalar(@_) > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ > 1) { } 2 0 2
35 2 %opts = @_;
36 2 ▶ if ($opts{debug}) { $self->{_debug} = $opts{debug}; }
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"debug"}) 1 1 2
37 2 foreach (qw(file writeto dryrun backup backupExt)) {
38 10 ▶ if (exists($opts{$_})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{$_}) 8 2 10
39 8 ▶ $self->_debug("Setting option: $_ = " . (defined($opts{$_}) ? $opts{$_} : "undef"));
Branch: 1 branch
BranchTrueFalseTotal
defined $opts{$_} ? : 8 0 8
40 8 $self->{$_} = $opts{$_};
41 }
42 }
43 2 ▶ if ($self->{writeto}) { $self->{backup} = 0; }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"writeto"}) 2 0 2
44 }
45 0 else { $self->{file} = shift; }
46 0 ▶ if (!$self->{writeto} && $self->{file}) { $self->{writeto} = $self->{file}}
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"writeto"} and $self->{"file"}) 0 2 2
Condition33%1/3
not $self->{"writeto"} and $self->{"file"}
!ll&&!rl&&r
200
MC/DC0%0/2
not $self->{"writeto"} and $self->{"file"}
not $self->{"writeto"} $self->{"file"}
Truth table33%1/3
not(A) and B
A= not $self->{"writeto"}B= $self->{"file"}
ABresult
0X0
100
111
47 2 $self->_debug("Created object $class as $self (" . ref($self) . ")");
48 2 $self->_clearError();
49 2 $self->{ruleorder} = [];
50 2 $self->{blockorder} = [];
51 # Define the "ALL" block, which includes the whole file and is used
52 # for rules with no specific block defined
53
54 2 return $self;
55 }
56
57 # Block processing not implemented yet
58 #sub defineBlock {
59 # my $self = shift;
60 # my $name = shift;
61 # my %opts = @_;
62 # if (exists($self->{block}->{$name})) {
63 # $self->_setError("Block $name already defined");
64 # return 0;
65 # }
66 # if ($opts{fromline}) {
67 # $self->{block}->{$name}->{from} = $opts{fromline};
68 # } elsif ($opts{frommatch}) {
69 # $self->{block}->{$name}->{frommatch} = $opts{frommatch};
70 # } else {
71 # $self->{block}->{$name}->{from} = 0;
72 # }
73 # if ($opts{toline}) {
74 # $self->{block}->{$name}->{to} = $opts{toline};
75 # } elsif ($opts{frommatch}) {
76 # $self->{block}->{$name}->{tomatch} = $opts{tomatch};
77 # } else {
78 # $self->{block}->{$name}->{to} = 999999;
79 # }
80 # push @{$self->{blockorder}},$name;
81 # return 1;
82 #}
83 #
84 #sub undefineBlock {
85 # my $self = shift;
86 # my $name = shift;
87 # if (exists($self->{block}->{$name})) {
88 # $self->_debug("Undefining block $name");
89 # delete($self->{block}->{$name});
90 # my @tmp = @{$self->{blockorder}};
91 # @{$self->{blockorder}} = grep($_ ne $name, @tmp);
92 # } else {
93 # $self->_debug("Block $name not defined, ignoring");
94 # }
95 # return 1;
96 #}
97 #
98 #sub listMatchBlocks {
99 # my $self = shift;
100 # return (grep { !defined($self->{block}->{$_}->{from}) || !defined($self->{block}->{$_}->{to}) } $self->listBlocks());
101 #}
102 #
103 #sub listCurrentBlocks {
104 # my $self = shift;
105 # return (grep { $self->{block}->{$_}->{active} } $self->listBlocks());
106 #}
107 #
108 #sub listBlocks {
109 # my $self = shift;
110 # return @{$self->{blockorder}};
111 #}
112
113 ### TODO Need to define all methods and also options like
114 ### TODO addIfMissing to add a required line even if it is not found at end/start of file or block
115 # ->replace( replace => "SAD", with => "FUNNY", ignorecase => 1, addIfMissing => 1 )
116 # ->replace( repalce => "sad (\d+) day", with => "funny \$1 week", ignorecase => 1, addIfMissing => 1 )
117
118 11 ▶sub defineRule {
Subroutine defineRule: called CC=7 SCAR=21.4
Pod documented
119 11 my $self = shift;
120 11 my %opts = @_;
121 ### TODO need to generate a better name if undefined
122 11 my $name = $opts{name};
123 11 ▶ if (!$name) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($name) 11 0 11
124 11 $name = "rule" . ($#{$self->{ruleorder}}+1);
125 }
126 11 ▶ return 0 if (!%opts);
Branch: 1 branch
BranchTrueFalseTotal
unless %opts 0 11 11
127 11 $self->_debug("Defining rule '$name': " . join(",",%opts));
128 11 ▶ if (!$opts{replace} && !$opts{insert} && !$opts{'delete'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts{"replace"} or $opts{"insert"} or $opts{"delete"}) 0 11 11
Condition66%4/6
$opts{"replace"} or $opts{"insert"} or $opts{"delete"}
l!l&&r!l&&!r
1010
$opts{"replace"} or $opts{"insert"}
l!l&&r!l&&!r
1001
MC/DC0%0/3
$opts{"replace"} or $opts{"insert"} or $opts{"delete"}
$opts{"replace"} $opts{"insert"} $opts{"delete"}
Truth table50%2/4
A or B or C
A= $opts{"replace"}B= $opts{"insert"}C= $opts{"delete"}
ABCresult
1XX1
01X1
0011
0000
129 0 $self->_addError("Failed to define rule $name");
130 0 return 0;
131 }
132 11 $self->{rule}->{$name} = new Text::Modify::Rule(%opts, debug => $self->{_debug});
133 11 ▶ if (!$self->{rule}->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"rule"}{$name}) 0 11 11
134 0 $self->_setError("Could not init rule $name");
135 0 return 0;
136 }
137 11 push @{$self->{ruleorder}},$name;
138 11 return 1;
139 }
140
141 0 ▶sub undefineRule {
Subroutine undefineRule: not called CC=2 SCAR=17.9
Pod documented
142 0 my $self = shift;
143 0 my $name = shift;
144 0 ▶ if (exists($self->{rule}->{$name})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'rule'}{$name}) { } 0 0 0
145 0 $self->_debug("Undefining rule $name");
146 0 delete($self->{rule}->{$name});
147 0 my @tmp = @{$self->{ruleorder}};
148 0 @{$self->{ruleorder}} = grep($_ ne $name, @tmp);
149 } else {
150 0 $self->_debug("Rule $name not defined, ignoring");
151 }
152 0 return 1;
153 }
154
155 # Simple syntax ->replace("MY","HIS") or ->replaceLine("WHAT","WITH",ignorecase => 1)
156 # supported options are:
157 # dryrun do not apply changes
158 # ignorecase ignore case for matching
159 # ifmissing insert/append/ignore/fail string if missing (cannot use results of regex then)
160 # matchfirst only match X times for replacing, 1 would only replace the first occurence
161 3 ▶sub replace {
Subroutine replace: called CC=1 SCAR=0.0
Pod documented
162 3 my $self = shift;
163 3 return $self->replaceRegex(@_);
164 }
165
166 2 ▶sub replaceString {
Subroutine replaceString: called CC=1 SCAR=0.0
Pod documented
167 2 my ($self,$what,$with,%opts) = @_;
168 2 $self->_debug("Adding string replace rule: '$what' with '$with'");
169 2 return $self->defineRule(replace=>$what,type=>'string',string=>$what,with=>$with,%opts);
170 }
171
172 1 ▶sub replaceWildcard {
Subroutine replaceWildcard: called CC=1 SCAR=0.0
Pod documented
173 1 my ($self,$what,$with,%opts) = @_;
174 1 $self->_debug("Adding wildcard replace rule: '$what' with '$with'");
175 1 return $self->defineRule(replace=>$what,type=>'wildcard',wildcard=>$what,with=>$with,%opts);
176 }
177
178 5 ▶sub replaceRegex {
Subroutine replaceRegex: called CC=1 SCAR=0.0
Pod documented
179 5 my ($self,$what,$with,%opts) = @_;
180 5 $self->_debug("Adding regex replace rule: '$what' with '$with'");
181 5 return $self->defineRule(replace=>$what,type=>'regex',regex=>$what,with=>$with,%opts);
182 }
183
184 # TODO sub replaceInBlock { }
185
186 # Usage: Delete line matching expressions MATCH
187 # Syntax: ->deleteLine("MATCH", ignorecase => 1, matchfirst => 1)
188 # supported options are:
189 # dryrun do not apply changes
190 # ignorecase ignore case for matching
191 # ifmissing ignore|fail if missing
192 # matchfirst only match X times for replacing, 1 would only replace the first occurence
193
194 1 ▶sub delete {
Subroutine delete: called CC=1 SCAR=0.0
Pod documented
195 1 my ($self,$what,%opts) = @_;
196 1 $opts{'delete'} = $what;
197 1 return $self->defineRule(%opts);
198 }
199 # TODO sub deleteInBlock { }
200
201 0 ▶sub insert {
Subroutine insert: not called CC=1 SCAR=6.9
Pod documented
202 0 my ($self,$what,%opts) = @_;
203 0 $opts{insert} = $what;
204 0 $opts{at} = "top";
205 0 return $self->defineRule(%opts);
206 }
207 # TODO sub insertInBlock { }
208
209 0 ▶sub append {
Subroutine append: not called CC=1 SCAR=6.9
Pod documented
210 0 my ($self,$what,%opts) = @_;
211 0 $opts{insert} = $what;
212 0 $opts{at} = "bottom";
213 0 return $self->defineRule(%opts);
214 }
215
216 # TODO sub appendInBlock { }
217
218
219 2 ▶sub listRules {
Subroutine listRules: called CC=1 SCAR=0.0
Pod documented
220 ### TODO maybe it would be better to place rules outside of blocks
221 2 my $self = shift;
222 2 $self->_debug("Returning ordered rules: " . join(", ",@{$self->{ruleorder}}));
223 2 return @{$self->{ruleorder}};
224 }
225
226 0 ▶sub backupExtension {
Subroutine backupExtension: not called CC=2 SCAR=17.9
Pod documented
227 0 my $self = shift;
228 0 my $ext = shift;
229 0 ▶ if (defined($ext)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ext) 0 0 0
230 0 $self->{backupExt} = $ext;
231 0 return 1;
232 }
233 0 return $self->{backupExt};
234 }
235
236 0 ▶sub _getBackupFilename {
Subroutine _getBackupFilename: not called CC=5 SCAR=34.0
237 0 my $self = shift;
238 0 ▶ my $file = $self->{'file'} || shift;
Condition0%0/3
$self->{'file'} || shift()
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'file'} || shift()
$self->{'file'} shift()
Truth table0%0/3
A || B
A= $self->{'file'}B= shift()
ABresult
1X1
011
000
239 0 my $bakfile = $file . $self->{'backupExt'};
240
241 0 ▶ if (-f $bakfile) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $bakfile) 0 0 0
242 0 $self->_debug("Bakfile $bakfile already existing, using next available");
243 # TODO Need to do backupfile rotation or merge into createBackup
244 0 my $cnt = 1;
245 0 ▶ while (-f "$bakfile.$cnt" && $cnt) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f "$bakfile.$cnt" and $cnt) 0 0 0
Condition0%0/3
-f "$bakfile.$cnt" and $cnt
!ll&&!rl&&r
000
MC/DC0%0/2
-f "$bakfile.$cnt" and $cnt
-f "$bakfile.$cnt" $cnt
Truth table0%0/3
A and B
A= -f "$bakfile.$cnt"B= $cnt
ABresult
0X0
100
111
246 0 $cnt++;
247 }
248 0 $bakfile = "$bakfile.$cnt";
249 }
250 0 return $bakfile;
251 }
252
253 #=====================================================
254 # create backup of set or supplied file
255 #=====================================================
256 0 ▶sub createBackup {
Subroutine createBackup: not called CC=4 SCAR=30.0
Pod documented
257 0 my $self = shift;
258 0 ▶ my $file = $self->{'file'} || shift;
Condition0%0/3
$self->{'file'} || shift()
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'file'} || shift()
$self->{'file'} shift()
Truth table0%0/3
A || B
A= $self->{'file'}B= shift()
ABresult
1X1
011
000
259 0 my $bakfile = $self->_getBackupFilename();
260 ### Create a backup if bakfile is set
261 0 ▶ if ($bakfile && $bakfile ne $file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bakfile and $bakfile ne $file) 0 0 0
Condition0%0/3
$bakfile and $bakfile ne $file
!ll&&!rl&&r
000
MC/DC0%0/2
$bakfile and $bakfile ne $file
$bakfile $bakfile ne $file
Truth table0%0/3
A and B
A= $bakfileB= $bakfile ne $file
ABresult
0X0
100
111
262 0 $self->_debug("- Creating backup copy $bakfile");
263 0 copy($file,$bakfile);
264 # TODO restore permissions and ownership of file
265 }
266 0 return $bakfile;
267 }
268
269 2 ▶sub process {
Subroutine process: called CC=9 SCAR=24.7
Pod documented
270 2 my $self = shift;
271 2 my $file = $self->{'file'};
272 2 my $bakfile = "";
273 2 ▶ if ($self->{'backup'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"backup"}) 0 2 2
274 0 $self->_debug("Creating backup");
275 0 $bakfile = $self->createBackup();
276 0 ▶ if ($self->isError()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->isError) 0 0 0
277 0 Error($self->getError());
278 0 return 0;
279 }
280 }
281 2 my $txtbuf = Text::Buffer->new(file => $file);
282 2 $self->{linesread} = $txtbuf->getLineCount();
283 2 $self->{_buffer} = $txtbuf;
284 2 $self->_debug("Read $self->{linesread} from $file");
285
286 2 $self->{replacecount} = 0;
287 2 $self->{matchcount} = 0;
288 2 $self->{addcount} = 0;
289 2 $self->{deletecount} = 0;
290 2 $self->{lineschanged} = 0;
291 2 $self->{linesprocessed} = 0;
292
293 2 ▶ $self->_debug("Starting processing of data " . (defined($self->{data}) ? $self->{data} : "undef") . " (error=" . $self->isError(). ")");
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'data'} ? : 0 2 2
294 2 foreach ($self->listRules()) {
295 11 my $rule = $self->{rule}->{$_};
296 11 $self->_debug("Processing rule $_");
297 11 my $changecount = $rule->process($self->{_buffer});
298 11 $self->{changecount} += $changecount;
299 11 my ($match, $add, $del, $repl) = $rule->getModificationStats();
300 11 $self->{replacecount} += $repl;
301 11 $self->{matchcount} += $match;
302 11 $self->{addcount} += $add;
303 11 $self->{deletecount} += $del;
304 11 $self->_debug("Stats rule $_ (change/match/repl/add/del): " .
305 "$self->{lineschanged}/$match/$repl/$add/$del");
306 11 ▶ if ($rule->isError()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule->isError) 0 11 11
307 0 $self->_addError($rule->getError());
308 0 last;
309 }
310 }
311 2 ▶ if ($self->isError()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->isError) 0 2 2
312 0 return 0;
313 }
314
315 ### Now mv the temp. file to overwrite the original configfile
316 2 ▶ if (!$self->{dryrun}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{'dryrun'}) { } 2 0 2
317 # Force saving now
318 2 $self->{_buffer}->setModified();
319 2 ▶ if (!$self->{_buffer}->save($self->{writeto})) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_buffer"}->save($self->{"writeto"})) 0 2 2
320 0 $self->_debug("Error saving file to " . $self->{writeto});
321 0 return 0;
322 }
323 } else {
324 0 $self->_debug("Dryrun, not writing file");
325 }
326 2 $self->_debug("Statistics:
327 Lines read: $self->{linesread}
328 Lines changed: $self->{lineschanged}
329 Lines matched: $self->{matchcount}
330 Lines replaced: $self->{replacecount}
331 Lines added: $self->{addcount}
332 Lines deleted: $self->{deletecount}");
333 2 return 1;
334 }
335
336 0 ▶sub dryrun {
Subroutine dryrun: not called CC=1 SCAR=6.9
Pod documented
337 0 my $self = shift;
338 0 my $old = $self->{dryrun};
339 0 $self->{dryrun} = 1;
340 0 my $rc = $self->process();
341 0 $self->{dryrun} = $old;
342 0 return $rc;
343 }
344
345 0 ▶sub isDryRun { return shift->{dryrun}; }
Subroutine isDryRun: not called CC=1 SCAR=6.9
Pod documented
346 0 ▶sub getLinesModified { return shift->{lineschanged}; }
Subroutine getLinesModified: not called CC=1 SCAR=6.9
Pod documented
347 0 ▶sub getLinesProcessed { return shift->{linesprocessed}; }
Subroutine getLinesProcessed: not called CC=1 SCAR=6.9
Pod documented
348 0 ▶sub getReplaceCount { return shift->{replacecount}; }
Subroutine getReplaceCount: not called CC=1 SCAR=6.9
Pod documented
349 0 ▶sub getMatchCount { return shift->{matchcount}; }
Subroutine getMatchCount: not called CC=1 SCAR=6.9
Pod documented
350 0 ▶sub getAddCount { return shift->{addcount}; }
Subroutine getAddCount: not called CC=1 SCAR=6.9
Pod documented
351 0 ▶sub getDeleteCount { return shift->{deletecount}; }
Subroutine getDeleteCount: not called CC=1 SCAR=6.9
Pod documented
352
353
354 #=============================================================
355 # ErrorHandling Methods
356 #=============================================================
357 0 ▶sub _addError { my $self = shift; $self->{error} .= shift; }
Subroutine _addError: not called CC=1 SCAR=6.9
358 4 ▶sub isError { return (shift->{'error'} ? 1 : 0); }
Subroutine isError: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->{'error'} ? : 0 4 4
359 0 ▶sub _setError { my $self = shift; $self->{error} = shift; }
Subroutine _setError: not called CC=1 SCAR=6.9
360 0 ▶sub getError {
Subroutine getError: not called CC=1 SCAR=6.9
Pod documented
361 0 my $self = shift;
362 0 my $error = $self->{error};
363 0 $self->_clearError();
364 0 return $error;
365 }
366 2 ▶sub _clearError { shift->{error} = ""; }
Subroutine _clearError: called CC=1 SCAR=0.0
367
368 #=============================================================
369 # Private methods (for internal use )
370 #=============================================================
371
372 # Only internal function for debug output
373 59 ▶sub _debug {
Subroutine _debug: called CC=3 SCAR=11.4
374 59 my $self = shift;
375 59 ▶ if ($#_ == -1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($#_ == -1) { } 0 59 59
elsif ($self->{'_debug'}) { } 29 30 59
376 0 return $self->{_debug};
377 }
378 elsif ( $self->{_debug} ) {
379 29 print "[DEBUG] @_\n";
380 }
381 }
382
383 3 1;
384 __END__
« Buffer.pm ↑ Summary