aboutsummaryrefslogtreecommitdiffstats
path: root/lib/Youri/Check/Output/File.pm
blob: 5363e8e619a5f5d63dbb808ae84794aeb4246d09 (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
# $Id: Text.pm 523 2005-10-11 08:36:49Z misc $
package Youri::Check::Output::File;

=head1 NAME

Youri::Check::Output::File - Report results in files

=head1 DESCRIPTION

This plugin reports results in files. Additional subplugins handle specific
formats.

=cut

use warnings;
use strict;
use Carp;
use File::Basename;
use File::Path;
use DateTime;
use Youri::Utils;
use base 'Youri::Check::Output';

sub _init {
    my $self = shift;
    my %options = (
        to      => '', # target directory
        noclean => 0,  # don't clean up target directory
        noempty => 0,  # don't generate empty reports
        formats => undef,
        @_
    );

    croak "no format defined" unless $options{formats};
    croak "formats should be an hashref" unless ref $options{formats} eq 'HASH';

    my $now = DateTime->now(time_zone => 'local');
    my $time = "the " . $now->ymd() . " at " . $now->hms();

    $self->{_to}      = $options{to};
    $self->{_noclean} = $options{noclean};
    $self->{_noempty} = $options{noempty};
    $self->{_time}    = $time;

    foreach my $id (keys %{$options{formats}}) {
        print "Creating format $id\n" if $options{verbose};
        eval {
            push(
                @{$self->{_formats}},
                create_instance(
                    'Youri::Check::Output::File::Format',
                    id        => $id,
                    test      => $options{test},
                    verbose   => $options{verbose},
                    %{$options{formats}->{$id}}
                )
            );
        };
        print STDERR "Failed to create format $id: $@\n" if $@;
    }

    croak "no formats created" unless @{$self->{_formats}};
}

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

    # clean up output directory
    unless ($self->{_test} || $self->{_noclean} || !$self->{_to}) {
        my @files = glob($self->{_to} . '/*');
        rmtree(\@files) if @files;
    }
}

sub _global_report {
    my ($self, $resultset, $type, $columns, $links) = @_;

    foreach my $format (@{$self->{_formats}}) {
        my $iterator = $resultset->get_iterator(
            $type,
            [ 'package' ]
        );

        return if $self->{_noempty} && ! $iterator->has_results();

        my $content = $format->get_report(
            $self->{_time},
            "$type global report",
            $iterator,
            $type,
            $columns,
            $links,
            undef
        );

        # create and register file
        my $extension = $format->extension();
        $self->_write_file(
            "$self->{_to}/$type.$extension",
            $content
        );
        push(
            @{$self->{_files}->{global}->{$type}},
            $extension
        );
    }
}

sub _individual_report {
    my ($self, $resultset, $type, $columns, $links, $maintainer) = @_;

    foreach my $format (@{$self->{_formats}}) {
        my $iterator = $resultset->get_iterator(
            $type,
            [ 'package' ],
            { maintainer => [ $maintainer ] }
        );

        return if $self->{_noempty} && ! $iterator->has_results();

        my $content = $format->get_report(
            $self->{_time},
            "$type individual report for $maintainer",
            $iterator,
            $type,
            $columns,
            $links,
            $maintainer
        );

        # create and register file
        my $extension = $format->extension();
        $self->_write_file(
            "$self->{_to}/$maintainer/$type.$extension",
            $content
        );
        push(
            @{$self->{_files}->{maintainers}->{$maintainer}->{$type}},
            $extension
        );
    }
}

sub _finish_report {
    my ($self, $types, $maintainers) = @_;

    foreach my $format (@{$self->{_formats}}) {
        next unless $format->can('get_index');
        my $extension = $format->extension();
        print STDERR "writing global index page\n" if $self->{_verbose};
        $self->_write_file(
            "$self->{_to}/index.$extension",
            $format->get_index(
                $self->{_time},
                "QA global report",
                $self->{_files}->{global},
                [ keys %{$self->{_files}->{maintainers}} ],
            )
        );
        foreach my $maintainer (@$maintainers) {
            print STDERR "writing index page for $maintainer\n" if $self->{_verbose};

            $self->_write_file(
                "$self->{_to}/$maintainer/index.$extension",
                $format->get_index(
                    $self->{_time},
                    "QA report for $maintainer",
                    $self->{_files}->{maintainers}->{$maintainer},
                    undef,
                )
            );
        }
    }
}

sub _write_file {
    my ($self, $file, $content) = @_;

    return unless $content;

    my $dirname = dirname($file);
    mkpath($dirname) unless -d $dirname;
    
    if ($self->{_test}) {
        *OUT = *STDOUT;
    } else {
        open(OUT, ">$file") or die "Can't open file $file: $!";
    }

    print OUT $$content;

    close(OUT) unless $self->{_test};
}

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2002-2006, YOURI project

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

=cut

1;