/usr/share/perl5/FCM1/ExtractConfigComparator.pm is in fcm 2016.12.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | # ------------------------------------------------------------------------------
# (C) British Crown Copyright 2006-16 Met Office.
#
# This file is part of FCM, tools for managing and building source code.
#
# FCM 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 3 of the License, or
# (at your option) any later version.
#
# FCM 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 FCM. If not, see <http://www.gnu.org/licenses/>.
# ------------------------------------------------------------------------------
use strict;
use warnings;
################################################################################
# A generic reporter of the comparator's result
{
package Reporter;
############################################################################
# Class method: Constructor
sub new {
my ($class) = @_;
return bless(\do{my $annon_scalar}, $class);
}
############################################################################
# Class method: Factory for Reporter object
sub get_reporter {
my ($self, $comparator) = @_;
my $class = defined($comparator->get_wiki()) ? 'WikiReporter'
: 'TextReporter'
;
return $class->new();
}
############################################################################
# Reports the results
sub report {
my ($self, $comparator) = @_;
if (keys(%{$comparator->get_log_of()})) {
print("Revisions at which extract declarations are modified:\n\n");
}
$self->report_impl($comparator);
}
############################################################################
# Does the actual reporting
sub report_impl {
my ($self, $comparator) = @_;
}
}
################################################################################
# Reports the comparator's result in Trac wiki format
{
package WikiReporter;
our @ISA = qw{Reporter};
use FCM1::CmUrl;
use FCM1::Keyword;
use FCM1::Util qw{tidy_url};
############################################################################
# Reports the comparator's result
sub report_impl {
my ($self, $comparator) = @_;
# Output in wiki format
my $wiki_url = FCM1::CmUrl->new(
URL => tidy_url(FCM1::Keyword::expand($comparator->get_wiki()))
);
my $base_trac
= $comparator->get_wiki()
? FCM1::Keyword::get_browser_url($wiki_url->project_url())
: $wiki_url;
if (!$base_trac) {
$base_trac = $wiki_url;
}
for my $key (sort keys(%{$comparator->get_log_of()})) {
my $branch_trac = FCM1::Keyword::get_browser_url($key);
$branch_trac =~ s{\A $base_trac (?:/*|\z)}{source:}xms;
print("[$branch_trac]:\n");
my %branch_of = %{$comparator->get_log_of()->{$key}};
for my $rev (sort {$b <=> $a} keys(%branch_of)) {
print(
$branch_of{$rev}->display_svnlog($rev, $base_trac), "\n",
);
}
print("\n");
}
}
}
################################################################################
# Reports the comparator's result in simple text format
{
package TextReporter;
our @ISA = qw{Reporter};
use FCM1::Config;
my $SEPARATOR = q{-} x 80 . "\n";
############################################################################
# Reports the comparator's result
sub report_impl {
my ($self, $comparator) = @_;
for my $key (sort keys(%{$comparator->get_log_of()})) {
# Output in plain text format
print $key, ':', "\n";
my %branch_of = %{$comparator->get_log_of()->{$key}};
if (FCM1::Config->instance()->verbose() > 1) {
for my $rev (sort {$b <=> $a} keys(%branch_of)) {
print(
$SEPARATOR, $branch_of{$rev}->display_svnlog($rev), "\n"
);
}
}
else {
print(join(q{ }, sort {$b <=> $a} keys(%branch_of)), "\n");
}
print $SEPARATOR, "\n";
}
}
}
package FCM1::ExtractConfigComparator;
use FCM1::CmUrl;
use FCM1::Extract;
################################################################################
# Class method: Constructor
sub new {
my ($class, $args_ref) = @_;
return bless({%{$args_ref}}, $class);
}
################################################################################
# Returns an array containing the 2 configuration files to compare
sub get_files {
my ($self) = @_;
return (wantarray() ? @{$self->{files}} : $self->{files});
}
################################################################################
# Returns the wiki link on wiki mode
sub get_wiki {
my ($self) = @_;
return $self->{wiki};
}
################################################################################
# Returns the result log
sub get_log_of {
my ($self) = @_;
return (wantarray() ? %{$self->{log_of}} : $self->{log_of});
}
################################################################################
# Invokes the comparator
sub invoke {
my ($self) = @_;
# Reads the extract configurations
my (@cfg, $rc);
for my $i (0 .. 1) {
$cfg[$i] = FCM1::Extract->new();
$cfg[$i]->cfg()->src($self->get_files()->[$i]);
$cfg[$i]->parse_cfg();
$rc = $cfg[$i]->expand_cfg();
if (!$rc) {
e_report();
}
}
# Get list of URLs
# --------------------------------------------------------------------------
my @urls = ();
for my $i (0 .. 1) {
# List of branches in each extract configuration file
my @branches = @{$cfg[$i]->branches()};
BRANCH:
for my $branch (@branches) {
# Ignore declarations of local directories
if ($branch->type() eq 'user') {
next BRANCH;
}
# List of SRC declarations in each branch
my %dirs = %{$branch->dirs()};
for my $dir (values(%dirs)) {
# Set up a new instance of FCM1::CmUrl object for each SRC
my $cm_url = FCM1::CmUrl->new (
URL => $dir . (
$branch->revision() ? '@' . $branch->revision() : q{}
),
);
$urls[$i]{$cm_url->branch_url()}{$dir} = $cm_url;
}
}
}
# Compare
# --------------------------------------------------------------------------
$self->{log_of} = {};
for my $i (0 .. 1) {
# Compare the first file with the second one and then vice versa
my $j = ($i == 0) ? 1 : 0;
for my $branch (sort keys(%{$urls[$i]})) {
if (exists($urls[$j]{$branch})) {
# Same REPOS declarations in both files
DIR:
for my $dir (sort keys(%{$urls[$i]{$branch}})) {
if (exists($urls[$j]{$branch}{$dir})) {
if ($i == 1) {
next DIR;
}
my $this_url = $urls[$i]{$branch}{$dir};
my $that_url = $urls[$j]{$branch}{$dir};
# Compare their last changed revisions
my $this_rev
= $this_url->svninfo(FLAG => 'commit:revision');
my $that_rev
= $that_url->svninfo(FLAG => 'commit:revision');
# Make sure last changed revisions differ
if ($this_rev eq $that_rev) {
next DIR;
}
# Not interested in the log before the minimum revision
my $min_rev
= $this_url->pegrev() > $that_url->pegrev()
? $that_url->pegrev() : $this_url->pegrev();
$this_rev = $min_rev if $this_rev < $min_rev;
$that_rev = $min_rev if $that_rev < $min_rev;
# Get list of changed revisions using the commit log
my $u = ($this_rev > $that_rev) ? $this_url : $that_url;
my %revs = $u->svnlog(REV => [$this_rev, $that_rev]);
REV:
for my $rev (keys %revs) {
# Check if revision is already in the list
if (
exists($self->{log_of}{$branch}{$rev})
|| $rev == $min_rev
) {
next REV;
}
# Get list of changed paths. Accept this revision
# only if it contains changes in the current branch
my %paths = %{$revs{$rev}{paths}};
PATH:
for my $path (keys(%paths)) {
my $change_url
= FCM1::CmUrl->new(URL => $u->root() . $path);
if ($change_url->branch() eq $u->branch()) {
$self->{log_of}{$branch}{$rev} = $u;
last PATH;
}
}
}
}
else {
$self->_report_added(
$urls[$i]{$branch}{$dir}->url_peg(), $i, $j);
}
}
}
else {
$self->_report_added($branch, $i, $j);
}
}
}
my $reporter = Reporter->get_reporter($self);
$reporter->report($self);
return $rc;
}
################################################################################
# Reports added/deleted declaration
sub _report_added {
my ($self, $branch, $i, $j) = @_;
printf(
"%s:\n in : %s\n not in: %s\n\n",
$branch, $self->get_files()->[$i], $self->get_files()->[$j],
);
}
1;
__END__
=head1 NAME
FCM1::ExtractConfigComparator
=head1 SYNOPSIS
use FCM1::ExtractConfigComparator;
my $comparator = FCM1::ExtractConfigComparator->new({files => \@files});
$comparator->invoke();
=head1 DESCRIPTION
An object of this class represents a comparator of FCM extract configuration.
It is used to compare the VC branch declarations in 2 FCM extract configuration
files.
=head1 METHODS
=over 4
=item C<new({files =E<gt> \@files, wiki =E<gt> $wiki})>
Constructor.
=item get_files()
Returns an array containing the 2 configuration files to compare.
=item get_wiki()
Returns the wiki link on wiki mode.
=item invoke()
Invokes the comparator.
=back
=head1 TO DO
More documentation.
Improve the parser for extract configuration.
Separate the comparator with the reporters.
Add reporter to display HTML.
More unit tests.
=head1 SEE ALSO
L<FCM1::Extract|FCM1::Extract>
=head1 COPYRIGHT
E<169> Crown copyright Met Office. All rights reserved.
=cut
|