/usr/share/perl5/FCM/System/Misc.pm is in fcm 2017.10.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 | # ------------------------------------------------------------------------------
# (C) British Crown Copyright 2006-17 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;
# ------------------------------------------------------------------------------
package FCM::System::Misc;
use base qw{FCM::Class::CODE};
use Cwd qw{cwd};
use FCM::Context::Event;
use FCM::Context::Locator;
use FCM::System::Exception;
use FCM::Util::ConfigReader;
use File::Path qw{mkpath rmtree};
use File::Spec::Functions qw{catfile};
use List::Util qw{max};
use Text::ParseWords qw{shellwords};
# The (keys) named actions of this class and (values) their implementations.
our %ACTION_OF = (
browse => \&_browse,
config_parse => \&_config_parse,
export_items => \&_export_items,
keyword_find => \&_keyword_find,
);
# Alias to exception class
my $E = 'FCM::System::Exception';
# Creates the class.
__PACKAGE__->class({util => '&'}, {action_of => \%ACTION_OF});
# Launches a web browser to display some version controlled resources.
sub _browse {
my ($attrib_ref, $option_ref, @args) = @_;
my $UTIL = $attrib_ref->{util};
my @command = shellwords(
exists($option_ref->{browser}) ? $option_ref->{browser}
: $UTIL->external_cfg_get('browser')
);
if (!@args) {
@args = (cwd());
}
for my $value (@args) {
my $locator = FCM::Context::Locator->new($value);
my $url = $UTIL->loc_browser_url($locator);
my %value_of = %{$UTIL->shell_simple([@command, $url])};
if ($value_of{rc}) {
return $E->throw(
$E->SHELL,
{command_list => [@command, $url], %value_of},
$value_of{e},
);
}
$attrib_ref->{util}->event(FCM::Context::Event->OUT, $value_of{o});
}
return;
}
# Parses and displays the content of a FCM configuration file.
sub _config_parse {
my ($attrib_ref, $option_ref, @args) = @_;
my $reader_attrib_ref;
if (exists($option_ref->{'fcm1'})) {
$reader_attrib_ref = \%FCM::Util::ConfigReader::FCM1_ATTRIB;
}
for my $value (@args) {
my $locator = FCM::Context::Locator->new($value);
my $iter = $attrib_ref->{util}->config_reader(
$locator, $reader_attrib_ref,
);
while (my $entry = $iter->()) {
$attrib_ref->{util}->event(
FCM::Context::Event->CONFIG_ENTRY,
$entry,
exists($option_ref->{'fcm1'}),
);
}
}
return;
}
# Exports directories in a project as sequential versioned items.
sub _export_items {
my ($attrib_ref, $option_ref, $location) = @_;
if (!$location) {
return $E->throw($E->EXPORT_ITEMS_SRC);
}
$location ||= q{.};
my $UTIL = $attrib_ref->{util};
# Options and arguments
$option_ref->{directory} ||= cwd();
$option_ref->{'config-file'} ||= ['fcm-export-items.cfg'];
my $locator = FCM::Context::Locator->new($location);
$UTIL->loc_as_invariant($locator);
# Timer
my $time_start = time();
my $timer = $UTIL->timer();
my %EVENT = (
'create' => sub {
$UTIL->event(FCM::Context::Event->EXPORT_ITEM_CREATE, @_);
},
'delete' => sub {
$UTIL->event(FCM::Context::Event->EXPORT_ITEM_DELETE, @_);
},
'timer' => sub {
$UTIL->event(
FCM::Context::Event->TIMER, 'export-items', $time_start, @_,
);
},
);
$EVENT{'timer'}->();
# Reads configuration file
my $config_reader = $attrib_ref->{util}->config_reader(
FCM::Context::Locator->new($option_ref->{'config-file'}->[0]),
{ %FCM::Util::ConfigReader::FCM1_ATTRIB,
event_level => $attrib_ref->{util}->util_of_report()->LOW,
},
);
my %conditions_of;
while (defined(my $entry = $config_reader->())) {
# Value: conditions
my @conditions;
for my $word (shellwords($entry->get_value())) {
my ($operator, $rev) = $word =~ qr{\A ([<>]=?|[!=]=) (.+) \z}imsx;
if (!$operator || !$rev) {
return $E->throw($E->CONFIG_VALUE, $entry);
}
push(@conditions, $operator . $rev); # FIXME: keyword?
}
# Label: targets and namespaces
my ($target) = $entry->get_label() =~ qr{\A (.+) / \*\z}msx;
if ($target) {
my $l_target = $UTIL->loc_cat($locator, $target);
$UTIL->loc_find(
$l_target,
sub {
my ($l_child, $attrib_of_child_ref) = @_;
if (!$attrib_of_child_ref->{is_dir}) {
my $ns_of_child = $attrib_of_child_ref->{ns};
my $iter
= $UTIL->ns_iter($ns_of_child, $UTIL->NS_ITER_UP);
$iter->(); # discard
my $ns = $UTIL->ns_cat($target, $iter->());
if (!exists($conditions_of{$ns})) {
$conditions_of{$ns} = \@conditions;
}
}
},
);
}
else {
$conditions_of{$entry->get_label()} = \@conditions;
}
}
# Export
NS:
while (my ($ns, $conditions_ref) = each(%conditions_of)) {
# FIXME: this should be encapsulated by the locator util.
my @command_list = (
qw{svn log -q},
$UTIL->loc_cat($locator, $ns)->get_value(),
);
my %value_of = %{$UTIL->shell_simple(\@command_list)};
if ($value_of{rc}) {
return $E->throw(
$E->SHELL,
{command_list => \@command_list, %value_of},
$value_of{e},
);
}
my @revs = map {($_ =~ qr{\Ar(\d+)})} split("\n", $value_of{o});
my %v_of;
my $v = 0;
for my $rev (reverse(@revs)) {
$v_of{$rev} = 'v' . ++$v;
}
my %cur_v_of = %v_of;
# Exports only revisions matching the conditions
for my $condition (@{$conditions_ref}) {
for my $rev (keys(%cur_v_of)) {
if (!eval($rev . $condition)) {
delete($cur_v_of{$rev});
}
}
}
# Destination directory
my $path = catfile($option_ref->{directory}, $ns);
if (-d $path) {
if ($option_ref->{new} || !keys(%cur_v_of)) {
rmtree($path);
}
else {
# Delete excluded revisions if they exist in incremental mode
if (opendir(my $handle, $path)) {
while (my $item = readdir($handle)) {
if (exists($v_of{$item}) && !exists($cur_v_of{$item})) {
for (($item, $v_of{$item})) {
my $p = catfile($path, $_);
rmtree($p);
$EVENT{'delete'}->($ns, $item, $p);
}
}
}
closedir($handle);
}
}
}
if (!keys(%cur_v_of)) {
next NS;
}
if (!-d $path) {
mkpath($path);
}
# Exports each revision, and creates symlink for each v
while (my ($rev, $v) = each(%cur_v_of)) {
my $target = catfile($option_ref->{directory}, $ns, $v);
if (-l $target || -f $target) {
unlink($target);
$EVENT{'delete'}->($ns, $v, $target);
}
if (!-d $target) {
my $url_peg_rev = $UTIL->loc_cat($locator, $ns)->get_value();
my ($url) = $url_peg_rev =~ qr{\A(.*?)(?:@[^@/]+)?\z}msx;
my @command_list = (qw{svn export -q -r}, $rev, $url, $target);
my %value_of = %{$UTIL->shell_simple(\@command_list)};
if ($value_of{rc} || !-d $target) {
return $E->throw(
$E->SHELL,
{command_list => \@command_list, %value_of},
$value_of{e},
);
}
$EVENT{'create'}->($ns, $v, $target);
}
my $link = catfile($option_ref->{directory}, $ns, $rev);
if (-e $link && !-l $link) {
rmtree($link);
$EVENT{'delete'}->($ns, $rev, $link);
}
elsif (-l $link && readlink($link) ne $v) {
unlink($link);
$EVENT{'delete'}->($ns, $rev, $link);
}
if (!-e $link) {
symlink($v, $link);
$EVENT{'create'}->($ns, $rev, $link);
}
}
# Symbolic link to the "latest" version directory
my $link_of_latest = catfile($option_ref->{directory}, $ns, 'latest');
my $v_of_latest = $cur_v_of{max(keys(%cur_v_of))};
if (-e $link_of_latest && !-l $link_of_latest) {
rmtree($link_of_latest);
$EVENT{'delete'}->($ns, 'latest', $link_of_latest);
}
elsif (-l $link_of_latest && readlink($link_of_latest) ne $v_of_latest) {
unlink($link_of_latest);
$EVENT{'delete'}->($ns, 'latest', $link_of_latest);
}
if (!-l $link_of_latest) {
symlink($v_of_latest, $link_of_latest);
$EVENT{'create'}->($ns, 'latest', $link_of_latest);
}
}
$EVENT{'timer'}->($timer->());
}
# Searches FCM keywords.
sub _keyword_find {
my ($attrib_ref, $option_ref, @args) = @_;
my $UTIL = $attrib_ref->{util};
my @entries;
if (@args) {
for my $key (@args) {
my $iter = $UTIL->loc_kw_iter(FCM::Context::Locator->new($key));
while (my $entry = $iter->()) {
if (!$entry->get_implied()) {
$UTIL->loc_kw_load_rev_prop($entry);
push(@entries, $entry);
}
}
}
}
else {
@entries = values(%{$UTIL->loc_kw_ctx()->get_entry_by_key()});
}
for my $entry (sort {$a->get_key() cmp $b->get_key()} @entries) {
$UTIL->event(FCM::Context::Event->KEYWORD_ENTRY, $entry);
}
return;
}
# ------------------------------------------------------------------------------
1;
__END__
=head1 NAME
FCM::System::Misc
=head1 SYNOPSIS
use FCM::System::Misc;
my $system = FCM::System::Misc->new(\%attrib);
$system->keyword_find(@args);
=head1 DESCRIPTION
The rest of the FCM system.
=head1 METHODS
Implements the browse(), config_parse(), export_items() and keyword_find()
methods for L<FCM::System|FCM::System>. See L<FCM::System|FCM::System> for a
description of the calling interfaces of these functions.
=head1 DIAGNOSTICS
=head2 FCM::System::Exception
Methods of this class may throw a FCM::System::Exception.
=head1 COPYRIGHT
(C) Crown copyright Met Office. All rights reserved.
=cut
|