/usr/share/perl5/FCM/Util.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 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 | # ------------------------------------------------------------------------------
# (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;
# ------------------------------------------------------------------------------
package FCM::Util;
use base qw{FCM::Class::CODE};
use Digest::MD5;
use Digest::SHA;
use FCM::Context::Event;
use FCM::Context::Locator;
use FCM::Util::ConfigReader;
use FCM::Util::ConfigUpgrade;
use FCM::Util::Event;
use FCM::Util::Exception;
use FCM::Util::Locator;
use FCM::Util::Reporter;
use FCM::Util::Shell;
use FCM::Util::TaskRunner;
use File::Basename qw{basename dirname};
use File::Path qw{mkpath};
use File::Spec::Functions qw{catfile};
use FindBin;
use Scalar::Util qw{blessed reftype};
use Text::ParseWords qw{shellwords};
use Time::HiRes qw{gettimeofday tv_interval};
use constant {NS_ITER_UP => 1};
# The (keys) named actions of this class and (values) their implementations.
our %ACTION_OF = (
cfg_init => \&_cfg_init,
class_load => \&_class_load,
config_reader => _util_of_func('config_reader', 'main'),
external_cfg_get => \&_external_cfg_get,
event => \&_event,
file_checksum => \&_file_checksum,
file_ext => \&_file_ext,
file_head => \&_file_head,
file_load => \&_file_load,
file_load_handle => \&_file_load_handle,
file_md5 => \&_file_md5,
file_save => \&_file_save,
file_tilde_expand => \&_file_tilde_expand,
hash_cmp => \&_hash_cmp,
loc_as_invariant => _util_of_loc_func('as_invariant'),
loc_as_keyword => _util_of_loc_func('as_keyword'),
loc_as_normalised => _util_of_loc_func('as_normalised'),
loc_as_parsed => _util_of_loc_func('as_parsed'),
loc_browser_url => _util_of_loc_func('browser_url'),
loc_cat => _util_of_loc_func('cat'),
loc_dir => _util_of_loc_func('dir'),
loc_export => _util_of_loc_func('export'),
loc_export_ok => _util_of_loc_func('export_ok'),
loc_exists => _util_of_loc_func('test_exists'),
loc_find => _util_of_loc_func('find'),
loc_kw_ctx => _util_of_loc_func('kw_ctx'),
loc_kw_ctx_load => _util_of_loc_func('kw_ctx_load'),
loc_kw_iter => _util_of_loc_func('kw_iter'),
loc_kw_load_rev_prop => _util_of_loc_func('kw_load_rev_prop'),
loc_kw_prefix => _util_of_func('locator', 'kw_prefix'),
loc_origin => _util_of_loc_func('origin'),
loc_reader => _util_of_loc_func('reader'),
loc_rel2abs => _util_of_loc_func('rel2abs'),
loc_trunk_at_head => _util_of_loc_func('trunk_at_head'),
loc_what_type => _util_of_loc_func('what_type'),
loc_up_iter => _util_of_loc_func('up_iter'),
ns_cat => \&_ns_cat,
ns_common => \&_ns_common,
ns_in_set => \&_ns_in_set,
ns_iter => \&_ns_iter,
ns_sep => sub {$_[0]->{ns_sep}},
report => _util_of_func('reporter', 'report'),
shell => _util_of_func('shell', 'invoke'),
shell_simple => _util_of_func('shell', 'invoke_simple'),
shell_which => _util_of_func('shell', 'which'),
task_runner => _util_of_func('task_runner', 'main'),
timer => \&_timer,
uri_match => \&_uri_match,
util_of_event => _util_impl_func('event'),
util_of_report => _util_impl_func('reporter'),
version => \&_version,
);
# The default paths to the configuration files.
our @FCM1_KEYWORD_FILES = (
catfile((getpwuid($<))[7], qw{.fcm}),
);
our @CONF_PATHS = (
catfile($FindBin::Bin, qw{.. etc fcm}),
catfile((getpwuid($<))[7], qw{.met-um fcm}),
catfile((getpwuid($<))[7], qw{.metomi fcm}),
);
our %CFG_BASENAME_OF = (
external => 'external.cfg',
keyword => 'keyword.cfg',
);
# Values of external commands
our %EXTERNAL_VALUE_OF = (
'browser' => 'firefox',
'diff3' => 'diff3',
'diff3.flags' => '-E -m',
'graphic-diff' => 'xxdiff',
'graphic-merge' => 'xxdiff',
'ssh' => 'ssh',
'ssh.flags' => '-n -oBatchMode=yes',
'rsync' => 'rsync',
'rsync.flags' => '-a --exclude=.* --delete-excluded --timeout=900'
. ' --rsh="ssh -oBatchMode=yes"',
);
# The name-space separator
our $NS_SEP = '/';
# The (keys) named utilities and their implementation classes.
our %UTIL_CLASS_OF = (
config_reader => 'FCM::Util::ConfigReader',
event => 'FCM::Util::Event',
locator => 'FCM::Util::Locator',
reporter => 'FCM::Util::Reporter',
shell => 'FCM::Util::Shell',
task_runner => 'FCM::Util::TaskRunner',
);
# Alias
my $E = 'FCM::Util::Exception';
# Regular expression: match a URI
my $RE_URI = qr/
\A (?# start)
( (?# capture 1, scheme, start)
[A-Za-z] (?# alpha)
[\w\+\-\.]* (?# optional alpha, numeric, plus, minus and dot)
) (?# capture 1, scheme, end)
: (?# colon)
(.*) (?# capture 2, opaque, rest of string)
\z (?# end)
/xms;
# Creates the class.
__PACKAGE__->class(
{ cfg_basename_of => {isa => '%', default => {%CFG_BASENAME_OF}},
conf_paths => {isa => '@', default => [@CONF_PATHS]},
event => '&',
external_value_of => {isa => '%', default => {%EXTERNAL_VALUE_OF}},
ns_sep => {isa => '$', default => $NS_SEP},
util_class_of => {isa => '%', default => {%UTIL_CLASS_OF}},
util_of => '%',
},
{init => \&_init, action_of => \%ACTION_OF},
);
# Initialises attributes.
sub _init {
my ($attrib_ref, $self) = @_;
# Initialise the utilities
while (my ($key, $util_class) = each(%{$attrib_ref->{util_class_of}})) {
if (!defined($attrib_ref->{util_of}{$key})) {
_class_load($attrib_ref, $util_class);
$attrib_ref->{util_of}{$key} = $util_class->new({util => $self});
}
}
if (exists($ENV{FCM_CONF_PATH})) {
$attrib_ref->{conf_paths} = [shellwords($ENV{FCM_CONF_PATH})];
}
}
# Loads the named configuration from its configuration files.
sub _cfg_init {
my ($attrib_ref, $basename, $action_ref) = @_;
if (exists($ENV{FCM_CONF_PATH})) {
$attrib_ref->{conf_paths} = [shellwords($ENV{FCM_CONF_PATH})];
}
for my $path (
grep {-f} map {catfile($_, $basename)} @{$attrib_ref->{conf_paths}}
) {
my $config_reader = $ACTION_OF{config_reader}->(
$attrib_ref, FCM::Context::Locator->new($path),
);
$action_ref->($config_reader);
}
}
# Loads a class/package.
sub _class_load {
my ($attrib_ref, $name, $test_method) = @_;
$test_method ||= 'new';
if (!UNIVERSAL::can($name, $test_method)) {
eval('require ' . $name);
if (my $e = $@) {
return $E->throw($E->CLASS_LOADER, $name, $e);
}
}
return $name;
}
# Invokes an event.
sub _event {
my ($attrib_ref, $event, @args) = @_;
if (!blessed($event)) {
$event = FCM::Context::Event->new({code => $event, args => \@args}),
}
$attrib_ref->{'util_of'}{'event'}->main($event);
}
# Returns the value of an external tool.
{ my $EXTERNAL_CFG_INIT;
sub _external_cfg_get {
my ($attrib_ref, $key) = @_;
my $value_hash_ref = $attrib_ref->{external_value_of};
if (!$EXTERNAL_CFG_INIT) {
$EXTERNAL_CFG_INIT = 1;
_cfg_init(
$attrib_ref,
$attrib_ref->{cfg_basename_of}{external},
sub {
my $config_reader = shift();
while (defined(my $entry = $config_reader->())) {
my $k = $entry->get_label();
if ($k && exists($value_hash_ref->{$k})) {
$value_hash_ref->{$k} = $entry->get_value();
}
}
}
);
}
if (!$key || !exists($value_hash_ref->{$key})) {
return;
}
return $value_hash_ref->{$key};
}
}
# Returns the checksum of the content in a file system path.
sub _file_checksum {
my ($attrib_ref, $path, $algorithm) = @_;
my $handle = _file_load_handle($attrib_ref, $path);
binmode($handle);
$algorithm ||= 'md5';
my $digest = $algorithm eq 'md5'
? Digest::MD5->new() : Digest::SHA->new($algorithm);
$digest->addfile($handle);
my $checksum = $digest->hexdigest();
close($handle);
return $checksum;
}
# Returns the file extension of a file system path.
sub _file_ext {
my ($attrib_ref, $path) = @_;
my $pos_of_dot = rindex($path, q{.});
if ($pos_of_dot == -1) {
return (wantarray() ? (undef, $path) : undef);
}
my $ext = substr($path, $pos_of_dot + 1);
wantarray() ? ($ext, substr($path, 0, $pos_of_dot)) : $ext;
}
# Loads the first $n lines from a file system path.
sub _file_head {
my ($attrib_ref, $path, $n) = @_;
$n ||= 1;
my $handle = _file_load_handle(@_);
my $content = q{};
for (1 .. $n) {
$content .= readline($handle);
}
close($handle);
(wantarray() ? (map {$_ . "\n"} split("\n", $content)) : $content);
}
# Loads the contents from a file system path.
sub _file_load {
my ($attrib_ref, $path) = @_;
my $handle = _file_load_handle(@_);
my $content = do {local($/); readline($handle)};
close($handle);
(wantarray() ? (map {$_ . "\n"} split("\n", $content)) : $content);
}
# Opens a file handle to read from a file system path.
sub _file_load_handle {
my ($attrib_ref, $path) = @_;
open(my($handle), '<', $path) || return $E->throw($E->IO, $path, $!);
$handle;
}
# Returns the MD5 checksum of the content in a file system path.
sub _file_md5 {
my ($attrib_ref, $path) = @_;
_file_checksum($attrib_ref, $path, 'md5');
}
# Saves content to a file system path.
sub _file_save {
my ($attrib_ref, $path, $content) = @_;
if (!-e dirname($path)) {
eval {mkpath(dirname($path))};
if (my $e = $@) {
return $E->throw($E->IO, $path, $e);
}
}
open(my($handle), '>', $path) || return $E->throw($E->IO, $path, $!);
if (ref($content) && ref($content) eq 'ARRAY') {
print($handle @{$content}) || return $E->throw($E->IO, $path, $!);
}
else {
print($handle $content) || return $E->throw($E->IO, $path, $!);
}
close($handle) || return $E->throw($E->IO, $path, $!);
}
# Expand leading ~ and ~USER syntax in $path and return the resulting string.
sub _file_tilde_expand {
my ($attrib_ref, $path) = @_;
$path =~ s{\A~([^/]*)}{$1 ? (getpwnam($1))[7] : (getpwuid($<))[7]}exms;
return $path;
}
# Compares contents of 2 HASH references.
sub _hash_cmp {
my ($attrib_ref, $hash_1_ref, $hash_2_ref, $keys_only) = @_;
my %hash_2 = %{$hash_2_ref};
my %modified;
while (my ($key, $v1) = each(%{$hash_1_ref})) {
if (exists($hash_2{$key})) {
my $v2 = $hash_2{$key};
if ( !$keys_only
&& (
defined($v1) && defined($v2) && $v1 ne $v2
|| defined($v1) && !defined($v2)
|| !defined($v1) && defined($v2)
)
) {
$modified{$key} = 0;
}
delete($hash_2{$key});
}
else {
$modified{$key} = -1;
}
}
while (my $key = each(%hash_2)) {
if (!exists($hash_1_ref->{$key})) {
$modified{$key} = 1;
}
}
return %modified;
}
# Concatenates 2 name-spaces.
sub _ns_cat {
my ($attrib_ref, @ns_list) = @_;
join(
$attrib_ref->{ns_sep},
grep {$_ && $_ ne $attrib_ref->{ns_sep}} @ns_list,
);
}
# Returns the common parts of 2 name-spaces.
sub _ns_common {
my ($attrib_ref, $ns1, $ns2) = @_;
my $iter1 = _ns_iter($attrib_ref, $ns1);
my $iter2 = _ns_iter($attrib_ref, $ns2);
my $common_ns = q{};
while (defined(my $s1 = $iter1->()) && defined(my $s2 = $iter2->())) {
if ($s1 ne $s2) {
return $common_ns;
}
$common_ns = $s1;
}
return $common_ns;
}
# Returns true if $ns is in one of the name-spaces given by keys(%set).
sub _ns_in_set {
my ($attrib_ref, $ns, $ns_set_ref) = @_;
if (!keys(%{$ns_set_ref})) {
return;
}
my @ns_list;
my $ns_iter = _ns_iter($attrib_ref, $ns);
while (defined(my $n = $ns_iter->())) {
push(@ns_list, $n);
}
grep {exists($ns_set_ref->{$_})} @ns_list;
}
# Returns an iterator to walk up/down a name-space.
sub _ns_iter {
my ($attrib_ref, $ns, $up) = @_;
if ($ns eq $attrib_ref->{ns_sep}) {
$ns = q{};
}
my @give = split($attrib_ref->{ns_sep}, $ns);
my @take = ();
my $next = q{};
if ($up) {
@give = reverse(@give);
$next = $ns;
}
sub {
my $ret = $next;
$next = undef;
if (@give) {
push(@take, shift(@give));
$next = join($attrib_ref->{ns_sep}, ($up ? reverse(@give) : @take));
}
return $ret;
};
}
# Returns a timer.
sub _timer {
my ($attrib_ref, $start_ref) = @_;
$start_ref ||= [gettimeofday()];
sub {tv_interval($start_ref)};
}
# Matches a URI.
sub _uri_match {
my ($attrib_ref, $string) = @_;
$string =~ $RE_URI;
}
# Returns a function to return/set the object in the "util_of" basket.
sub _util_impl_func {
my ($id) = @_;
sub {
my ($attrib_ref, $value) = @_;
if (defined($value) && ref($value) && reftype($value) eq 'CODE') {
$attrib_ref->{'util_of'}{$id} = $value;
}
$attrib_ref->{'util_of'}{$id};
};
}
# Returns a function to delegate a method to a utility in the "util_of" basket.
sub _util_of_func {
my ($id, $method) = @_;
sub {
my $attrib_ref = shift();
$attrib_ref->{util_of}{$id}->(($method ? ($method) : ()), @_);
};
}
# Returns a function to delegate a method to the locator utility.
{ my $KEYWORD_CFG_INIT;
sub _util_of_loc_func {
my ($method) = @_;
sub {
my $attrib_ref = shift();
if (!$KEYWORD_CFG_INIT) {
$KEYWORD_CFG_INIT = 1;
my $config_upgrade = FCM::Util::ConfigUpgrade->new();
for my $path (grep {-f} @FCM1_KEYWORD_FILES) {
my $config_reader = $ACTION_OF{config_reader}->(
$attrib_ref,
FCM::Context::Locator->new($path),
\%FCM::Util::ConfigReader::FCM1_ATTRIB,
);
$ACTION_OF{loc_kw_ctx_load}->(
$attrib_ref,
sub {$config_upgrade->upgrade($config_reader->())},
);
}
_cfg_init(
$attrib_ref,
$attrib_ref->{cfg_basename_of}{keyword},
sub {$ACTION_OF{loc_kw_ctx_load}->($attrib_ref, @_)},
);
}
$attrib_ref->{util_of}{locator}->($method, @_);
};
}
}
# Returns the FCM version string.
{ my $FCM_VERSION;
sub _version {
my ($attrib_ref) = @_;
if (!defined($FCM_VERSION)) {
my $fcm_home = dirname($FindBin::Bin);
# Try "git describe"
my $value_hash_ref = eval {
$ACTION_OF{shell_simple}->(
$attrib_ref,
['git', "--git-dir=$FindBin::Bin/../.git", 'describe'],
);
};
if (my $e = $@) {
if (!$E->caught($e)) {
die($e);
}
$@ = undef;
}
my $version;
if ($value_hash_ref->{o} && !$value_hash_ref->{rc}) {
chomp($value_hash_ref->{o});
$version = $value_hash_ref->{o};
}
else {
# Read fcm-version.js file
my $path = catfile("", qw{etc fcm fcm-version.js});
open(my($handle), '<', $path) || die("$path: $!");
my $content = do {local($/); readline($handle)};
close($handle);
($version) = $content =~ qr{\AFCM\.VERSION="(.*)";}msx;
}
$FCM_VERSION = sprintf("%s (%s)", $version, $fcm_home);
}
return $FCM_VERSION;
}
}
# ------------------------------------------------------------------------------
1;
__END__
=head1 NAME
FCM::Util
=head1 SYNOPSIS
use FCM::Util;
$u = FCM::Util->new();
$u->class_load('Foo');
=head1 DESCRIPTION
Utilities used by the FCM system.
=head1 METHODS
=over 4
=item $class->new(\%attrib)
Returns a new instance. The %attrib hash can be used configure the behaviour of
the instance:
=over 4
=item conf_paths
The search paths to the configuration files. The default is the value in
@FCM::Util::CONF_PATHS.
=item cfg_basename_of
A HASH to map the named configuration with the base names of their paths.
(default=%CFG_BASENAME_OF)
=item external_value_of
A HASH to map the named external tools with their default values.
(default=%EXTERNAL_VALUE_OF)
=item event
A CODE to handle event.
=item ns_sep
The name space separator. (default=/)
=item util_class_of
A HASH to map (keys) utility names to (values) their implementation classes. See
%FCM::System::UTIL_CLASS_OF.
=item util_of
A HASH to map (keys) utility names to (values) their implementation instances.
=back
=item $u->cfg_init($basename,\&action)
Search site/user configuration given by $basename. Invoke the callback
&action($config_reader) for each configuration file found.
=item $u->class_load($name,$test_method)
If $name can call $test_method, returns $name. (If $test_method is not defined,
the default is "new".) Otherwise, calls require($name). Returns $name.
=item $u->config_reader($locator,\%reader_attrib)
Returns an iterator for getting the configuration entries from $locator (which
should be an instance of L<FCM::Context::Locator|FCM::Context::Locator>.
The iterator returns the next useful entry of the configuration file as an
object of L<FCM::Context::ConfigEntry|FCM::Context::ConfigEntry>. It returns
under if there is no more useful entry to return.
The %reader_attrib may be used to override the default attributes. The HASH
should contain a {parser} and a {processor}. The {parser} is a CODE reference to
parse a declaration in the configuration file into an entry. The {processor} is
a CODE reference to process the entry. If the {processor} returns true, the
entry is considered a special entry (e.g. a variable declaration or an
C<include> declaration) that is processed, and will not be returned by the
iterator.
The %reader_attrib can be defined using the following pre-defined sets:
=over 4
=item %FCM::Util::ConfigReader::FCM1_ATTRIB
Using this will generate a reader for configuration files written in the FCM 1
format.
=item %FCM::Util::ConfigReader::FCM2_ATTRIB
Using this will generate a reader for configuration files written in the FCM 2
format. (default)
=back
In addition, $reader_attrib{event_level} can be used to adjust the event
verbosity level.
The parser and the processor are called with a %state, which contains the
current state of the reader, and has the following elements:
=over 4
=item cont
This is set to true if there is a continue marker at the end of the current
line. The next line should be parsed as part of the current context.
=item ctx
The context of the current entry, which should be an instance of
L<FCM::Context::ConfigEntry|FCM::Context::ConfigEntry>.
=item line
The content of the current line.
=item stack
An ARRAY reference that represents an include stack. The top of the stack
(the final element) represents the most current file being read. An include file
will be put on top of the stack, and removed when EOF is reached. When the stack
is empty, the iterator is exhausted.
Each element of the stack is an 4-element ARRAY reference. Element 1 is the
L<FCM::Context::Locator|FCM::Context::Locator> object that represents the
current file. Element 2 is the line number of the current file. Element 3 is the
file handle for reading the current file. Element 4 is a CODE reference with an
interface $f->($path), for turning $path from a relative location under the
container of the current file into an absolute location.
=item var
A HASH reference containing the variables (from the environment and local to the
configuration file) that can be used for substitution.
=back
=item $u->external_cfg_get($key)
Returns the value of a named tool.
=item $u->event($event,@args)
Raises an event. The 1st argument $event can either be a blessed reference of
L<FCM::Context::Event|FCM::Context::Event> or a valid event code. If the former
is true, @args is not used, otherwise, @args should be the event arguments for
the specified event code.
=item $u->file_checksum($path, $algorithm)
Returns the checksum of $path. If $algorithm is not specified, the default
algorithm to use is MD5. Otherwise, any algorithm supported by Perl's
Digest::SHA module can be used.
=item $u->file_ext($path)
Returns file extension of $path. E.g.:
my $path = '/foo/bar.baz';
my $extension = $u->file_ext($path); # 'baz'
my ($extension, $root) = $u->file_ext($path); # ('baz', '/foo/bar')
=item $u->file_head($path, $n)
Loads $n lines (or 1 line if $n not specified) from a $path in the file system.
In scalar context, returns the content in a scalar. In list context, separate
the content by the new line character "\n", and returns the resulting list.
=item $u->file_load($path)
Loads contents from a $path in the file system. In scalar context, returns the
content in a scalar. In list context, separate the content by the new line
character "\n", and returns the resulting list.
=item $u->file_load_handle($path)
Returns a file handle for loading contents from $path.
=item $u->file_md5($path)
Deprecated. Equivalent to $u->file_checksum($path, 'md5').
=item $u->file_save($path, $content)
Saves $content to a $path in the file system.
=item $u->file_tilde_expand($path)
Expand any leading "~" or "~USER" syntax to the HOME directory of the current
user or the HOME directory of USER. Return the modified string.
=item $u->hash_cmp(\%hash_1,\%hash_2,$keys_only)
Compares the contents of 2 HASH references. If $keys_only is specified, only
compares the keys. Returns a HASH where each element represents a difference
between %hash_1 and %hash_2 - if the value is positive, the key exists in
%hash_2 but not %hash_1, if the value is negative, the key exists in %hash_1 but
not %hash_2, and if the value is zero, the key exists in both, but the values
are different.
=item $u->loc_as_invariant($locator)
If the $locator->get_value_level() is below FCM::Context::Locator->L_INVARIANT,
determines the invariant value of $locator, and sets its value to the result.
Returns $locator->get_value().
See L<FCM::Context::Locator|FCM::Context::Locator> for information on locator
value level.
=item $u->loc_as_keyword($locator)
Calls $u->loc_as_normalised($locator) if $locator->get_value_level() is below
FCM::Context::Locator->L_NORMALISED. Returns the value of the locator as an FCM
keyword, where possible.
=item $u->loc_as_normalised($locator)
If the $locator->get_value_level() is below FCM::Context::Locator->L_NORMALISED,
determines the normalised value of $locator, and sets its value to the result.
Returns $locator->get_value().
See L<FCM::Context::Locator|FCM::Context::Locator> for information on locator
value level.
=item $u->loc_as_parsed($locator)
If the $locator->get_value_level() is below FCM::Context::Locator->L_PARSED,
determines the parsed value of $locator, and sets its value to the result.
Returns $locator->get_value().
See L<FCM::Context::Locator|FCM::Context::Locator> for information on locator
value level.
=item $u->loc_browser_url($locator)
Calls $u->loc_as_normalised($locator) if $locator->get_value_level() is below
FCM::Context::Locator->L_NORMALISED. Returns the value of the locator as a
browser URL, where possible.
=item $u->loc_cat($locator,@paths)
Calls $u->loc_as_parsed($locator) if $locator->get_value_level() is below
FCM::Context::Locator->L_PARSED. Concatenates the value of the $locator with the
given @paths according to the $locator type. Returns a new FCM::Context::Locator
that represents the concatenated value.
=item $u->loc_dir($locator)
Calls $u->loc_as_parsed($locator) if $locator->get_value_level() is below
FCM::Context::Locator->L_PARSED. Determines the "directory" name of the value of
the $locator according to the $locator type. Returns a new FCM::Context::Locator
that represents the resulting value.
=item $u->loc_exists($locator)
Calls $u->loc_as_normalised($locator) if $locator->get_value_level() is below
FCM::Context::Locator->L_NORMALISED. Return a true value if the location
represented by $locator exists.
=item $u->loc_export($locator,$dest)
Calls $u->loc_as_normalised($locator) if $locator->get_value_level() is below
FCM::Context::Locator->L_NORMALISED. Exports the file or directory tree
represented by $locator to a file system $dest.
=item $u->loc_export_ok($locator)
Calls $u->loc_as_parsed($locator) if $locator->get_value_level() is below
FCM::Context::Locator->L_PARSED. Returns true if it is possible and safe to
call $u->loc_export($locator).
=item $u->loc_find($locator,\&callback)
Searches the directory tree of $locator. Invokes &callback for each node with
the following interface:
$callback_ref->($locator_of_child_node, \%target_attrib);
where %target_attrib contains the keys:
=over 4
=item {is_dir}
This is set to true if the child node is a directory.
=item {last_modified_rev}
This is set to the last modified revision of the child node, if relevant.
=item {last_modified_time}
This is set to the last modified time of the child node.
=item {ns}
This is set to the relative name-space (i.e. the relative path) of the child
node.
=back
=item $u->loc_kw_ctx()
Returns the keyword context (an instance of FCM::Context::Keyword).
=item $u->loc_kw_ctx_load(@config_entry_iterators)
Loads configuration entries into the keyword context. The
@config_entry_iterators should be a list of CODE references, with the following
calling interfaces:
while (my $config_entry = $config_entry_iterator->()) {
# ... $config_entry should be an instance of FCM::Context::ConfigEntry
}
=item $u->loc_kw_iter($locator)
Returns an iterator. When called, the iterator returns location keyword entry
context (as an instance of
L<FCM::Context::Keyword::Entry::Location|FCM::Context::Keyword>) for $locator
until exhausted.
my $iterator = $u->loc_kw_iter($locator)
while (my $kw_ctx_entry = $iterator->()) {
# ... do something with $kw_ctx_entry
}
=item $u->loc_kw_load_rev_prop($entry)
Loads the revision keywords to $entry
(L<FCM::Context::Keyword::Entry::Location|FCM::Context::Keyword>), assuming that
$entry is not an implied location keyword, and that the keyword locator points
to a VCS location that supports setting up revision keywords in properties.
=item $u->loc_kw_prefix()
Returns the prefix of a FCM keyword. This should be "fcm".
=item $u->loc_origin($locator)
Calls $u->loc_as_parsed($locator) if $locator->get_value_level() is below
FCM::Context::Locator->L_PARSED. Determines the origin of $locator, and returns
a new FCM::Context::Locator that represents the result. E.g. if $locator points
to a Subversion working copy, it returns a new locator that represents the URL
of the working copy.
=item $u->loc_reader($locator)
Calls $u->loc_as_normalised($locator) if $locator->get_value_level() is below
FCM::Context::Locator->L_NORMALISED. Returns a file handle for reading the
content from $locator.
=item $u->loc_rel2abs($locator,$locator_base)
If the value of $locator is a relative path, sets it to an absolute path base on
the $locator_base, provided that $locator and $locator_base is the same type.
=item $u->loc_trunk_at_head($locator)
Returns a string to represent the relative path to the latest main tree, if it
is relevant for $locator.
=item $u->loc_what_type($locator)
Sets $locator->get_type() and returns its value. Currently, this can either be
"svn" for a locator pointing to a Subversion resource or "fs" for a locator
pointing to a file system resource.
=item $u->loc_up_iter($locator)
Returns an iterator that walks up the hierarchy of the $locator, according to
its type.
=item $u->ns_cat(@name_spaces)
Concatenates name-spaces and returns the result.
=item $u->ns_common($ns1,$ns2)
Returns the common parts of 2 name-spaces. For example, if $ns1 is
"egg/ham/bacon" and $ns2 is "egg/ham/sausage", it should return "egg/ham".
=item $u->ns_in_set($ns,\%set)
Returns true if $ns is in a name-space given by the keys of %set.
=item $u->ns_iter($ns,$up)
Returns an iterator that walks up or down a name-space. E.g.:
$iter_ref = $u->ns_iter('a/bee/cee', $u->NS_ITER_UP);
while (defined(my $item = $iter_ref->())) {
print("[$item]");
}
# should print: [a/bee/cee][a/bee][a][]
$iter_ref = $u->ns_iter('a/bee/cee');
while (defined(my $item = $iter_ref->())) {
print("[$item]");
}
# should print: [][a][a/bee][a/bee/cee]
=item $u->ns_sep()
Returns the name-space separator, (i.e. normally "/").
=item $u->report(\%option,$message)
Reports messages using $u->util_of_report(). The default is an instance of
L<FCM::Util::Reporter|FCM::Util::Reporter>. See
L<FCM::Util::Reporter|FCM::Util::Reporter> for detail.
=item $u->shell($command,\%action_of)
Invokes the $command, which can be scalar or a reference to an ARRAY. If a
scalar is specified, it will be separated into an array using the shellwords()
function in L<Text::ParseWords|Text::ParseWords>. If it is a reference to an
ARRAY, the ARRAY will be passed to open3() as is.
The %action_of should contain the actions for i: standard input, e: standard
error output and o: standard output. The default for each of these is an
anonymous subroutinue that does nothing.
Each time the pipe to the child standard input is available for writing, it will
call $action_of{i}->(). If it returns a defined value, the value will be written
to the pipe. If it returns undef, the pipe will be closed.
Each time the pipe from the child standard (error) output is available for
reading, it will read some values to a buffer, and invoke the callback
$action_of{o}->($buffer) (or $action_of{e}->($buffer)). The return value of the
callback will be ignored.
On normal completion, it returns the status code of the command and raises an
FCM::Context::Event->SHELL event:
Any abnormal failure will cause an instance of FCM::Util::Exception to be
thrown. (The return of a non-zero status code by the child is considered a
normal completion.)
=item $u->shell_simple($command)
Wraps $u->shell(), and returns a HASH reference containing {e} (the
standard error), {o} (the standard output) and {rc} (the return code).
=item $u->shell_which($name)
Returns the full path of an executable command $name if it can be found in the
system PATH.
=item $u->task_runner($action_code_ref,$n_workers)
Returns a runner of tasks. It can be configured to work in serial (default) or
parallel. The runner has the following methods:
$n_done = $runner->main($get_code_ref,$put_code_ref);
$runner->destroy();
For each $task (L<FCM::Context::Task|FCM::Context::Task>) returned by the
$get_code_ref->() iterator, invokes $action_ref->($task->get_ctx()). When
$action_ref returns, send the $task back to the caller by calling
$put_code_ref->($task). When it is done, the runner returns the number of tasks
it has done.
The $runner->destroy() method should be called to destroy the $runner when it is
not longer used.
=item $u->timer(\@start)
Returns a CODE reference, which can be called to return the elapsed time. The
@start argument is optional. If specified, it should be in a format as returned
by Time::HiRes::gettimeofday(). If not specified, the current gettimeofday() is
used.
=item $u->uri_match($string)
Returns true if $string is a URI. In array context, returns the scheme and the
opague part of the URI if $string is a URI, or an empty list otherwise.
=item $u->util_of_event($value)
Returns and/or sets the L<FCM::Util::Event|FCM::Util::Event> object that is used
to handle the $u->report() method.
=item $u->util_of_report($value)
Returns and/or sets the L<FCM::Util::Reporter|FCM::Util::Reporter> object that
is used to handle the $u->report() method.
=item $u->version()
Returns the FCM version string in the form C<VERSION (BIN)> where VERSION is the
version string returned by "git describe" or the version file and BIN is
absolute path of the "fcm" command.
=back
=head1 DIAGNOSTICS
=head2 FCM::Util::Exception
This exception is a sub-class of L<FCM::Exception|FCM::Exception> and is thrown
by methods of this class on error.
=head1 COPYRIGHT
(C) Crown copyright Met Office. All rights reserved.
=cut
|