lib/IRC/Indexer/Conf.pm

filter: Statementstmt 58.9% 46 / 78 Branchbran 17.8% 5 / 28 Conditioncond 20.0% 1 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 73.6% 14 / 19 Podpod 45.4% 5 / 11 totaltotal 49.3% 71 / 144 CC 18 SCAR 22.6
CC
18
Cov
47%
parse_nets
30.0
find_nets
20.0
__ANON__
12.0
CRAP 9.6
« lib/IRC/Indexer/Output.pm ↑ Summary lib/IRC/Indexer/Trawl/Forking.pm »
1 package IRC::Indexer::Conf;
2  
3 4 ▶use 5.10.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 ▶use Scalar::Util qw/openhandle/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 4 ▶use File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 4 ▶use YAML::XS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 ▶sub new { bless {}, shift }
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
15  
16 3 ▶sub slurp {
Subroutine slurp: called CC=3 SCAR=14.7
Pod undocumented
17 3 my ($self, $path) = @_;
18
19 3 my $slurped;
20
21 3 ▶ if ( openhandle($path) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&openhandle($path)) { } 3 0 3
22 3 local $/; $slurped = <$path>;
23 } else {
24 0 ▶ open my $fh, '<:encoding(utf8)', $path
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:encoding(utf8)", $path or croak("Conf open failed: ${path}: $!") 0 0 0
25 or croak "Conf open failed: $path: $!";
26 0 { local $/; $slurped = <$fh> }
27 0 close $fh;
28 }
29  
30 3 return $slurped
31 }
32  
33 3 ▶sub parse_conf {
Subroutine parse_conf: called CC=2 SCAR=7.0
Pod documented
34 3 my ($self, $path) = @_;
35 3 my $yaml = $self->slurp($path);
36 3 ▶ croak "No data returned from parse_conf: $path"
Branch: 1 branch
BranchTrueFalseTotal
unless $yaml 0 3 3
37 unless $yaml;
38  
39 3 my $ref = YAML::XS::Load($yaml);
40  
41 3 return $ref
42 }
43  
44 0 ▶sub parse_nets {
Subroutine parse_nets: not called CC=5 SCAR=34.0
Pod documented
45 0 my ($self, $dir) = @_;
46
47 0 my $nethash = {};
48
49 0 ▶ my @specfiles = ref $dir eq 'ARRAY' ? @$dir
Branch: 1 branch
BranchTrueFalseTotal
ref $dir eq 'ARRAY' ? : 0 0 0
50 : $self->find_nets($dir) ;
51  
52 0 SERV: for my $specpath (@specfiles) {
53 0 my $this_spec = $self->parse_conf($specpath);
54
55 0 ▶ unless ($this_spec->{Server}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($this_spec->{"Server"}) 0 0 0
56 0 croak "specfile missing Server definition: $specpath"
57 }
58
59 0 ▶ unless ($this_spec->{Network}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($this_spec->{"Network"}) 0 0 0
60 0 croak "specfile missing Network definition: $specpath"
61 }
62  
63 0 my $servname = $this_spec->{Server};
64 0 my $netname = $this_spec->{Network};
65
66 0 $nethash->{$netname}->{$servname} = $this_spec;
67 }
68  
69 0 return $nethash
70 }
71  
72 0 ▶sub find_nets {
Subroutine find_nets: not called CC=4 SCAR=30.0
Pod documented
73 0 my ($self, $dir) = @_;
74
75 0 ▶ croak "find_nets called with no NetworkDir"
Branch: 1 branch
BranchTrueFalseTotal
unless $dir 0 0 0
76 unless $dir;
77
78 0 ▶ croak "find_nets called against non-directory $dir"
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 0 0 0
79 unless -d $dir;
80
81 0 my @found;
82 find(
83 0 ▶ sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
84 0 ▶ my $thisext = (split /\./)[-1] // return;
Condition0%0/2
(split(/\./, $_, 0))[-1] // (return)
l!l
00
MC/DC0%0/1
(split(/\./, $_, 0))[-1] // (return)
(split(/\./, $_, 0))[-1]
Truth table0%0/2
A
A= (split(/\./, $_, 0))[-1]
Aresult
11
00
85 0 ▶ push(@found, $File::Find::name)
Branch: 1 branch
BranchTrueFalseTotal
if $thisext eq "server" 0 0 0
86 if $thisext eq 'server';
87 },
88 0 $dir
89 );
90  
91 0 ▶ return wantarray ? @found : \@found ;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
92 }
93  
94  
95 ## Example CF
96  
97 0 ▶sub get_example_conf { get_example_cf(@_) }
Subroutine get_example_conf: not called CC=1 SCAR=6.9
Pod undocumented
98 5 ▶sub get_example_cf {
Subroutine get_example_cf: called CC=2 SCAR=7.4
Pod documented
99 5 my ($self, $cftype) = @_;
100 5 my $method = 'example_cf_'.$cftype;
101
102 5 ▶ unless ($self->can($method)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->can($method)) 0 5 5
103 0 croak "Invalid example conf type: $cftype"
104 }
105  
106 5 return $self->$method
107 }
108  
109 0 ▶sub write_example_conf { write_example_cf(@_) }
Subroutine write_example_conf: not called CC=1 SCAR=6.9
Pod undocumented
110 1 ▶sub write_example_cf {
Subroutine write_example_cf: called CC=5 SCAR=21.6
Pod documented
111 1 my ($self, $cftype, $path) = @_;
112 1 ▶ croak "write_example_cf requires a type and path"
Branch: 1 branch
BranchTrueFalseTotal
unless $cftype and $path 0 1 1
Condition33%1/3
$cftype and $path
!ll&&!rl&&r
001
MC/DC0%0/2
$cftype and $path
$cftype $path
Truth table33%1/3
A and B
A= $cftypeB= $path
ABresult
0X0
100
111
113 unless $cftype and $path;
114
115 1 my $conf = $self->get_example_cf($cftype);
116  
117 1 ▶ if ( openhandle($path) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&openhandle($path)) { } 1 0 1
118 1 print $path $conf;
119 } else {
120 0 ▶ open my $fh, '>', $path or die "open failed: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $path or die "open failed: $!\n" 0 0 0
121 0 print $fh $conf;
122 0 close $fh;
123 }
124 }
125  
126 2 ▶sub example_cf_spec {
Subroutine example_cf_spec: called CC=1 SCAR=0.0
Pod undocumented
127 2 my $conf = <<END;
128 ---
129 ### Example server spec file
130  
131 Network: CobaltIRC
132 Server: eris.oppresses.us
133 Port: 6667
134 # Defaults are probably fine here:
135 #Nickname:
136 #BindAddr:
137 #UseIPV6:
138 #Timeout: 90
139 #Interval: 5
140  
141 END
142  
143 2 return $conf
144 }
145  
146 3 ▶sub example_cf_httpd {
Subroutine example_cf_httpd: called CC=1 SCAR=0.0
Pod undocumented
147 3 my $conf = <<END;
148 ---
149 ### Example HTTPD conf
150  
151 ## ServerPort:
152 ##
153 ## Port to run this HTTPD instance on.
154 ServerPort: 8700
155  
156 ## BindAddr:
157 ##
158 ## Optional local address to bind HTTPD to.
159 #BindAddr: '0.0.0.0'
160  
161 ## NetworkDir:
162 ##
163 ## Network spec files will be found recursively under NetworkDir:
164 ## A network spec file should end in ".server"
165 ## These specs tie networks together under their specified Network:
166 ## The files should be YAML, looking something like:
167 # ---
168 # Network: CobaltIRC
169 # Server: eris.oppresses.us
170 # Port: 6667
171 # Timeout: 90
172 # Interval: 5
173 ##
174 ## If you have multiple .server files for one Network, their Servers
175 ## will be cycled.
176 ## Specifying a round-robin will also generally do what you mean.
177 NetworkDir: /home/ircindex/networks
178  
179 ## CacheDir:
180 ##
181 ## Large pools of trawlers will store quite a bit of data after
182 ## a few runs have completed.
183 ## This trades some performance for significant memory savings.
184 ##
185 ## You probably want this.
186 ##
187 ## If a CacheDir is not specified, resultsets will live in memory;
188 ## additionally, due to the nature of the forking encoders,
189 ## there will be some extra processing overhead when results are
190 ## cached.
191 CacheDir: /home/ircindex/jsoncache
192  
193 ## LogFile:
194 ##
195 ## Path to log file.
196 ## If omitted, no logging takes place.
197 LogFile: /home/ircindex/indexer.log
198  
199 ## LogLevel:
200 ##
201 ## Log verbosity level.
202 ## 'debug', 'info', or 'warn'
203 LogLevel: info
204  
205 ## LogHTTP:
206 ##
207 ## If true, log HTTP-related activity
208 ## Defaults to ON
209 LogHTTP: 1
210  
211 ## LogIRC:
212 ##
213 ## If true, log trawling-related activity
214 ## Defaults to ON
215 LogIRC: 1
216  
217 ## PidFile:
218 ##
219 ## If a PidFile is specified, the server's PID will be written to the
220 ## specified file.
221 #PidFile:
222  
223 ## TrawlInterval:
224 ##
225 ## Delay (in seconds) between trawl runs per-network.
226 ## Can be overriden with the --interval command opt.
227 ## Defaults to 600 (10 mins)
228 #TrawlInterval: 600
229  
230 ## Forking:
231 ##
232 ## If Forking is enabled, trawlers will be spawned as external
233 ## processes rather than running asynchronously as part of a
234 ## single process.
235 ##
236 ## A trawler that is composing a very large list of channels
237 ## can use a fair bit of CPU; if you can spare a little extra
238 ## memory during runs and have the cores for it, fork them instead.
239 Forking: 0
240  
241 ## MaxTrawlers:
242 ##
243 ## If you are handling a lot of networks, you may want to limit
244 ## the number of forked trawlers that can be running at a given
245 ## time.
246 ##
247 ## If Forking is not enabled, this controls the number of async
248 ## trawlers that will be ->run() at a time.
249 ##
250 ## Only one trawler will run per network; in other words, the
251 ## theoretical maximum number of trawlers if MaxTrawlers is not
252 ## set is the number of configured networks.
253 ##
254 ## You might want a shorter per-trawler timeout if you are severely
255 ## limiting your MaxTrawlers.
256 ##
257 ## Defaults to 20; 0 or empty string '' disables.
258 #MaxTrawlers: 10
259  
260 ## MaxEncoders:
261 ##
262 ## Workers are forked off to handle the potentially expensive
263 ## JSON encoding of server trawl results; if you're trawling
264 ## a lot of networks, you may want to throttle these to avoid
265 ## resource starvation.
266 ##
267 ## It's perfectly safe to set to '1' -- the only downside is
268 ## increasing the odds of keeping more networks in memory
269 ## longer before they can be shuffled off to an encoder.
270 ##
271 ## Defaults to 20.
272 #MaxEncoders: 10
273  
274 ## ListChans:
275 ##
276 ## If ListChans is enabled, the server will create sorted lists of
277 ## channels as documented in IRC::Indexer::POD::ServerSpec
278 ##
279 ## Expensive in terms of CPU and space.
280 ListChans: 0
281  
282 END
283  
284 3 return $conf
285 }
286  
287 4 1;
288 __END__
« lib/IRC/Indexer/Output.pm ↑ Summary lib/IRC/Indexer/Trawl/Forking.pm »