| 1 | # This class represents one execution of `analizo metrics` on the current | ||||||||||||||||||||
| 2 | # directory. Subclasses must implement specific ways of obtaining the source | ||||||||||||||||||||
| 3 | # code to be analysed. | ||||||||||||||||||||
| 4 | package Analizo::Batch::Job; | ||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 18 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 18 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 18 | ▶ | use parent qw(Class::Accessor::Fast Analizo::Filter::Client); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 18 | ▶ | use File::Basename; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 18 | ▶ | use File::Temp qw/ tempfile /; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 18 | ▶ | use File::HomeDir; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | 18 | ▶ | use File::Spec; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | 18 | ▶ | use File::Temp qw/ tempdir /; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | 18 | ▶ | use Digest::SHA; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | 18 | ▶ | use CHI; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | 18 | ▶ | use Analizo; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 18 | 18 | ▶ | use Analizo::Model; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 19 | 18 | ▶ | use Analizo::Extractor; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 20 | 18 | ▶ | use Analizo::Metrics; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | 18 | __PACKAGE__->mk_accessors(qw(model metrics id directory extractor)); | |||||||||||||||||||
| 23 | 18 | __PACKAGE__->mk_accessors(qw(includedirs libdirs libs)); | |||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | 97 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 26 | 97 | my ($class, @options) = @_; | |||||||||||||||||||
| 27 | 97 | return bless { @options }, $class; | |||||||||||||||||||
| 28 | } | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | # This method must be overriden by by subclasses. | ||||||||||||||||||||
| 31 | # | ||||||||||||||||||||
| 32 | # This method must do all the work required to leave the current process in the | ||||||||||||||||||||
| 33 | # directory whose source code must be analysed. B<This may include changing the | ||||||||||||||||||||
| 34 | # current directory>. | ||||||||||||||||||||
| 35 | # | ||||||||||||||||||||
| 36 | # Everything that is done in this method must be undone in the I<cleanup> | ||||||||||||||||||||
| 37 | # method. | ||||||||||||||||||||
| 38 | 1 | ▶ | sub prepare { | ||||||||||||||||||
|
Subroutine
prepare: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 39 | } | ||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | # This method must be overriden by by subclasses. | ||||||||||||||||||||
| 42 | # | ||||||||||||||||||||
| 43 | # This method must clean up after the I<prepare> method. For example, if | ||||||||||||||||||||
| 44 | # I<prepare> chenged the working directory of the current process, cleanup must | ||||||||||||||||||||
| 45 | # change the working directory back to the working directory that was the | ||||||||||||||||||||
| 46 | # current before I<prepare> runs. | ||||||||||||||||||||
| 47 | 0 | ▶ | sub cleanup { | ||||||||||||||||||
|
Subroutine
cleanup: not called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 48 | } | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | # When this method is called, the job must activate its mode for safe parallel | ||||||||||||||||||||
| 51 | # processing. When such mode is activated, the job must be prepared, executed | ||||||||||||||||||||
| 52 | # and cleaned in a way that it is possible to process several jobs in parallel. | ||||||||||||||||||||
| 53 | # This means for example that if any change is required in the directory where | ||||||||||||||||||||
| 54 | # the job will run, the job must first make copy of that directory and operate | ||||||||||||||||||||
| 55 | # over it instead of operating over the original directory. | ||||||||||||||||||||
| 56 | # | ||||||||||||||||||||
| 57 | # Analizo's parallelism model uses separate PROCESSES and not separate threads. | ||||||||||||||||||||
| 58 | # This is due to the fact that to process some jobs, Analizo will need to | ||||||||||||||||||||
| 59 | # chdir() into different directories, and if we used threads this would cause | ||||||||||||||||||||
| 60 | # problems since the current work directory is shared by all threads, and | ||||||||||||||||||||
| 61 | # therefore one thread would interfere with the others when using chdir(). | ||||||||||||||||||||
| 62 | # | ||||||||||||||||||||
| 63 | # This method must be inherited by subclasses. | ||||||||||||||||||||
| 64 | # | ||||||||||||||||||||
| 65 | # When called, it will be called BEFORE the B<prepare> method. After the first | ||||||||||||||||||||
| 66 | # job has its B<parallel_prepare> method called, any subsequent invocations of | ||||||||||||||||||||
| 67 | # this method on jobs in the same process must be indepotent. | ||||||||||||||||||||
| 68 | 0 | ▶ | sub parallel_prepare { | ||||||||||||||||||
|
Subroutine
parallel_prepare: not called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 69 | } | ||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | # This method must cleanup any resources that may have been created to support | ||||||||||||||||||||
| 72 | # parallel execution. Such resources were probably allocated by the | ||||||||||||||||||||
| 73 | # B<parallel_safe> method. | ||||||||||||||||||||
| 74 | # | ||||||||||||||||||||
| 75 | # This method will not be called for all jobs in a given batch. I will be | ||||||||||||||||||||
| 76 | # called only for the last job in the batch for the current process, which will | ||||||||||||||||||||
| 77 | # be responsible for releasing any resources that were created for supporting | ||||||||||||||||||||
| 78 | # the execution of jobs by a given process. | ||||||||||||||||||||
| 79 | 0 | ▶ | sub parallel_cleanup { | ||||||||||||||||||
|
Subroutine
parallel_cleanup: not called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 80 | } | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | # This method must return metadata about this job, in the form of an ARRAY | ||||||||||||||||||||
| 83 | # reference. Each element of the ARRAY must be itself an ARRAY with two | ||||||||||||||||||||
| 84 | # elements, the first the name of the field and the second the value of the | ||||||||||||||||||||
| 85 | # field. Both field names and values must be SCALARs. Example: | ||||||||||||||||||||
| 86 | # | ||||||||||||||||||||
| 87 | # return [ | ||||||||||||||||||||
| 88 | # ['field1', 'value1'], | ||||||||||||||||||||
| 89 | # ['field2', 10], | ||||||||||||||||||||
| 90 | # ]; | ||||||||||||||||||||
| 91 | # | ||||||||||||||||||||
| 92 | # The implementation in this class returns an empty ARRAY reference. This | ||||||||||||||||||||
| 93 | # method may be overriden by subclasses. | ||||||||||||||||||||
| 94 | 0 | ▶ | sub metadata { | ||||||||||||||||||
|
Subroutine
metadata: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 95 | 0 | [] | |||||||||||||||||||
| 96 | } | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | # Returns the same metadata as the B<metadata> method, but as a HASH reference | ||||||||||||||||||||
| 99 | # instead of an ARRAY reference. For example, assume that B<metadata> returns | ||||||||||||||||||||
| 100 | # the following: | ||||||||||||||||||||
| 101 | # | ||||||||||||||||||||
| 102 | # [ | ||||||||||||||||||||
| 103 | # ['field1', 'value1'], | ||||||||||||||||||||
| 104 | # ['field2', 10], | ||||||||||||||||||||
| 105 | # ] | ||||||||||||||||||||
| 106 | # | ||||||||||||||||||||
| 107 | # In this case, B<metadata_hashref> must return the following: | ||||||||||||||||||||
| 108 | # | ||||||||||||||||||||
| 109 | # { | ||||||||||||||||||||
| 110 | # 'field1' => 'value1', | ||||||||||||||||||||
| 111 | # 'field2' => 10, | ||||||||||||||||||||
| 112 | # } | ||||||||||||||||||||
| 113 | # | ||||||||||||||||||||
| 114 | 0 | ▶ | sub metadata_hashref($) { | ||||||||||||||||||
|
Subroutine
metadata_hashref: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 115 | 0 | my ($self) = @_; | |||||||||||||||||||
| 116 | 0 | my %hash = map { $_->[0] => $_->[1] } @{$self->metadata()}; | |||||||||||||||||||
| 117 | 0 | return \%hash; | |||||||||||||||||||
| 118 | } | ||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | 3 | ▶ | sub execute { | ||||||||||||||||||
|
Subroutine
execute: called CC=4 SCAR=18.3
Pod
undocumented
| |||||||||||||||||||||
| 121 | 3 | my ($self) = @_; | |||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | 3 | $self->prepare(); | |||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | 3 | my $tree_id = $self->tree_id(); | |||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | # extract model from source | ||||||||||||||||||||
| 128 | 3 | my $model_cache_key = "model://$tree_id"; | |||||||||||||||||||
| 129 | 3 | my $model = $self->cache->get($model_cache_key); | |||||||||||||||||||
| 130 | 3 | ▶ | if (!defined $model) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 131 | 3 | $model = Analizo::Model->new; | |||||||||||||||||||
| 132 | 3 | my %options = ( | |||||||||||||||||||
| 133 | model => $model, | ||||||||||||||||||||
| 134 | includedirs => $self->includedirs, | ||||||||||||||||||||
| 135 | libdirs => $self->libdirs, | ||||||||||||||||||||
| 136 | libs => $self->libs, | ||||||||||||||||||||
| 137 | ); | ||||||||||||||||||||
| 138 | 3 | my @extractors = ( | |||||||||||||||||||
| 139 | Analizo::Extractor->load($self->extractor, %options), | ||||||||||||||||||||
| 140 | ); | ||||||||||||||||||||
| 141 | 3 | for my $extractor (@extractors) { | |||||||||||||||||||
| 142 | 3 | $self->share_filters_with($extractor); | |||||||||||||||||||
| 143 | 3 | $extractor->process('.'); | |||||||||||||||||||
| 144 | } | ||||||||||||||||||||
| 145 | 0 | $model->build_graphs; | |||||||||||||||||||
| 146 | 0 | $self->cache->set($model_cache_key, $model); | |||||||||||||||||||
| 147 | } | ||||||||||||||||||||
| 148 | 0 | $self->model($model); | |||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | # calculate metrics | ||||||||||||||||||||
| 151 | 0 | my $metrics_cache_key = "metrics://$tree_id"; | |||||||||||||||||||
| 152 | 0 | my $metrics = $self->cache->get($metrics_cache_key); | |||||||||||||||||||
| 153 | 0 | ▶ | if (!defined $metrics) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 154 | 0 | $metrics = Analizo::Metrics->new(model => $self->model); | |||||||||||||||||||
| 155 | 0 | $metrics->data(); | |||||||||||||||||||
| 156 | 0 | $self->cache->set($metrics_cache_key, $metrics); | |||||||||||||||||||
| 157 | } | ||||||||||||||||||||
| 158 | 0 | $self->metrics($metrics); | |||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | 0 | $self->cleanup(); | |||||||||||||||||||
| 161 | } | ||||||||||||||||||||
| 162 | |||||||||||||||||||||
| 163 | 3 | ▶ | sub project_name($) { | ||||||||||||||||||
|
Subroutine
project_name: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 164 | 3 | my ($self) = @_; | |||||||||||||||||||
| 165 | 3 | return basename($self->directory); | |||||||||||||||||||
| 166 | } | ||||||||||||||||||||
| 167 | |||||||||||||||||||||
| 168 | 3 | ▶ | sub cache($) { | ||||||||||||||||||
|
Subroutine
cache: called CC=2 SCAR=8.1
Pod
undocumented
| |||||||||||||||||||||
| 169 | 3 | my ($self) = @_; | |||||||||||||||||||
| 170 | 3 | ▶ | $self->{cache} ||= CHI->new(driver => 'File', root_dir => _get_cache_dir()); | ||||||||||||||||||
|
Condition33%1/3
$self->{"cache"} ||= "CHI"->new("driver", "File", "root_dir", _get_cache_dir())
MC/DC0%0/2
$self->{"cache"} ||= "CHI"->new("driver", "File", "root_dir", _get_cache_dir())
$self->{"cache"}
"CHI"->new("driver", "File", "root_dir", _get_cache_dir())
Truth table33%1/3
A ||= B
A= $self->{"cache"}B= "CHI"->new("driver", "File", "root_dir", _get_cache_dir())
| |||||||||||||||||||||
| 171 | } | ||||||||||||||||||||
| 172 | |||||||||||||||||||||
| 173 | 3 | ▶ | sub _get_cache_dir { | ||||||||||||||||||
|
Subroutine
_get_cache_dir: called CC=4 SCAR=24.6
| |||||||||||||||||||||
| 174 | 3 | ▶ | if ($ENV{ANALIZO_CACHE}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 175 | 3 | return $ENV{ANALIZO_CACHE}; | |||||||||||||||||||
| 176 | } | ||||||||||||||||||||
| 177 | |||||||||||||||||||||
| 178 | # automated test environment should not mess with the real cache | ||||||||||||||||||||
| 179 | 0 | my @program_path = File::Spec->splitdir($0); | |||||||||||||||||||
| 180 | 0 | ▶ | if ($program_path[0] eq '.') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 181 | 0 | shift @program_path; | |||||||||||||||||||
| 182 | } | ||||||||||||||||||||
| 183 | 0 | ▶ | if ($program_path[0] eq 't') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 184 | 0 | return tempdir(CLEANUP => 1); | |||||||||||||||||||
| 185 | } | ||||||||||||||||||||
| 186 | |||||||||||||||||||||
| 187 | 0 | return File::Spec->catfile(File::HomeDir->my_home, '.cache', 'analizo', $Analizo::VERSION) | |||||||||||||||||||
| 188 | } | ||||||||||||||||||||
| 189 | |||||||||||||||||||||
| 190 | 3 | ▶ | sub tree_id($) { | ||||||||||||||||||
|
Subroutine
tree_id: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 191 | 3 | my ($self) = @_; | |||||||||||||||||||
| 192 | 3 | my @input = sort($self->apply_filters('.')); | |||||||||||||||||||
| 193 | 3 | my $sha1 = Digest::SHA->new; | |||||||||||||||||||
| 194 | 3 | foreach my $input_file (@input) { | |||||||||||||||||||
| 195 | 21 | $sha1->addfile($input_file); | |||||||||||||||||||
| 196 | } | ||||||||||||||||||||
| 197 | 3 | return $sha1->hexdigest; | |||||||||||||||||||
| 198 | } | ||||||||||||||||||||
| 199 | |||||||||||||||||||||
| 200 | 18 | 1; | |||||||||||||||||||