DependentOptions.pm 7.57 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
=pod 

=head1 NAME

    Bio::EnsEMBL::Hive::DependentOptions

=head1 DESCRIPTION

    A parser for PipeConfig files that understands how and when to substitute $self->o() expressions.

=head1 LICENSE

13
    Copyright [1999-2015] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
Matthieu Muffato's avatar
Matthieu Muffato committed
14
    Copyright [2016-2018] EMBL-European Bioinformatics Institute
15 16 17 18 19 20 21 22 23 24 25 26

    Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

         http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software distributed under the License
    is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and limitations under the License.

=head1 CONTACT

27
  Please subscribe to the Hive mailing list:  http://listserver.ebi.ac.uk/mailman/listinfo/ehive-users  to discuss Hive-related questions or to be notified of our updates
28 29 30

=cut

Leo Gordon's avatar
Leo Gordon committed
31 32 33 34 35 36 37

package Bio::EnsEMBL::Hive::DependentOptions;

use strict;
use warnings;
use Getopt::Long qw(:config pass_through);

38 39
use Bio::EnsEMBL::Hive::Utils ('stringify');

Leo Gordon's avatar
Leo Gordon committed
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

sub new {
    my $class = shift @_;

    my $self = bless { @_ }, $class;

    return $self;
}


sub use_cases {      # getter/setter for the list of methods from where $self->o() is called
    my $self    = shift @_;

    if(@_) {
        $self->{_use_cases} = shift @_;
    }
    return $self->{_use_cases} || die "use_cases() has to be set before using";
}


sub load_cmdline_options {
    my $self        = shift @_;
    my $expected    = shift @_;
    my $target      = shift @_ || {};

    local @ARGV = @ARGV;    # make this function reenterable by forbidding it to modify the original parameters
    GetOptions( $target,
        map { my $ref_type = ref($expected->{$_}); $_=~m{\!$} ? $_ : ($ref_type eq 'HASH') ? "$_=s%" : ($ref_type eq 'ARRAY') ? "$_=s@" : "$_=s" } keys %$expected
    );
    return $target;
}


sub root {      # getter/setter for the root
    my $self    = shift @_;

    if(@_) {
        $self->{_root} = shift @_;
    }
    return $self->{_root} ||= {};
}


