Debian Codegraph @

lib/Analizo/Batch/Job.pm

raw
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 use strict;
7 use warnings;
8 use parent qw(Class::Accessor::Fast Analizo::Filter::Client);
9 use File::Basename;
10 use File::Temp qw/ tempfile /;
11 use File::HomeDir;
12 use File::Spec;
13 use File::Temp qw/ tempdir /;
14 use Digest::SHA;
15 use CHI;
16
17 use Analizo;
18 use Analizo::Model;
19 use Analizo::Extractor;
20 use Analizo::Metrics;
21
22 __PACKAGE__->mk_accessors(qw(model metrics id directory extractor));
23 __PACKAGE__->mk_accessors(qw(includedirs libdirs libs));
24
25 sub new {
26 my ($class, @options) = @_;
27 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 sub prepare {
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 sub cleanup {
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 sub parallel_prepare {
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 sub parallel_cleanup {
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 sub metadata {
95 []
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 sub metadata_hashref($) {
115 my ($self) = @_;
116 my %hash = map { $_->[0] => $_->[1] } @{$self->metadata()};
117 return \%hash;
118 }
119
120 sub execute {
121 my ($self) = @_;
122
123 $self->prepare();
124
125 my $tree_id = $self->tree_id();
126
127 # extract model from source
128 my $model_cache_key = "model://$tree_id";
129 my $model = $self->cache->get($model_cache_key);
130 if (!defined $model) {
131 $model = Analizo::Model->new;
132 my %options = (
133 model => $model,
134 includedirs => $self->includedirs,
135 libdirs => $self->libdirs,
136 libs => $self->libs,
137 );
138 my @extractors = (
139 Analizo::Extractor->load($self->extractor, %options),
140 );
141 for my $extractor (@extractors) {
142 $self->share_filters_with($extractor);
143 $extractor->process('.');
144 }
145 $model->build_graphs;
146 $self->cache->set($model_cache_key, $model);
147 }
148 $self->model($model);
149
150 # calculate metrics
151 my $metrics_cache_key = "metrics://$tree_id";
152 my $metrics = $self->cache->get($metrics_cache_key);
153 if (!defined $metrics) {
154 $metrics = Analizo::Metrics->new(model => $self->model);
155 $metrics->data();
156 $self->cache->set($metrics_cache_key, $metrics);
157 }
158 $self->metrics($metrics);
159
160 $self->cleanup();
161 }
162
163 sub project_name($) {
164 my ($self) = @_;
165 return basename($self->directory);
166 }
167
168 sub cache($) {
169 my ($self) = @_;
170 $self->{cache} ||= CHI->new(driver => 'File', root_dir => _get_cache_dir());
171 }
172
173 sub _get_cache_dir {
174 if ($ENV{ANALIZO_CACHE}) {
175 return $ENV{ANALIZO_CACHE};
176 }
177
178 # automated test environment should not mess with the real cache
179 my @program_path = File::Spec->splitdir($0);
180 if ($program_path[0] eq '.') {
181 shift @program_path;
182 }
183 if ($program_path[0] eq 't') {
184 return tempdir(CLEANUP => 1);
185 }
186
187 return File::Spec->catfile(File::HomeDir->my_home, '.cache', 'analizo', $Analizo::VERSION)
188 }
189
190 sub tree_id($) {
191 my ($self) = @_;
192 my @input = sort($self->apply_filters('.'));
193 my $sha1 = Digest::SHA->new;
194 foreach my $input_file (@input) {
195 $sha1->addfile($input_file);
196 }
197 return $sha1->hexdigest;
198 }
199
200 1;