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
|
#!/usr/bin/env perl
# Copyright (c) MediaTek USA Inc., 2020-2024
#
# 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, see
# <http://www.gnu.org/licenses/>.
#
# annotateutil.pm: some common utilities used by sample 'annotate' scripts
#
package annotateutil;
use strict;
use POSIX qw(strftime);
use Cwd qw(abs_path);
our @ISA = qw(Exporter);
our @EXPORT_OK = qw(get_modify_time compute_md5 not_in_repo
resolve_cache_dir find_in_cache store_in_cache
call_annotate call_get_version);
sub get_modify_time($)
{
my $filename = shift;
my @stat = stat $filename;
my $tz = strftime("%z", localtime($stat[9]));
$tz =~ s/([0-9][0-9])$/:\1/;
return strftime("%Y-%m-%dT%H:%M:%S", localtime($stat[9])) . $tz;
}
sub not_in_repo
{
my ($pathname, $lines) = @_;
my $context = '';
eval { $context = MessageContext::context(); };
my $mtime = get_modify_time($pathname); # when was the file last modified?
# who does the filesystem think owns it?
my $owner = getpwuid((stat($pathname))[4]);
open(HANDLE, $pathname) or die("unable to open '$pathname'$context: $!");
while (my $line = <HANDLE>) {
chomp $line;
# Also remove CR from line-end
s/\015$//;
push(@$lines, [$line, $owner, undef, $mtime, "NONE"]);
}
close(HANDLE) or die("unable to close '$pathname'$context");
}
sub compute_md5
{
my $filename = shift;
die("$filename not found") unless -e $filename;
my $null = File::Spec->devnull();
my $md5 = `md5sum $filename 2>$null`;
$md5 =~ /^(\S+)/;
return $1;
}
sub call_annotate
{
my $cb = shift;
my $class;
my $filename = pop;
eval { $class = $cb->new(@_); };
die("$cb construction error: $@") if $@;
my ($status, $list) = $class->annotate($filename);
foreach my $line (@$list) {
my ($text, $abbrev, $full, $when, $cl) = @$line;
print("$cl|$abbrev", $full ? ";$full" : '', "|$when|$text\n");
}
exit $status;
}
sub call_get_version
{
my $cb = shift;
my $class;
my $filename = pop;
eval { $class = $cb->new(@_); };
die("$cb construction error: $@") if $@;
my $v = $class->extract_version($filename);
print($v, "\n");
exit 0;
}
sub resolve_cache_dir
{
my $cache_dir = shift;
if ($cache_dir) {
lcovutil::ignorable_warning($lcovutil::ERROR_USAGE,
'It is unwise to use an --annotate-script callback with --cache-dir without a --version-script to verify version match.'
) unless $lcovutil::versionCallback;
if (-e $cache_dir) {
die("cache '$cache_dir' not writable directory")
unless -d $cache_dir && -w $cache_dir;
} else {
File::Path::make_path($cache_dir) or
die("unable to create '$cache_dir': $!");
}
$cache_dir = abs_path($cache_dir);
}
return $cache_dir;
}
sub find_in_cache
{
my ($cache_dir, $filename) = @_;
my ($cachepath, $version);
my $cachepath = File::Spec->catfile($cache_dir,
File::Spec->file_name_is_absolute(
$filename) ?
substr($filename, 1) :
$filename);
if (-f $cachepath) {
# matching version?
my ($cache_version, $lines);
eval {
my $data = Storable::retrieve($cachepath);
if (defined($data)) {
($cache_version, $lines) = @$data;
$version = lcovutil::extractFileVersion($filename);
}
};
if ($@) {
lcovutil::ignorable_error($lcovutil::ERROR_CORRUPT,
"unable to deserialize $cachepath for $filename annotation: $@\n");
}
if (defined($lines)) {
# pass 'silent' to version check so we don't get error on mismatch
return (0, $version, $lines)
if (!$lcovutil::versionCallback ||
lcovutil::is_ignored($lcovutil::ERROR_VERSION) ||
!(defined($version) != defined($cache_version))
||
lcovutil::checkVersionMatch(
$filename, $version, $cache_version, "annotate-cache", 1
));
lcovutil::info(1, "annotate: cache version check failed\n");
}
}
return ($cachepath, $version);
}
sub store_in_cache
{
my ($cache_path, $filename, $version, $lines) = @_;
$version = lcovutil::extractFileVersion($filename)
unless $version;
my $parent = File::Basename::dirname($cache_path);
unless (-d $parent) {
File::Path::make_path($parent) or
die("unable to create cache directory $parent: $!");
}
Storable::store([$version, $lines], $cache_path) or
die("unable to store $cache_path");
}
1;
|