83
sub is_fully_substituted_string {
Leo Gordon's avatar
Leo Gordon committed
84 85 86
    my $self    = shift @_;
    my $input   = shift @_;

87
    return (!defined($input) || $input !~ /#\:.+?\:#/);
Leo Gordon's avatar
Leo Gordon committed
88 89 90
}


91
sub is_fully_substituted_structure {
Leo Gordon's avatar
Leo Gordon committed
92 93 94 95 96
    my $self    = shift @_;
    my $input   = shift @_;

    unless(my $ref_type = ref($input)) {

97
        return $self->is_fully_substituted_string($input);
Leo Gordon's avatar
Leo Gordon committed
98 99 100

    } elsif($ref_type eq 'HASH') {
        foreach my $value (values %$input) {
101
            unless($self->is_fully_substituted_structure($value)) {
Leo Gordon's avatar
Leo Gordon committed
102 103 104 105
                return 0;
            }
        }
    } elsif($ref_type eq 'ARRAY') {
106
        foreach my $element (@$input) {
107
            unless($self->is_fully_substituted_structure($element)) {
Leo Gordon's avatar
Leo Gordon committed
108 109 110 111 112 113 114 115 116
                return 0;
            }
        }
    }
    return 1;
}


sub hash_leaves {
117
    my ($self, $hash_to, $source, $prefix) = @_;
Leo Gordon's avatar
Leo Gordon committed
118 119 120 121 122 123 124 125 126 127 128 129 130 131

    if(ref($source) eq 'HASH') {
        while(my ($key, $value) = each %$source) {
            my $hash_element_prefix = ($prefix ? "$prefix->" : '') . "{'$key'}";

            $self->hash_leaves($hash_to, $value, $hash_element_prefix);
        }
    } elsif(ref($source) eq 'ARRAY') {
        foreach my $index (0..scalar(@$source)-1) {
            my $element = $source->[$index];
            my $array_element_prefix = ($prefix ? "$prefix->" : '') . "[$index]";

            $self->hash_leaves($hash_to, $element, $array_element_prefix);
        }
132
    } elsif(!$self->is_fully_substituted_string($source)) {
Leo Gordon's avatar
Leo Gordon committed
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
        $hash_to->{$prefix} = 1;
    }

    return $hash_to;
}


sub o {
    my $self    = shift @_;

    my $ptr = $self->root();

    my @syll_seen = ();

    while(defined(my $option_syll = shift @_)) {
        push @syll_seen, $option_syll;

        if( exists($ptr->{$option_syll})
151
        and ((ref($ptr->{$option_syll}) eq 'HASH') or $self->is_fully_substituted_string( $ptr->{$option_syll} ))
Leo Gordon's avatar
Leo Gordon committed
152 153 154
        ) {
            $ptr = $ptr->{$option_syll};        # just descend one level
        } elsif(@_) {
155
            $ptr = $ptr->{$option_syll} = {};   # force intermediate level vivification, even if it overwrites a fully_substituted_string
Leo Gordon's avatar
Leo Gordon committed
156 157 158 159 160 161 162 163 164 165 166 167
        } else {
            $ptr = $ptr->{$option_syll} = "#:subst ".join('->',@syll_seen).":#";   # force leaf level vivification
        }
    }
    return $ptr;
}


sub substitute {
    my $self    = shift @_;
    my $ref     = shift @_;

168
    my $ref_type = ref($$ref);
Leo Gordon's avatar
Leo Gordon committed
169

170
    if($ref_type eq 'HASH') {
Leo Gordon's avatar
Leo Gordon committed
171 172 173 174 175 176 177
        foreach my $value (values %$$ref) {
            $self->substitute( \$value );
        }
    } elsif($ref_type eq 'ARRAY') {
        foreach my $value (@$$ref) {
            $self->substitute( \$value );
        }
178 179 180 181 182 183 184
    } elsif( !$ref_type and defined($$ref) ) {

        if($$ref =~ /^#\:subst ([^:]+)\:#$/) {      # if the given string is one complete substitution, we don't want to force the output into a string
            $$ref = $self->o(split/->/,$1);
        } else {
            $$ref =~ s{(?:#\:subst (.+?)\:#)}{$self->o(split(/->/,$1))}eg;
        }
Leo Gordon's avatar
Leo Gordon committed
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
    }
    return $$ref;
}


sub merge_from_rules {
    my $self    = shift @_;
    my $from    = shift @_;
    my $top     = shift @_;

    my $ref_type = ref($$top);

    unless($ref_type) {
        $$top = $from;
    } elsif($ref_type eq 'HASH') {
        foreach my $key (keys %$from) {
            $self->merge_from_rules( $from->{$key}, \$$top->{$key} );
        }
    }
}

sub process_options {
    my $self    = shift @_;

    my $definitely_used_options = $self->root();

        # dry-run of these methods allows us to collect definitely_used_options
    foreach my $method (@{ $self->use_cases() }) {
        $self->$method();
    }

216
    my $possibly_used_options = { 'ENV' => \%ENV };
Leo Gordon's avatar
Leo Gordon committed
217 218 219 220 221 222 223 224 225 226 227
    $self->root( $possibly_used_options );

        # the first run of this method allows us to collect possibly_used_options
    my $rules = $self->default_options();

    $self->load_cmdline_options( { %$definitely_used_options, %$possibly_used_options }, $rules );

    $self->root( $definitely_used_options );


    my $rules_to_go;
Leo Gordon's avatar
Leo Gordon committed
228
    my $attempts = 32;
Leo Gordon's avatar
Leo Gordon committed
229 230 231 232 233 234 235 236 237
    do {
        $rules_to_go = 0;
        foreach my $key (keys %$definitely_used_options) {
            if(exists $rules->{$key}) {
                my $value = $self->substitute( \$rules->{$key} );

                    # it has to be intelligently (recursively, on by-element basis) merged back into the tree under $self->o($key):
                $self->merge_from_rules( $value, \$self->root->{$key} );

238 239
                if($self->is_fully_substituted_structure($value)) {
                    #warn "Resolved rule: $key -> ".stringify($value)."\n";
Leo Gordon's avatar
Leo Gordon committed
240
                } else {
241
                    #warn "Unresolved rule: $key -> ".stringify($value)."\n";
Leo Gordon's avatar
Leo Gordon committed
242 243 244 245 246
                    $rules_to_go++;
                }
            }
        }
        $attempts--;
247 248
        #warn "=======================[$rules_to_go rules to go; $attempts attempts to go]=================\n\n";
        #warn " definitely_used_options{} contains: ".stringify($definitely_used_options)."\n";
Leo Gordon's avatar
Leo Gordon committed
249 250
    } while($rules_to_go and $attempts);

251 252
    #warn "=======================[out of the substitution loop]=================\n\n";

253 254
    my $missing_options = $self->hash_leaves( {}, $self->root, '' );

Leo Gordon's avatar
Leo Gordon committed
255 256 257 258 259 260 261
    if(scalar(keys %$missing_options)) {
        warn "Missing or incomplete definition of the following options:\n";
        foreach my $key (sort keys %$missing_options) {
            print "\t$key\n";
        }
        exit(1);
    } else {
262
        #warn "Done parsing options!\n";
Leo Gordon's avatar
Leo Gordon committed
263 264 265 266 267
    }
}

1;