blib/lib/PMLTQ/Command/printtrees.pm

filter: Statementstmt 37.1% 39 / 105 Branchbran 2.9% 1 / 34 Conditioncond 3.1% 1 / 32 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 55.5% 10 / 18 Podpod 0.0% 0 / 7 totaltotal 23.7% 51 / 215 CC 30 SCAR 53.6
CC
30
Cov
24%
run
462.0
DEFAULT_CONFIG
42.0
load_filelist
12.0
CRAP 213.3
↑ Summary
1 package PMLTQ::Command::printtrees;
2 1 our $AUTHORITY = 'cpan:MATY';
3 1 $PMLTQ::Command::printtrees::VERSION = '1.0.1';
4 # ABSTRACT: generate svg trees for given treebank
5  
6 1 ▶use PMLTQ::Base 'PMLTQ::Command';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use PMLTQ;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use File::Which;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use File::Path qw( make_path );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use File::Basename qw/fileparse dirname/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use File::ShareDir 'dist_dir';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Hash::Merge 'merge';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Parallel::ForkManager;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 0 ▶has usage => sub { shift->extract_usage };
Subroutine __ANON__: not called CC=1 SCAR=1.2
17  
18 1 my $local_shared_dir = File::Spec->catdir(dirname(__FILE__), File::Spec->updir, File::Spec->updir, File::Spec->updir, 'share');
19 1 my $shared_dir = eval { dist_dir('PMLTQ-Command-printtrees') };
20  
21 # Assume installation
22 1 ▶if (-d $local_shared_dir or !$shared_dir) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $local_shared_dir or not $shared_dir) 0 1 1
Condition33%1/3
-d $local_shared_dir or not $shared_dir
l!l&&r!l&&!r
001
MC/DC0%0/2
-d $local_shared_dir or not $shared_dir
-d $local_shared_dir not $shared_dir
Truth table33%1/3
A or not(B)
A= -d $local_shared_dirB= not $shared_dir
ABresult
1X1
011
000
23 0 $shared_dir = $local_shared_dir;
24 }
25  
26 0 ▶sub DEFAULT_CONFIG {
Subroutine DEFAULT_CONFIG: not called CC=6 SCAR=37.4
Pod undocumented
27 0 ▶ my $btred = shift || $ENV{BTRED};
Condition0%0/3
shift() || $ENV{'BTRED'}
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $ENV{'BTRED'}
shift() $ENV{'BTRED'}
Truth table0%0/3
A || B
A= shift()B= $ENV{'BTRED'}
ABresult
1X1
011
000
28 0 ▶ ($btred && -f $btred ) || which('btred') || die 'path to btred is not in --printserver-btred option nor in BTRED variable nor in PATH';
Branch: 1 branch
BranchTrueFalseTotal
$btred and -f $btred or which("btred") or die "path to btred is not in --printserver-btred option nor in BTRED variable nor in PATH" 0 0 0
Condition0%0/6
$btred and -f $btred or which("btred")
l!l&&r!l&&!r
000
$btred and -f $btred
!ll&&!rl&&r
000
MC/DC0%0/3
$btred and -f $btred or which("btred")
$btred -f $btred which("btred")
Truth table0%0/5
A and B or C
A= $btredB= -f $btredC= which("btred")
ABCresult
11X1
0X11
1011
0X00
1000
29 0 my $extensions = join(',', grep {! m/^!/} split("\n",`$btred --very-quiet --list-extensions`));
30 return {
31 0 ▶ btred_rc => File::Spec->catdir(shared_dir(),'btred.rc'),
Condition0%0/3
$btred || which('btred')
l!l&&r!l&&!r
000
MC/DC0%0/2
$btred || which('btred')
$btred which('btred')
Truth table0%0/3
A || B
A= $btredB= which('btred')
ABresult
1X1
011
000
32 tree_dir => 'svg',
33 btred => $btred || which('btred'),
34 extensions => $extensions,
35 parallel => {
36 job_size => 50,
37 forks => 8
38 }
39 };
40 }
41  
42 1 my %opts;
43  
44 0 ▶sub run {
Subroutine run: not called CC=21 SCAR=61.4
Pod undocumented
45 0 my $self = shift;
46 0 my @args = @_;
47 0 my $config = $self->config;
48  
49 0 ▶ my $printtrees_config = merge( $config->{'printtrees'}||{}, DEFAULT_CONFIG(($config->{'printtrees'}||{})->{btred} ));
Condition0%0/4
$config->{'printtrees'} || {}
l!l
00
$config->{'printtrees'} || {}
l!l
00
MC/DC0%0/1
$config->{'printtrees'} || {}
$config->{'printtrees'}
MC/DC0%0/1
$config->{'printtrees'} || {}
$config->{'printtrees'}
Truth table0%0/2
A
A= $config->{'printtrees'}
Aresult
11
00
Truth table0%0/2
A
A= $config->{'printtrees'}
Aresult
11
00
50 0 my $tree_dir = $printtrees_config->{'tree_dir'};
51  
52 0 my $data_dir = $config->{data_dir};
53  
54 0 ▶ unless ($self->test_btred_version($printtrees_config->{'btred'})) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->test_btred_version($printtrees_config->{"btred"})) 0 0 0
55 0 print STDERR 'Minimum required BTrEd version is not satisfied';
56 0 return;
57 }
58  
59 0 ▶ unless ( $config->{layers} && @{ $config->{layers} } > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($config->{"layers"} and @{$config->{"layers"};} > 0) 0 0 0
Condition0%0/3
$config->{"layers"} and @{$config->{"layers"};} > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$config->{"layers"} and @{$config->{"layers"};} > 0
$config->{"layers"} @{$config->{"layers"};} > 0
Truth table0%0/3
A and B
A= $config->{"layers"}B= @{$config->{"layers"};} > 0
ABresult
0X0
100
111
60 0 print STDERR 'Nothing to print, no layers configured';
61 0 return;
62 }
63  
64 0 ▶ unless ( -d $tree_dir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $tree_dir) 0 0 0
65 0 ▶ make_path($tree_dir) or die "Unable to create directory $tree_dir\n";
Branch: 1 branch
BranchTrueFalseTotal
make_path($tree_dir) or die "Unable to create directory $tree_dir\n" 0 0 0
66 0 print "Path '$tree_dir' has been created\n";
67 }
68 0 ▶ print STDERR "WARNING: No extension is loaded !!!" unless $printtrees_config->{extensions};
Branch: 1 branch
BranchTrueFalseTotal
unless $printtrees_config->{"extensions"} 0 0 0
69  
70 0 ▶ unless($printtrees_config->{parallel}->{forks} || $printtrees_config->{parallel}->{forks} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($printtrees_config->{"parallel"}{"forks"} or $printtrees_config->{"parallel"}{"forks"} > 0) 0 0 0
Condition0%0/3
$printtrees_config->{"parallel"}{"forks"} or $printtrees_config->{"parallel"}{"forks"} > 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$printtrees_config->{"parallel"}{"forks"} or $printtrees_config->{"parallel"}{"forks"} > 0
$printtrees_config->{"parallel"}{"forks"} $printtrees_config->{"parallel"}{"forks"} > 0
Truth table0%0/3
A or B
A= $printtrees_config->{"parallel"}{"forks"}B= $printtrees_config->{"parallel"}{"forks"} > 0
ABresult
1X1
011
000
71 0 ▶ print STDERR "invalid --printtrees-parallel-forks value ". ($printtrees_config->{parallel}->{forks}//'undef');
Condition0%0/2
$printtrees_config->{"parallel"}{"forks"} // "undef"
l!l
00
MC/DC0%0/1
$printtrees_config->{"parallel"}{"forks"} // "undef"
$printtrees_config->{"parallel"}{"forks"}
Truth table0%0/2
A
A= $printtrees_config->{"parallel"}{"forks"}
Aresult
11
00
72 0 return 1;
73 }
74  
75 0 ▶ unless($printtrees_config->{parallel}->{job_size} || $printtrees_config->{parallel}->{job_size} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($printtrees_config->{"parallel"}{"job_size"} or $printtrees_config->{"parallel"}{"job_size"} > 0) 0 0 0
Condition0%0/3
$printtrees_config->{"parallel"}{"job_size"} or $printtrees_config->{"parallel"}{"job_size"} > 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$printtrees_config->{"parallel"}{"job_size"} or $printtrees_config->{"parallel"}{"job_size"} > 0
$printtrees_config->{"parallel"}{"job_size"} $printtrees_config->{"parallel"}{"job_size"} > 0
Truth table0%0/3
A or B
A= $printtrees_config->{"parallel"}{"job_size"}B= $printtrees_config->{"parallel"}{"job_size"} > 0
ABresult
1X1
011
000
76 0 ▶ print STDERR "invalid --printtrees-parallel-job_size value ". ($printtrees_config->{parallel}->{job_size}//'undef');
Condition0%0/2
$printtrees_config->{"parallel"}{"job_size"} // "undef"
l!l
00
MC/DC0%0/1
$printtrees_config->{"parallel"}{"job_size"} // "undef"
$printtrees_config->{"parallel"}{"job_size"}
Truth table0%0/2
A
A= $printtrees_config->{"parallel"}{"job_size"}
Aresult
11
00
77 0 return 1;
78 }
79  
80 0 my @layer_files;
81 0 my $pm = Parallel::ForkManager->new($printtrees_config->{parallel}->{forks});
82 0 my $maxlen = 0;
83  
84 0 for my $layer ( @{ $config->{layers} } ) {
85 0 my @layerf = $self->files_for_layer($layer);
86 0 ▶ $maxlen = scalar @layerf > $maxlen ? scalar @layerf : $maxlen;
Branch: 1 branch
BranchTrueFalseTotal
scalar @layerf > $maxlen ? : 0 0 0
87 0 push @layer_files, [@layerf];
88 }
89  
90 0 ▶ my @all_layer_files = map {my $idx = $_; map {defined $_->[$idx] ? $_->[$idx] : ()} @layer_files} (0 .. ($maxlen-1)); # schuffle files - balancing job difficultness
Branch: 1 branch
BranchTrueFalseTotal
defined $_->[$idx] ? : 0 0 0
91 0 my @all_files = ();
92 0 ▶ push @all_files, [ splice @all_layer_files, 0, $printtrees_config->{parallel}->{job_size} ] while @all_layer_files;
Branch: 1 branch
BranchTrueFalseTotal
@all_layer_files and do { push @all_files, [splice(@all_layer_files, 0, $printtrees_config->{"parallel"}{"job_size"})] } 0 0 0
93 0 foreach my $files (@all_files){
94 0 ▶ $pm->start and next;
Branch: 1 branch
BranchTrueFalseTotal
$pm->start and next 0 0 0
95 system($printtrees_config->{btred},
96 '--config-file', $printtrees_config->{btred_rc},
97 '-Z',$self->config->{resources},
98 '-m', File::Spec->catdir(shared_dir(),'print_trees.btred'),
99 '--enable-extensions', $printtrees_config->{extensions},
100 0 '-o',
101 '--data-dir', $data_dir,
102 '--output-dir', $tree_dir,
103 @$files,
104 '--');
105 0 $pm->finish;
106 }
107  
108 0 $pm->wait_all_children;
109 0 return 1;
110 }
111  
112  
113 0 ▶sub files_for_layer {
Subroutine files_for_layer: not called CC=3 SCAR=18.7
Pod undocumented
114 0 my ( $self, $layer ) = @_;
115  
116 0 ▶ if ( $layer->{data} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($layer->{'data'}) { } 0 0 0
elsif ($layer->{'filelist'}) { } 0 0 0
117 0 ▶ return glob( File::Spec->catfile( $self->config->{data_dir}, $layer->{data} ) );
Subroutine BEGIN: called CC=1 SCAR=0.2
118 } elsif ( $layer->{filelist} ) {
119 0 return $self->load_filelist( $layer->{filelist} );
120 }
121 }
122  
123 0 ▶sub load_filelist {
Subroutine load_filelist: not called CC=3 SCAR=24.8
Pod undocumented
124 0 my ( $self, $filelist ) = @_;
125  
126 0 ▶ die "Filelist '$filelist' does not exists or is not readable!" unless -r $filelist;
Branch: 1 branch
BranchTrueFalseTotal
unless -r $filelist 0 0 0
127  
128 0 ▶ map { File::Spec->file_name_is_absolute($_) ? $_ : File::Spec->catfile( $self->config->{data_dir}, $_ ) }
Branch: 1 branch
BranchTrueFalseTotal
'File::Spec'->file_name_is_absolute($_) ? : 0 0 0
129 0 grep { $_ !~ m/^\s*$/ } read_file( $filelist, chomp => 1, binmode => ':utf8' );
130 }
131  
132 0 ▶sub shared_dir { $shared_dir }
Subroutine shared_dir: not called CC=1 SCAR=6.9
Pod undocumented
133  
134 0 ▶sub minimum_btred_version { 2.5157 }
Subroutine minimum_btred_version: not called CC=1 SCAR=6.9
Pod undocumented
135  
136 0 ▶sub test_btred_version {
Subroutine test_btred_version: not called CC=1 SCAR=6.9
Pod undocumented
137 0 my $self = shift;
138 0 my $btred = shift;
139 0 my $version = `$btred --version`;
140 0 $version =~ s/^.*BTrEd\s*([0-9\.]*)\n.*$/$1/ms;
141 0 return $version >= $self->minimum_btred_version;
142 }
143 =head1 SYNOPSIS
144  
145 pmltq printtrees
146  
147 =head1 DESCRIPTION
148  
149 Generate svg trees for given treebank. It works on local PML files.
150  
151 =head1 OPTIONS
152  
153 =over 5
154  
155 =item B<--printtrees-btred>
156  
157 Path to btred script. If not set path in $BTRED variable is used. If neither of previous is set script seeks for btred in $PATH.
158  
159 =item B<--printtrees-btred_rc>
160  
161 Path to btred configuration. The most important settings is path to extensions.
162  
163 =item B<--printtrees-extensions>
164  
165 Comma separated list with extensions. Defaultly are used the same ones as in TrEd (list in extensions.lst optained with `btred --very-quiet --list-extensions`).
166  
167 =item B<--printtrees-tree_dir>
168  
169 Directory where sould be images stored. Default value is 'svg'.
170  
171 =item B<printtrees-parallel-job_size>
172  
173 Size of one job, default is 50.
174  
175 =item B<printtrees-parallel-forks>
176  
177 Maximum number of parallel jobs, default is 8.
178  
179 =back
180  
181 =cut
182  
183 1 1;
↑ Summary