File: loadanalysisdb

package info (click to toggle)
libperl-critic-perl 1.156-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 3,544 kB
  • sloc: perl: 24,092; lisp: 341; makefile: 7
file content (344 lines) | stat: -rwxr-xr-x 8,171 bytes parent folder | download | duplicates (6)
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
#!/usr/bin/perl

use 5.008001;
use strict;
use warnings;

use version; our $VERSION = qv('1.116');

use Carp qw{ croak };
use English qw{ -no_match_vars };
use Readonly;

use DBI qw{ :sql_types };
use File::Spec qw{ };
use Perl6::Say;

use Perl::Critic::Utils qw{ all_perl_files policy_short_name $EMPTY };
use Perl::Critic;


if ( ! @ARGV ) {
    die qq{usage: loadanalysisdb path [...]\n};
}

main();

exit 0;


sub main {
    say 'Connecting to database.';
    say;

    my $database_connection = connect_to_database();
    my $insert_statement    = prepare_insert_statement($database_connection);

    foreach my $path ( @ARGV ) {
        say "Looking at $path.";

        my @files = all_perl_files($path);
        say 'Analyzing ', scalar @files, ' files.';

        load( \@files, File::Spec->canonpath($path), $insert_statement );

        say; say;
    }

    say 'Disconnecting from database.';
    say;

    close_insert_statement($insert_statement);
    # Need to do this or DBI emits warning at disconnect
    $insert_statement = undef;

    disconnect_from_database($database_connection);

    say 'Done.';
    say;

    return;
}


sub load {
    my ( $files, $path, $insert_statement ) = @_;

    # Force reporting level to be really strict, just so that the database
    # has everything.
    my $critic = Perl::Critic->new( -severity => 1 );

    foreach my $file ( @{$files} ) {
        my $relative_path;

        if ($file eq $path) {
            $relative_path = $file;
        } else {
            my $absolute_path_length = ( length $path ) + 1;

            $relative_path = substr $file, $absolute_path_length;
        }

        say "Processing $relative_path.";

        foreach my $violation ( $critic->critique($file) ) {
            my ($line, $column) = @{ $violation->location() };

            execute_insert_statement(
                $insert_statement,
                $relative_path,
                $line,
                $column,
                $violation->severity(),
                policy_short_name( $violation->policy() ),
                $violation->explanation(),
                $violation->source(),
            );
        }
    }

    return;
}


sub connect_to_database {
    my $database_file_name = 'perl_critic_analysis.sqlite';

    my $database_connection =
        DBI->connect(
            "dbi:SQLite:dbname=$database_file_name",
            $EMPTY,  # login
            $EMPTY,  # password
            {
                AutoCommit => 1,    # In real life, this should be 0
                RaiseError => 1,
            }
        );

    defined $database_connection or
        croak "Could not connect to $database_file_name.";

    return $database_connection;
}


sub prepare_insert_statement {
    my ( $database_connection ) = @_;

    my $insert_statement =
        $database_connection->prepare(<<'END_SQL');
            INSERT INTO
                violation
            (
                file_path,
                line_number,
                column_number,
                severity,
                policy,
                explanation,
                source_code
            )
            VALUES
                (?, ?, ?, ?, ?, ?, ?)
END_SQL


    # The following values are bogus-- these statements are simply to tell
    # the driver what the parameter types are so that we can use execute()
    # without calling bind_param() each time. See "Binding Values Without
    # bind_param()" on pages 126-7 of "Programming the Perl DBI".

    ## no critic (ProhibitMagicNumbers)
    $insert_statement->bind_param( 1, 'x', SQL_VARCHAR);
    $insert_statement->bind_param( 2,   1, SQL_INTEGER);
    $insert_statement->bind_param( 3,   1, SQL_INTEGER);
    $insert_statement->bind_param( 4,   1, SQL_INTEGER);
    $insert_statement->bind_param( 5, 'x', SQL_VARCHAR);
    $insert_statement->bind_param( 6, 'x', SQL_VARCHAR);
    $insert_statement->bind_param( 7, 'x', SQL_VARCHAR);
    ## use critic

    return $insert_statement;
}


sub execute_insert_statement {  ##no critic(ProhibitManyArgs)
    my (
        $statement,
        $file_path,
        $line_number,
        $column_number,
        $severity,
        $policy,
        $explanation,
        $source_code,
    )
        = @_;

    $statement->execute(
        $file_path,
        $line_number,
        $column_number,
        $severity,
        $policy,
        $explanation,
        $source_code,
    );

    return;
}


sub close_insert_statement {
    my ( $insert_statement ) = @_;

    $insert_statement->finish();

    return;
}

sub disconnect_from_database {
    my ( $database_connection ) = @_;

    $database_connection->disconnect();

    return;
}


__END__

=pod

=for stopwords SQLite DBI analyses perlartistic

=head1 NAME

C<loadanalysisdb> - Critique a body of code and load the results into a database for later processing.


=head1 USAGE

    loadanalysisdb path [...]


=head1 DESCRIPTION

Scan a body of code and, rather than emit the results in a textual
format, put them into a database so that analyses can be made.

This example doesn't put anything into the database that isn't
available from L<Perl::Critic::Violation|Perl::Critic::Violation> in
order to keep the code easier to understand.  In a full application of
the idea presented here, one might want to include the current date
and a distribution name in the database so that progress on cleaning
up a code corpus can be tracked.

Note the explanation attribute of
L<Perl::Critic::Violation|Perl::Critic::Violation> is constant for
most policies, but some of them do provide more specific diagnostics
of the code in question.


=head1 REQUIRED ARGUMENTS

A list of paths to files and directories to find code in.


=head1 OPTIONS

None.


=head1 DIAGNOSTICS

Errors from L<DBI|DBI>.


=head1 EXIT STATUS

0


=head1 CONFIGURATION

None.


=head1 DEPENDENCIES

L<Perl::Critic|Perl::Critic>
L<DBD::SQLite|DBD::SQLite>
L<Perl6::Say|Perl6::Say>
L<Readonly|Readonly>

An SQLite database named "perl_critic_analysis.sqlite" with the
following schema:

  CREATE TABLE violation (
      file_path     VARCHAR(1024),
      line_number   INTEGER,
      column_number INTEGER,
      severity      INTEGER,
      policy        VARCHAR(512),
      explanation   TEXT,
      source_code   TEXT
  )


=head1 INCOMPATIBILITIES

None reported.


=head1 BUGS AND LIMITATIONS

This is an example program and thus does minimal error handling.


=head1 AUTHOR

Elliot Shank  C<< <perl@galumph.com> >>


=head1 COPYRIGHT

Copyright (c) 2006-2011, Elliot Shank.

This module is free software; you can redistribute it and/or modify it
under the same terms as Perl itself. See L<perlartistic|perlartistic>.


=head1 DISCLAIMER OF WARRANTY

BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT
WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER
PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND,
EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE
SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME
THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.

IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENSE, BE LIABLE
TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR
CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE
SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
DAMAGES.

=cut

# Local Variables:
#   mode: cperl
#   cperl-indent-level: 4
#   fill-column: 78
#   indent-tabs-mode: nil
#   c-indentation-style: bsd
# End:
# ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :