summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatt Spinler <spinler@us.ibm.com>2018-03-08 16:08:32 -0600
committerMatt Spinler <spinler@us.ibm.com>2018-03-14 21:05:48 +0000
commitab2b9c3f6b0f82c5465a4c6ca66c8b3feef2d5cb (patch)
tree2c51ec5bd07bac786d438218b91fac7047f2be8e
parent312fa6baba47e6a6fa9001f577d2a8993e148dc9 (diff)
downloadphosphor-mrw-tools-ab2b9c3f6b0f82c5465a4c6ca66c8b3feef2d5cb.tar.gz
phosphor-mrw-tools-ab2b9c3f6b0f82c5465a4c6ca66c8b3feef2d5cb.zip
gen_settings.pl: Pass in new commandline argument
This is the first commit in a series to solve the problem of needing to adjust an MRW attribute's value before writing it back out to the settings override value. This will be done by allowing mathematical expressions to be present in the input file, and evaluating them to get a final result that will get written out. These expressions can also use variables whose names and values are passed in from the command line. This commit adds the ability to pass in the optional variables and values from the command line with the -v argument, like: -v "VAR1=VAL1 VAR2=VAL2" Change-Id: I3baf47b38d6da25c5abdaae175ddfc6e97176927 Signed-off-by: Matt Spinler <spinler@us.ibm.com>
-rwxr-xr-xgen_settings.pl55
1 files changed, 52 insertions, 3 deletions
diff --git a/gen_settings.pl b/gen_settings.pl
index d3389b5..2f94e49 100755
--- a/gen_settings.pl
+++ b/gen_settings.pl
@@ -1,4 +1,20 @@
#!/usr/bin/env perl
+
+#This script replaces MRW attribute names with their values from the
+#MRW XML in any file. In addition, it can evaluate mathematical
+#expressions if they are in [[ ]]s and can use variables passed in from
+#the command line in those expressions.
+#
+#For example, if the attribute FOO has a value of 50 in the MRW, and
+#the program was started with: -v "MY_VAR1=200 MY_VAR2=400"
+#
+#then the line
+# [[(MRW_FOO * MY_VAR1) + 5]]..[[MRW_FOO * MY_VAR2]]
+#
+#would get written out as:
+# 10005..20000
+#
+
use strict;
use warnings;
@@ -6,11 +22,13 @@ use mrw::Targets; # Set of APIs allowing access to parsed ServerWiz2 XML output
use Getopt::Long; # For parsing command line arguments
# Globals
-my $force = 0;
+my $force = 0;
my $serverwizFile = "";
-my $debug = 0;
+my $debug = 0;
my $outputFile = "";
my $settingsFile = "";
+my $expressionVars = "";
+my %exprVars;
# Command line argument parsing
GetOptions(
@@ -18,6 +36,7 @@ GetOptions(
"i=s" => \$serverwizFile, # string
"o=s" => \$outputFile, # string
"s=s" => \$settingsFile, # string
+"v=s" => \$expressionVars, # string
"d" => \$debug,
)
or printUsage();
@@ -45,6 +64,11 @@ print "Loaded MRW XML: $serverwizFile \n";
open(my $inFh, '<', $settingsFile) or die "Could not open file '$settingsFile' $!";
open(my $outFh, '>', $outputFile) or die "Could not open file '$outputFile' $!";
+if (length($expressionVars) > 0)
+{
+ loadVars($expressionVars);
+}
+
# Process all the targets in the XML
foreach my $target (sort keys %{$targetObj->getAllTargets()})
{
@@ -67,16 +91,41 @@ foreach my $target (sort keys %{$targetObj->getAllTargets()})
close $outFh;
}
+#Parse the variable=value string passed in from the
+#command line and load it into %exprVars.
+sub loadVars
+{
+ my $varString = shift;
+
+ #Example: "VAR1=VALUE1 VAR2=VALUE2"
+ my @entries = split(' ', $varString);
+
+ for my $entry (@entries)
+ {
+ my ($var, $value) = $entry =~ /(.+)=(.+)/;
+
+ if ((not defined $var) || (not defined $value))
+ {
+ die "Could not parse expression variable string $varString\n";
+ }
+
+ $exprVars{$var} = $value;
+ }
+}
+
# Usage
sub printUsage
{
print "
- $0 -i [XML filename] -s [Settings YAML] -o [Output filename] [OPTIONS]
+ $0 -i [XML filename] -s [Settings YAML] -o [Output filename] -v [expr vars] [OPTIONS]
Required:
-i = MRW XML filename
-s = The Setting YAML with MRW variables in MRW_<MRW variable name> format
-o = YAML output filename
+Optional:
+ -v = Variables and values for any [[expression]] evaluation
+ in the form: \"VAR1=VALUE1 VAR2=VALUE2\"
Options:
-f = force output file creation even when errors
-d = debug mode
OpenPOWER on IntegriCloud