openfoam-selector-menu 6.7 KB
Newer Older
1
#!/usr/bin/perl
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#
# Copyright (c) 2007 Cisco Systems, Inc.  All rights reserved.
#
# Simple perl script to effect system-wide and per-user default
# selections of which MPI implementation to use.
#

use strict;
use Getopt::Long;
use Text::Wrap;
use File::Copy;

#===========================================================================

=head1 NAME

18
openfoam-selector-menu - A menu-based wrapper around the openfoam-selector command
19 20 21

=head1 SYNOPSIS

22
openfoam-selector-menu [--version] [--help]
23 24 25

=head1 DESCRIPTION

26 27
Simple wrapper around the openfoam-selector command-line
Adapted from mpi-selector-menu.
28 29 30 31 32 33 34 35 36

=head1 AUTHOR

Written by Jeff Squyres.

=head1 REPORTING BUGS

Send bug reports to the OpenFabrics general mailing list (see
L<http://www.openfabrics.org/>).  This is a high-volume mailing list,
37
so be sure to put "openfoam-selector" in the subject to ensure that it is
38 39 40 41 42 43 44 45 46 47
not missed.

=head1 COPYRIGHT

Copyright (c) 2007 Cisco Systems, Inc.  All rights reserved.

This is free software; see the source for copying conditions.  There
is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.

48 49
Copyright (C) 2018 OpenCFD Ltd.

50 51
=head1 SEE ALSO

52
openfoam-selector(1)
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74

=cut

#===========================================================================

sub show_help {
    our $silent;
    my $ret = shift;

    print "$0 options:

--help                This help message
--version             Display the version of $0.
";
    exit($ret);
}

#===========================================================================

sub make_safe_filename {
    my $name = shift;
    $name =~ s/[ :\/\\\*\&\$\#\@\!\t\n\[\]\{\}\(\)]/_/g;
75
    $name =~ s/^__internal//;  # Disallow reserved names
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
    return $name;
}

#===========================================================================

sub error {
    our $silent;
    print STDERR wrap("ERROR: ", "       ", @_) . "\n"
      if (!$silent);
    exit(1);
}


sub warning {
    our $silent;
    print STDERR wrap("WARNING: ", "         ", @_) . "\n"
      if (!$silent);
}


sub verbose {
    our $silent;
    our $verbose_flag;
    print wrap("", "", @_) . "\n"
      if ($verbose_flag && !$silent);
}

#===========================================================================

sub get_yn {
    my $prompt = shift;
    my $default = shift;

    if (defined($default)) {
        if ($default) {
            $default = 1;
            $prompt .= " (Y/n) ";
        } else {
            $default = 0;
            $prompt .= " (y/N) ";
        }
    } else {
        $prompt .= " (y/n/) ";
    }

    while (1) {
        print $prompt;
        my $ans = <STDIN>;
        chomp($ans);
        if ($ans =~ /y/i) {
            return 1;
        } elsif ($ans =~ /n/i) {
            return 0;
        } elsif ("" eq $ans) {
130
            return $default
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
              if (defined($default));
        }
        print "\nPlease choose Y or N\n";
    }
}

#===========================================================================

# Set autoflush
select STDOUT;
$| = 1;

# Module options
$Text::Wrap::columns = 76;
&Getopt::Long::Configure("bundling");

my $help = 0;
my $version = 0;

my $ok = Getopt::Long::GetOptions("help|h" => \$help,
                                  "version" => \$version,
                                  );

show_help(1) if (!$ok);
show_help(0) if ($help);

#---------------------------------------------------------------------------

# Version informtion

if ($version) {
    print "$0 version 1.0.0

Copyright (c) 2007 Cisco Systems, Inc.  All rights reserved.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Written by Jeff Squyres.\n";
    exit(0);
}

#---------------------------------------------------------------------------

174
# Ensure that we can find the openfoam-selector executable
175 176 177 178 179

#---------------------------------------------------------------------------

sub query {
    my $arg = shift;
180
    my $ret = `openfoam-selector --query $arg | head -n 1 | cut -d: -f2`;
181 182 183 184 185 186
    chomp($ret);
    $ret = "<none>" if ($ret eq "");
    return $ret;
}

sub list {
187 188
    my $list = [];
    open(CMD, "openfoam-selector --list|");
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
    while (<CMD>) {
        chomp;
        push(@$list, $_);
    }
    sort @$list;
    return $list;
}

sub get_user_or_system {
    my $ret;
    while (1) {
        print "Operator on the per-user or system-wide default (u/s)? ";
        $ret = <STDIN>;
        if (!defined($ret)) {
            print "\n";
            exit(0);
        }

        if ($ret =~ /^\s*u\s*$/i) {
            return "u";
        } elsif ($ret =~ /^\s*s\s*$/i) {
            return "s";
        }
    }
}

# Main menu

my $registered = list();
my $first = 1;
my $made_changes = 0;
220
my $warning = "\nWARNING: Changes made to openfoam-selector defaults will not be visible until
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
you start a new shell!\n\n";

while (1) {
    my $system_default = query("--system");
    my $user_default = query("--user");

    if ($first) {
        $first = 0;
    } else {
        print "\n";
    }

    print "Current system default: $system_default
Current user default:   $user_default

    \"u\" and \"s\" modifiers can be added to numeric and \"U\"
    commands to specify \"user\" or \"system-wide\".\n\n";

    my $i = 1;
    while ($i - 1 <= $#$registered) {
        print "$i. $$registered[$i - 1]\n";
        ++$i;
    }
    --$i;
    print "U. Unset default
Q. Quit

Selection (1-${i}[us], U[us], Q): ";
    my $val = <STDIN>;

    # Check for bozo: no stdin
    if (!defined($val)) {
        print "\n";
        print $warning
            if ($made_changes);
        exit(0);
    }

    # Check letter options
    if ($val =~ /^q$/i) {
        print $warning
            if ($made_changes);
        exit(0);
    } elsif ($val =~ /^\s*u\s*s\s*$/i) {
265
        system("openfoam-selector --unset --system");
266 267
        $made_changes = 1;
    } elsif ($val =~ /^\s*u\s*u\s*$/i) {
268
        system("openfoam-selector --unset --user");
269 270 271
        $made_changes = 1;
    } elsif ($val =~ /^\s*u\s*$/i) {
        $val = get_user_or_system();
272
        my $str = "openfoam-selector --unset " .
273 274 275
            ("s" eq $val ? "--system" : "--user");
        system($str);
        $made_changes = 1;
276
    }
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293

    # Numeric options
    elsif ($val =~ m/^\s*([0-9]+)\s*([us])*\s*$/) {
        # In range?
        if ($1 < 0 || $1 > $i) {
            printf("$1 is not a valid choice\n");
            next;
        }

        # Figure out if system or user
        if ("" eq $2) {
            $val = get_user_or_system();
        } else {
            $val = $2;
        }

        my $i = $1 - 1;
294
        my $str = "openfoam-selector --set $$registered[$i] " .
295 296 297 298 299 300 301 302
            ("s" eq $val ? "--system" : "--user");
        system($str);
        $made_changes = 1;
    }
}

# Shouldn't get here
exit(0);