source: LMDZ6/branches/Amaury_dev/tools/fcm/lib/FCM/Util/ConfigUpgrade.pm

Last change on this file was 5129, checked in by abarral, 4 months ago

Re-add removed by mistake fcm

File size: 3.8 KB
Line 
1# ------------------------------------------------------------------------------
2# Copyright (C) 2006-2021 British Crown (Met Office) & Contributors.
3#
4# This file is part of FCM, tools for managing and building source code.
5#
6# FCM is free software: you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation, either version 3 of the License, or
9# (at your option) any later version.
10#
11# FCM is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with FCM. If not, see <http://www.gnu.org/licenses/>.
18# ------------------------------------------------------------------------------
19use strict;
20use warnings;
21# ------------------------------------------------------------------------------
22package FCM::Util::ConfigUpgrade;
23use base qw{FCM::Class::CODE};
24
25use FCM::Context::ConfigEntry;
26
27my %DECL_PATTERN_OF = (
28    browser_mapping => qr{\A set::browser_mapping(?:_default|:: ([^:]+)):: (.+) \z}ixms,
29    keyword_loc     => qr{\A set::(?:repos|url):: (.+) \z}ixms,
30    keyword_rev     => qr{\A set::revision:: ([^:]+) :: (.+) \z}ixms,
31);
32my @UPGRADE_FUNCS = (
33    \&_upgrade_browser_mapping_decl,
34    \&_upgrade_keyword_loc_decl,
35    \&_upgrade_keyword_rev_decl,
36);
37
38# Creates the class.
39__PACKAGE__->class({}, {action_of => {upgrade => \&_upgrade}});
40
41sub _upgrade {
42    my ($attrib_ref, $config_entry) = @_;
43    if (!defined($config_entry)) {
44        return;
45    }
46    for my $func (@UPGRADE_FUNCS) {
47        $func->($config_entry);
48        if ($func->($config_entry)) {
49            return $config_entry;
50        }
51    }
52    return $config_entry;
53}
54
55# Upgrades a browser mapping declaration.
56sub _upgrade_browser_mapping_decl {
57    my ($config_entry) = @_;
58    my ($ns, $key)
59        = $config_entry->get_label() =~ $DECL_PATTERN_OF{browser_mapping};
60    if (!$key) {
61        return;
62    }
63    $config_entry->set_label(
64          $key eq 'browser_url_template' ? 'browser.loc-tmpl'
65        : $key eq 'browser_rev_template' ? 'browser.rev-tmpl'
66        :                                  'browser.comp-pat'
67    );
68    if ($ns) {
69        $config_entry->set_ns_list([$ns]);
70    }
71}
72
73# Upgrades a location keyword declaration.
74sub _upgrade_keyword_loc_decl {
75    my ($config_entry) = @_;
76    my ($ns) = $config_entry->get_label() =~ $DECL_PATTERN_OF{keyword_loc};
77    if (!$ns) {
78        return;
79    }
80    $config_entry->set_label('location');
81    $config_entry->get_modifier_of()->{primary} = 1;
82    $config_entry->set_ns_list([$ns]);
83}
84
85# Upgrades a revision keyword declaration.
86sub _upgrade_keyword_rev_decl {
87    my ($config_entry) = @_;
88    my ($ns, $key) = $config_entry->get_label() =~ $DECL_PATTERN_OF{keyword_rev};
89    if (!$ns || !$key) {
90        return;
91    }
92    $config_entry->set_label('revision');
93    $config_entry->set_ns_list([$ns, $key]);
94}
95
96# ------------------------------------------------------------------------------
971;
98__END__
99
100=head1 NAME
101
102FCM::Util::ConfigUpgrade
103
104=head1 SYNOPSIS
105
106    use FCM::Util::ConfigUpgrade;
107    $upgrade = FCM::Util::ConfigUpgrade->new();
108    if (!$upgrade->($entry)) {
109        die($entry->get_label(), ": cannot upgrade.\n");
110    }
111    # ... do something with $entry
112
113=head1 DESCRIPTION
114
115Provides a utility to upgrade FCM 1 configuration to FCM 2 configuration.
116
117=head1 METHODS
118
119=over 4
120
121=item $class->new()
122
123Creates and returns a new instance of this utility.
124
125=item $util->($entry)
126
127Upgrades the content of $entry, where possible. Only keyword related
128declarations in the FCM 1 common configuration files are currently supported.
129
130=back
131
132=head1 COPYRIGHT
133
134Copyright (C) 2006-2021 British Crown (Met Office) & Contributors.
135
136=cut
Note: See TracBrowser for help on using the repository browser.