summaryrefslogtreecommitdiffstats
path: root/lib/Lintian/Pool.pm
blob: db153f9897fab32078127bb98999100e6182c1b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
# Copyright (C) 2011 Niels Thykier <niels@thykier.net>
# Copyright (C) 2020-2021 Felix Lechner
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, you can find it on the World Wide
# Web at https://www.gnu.org/copyleft/gpl.html, or write to the Free
# Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
# MA 02110-1301, USA.

## Represents a pool of processables (Lintian::Processable)
package Lintian::Pool;

use v5.20;
use warnings;
use utf8;

use Const::Fast;
use Cwd qw(getcwd);
use List::SomeUtils qw(any);
use Time::HiRes qw(gettimeofday tv_interval);
use Path::Tiny;
use POSIX qw(:sys_wait_h);
use Proc::ProcessTable;
use Unicode::UTF8 qw(encode_utf8);

use Lintian::Group;

const my $SPACE => q{ };
const my $COMMA => q{,};
const my $SEMICOLON => q{;};
const my $LEFT_PARENS => q{(};
const my $RIGHT_PARENS => q{)};
const my $PLURAL_S => q{s};

const my $ANY_CHILD => -1;
const my $WORLD_WRITABLE_FOLDER => oct(777);

use Moo;
use namespace::clean;

=head1 NAME

Lintian::Pool -- Pool of processables

=head1 SYNOPSIS

 use Lintian::Pool;
 
 my $pool = Lintian::Pool->new;
 $pool->add_file('foo.changes');
 $pool->add_file('bar.dsc');
 $pool->add_file('baz.deb');
 $pool->add_file('qux.buildinfo');
 foreach my $gname ($pool->get_group_names){
    my $group = $pool->get_group($gname);
    process($gname, $group);
 }

=head1 METHODS

=over 4

=item $pool->groups

Returns a hash reference to the list of processable groups that are currently
in the pool. The key is a unique identifier based on name and version.

=item C<savedir>

=cut

has groups => (is => 'rw', default => sub{ {} });

has savedir => (is => 'rw', default => sub{ getcwd; });

# must be absolute; frontend/lintian depends on it
has basedir => (
    is => 'rw',
    default => sub {

        my $absolute
          = Path::Tiny->tempdir(TEMPLATE => 'lintian-pool-XXXXXXXXXX');

        $absolute->mkpath({mode => $WORLD_WRITABLE_FOLDER});

        return $absolute;
    }
);

=item $pool->basedir

Returns the base directory for the pool. Most likely it's a temporary directory.

=item $pool->add_group($group)

Adds a group to the pool.

=cut

sub add_group {
    my ($self, $group) = @_;

    my $name = $group->name;

    unless (exists $self->groups->{$name}){

        # group does not exist; just add whole
        $self->groups->{$name} = $group;

        return 1;
    }

    # group exists; merge & accept all new
    my $added = 0;

    my $old = $self->groups->{$name};

    for my $type (qw/source buildinfo changes/) {

        if (!defined $old->$type && defined $group->$type) {
            $old->add_processable($group->$type);
            $added = 1;
        }
    }

    for my $installable ($group->get_installables){
        # New binary package ?
        my $was_new = $old->add_processable($installable);
        $added ||= $was_new;
    }

    return $added;
}

=item $pool->process

Process the pool.

=cut

