| 1 | package Message::Rules; | ||||||||||||||||||||||||||||
| 2 | 3 | $Message::Rules::VERSION = '1.150170'; | |||||||||||||||||||||||||||
| 3 | 3 | $Message::Rules::VERSION = '1.142101'; | |||||||||||||||||||||||||||
| 4 | { | ||||||||||||||||||||||||||||
| 5 | 3 | $Message::Rules::VERSION = '1.132770'; | |||||||||||||||||||||||||||
| 6 | } | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use strict;use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use Message::Match qw(mmatch); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use Message::Transform qw(mtransform); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use File::Find; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use JSON; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 3 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 15 | 3 | my $class = shift; | |||||||||||||||||||||||||||
| 16 | 3 | my $self = {}; | |||||||||||||||||||||||||||
| 17 | 3 | bless ($self, $class); | |||||||||||||||||||||||||||
| 18 | 3 | return $self; | |||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | { | ||||||||||||||||||||||||||||
| 22 | 3 | my @loaded_configs; | |||||||||||||||||||||||||||
| 23 | 6 | ▶ | my $add_config = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 24 | 6 | my $thing = shift; | |||||||||||||||||||||||||||
| 25 | 6 | ▶ | return if $thing->{is_not_a_rule}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | 3 | ▶ | $thing->{order} = 0 unless $thing->{order}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | 3 | push @loaded_configs, $thing; | |||||||||||||||||||||||||||
| 28 | 3 | }; | |||||||||||||||||||||||||||
| 29 | 9 | ▶ | my $wanted = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=13 SCAR=27.6
| |||||||||||||||||||||||||||||
| 30 | 9 | my $f = $File::Find::name; | |||||||||||||||||||||||||||
| 31 | 9 | $f =~ s/.*\///; | |||||||||||||||||||||||||||
| 32 | 9 | ▶ | return unless -f $f; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | 6 | ▶ | return if $f =~ /\/\./; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | 6 | my $contents; | |||||||||||||||||||||||||||
| 35 | 6 | eval { | |||||||||||||||||||||||||||
| 36 | 6 | ▶ | open my $fh, '<', $f or die "open of $f failed: $!\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 6 | ▶ | read $fh, $contents, 10240000 or die "read of $f failed: $!\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | 6 | ▶ | close $fh or die "close of $f failed: $!\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | }; | ||||||||||||||||||||||||||||
| 40 | 6 | ▶ | die "Message::Rules::load_rules_from_directory: $@\n" if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 6 | my $conf; | |||||||||||||||||||||||||||
| 42 | 6 | eval { | |||||||||||||||||||||||||||
| 43 | 6 | ▶ | $conf = decode_json $contents or die 'failed to decode_json'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | }; | ||||||||||||||||||||||||||||
| 45 | 6 | ▶ | return unless $conf; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 6 | ▶ | if(not ref $conf) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | # die "Message::Rules::load_rules_from_directory: $f did not contain a reference"; | ||||||||||||||||||||||||||||
| 48 | 0 | return; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | 6 | ▶ | if(ref $conf eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | 3 | $add_config->($conf); | |||||||||||||||||||||||||||
| 52 | 3 | return; | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | 3 | ▶ | if(ref $conf eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 3 | $add_config->($_) for @{$conf}; | |||||||||||||||||||||||||||
| 56 | 3 | return; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | 0 | die "Message::Rules::load_rules_from_directory: $f did not contain either a HASH or ARRAY reference"; | |||||||||||||||||||||||||||
| 59 | 0 | return; | |||||||||||||||||||||||||||
| 60 | 3 | }; | |||||||||||||||||||||||||||
| 61 | 3 | ▶ | my $get_sorted_configs = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.2
| |||||||||||||||||||||||||||||
| 62 | 0 | my @configs = sort { $a->{order} <=> $b->{order}} @loaded_configs; | |||||||||||||||||||||||||||
| 63 | 3 | @loaded_configs = (); | |||||||||||||||||||||||||||
| 64 | 3 | return \@configs; | |||||||||||||||||||||||||||
| 65 | 3 | }; | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 3 | ▶ | sub load_rules_from_directory { | ||||||||||||||||||||||||||
|
Subroutine
load_rules_from_directory: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||||||||||||||||
| 68 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 69 | 3 | my $directory = shift; | |||||||||||||||||||||||||||
| 70 | 3 | ▶ | die "Message::Rules::load_rules_from_directory: passed directory ($directory) does not exist\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | if not -e $directory; | ||||||||||||||||||||||||||||
| 72 | 3 | ▶ | die "Message::Rules::load_rules_from_directory: passed directory ($directory) is not a directory\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | if not -d $directory; | ||||||||||||||||||||||||||||
| 74 | 3 | find($wanted, $directory); | |||||||||||||||||||||||||||
| 75 | 3 | $self->{rules} = $get_sorted_configs->(); | |||||||||||||||||||||||||||
| 76 | 3 | return $self->{rules}; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 3 | ▶ | sub apply_rules { | ||||||||||||||||||||||||||
|
Subroutine
apply_rules: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 81 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 82 | 2 | my $messages = shift; | |||||||||||||||||||||||||||
| 83 | 2 | ▶ | while(my($key,$value) = each %$messages) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 2 | my $message = $value; | |||||||||||||||||||||||||||
| 85 | 2 | $self->merge_rules($message); | |||||||||||||||||||||||||||
| 86 | 2 | $messages->{$key} = $message; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | 2 | return $messages; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 1 | ▶ | sub output_apply_rules { | ||||||||||||||||||||||||||
|
Subroutine
output_apply_rules: called CC=5 SCAR=16.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 92 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 93 | 1 | my $incoming_directory = shift; | |||||||||||||||||||||||||||
| 94 | 1 | my $outgoing_directory = shift; | |||||||||||||||||||||||||||
| 95 | 1 | my $messages = $self->load_messages($incoming_directory); | |||||||||||||||||||||||||||
| 96 | 1 | $self->apply_rules($messages); | |||||||||||||||||||||||||||
| 97 | 1 | ▶ | while(my($filename, $message) = each %$messages) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 1 | eval { | |||||||||||||||||||||||||||
| 99 | 1 | my $path = "$outgoing_directory/$filename"; | |||||||||||||||||||||||||||
| 100 | 1 | ▶ | open my $fh, '>', $path or die "failed to open $path for write: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 1 | print $fh JSON->new->canonical(1)->pretty(1)->encode($message); | |||||||||||||||||||||||||||
| 102 | 1 | ▶ | close $fh or die "failed to close $path: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | }; | ||||||||||||||||||||||||||||
| 104 | 1 | ▶ | die "Message::Rules::output_apply_rules: (\$filename=$filename): failed: $@\n" if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | 1 | return 1; | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 2 | ▶ | sub load_messages { | ||||||||||||||||||||||||||
|
Subroutine
load_messages: called CC=12 SCAR=27.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 110 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 111 | 2 | my $directory = shift; | |||||||||||||||||||||||||||
| 112 | 2 | my $messages = {}; | |||||||||||||||||||||||||||
| 113 | 2 | eval { | |||||||||||||||||||||||||||
| 114 | 2 | ▶ | die 'passed argument not a readable directory' | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
-d $directory and -r $directory
MC/DC0%0/2
-d $directory and -r $directory
-d $directory
-r $directory
Truth table33%1/3
A and B
A= -d $directoryB= -r $directory
| |||||||||||||||||||||||||||||
| 115 | if not -d $directory or not -r $directory; | ||||||||||||||||||||||||||||
| 116 | 0 | ▶ | local $SIG{ALRM} = sub { die "timed out\n"; }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 117 | 2 | alarm 5; | |||||||||||||||||||||||||||
| 118 | 2 | ▶ | opendir (my $dh, $directory) or die "opendir failed: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 2 | ▶ | my @files = grep { -f "$directory/$_" and not "$directory/$_" =~ /^\./ } readdir($dh); | ||||||||||||||||||||||||||
|
Condition66%2/3
-f "$directory/$_" && !(("$directory/$_") =~ /^\./)
MC/DC50%1/2
-f "$directory/$_" && !(("$directory/$_") =~ /^\./)
-f "$directory/$_"
!(("$directory/$_") =~ /^\./)
Truth table66%2/3
A && not(B)
A= -f "$directory/$_"B= !(("$directory/$_") =~ /^\./)
| |||||||||||||||||||||||||||||
| 120 | 2 | ▶ | closedir $dh or die "closedir failed: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 2 | foreach my $filename (@files) { | |||||||||||||||||||||||||||
| 122 | 2 | my $contents; | |||||||||||||||||||||||||||
| 123 | 2 | ▶ | open my $fh, "$directory/$filename" or die "failed to open file ($filename): $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | 2 | ▶ | read $fh, $contents, 1024000 or die "failed to read ($filename): $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 2 | ▶ | close $fh or die "failed to close file ($filename): $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | 2 | ▶ | my $conf = decode_json $contents or die 'failed to decode_json'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | 2 | $messages->{$filename} = $conf; | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | }; | ||||||||||||||||||||||||||||
| 130 | 2 | alarm 0; | |||||||||||||||||||||||||||
| 131 | 2 | ▶ | die "Message::Rules::load_messages: failed (\$directory=$directory) $@\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | if $@; | ||||||||||||||||||||||||||||
| 133 | 2 | return $messages; | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 3 | ▶ | sub merge_rules { | ||||||||||||||||||||||||||
|
Subroutine
merge_rules: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 137 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 138 | 3 | my $message = shift; | |||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 3 | foreach my $conf (@{$self->{rules}}) { | |||||||||||||||||||||||||||
| 141 | 3 | ▶ | next unless mmatch $message, $conf->{match}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | 3 | mtransform($message, $conf->{transform}); | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | 3 | return $message; | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 3 | 1; | |||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | __END__ | ||||||||||||||||||||||||||||