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
|
# Tags -- Perl tags functions for lintian
# $Id$
# Copyright (C) 1998-2004 Various authors
# Copyright (C) 2005 Frank Lichtenheld <frank@lichtenheld.de>
#
# 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 http://www.gnu.org/copyleft/gpl.html, or write to the Free
# Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
# MA 02110-1301, USA.
package Tags;
use strict;
use warnings;
use Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw(tag);
# configuration variables and defaults
our $verbose = $::verbose;
our $debug = $::debug;
our $show_info = 0;
our $show_overrides = 0;
our $output_formatter = \&print_tag;
our $min_severity = 1;
our $max_severity = 99;
our $min_significance = 1;
our $max_significance = 99;
# The master hash with all tag info. Key is the tag name, value another hash
# with the following keys:
# - tag: short name
# - type: error/warning/info/experimental
# - info: Description in HTML
# - ref: Any references
# - experimental: experimental status (possibly undef)
my %tags;
# Statistics per file. Key is the filename, value another hash with the
# following keys:
# - overrides
# - tags
# - severity
# - significance
my %stats;
# Info about a specific file. Key is the the filename, value another hash
# with the following keys:
# - pkg: package name
# - version: package version
# - arch: package architecture
# - type: one of 'binary', 'udeb' or 'source'
# - overrides: hash with all overrides for this file as keys
my %info;
# Currently selected file (not package!)
my $current;
# Compatibility stuff
my %codes = ( 'error' => 'E' , 'warning' => 'W' , 'info' => 'I' );
our %type_to_sev = ( error => 4, warning => 2, info => 0 );
our @sev_to_type = qw( info warning warning error error );
my @sig_to_qualifier = ( '??', '?', '', '!' );
my @sev_to_code = qw( I W W E E );
# Add a new tag, supplied as a hash reference
sub add_tag {
my $newtag = shift;
if (exists $tags{$newtag->{tag}}) {
warn "Duplicate tag: $newtag->{tag}\n";
return 0;
}
# smooth transition
$newtag->{type} = $sev_to_type[$newtag->{severity}]
unless $newtag->{type};
$newtag->{significance} = 2 unless exists $newtag->{significance};
$newtag->{severity} = $type_to_sev{$newtag->{type}}
unless exists $newtag->{severity};
$tags{$newtag->{'tag'}} = $newtag;
return 1;
}
# Add another file, will fail if there is already stored info about
# the file
sub set_pkg {
my ( $file, $pkg, $version, $arch, $type ) = @_;
if (exists $info{$file}) {
warn "File $file was already processed earlier\n";
return 0;
}
$current = $file;
$info{$file} = {
pkg => $pkg,
version => $version,
arch => $arch,
type => $type,
overrides => {},
};
$stats{$file} = {
severity => {},
significance => {},
tags => {},
overrides => {},
};
return 1;
}
# select another file as 'current' without deleting or adding any information
# the file must have been added with add_pkg
sub select_pkg {
my ( $file ) = @_;
unless (exists $info{$file}) {
warn "Can't select package $file";
return 0;
}
$current = $file;
return 1;
}
# only delete the value of 'current' without deleting any stored information
sub reset_pkg {
undef $current;
return 1;
}
# delete all the stored information (including tags)
sub reset {
undef %stats;
undef %info;
undef %tags;
undef $current;
return 1;
}
# Add an override. If you specifiy two arguments, the first will be taken
# as file to add the override to, otherwise 'current' will be assumed
sub add_override {
my ($tag, $file) = ( "", "" );
if (@_ > 1) {
($file, $tag) = @_;
} else {
($file, $tag) = ($current, @_);
}
unless ($file) {
warn "Don't know which package to add override $tag to";
return 0;
}
$info{$file}{overrides}{$tag} = 0;
return 1;
}
sub get_overrides {
my ($file) = @_;
unless ($file) {
warn "Don't know which package to get overrides from";
return undef;
}
return $info{$file}{overrides};
}
# Get the info hash for a tag back as a reference. The hash will be
# copied first so that you can edit it safely
sub get_tag_info {
my ( $tag ) = @_;
return { %{$tags{$tag}} } if exists $tags{$tag};
return undef;
}
sub check_range {
my ( $x, $min, $max ) = @_;
return -1 if $x < $min;
return 1 if $x > $max;
return 0;
}
# check if a certain tag has a override for the 'current' package
sub check_overrides {
my ( $tag_info, $information ) = @_;
my $extra = '';
$extra = " @$information" if @$information;
$extra = '' if $extra eq ' ';
if( exists $info{$current}{overrides}{$tag_info->{tag}}) {
$info{$current}{overrides}{$tag_info->{tag}}++;
return $tag_info->{tag};
} elsif( exists $info{$current}{overrides}{"$tag_info->{tag}$extra"} ) {
$info{$current}{overrides}{"$tag_info->{tag}$extra"}++;
return "$tag_info->{tag}$extra";
}
return '';
}
# sets all the overridden fields of a tag_info hash correctly
sub check_need_to_show {
my ( $tag_info, $information ) = @_;
$tag_info->{overridden}{override} = check_overrides( $tag_info,
$information );
my $min_sev = $show_info ? 0 : $min_severity; # compat hack
$tag_info->{overridden}{severity} = check_range( $tag_info->{severity},
$min_sev,
$max_severity );
$tag_info->{overridden}{significance} = check_range( $tag_info->{significance},
$min_significance,
$max_significance );
}
# records the stats for a given tag_info hash
sub record_stats {
my ( $tag_info ) = @_;
for my $k (qw( severity significance tag )) {
$stats{$current}{$k}{$tag_info->{$k}}++
unless $tag_info->{overridden}{override}
|| $tag_info->{overridden}{severity}
|| $tag_info->{overridden}{significance};
}
for my $k (qw( severity significance override )) {
$stats{$current}{overrides}{$k}{$tag_info->{overridden}{$k}}++
if $tag_info->{overridden}{$k};
}
}
# get the statistics for a file (one argument) or for all files (no argument)
sub get_stats {
my ( $file ) = @_;
return $stats{$file} if $file;
return \%stats;
}
sub print_tag {
my ( $pkg_info, $tag_info, $information ) = @_;
my $extra = '';
$extra = " @$information" if @$information;
$extra = '' if $extra eq ' ';
my $code = $codes{$tag_info->{type}};
$code = 'O' if $tag_info->{overridden}{override};
my $type = '';
$type = " $pkg_info->{type}" if $pkg_info->{type} ne 'binary';
print "$code: $pkg_info->{pkg}$type: $tag_info->{tag}$extra\n";
}
sub print_tag_new {
my ( $pkg_info, $tag_info, $information ) = @_;
my $extra = '';
$extra = " @$information" if @$information;
$extra = '' if $extra eq ' ';
my $code = $sev_to_code[$tag_info->{severity}];
$code = 'O' if $tag_info->{overridden}{override};
my $qualifier = $sig_to_qualifier[$tag_info->{significance}];
$qualifier = '' if $code eq 'O';
my $type = '';
$type = " $pkg_info->{type}" if $pkg_info->{type} ne 'binary';
print "$code$qualifier: $pkg_info->{pkg}$type: $tag_info->{tag}$extra\n";
}
sub tag {
my ( $tag, @information ) = @_;
unless ($current) {
warn "Tried to issue tag $tag without setting package\n";
return 0;
}
my $tag_info = get_tag_info( $tag );
unless ($tag_info) {
warn "Tried to issue unknown tag $tag\n";
return 0;
}
check_need_to_show( $tag_info, \@information );
record_stats( $tag_info );
return 1 if
$tag_info->{overridden}{severity} != 0
|| $tag_info->{overridden}{significance} != 0
|| ( $tag_info->{overridden}{override} &&
!$show_overrides);
&$output_formatter( $info{$current}, $tag_info, \@information );
return 1;
}
1;
# vim: ts=4 sw=4 noet
|