sub process{
    my ($self, $PROFILE, $exit_code_ref, $option)= @_;

    if ($self->empty) {
        say {*STDERR} encode_utf8('No packages selected.');
        return;
    }

    my %reported_count;
    my %override_count;
    my %ignored_overrides;
    my $unused_overrides = 0;

    for my $group (values %{$self->groups}) {

        my $total_start = [gettimeofday];

        $group->profile($PROFILE);
        $group->jobs($option->{'jobs'});

        my $success= $group->process(\%ignored_overrides, $option);

        for my $processable ($group->get_processables){

            my @keep;
            for my $hint (@{$processable->hints}) {

                my $tag = $PROFILE->get_tag($hint->tag_name);

                # discard experimental tags
                next
                  if $tag->experimental
                  && !$option->{'display-experimental'};

                # discard overridden tags
                next
                  if defined $hint->override
                  && !$option->{'show-overrides'};

                # discard outside the selected display level
                next
                  unless $PROFILE->display_level_for_tag($hint->tag_name);

                if (!defined $hint->override) {

                    ++$reported_count{$tag->visibility}
                      if !$tag->experimental;

                    ++$reported_count{experimental}
                      if $tag->experimental;
                }

                ++$reported_count{override}
                  if defined $hint->override;

                ++$unused_overrides
                  if $hint->tag_name eq 'unused-override'
                  || $hint->tag_name eq 'mismatched-override';

                push(@keep, $hint);
            }

            $processable->hints(\@keep);
        }

        ${$exit_code_ref} = 2
          if $success && any { $reported_count{$_} } @{$option->{'fail-on'}};

        # interruptions can leave processes behind (manpages); wait and reap
        if (${$exit_code_ref} == 1) {
            1 while waitpid($ANY_CHILD, WNOHANG) > 0;
        }

        if ($option->{debug}) {
            my $process_table = Proc::ProcessTable->new;
            my @leftover= grep { $_->ppid == $$ } @{$process_table->table};

            # announce left over processes, see commit 3bbcc3b
            if (@leftover) {
                warn encode_utf8(
                    "\nSome processes were left over (maybe unreaped):\n");

                my $FORMAT = '    %-12s %-12s %-8s %-24s %s';
                say encode_utf8(
                    sprintf(
                        $FORMAT,'PID', 'TTY', 'STATUS', 'START', 'COMMAND'
                    )
                );

                say encode_utf8(
                    sprintf($FORMAT,
                        $_->pid,$_->ttydev,
                        $_->state,scalar(localtime($_->start)),
                        $_->cmndline)
                )for @leftover;

                ${$exit_code_ref} = 1;
                die encode_utf8("Aborting.\n");
            }
        }

        my $total_raw_res = tv_interval($total_start);
        my $total_tres = sprintf('%.3fs', $total_raw_res);

        my $status = $success ? 'complete' : 'error';
        say {*STDERR}
          encode_utf8($status . $SPACE . $group->name . " ($total_tres)")
          if $option->{'status-log'};
        say {*STDERR} encode_utf8('Finished processing group ' . $group->name)
          if $option->{debug};

        ${$exit_code_ref} = 1
          unless $success;
    }

    my $OUTPUT;
    if ($option->{'output-format'} eq 'html') {
        require Lintian::Output::HTML;
        $OUTPUT = Lintian::Output::HTML->new;
    } elsif ($option->{'output-format'} eq 'json') {
        require Lintian::Output::JSON;
        $OUTPUT = Lintian::Output::JSON->new;
    } elsif ($option->{'output-format'} eq 'universal') {
        require Lintian::Output::Universal;
        $OUTPUT = Lintian::Output::Universal->new;
    } else {
        require Lintian::Output::EWI;
        $OUTPUT = Lintian::Output::EWI->new;
    }

    # pass everything, in case some groups or processables have no hints
    $OUTPUT->issue_hints($PROFILE, [values %{$self->groups}], $option);

    my $errors = $override_count{error} // 0;
    my $warnings = $override_count{warning} // 0;
    my $info = $override_count{info} // 0;
    my $total = $errors + $warnings + $info;

    if (   $option->{'output-format'} eq 'ewi'
        && !$option->{'no-override'}
        && !$option->{'show-overrides'}
        && ($total > 0 || $unused_overrides > 0)) {

        my @details;
        push(@details, quantity($errors, 'error'))
          if $errors;
        push(@details, quantity($warnings, 'warning'))
          if $warnings;
        push(@details, "$info info")
          if $info;

        my $text = quantity($total, 'hint') . ' overridden';
        $text
          .= $SPACE
          . $LEFT_PARENS
          . join($COMMA . $SPACE, @details)
          . $RIGHT_PARENS
          if @details;
        $text
          .= $SEMICOLON
          . $SPACE
          . quantity($unused_overrides, 'unused override');

        say encode_utf8("N: $text");
    }

    if ($option->{'output-format'} eq 'ewi' && %ignored_overrides) {
        say encode_utf8('N: Some overrides were ignored.');

        if ($option->{verbose}) {
            say encode_utf8(
'N: The following tags had at least one override but are mandatory:'
            );
            say encode_utf8("N:   - $_") for sort keys %ignored_overrides;

        } else {
            say encode_utf8('N: Use --verbose for more information.');
        }
    }

    path($self->basedir)->remove_tree
      if length $self->basedir && -d $self->basedir;

    return;
}

=item quantity

=cut

sub quantity {
    my ($count, $unit) = @_;

    my $text = $count . $SPACE . $unit;
    $text .= $PLURAL_S
      unless $count == 1;

    return $text;
}

=item $pool->get_group_names

Returns the name of all the groups in this pool.

Do not modify the list nor its contents.

=cut

sub get_group_names{
    my ($self) = @_;

    return keys %{ $self->groups };
}

=item $pool->get_group($name)

Returns the group called $name or C<undef>
if there is no group called $name.

=cut

sub get_group{
    my ($self, $group) = @_;

    return $self->groups->{$group};
}

=item $pool->empty

Returns true if the pool is empty.

=cut

sub empty{
    my ($self) = @_;

    return scalar keys %{$self->groups} == 0;
}

=back

=head1 AUTHOR

Originally written by Niels Thykier <niels@thykier.net> for Lintian.

=head1 SEE ALSO

lintian(1)

L<Lintian::Processable>

L<Lintian::Group>

=cut

1;

# Local Variables:
# indent-tabs-mode: nil
# cperl-indent-level: 4
# End:
# vim: syntax=perl sw=4 sts=4 sr et