CSE.pm

filter: Statementstmt 69.7% 138 / 198 Branchbran 21.0% 8 / 38 Conditioncond 16.6% 1 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 76.0% 35 / 46 Podpod 0.0% 0 / 8 totaltotal 60.8% 182 / 299 CC 24 SCAR 24.6
CC
24
Cov
61%
is_file_valid
72.0
_build_ignore_reassembl
20.0
valid_mime_type
12.0
CRAP 11.7
« CSE/Command/Unwatch.pm ↑ Summary CSE/Command/Search.pm »
1 11 ▶BEGIN{
Subroutine BEGIN: called CC=1 SCAR=0.0
2 # Avoid Slurp warnings on perl 5.8
3 11 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 require File::Slurp;
5 11 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 }
7 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 package App::CSE;
10 11 $App::CSE::VERSION = '0.016';
11  
12 11 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 11 ▶use Class::Load;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 11 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 11 ▶use App::CSE::Colorizer;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 11 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 11 ▶use File::MimeInfo::Magic;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 11 ▶use IO::Interactive;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 11 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 11 ▶use String::CamelCase;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 11 ▶use Path::Class::Dir;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 11 ▶use File::stat;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 11 ▶use Getopt::Long qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 11 ▶use Regexp::Assemble;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 11 ▶use Text::Glob;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 11 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 11 ▶use Log::Log4perl qw/:easy/;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 =head1 NAME
32  
33 App::CSE - Code search engine. Implements the 'cse' program
34  
35 =head1 INSTALLATION
36  
37 Using system wide cpan:
38  
39 sudo cpan -i App::CSE
40  
41 Using cpanm:
42  
43 cpanm App::CSE
44  
45 =head1 SYNOPSIS
46  
47 cse
48  
49 See L<App::CSE::Command::Help> For a description the available commands.
50  
51 =head1 FEATURES
52  
53 =over
54  
55 =item Hits highlighting
56  
57 =item Prefix* queries
58  
59 =item Complex queries syntax (Lucy)
60  
61 =item Dirty files indicator
62  
63 =item Directory watcher
64  
65 =item Declaration queries (Perl subs and packages)
66  
67 =item Directory filtering
68  
69 =item Paging
70  
71 =item Ignoring files
72  
73 =item Works with Perl 5.8.8 up to 5.20
74  
75 =back
76  
77 =head1 PROGRAMMATIC USAGE
78  
79 In addition of using this via the command line program 'cse', you can use this app
80 in an object oriented way.
81  
82 For instance:
83  
84 my $app = App::CSE->new( { command_name => 'index',
85 options => { 'idx' => '/path/to/the/index' ,
86 'dir' => '/code/directory/to/index'
87 });
88  
89 if( $app->execute() ){
90 .. and error occured ..
91 }else{
92 .. It is a success ..
93 }
94  
95 Retrieving search hits after a search:
96  
97 my $app = App::CSE->new( { command_name => 'search',
98 args => [ 'search_query' ],
99 options => { 'idx' => '/path/to/the/index' ,
100 'dir' => '/code/directory/to/index'
101 });
102 my $hits = $app->command()->hits();
103 # This is a L<Lucy::Search::Hits>
104  
105 See L<App::CSE::Command::Help> for a list of available commands and options.
106  
107 =head1 LOGGING
108  
109 App::CSE uses L<Log::Log4perl>
110  
111 =head1 BUILD STATUS
112  
113 =begin html
114  
115 <a href="https://travis-ci.org/jeteve/App-CSE"><img src="https://travis-ci.org/jeteve/App-CSE.svg?branch=master"></a>
116  
117 =end html
118  
119 =head1 COPYRIGHT
120  
121 See L<App::CSE::Command::Help>
122  
123 =cut
124  
125 11 my $LOGGER = Log::Log4perl->get_logger();
126  
127 11 has 'command_name' => ( is => 'ro', isa => 'Str', required => 1 , lazy_build => 1);
128 11 has 'command' => ( is => 'ro', isa => 'App::CSE::Command', lazy_build => 1);
129 11 has 'max_size' => ( is => 'ro' , isa => 'Int' , lazy_build => 1);
130  
131 11 has 'interactive' => ( is => 'ro' , isa => 'Bool' , lazy_build => 1 );
132 11 has 'colorizer' => ( is => 'ro' , isa => 'App::CSE::Colorizer' , lazy_build => 1);
133  
134 # GetOpt::Long options specs.
135 11 has 'options_specs' => ( is => 'ro' , isa => 'ArrayRef[Str]', lazy_build => 1);
136  
137 # The options as slurped by getopts long
138 11 has 'options' => ( is => 'ro' , isa => 'HashRef[Str]', lazy_build => 1);
139  
140 # The arguments after any option
141 11 has 'args' => ( is => 'ro' , isa => 'ArrayRef[Str]', lazy_build => 1);
142  
143  
144 11 has 'index_dir' => ( is => 'ro' , isa => 'Path::Class::Dir', lazy_build => 1);
145 11 has 'index_mtime' => ( is => 'ro' , isa => 'DateTime' , lazy_build => 1);
146 11 has 'index_dirty_file' => ( is => 'ro' , isa => 'Path::Class::File', lazy_build => 1);
147 11 has 'dirty_files' => ( is => 'ro', isa => 'HashRef[Str]', lazy_build => 1);
148  
149 11 has 'index_meta_file' => ( is => 'ro' , isa => 'Path::Class::File' , lazy_build => 1);
150 11 has 'index_meta' => ( is => 'ro', isa => 'HashRef[Str]', lazy_build => 1);
151  
152 # File utilities
153 11 has 'xml_parser' => ( is => 'ro' , isa => 'XML::LibXML', lazy_build => 1);
154  
155 # Environment slurping
156 11 has 'cseignore' => ( is => 'ro', isa => 'Maybe[Path::Class::File]', lazy_build => 1 );
157 11 has 'ignore_reassembl' => ( is => 'ro', isa => 'Regexp::Assemble', lazy_build => 1);
158  
159 {# Singleton flavour
160 11 my $instance;
161 3 ▶ sub BUILD{
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
162 3 my ($self) = @_;
163 3 $instance = $self;
164 }
165 0 ▶ sub instance{
Subroutine instance: not called CC=1 SCAR=6.9
Pod undocumented
166 0 return $instance;
167 }
168 }
169  
170 11 ▶sub _build_cseignore{
Subroutine _build_cseignore: not called CC=2 SCAR=15.7
171 0 my ($self) = @_;
172 0 my $file = Path::Class::Dir->new()->file('.cseignore');
173 0 ▶ unless( -e $file ){
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $file) 0 0 0
174 0 return;
175 }
176 0 $LOGGER->info("Will ignore patterns from '$file'");
177 0 return $file;
178 }
179  
180 0 ▶sub _build_ignore_reassembl{
Subroutine _build_ignore_reassembl: not called CC=4 SCAR=30.0
181 0 my ($self) = @_;
182 0 my $re = Regexp::Assemble->new();
183 0 ▶ if( my $cseignore = $self->cseignore() ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $cseignore = $self->cseignore) 0 0 0
184 0 my @lines = split(q/\n/ , $cseignore->slurp());
185 0 foreach my $line ( @lines ){
186 0 ▶ if( $line =~ /^\s*(?:#|$)/ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*(?:#|$)/) 0 0 0
187 0 next;
188 }
189 0 $line =~ s/^\s*//; $line =~ s/\s*$//;
190 0 $re->add( Text::Glob::glob_to_regex_string( $line ) );
191 }
192 }
193 0 return $re;
194 }
195  
196 0 ▶sub _build_xml_parser{
Subroutine _build_xml_parser: not called CC=1 SCAR=6.9
197 0 my ($self) = @_;
198 0 return XML::LibXML->new();
199 }
200  
201 1 ▶sub _build_colorizer{
Subroutine _build_colorizer: called CC=1 SCAR=0.0
202 1 my ($self) = @_;
203 1 return App::CSE::Colorizer->new( { cse => $self } );
204 }
205  
206 2 ▶sub _build_interactive{
Subroutine _build_interactive: called CC=1 SCAR=0.0
207 2 my ($self) = @_;
208 2 return IO::Interactive::is_interactive();
209 }
210  
211 0 ▶sub _build_index_meta_file{
Subroutine _build_index_meta_file: not called CC=1 SCAR=6.9
212 0 my ($self) = @_;
213 0 return $self->index_dir()->file('cse_meta.js');
214 }
215  
216 2 ▶sub _build_index_dirty_file{
Subroutine _build_index_dirty_file: called CC=1 SCAR=0.0
217 2 my ($self) = @_;
218 2 return $self->index_dir()->file('cse_dirty.js');
219 }
220  
221 0 ▶sub _build_index_meta{
Subroutine _build_index_meta: not called CC=2 SCAR=17.9
222 0 my ($self) = @_;
223 0 ▶ unless( -r $self->index_meta_file() ){
Branch: 1 branch
BranchTrueFalseTotal
unless (-r $self->index_meta_file) 0 0 0
224 0 return { version => '-unknown-' };
225 }
226 0 return JSON::decode_json(File::Slurp::read_file($self->index_meta_file().'' , { binmode => ':raw' }));
227 }
228  
229 2 ▶sub _build_dirty_files{
Subroutine _build_dirty_files: called CC=2 SCAR=6.9
230 2 my ($self) = @_;
231 2 ▶ unless( -r $self->index_dirty_file() ){
Branch: 1 branch
BranchTrueFalseTotal
unless (-r $self->index_dirty_file) 1 1 2
232 1 return {};
233 }
234 1 return JSON::decode_json(File::Slurp::read_file($self->index_dirty_file().'' , { binmode => ':raw' }));
235 }
236  
237 0 ▶sub _build_index_mtime{
Subroutine _build_index_mtime: not called CC=1 SCAR=6.9
238 0 my ($self) = @_;
239 0 my $st = File::stat::stat($self->index_dir());
240 0 return DateTime->from_epoch( epoch => $st->mtime() );
241 }
242  
243 0 ▶sub _build_max_size{
Subroutine _build_max_size: not called CC=2 SCAR=17.9
244 0 my ($self) = @_;
245 0 ▶ return $self->options()->{max_size} || 1048576; # 1 MB default. This is the buffer size of File::Slurp
Condition0%0/2
$self->options->{'max_size'} || 1048576
l!l
00
MC/DC0%0/1
$self->options->{'max_size'} || 1048576
$self->options->{'max_size'}
Truth table0%0/2
A
A= $self->options->{'max_size'}
Aresult
11
00
246 }
247  
248 2 ▶sub _build_index_dir{
Subroutine _build_index_dir: called CC=2 SCAR=7.6
249 2 my ($self) = @_;
250  
251 2 ▶ if( my $opt_idx = $self->options->{idx} ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $opt_idx = $self->options->{"idx"}) 2 0 2
252 2 return Path::Class::Dir->new($opt_idx);
253 }
254  
255 0 return Path::Class::Dir->new('.cse.idx');
256 }
257  
258 3 ▶sub _build_command_name{
Subroutine _build_command_name: called CC=4 SCAR=15.8
259 3 my ($self) = @_;
260  
261 3 ▶ unless( $ARGV[0] ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($ARGV[0]) 0 3 3
262 0 return 'help';
263 }
264  
265 3 ▶ if( $ARGV[0] =~ /^-/ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($ARGV[0] =~ /^-/) 0 3 3
266 # The first argv is an option. Assume search
267 0 return 'search';
268 }
269  
270 ## Ok the first argv is a normal string.
271 ## Attempt loading a command class.
272 3 my $command_class = eval{ Class::Load::load_class(__PACKAGE__.'::Command::'.String::CamelCase::camelize($ARGV[0])) };
273 3 ▶ if( $command_class ){
Branch: 1 branch
BranchTrueFalseTotal
if ($command_class) 3 0 3
274 # Valid command class. Return it.
275 3 return shift @ARGV;
276 };
277  
278  
279 ## This first word is not a valid commnad class.
280 ## Assume search.
281 0 return 'search';
282  
283 }
284  
285 3 ▶sub _build_command{
Subroutine _build_command: called CC=1 SCAR=0.0
286 3 my ($self) = @_;
287 3 my $command_class = Class::Load::load_class(__PACKAGE__.'::Command::'.String::CamelCase::camelize($self->command_name()));
288 3 my $command = $command_class->new({ cse => $self });
289 3 return $command;
290 }
291  
292 3 ▶sub _build_options_specs{
Subroutine _build_options_specs: called CC=1 SCAR=0.0
293 3 my ($self) = @_;
294 3 return $self->command()->options_specs();
295 }
296  
297 3 ▶sub _build_options{
Subroutine _build_options: called CC=1 SCAR=0.0
298 3 my ($self) = @_;
299  
300 3 my %options = ();
301  
302 3 my $p = Getopt::Long::Parser->new;
303 ## Avoid capturing unknown options, like -hello
304 3 $p->configure( 'pass_through' );
305 # Beware that accessing options_specs will consume the command as the first ARGV
306 3 $p->getoptions(\%options , 'idx=s', 'dir=s', 'max-size=i', 'verbose+', @{$self->options_specs()} );
307 3 return \%options;
308 }
309  
310 1 ▶sub _build_args{
Subroutine _build_args: called CC=1 SCAR=0.0
311 1 my ($self) = @_;
312 1 $self->options();
313 1 my @args = @ARGV;
314 1 return \@args;
315 }
316  
317 11 my $standard_log = q|
318 log4perl.rootLogger= INFO, Screen
319 log4perl.appender.Screen = Log::Log4perl::Appender::Screen
320 log4perl.appender.Screen.stderr = 0
321 log4perl.appender.Screen.layout = Log::Log4perl::Layout::PatternLayout
322 log4perl.appender.Screen.layout.ConversionPattern=%m%n
323 |;
324  
325 11 my $verbose_log = q|
326 log4perl.rootLogger= TRACE, Screen
327 log4perl.appender.Screen = Log::Log4perl::Appender::Screen
328 log4perl.appender.Screen.stderr = 0
329 log4perl.appender.Screen.layout = Log::Log4perl::Layout::PatternLayout
330 log4perl.appender.Screen.layout.ConversionPattern = %d [%p] %m%n
331 |;
332  
333  
334 2 ▶sub main{
Subroutine main: called CC=3 SCAR=11.4
Pod undocumented
335 2 my ($self) = @_;
336  
337 2 ▶ unless( Log::Log4perl->initialized() ){
Branch: 1 branch
BranchTrueFalseTotal
unless ("Log::Log4perl"->initialized) 2 0 2
338  
339 2 binmode STDOUT , ':utf8';
340 2 binmode STDERR , ':utf8';
341  
342 2 ▶ if( $self->options()->{verbose} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->options->{'verbose'}) { } 2 0 2
343 2 Log::Log4perl::init(\$verbose_log);
344 }else{
345 0 Log::Log4perl::init(\$standard_log);
346 }
347 }
348  
349 2 return $self->command()->execute();
350 }
351  
352 0 ▶sub save_index_meta{
Subroutine save_index_meta: not called CC=1 SCAR=6.9
Pod undocumented
353 0 my ($self) = @_;
354 0 File::Slurp::write_file($self->index_meta_file().'' , { binmode => ':raw' }, JSON::encode_json($self->index_meta));
355 0 return 1;
356 }
357  
358 1 ▶sub save_dirty_files{
Subroutine save_dirty_files: called CC=1 SCAR=0.0
Pod undocumented
359 1 my ($self) = @_;
360 1 File::Slurp::write_file($self->index_dirty_file().'' , { binmode => ':raw' }, JSON::encode_json($self->dirty_files));
361 1 return 1;
362 }
363  
364 2 ▶sub version{
Subroutine version: called CC=2 SCAR=7.2
Pod undocumented
365 2 my ($self) = @_;
366 2 ▶ return $App::CSE::VERSION || 'dev';
Condition50%1/2
$App::CSE::VERSION || 'dev'
l!l
20
MC/DC0%0/1
$App::CSE::VERSION || 'dev'
$App::CSE::VERSION
Truth table50%1/2
A
A= $App::CSE::VERSION
Aresult
11
00
367 }
368  
369  
370 # Performs very basic checks on a filename, see if its valid
371 # for indexing.
372 0 ▶sub is_file_valid{
Subroutine is_file_valid: not called CC=8 SCAR=42.8
Pod undocumented
373 0 my ($self, $file_name , $opts ) = @_;
374  
375 0 ▶ unless( defined( $opts ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts) 0 0 0
376 0 $opts = {};
377 }
378  
379 0 ▶ if( $self->ignore_reassembl()->match( $file_name ) ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->ignore_reassembl->match($file_name)) 0 0 0
380 0 $LOGGER->trace("File $file_name is ignoreed. Skipping");
381 0 ▶ return $opts->{on_skip} ? &{$opts->{on_skip}}() : undef;
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'on_skip'} ? : 0 0 0
382 }
383  
384 0 ▶ if( $file_name =~ /(?:\/|^)\.[^\/\.]+/ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($file_name =~ m[(?:/|^)\.[^/\.]+]) 0 0 0
385 0 $LOGGER->trace("File $file_name is hidden. Skipping");
386 0 ▶ return $opts->{on_hidden} ? &{$opts->{on_hidden}}() : undef;
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'on_hidden'} ? : 0 0 0
387 }
388  
389 0 ▶ unless( -r $file_name ){
Branch: 1 branch
BranchTrueFalseTotal
unless (-r $file_name) 0 0 0
390 0 $LOGGER->warn("Cannot read $file_name. Skipping");
391 0 ▶ return $opts->{on_unreadable} ? &{$opts->{on_unreadable}}() : undef;
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'on_unreadable'} ? : 0 0 0
392 }
393  
394 0 return 1;
395 }
396  
397  
398 # Returns this file mimetype if we find its
399 # not a blacklisted one.
400 {
401 11 my $BLACK_LIST = {
402 'application/x-trash' => 1
403 };
404 0 ▶ sub valid_mime_type{
Subroutine valid_mime_type: not called CC=3 SCAR=24.8
Pod undocumented
405 0 my ($self, $file_name , $opts) = @_;
406 0 ▶ my $mime_type = File::MimeInfo::Magic::mimetype($file_name.'') || 'application/octet-stream';
Condition0%0/2
File::MimeInfo::Magic::mimetype($file_name . '') || 'application/octet-stream'
l!l
00
MC/DC0%0/1
File::MimeInfo::Magic::mimetype($file_name . '') || 'application/octet-stream'
File::MimeInfo::Magic::mimetype($file_name . '')
Truth table0%0/2
A
A= File::MimeInfo::Magic::mimetype($file_name . '')
Aresult
11
00
407  
408 0 ▶ if( $BLACK_LIST->{$mime_type} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($BLACK_LIST->{$mime_type}) 0 0 0
409 0 return;
410 }
411 0 return $mime_type;
412 }
413  
414 }
415 11 __PACKAGE__->meta->make_immutable();
« CSE/Command/Unwatch.pm ↑ Summary CSE/Command/Search.pm »