blob: 2f94e495e6a7e941dc4d0dd1469a8f3274d8d4f3 [file] [log] [blame]
Gunnar Millsbff56842017-09-25 15:49:16 -05001#!/usr/bin/env perl
Matt Spinlerab2b9c32018-03-08 16:08:32 -06002
3#This script replaces MRW attribute names with their values from the
4#MRW XML in any file. In addition, it can evaluate mathematical
5#expressions if they are in [[ ]]s and can use variables passed in from
6#the command line in those expressions.
7#
8#For example, if the attribute FOO has a value of 50 in the MRW, and
9#the program was started with: -v "MY_VAR1=200 MY_VAR2=400"
10#
11#then the line
12# [[(MRW_FOO * MY_VAR1) + 5]]..[[MRW_FOO * MY_VAR2]]
13#
14#would get written out as:
15# 10005..20000
16#
17
Gunnar Millsbff56842017-09-25 15:49:16 -050018use strict;
19use warnings;
20
21use mrw::Targets; # Set of APIs allowing access to parsed ServerWiz2 XML output
22use Getopt::Long; # For parsing command line arguments
23
24# Globals
Matt Spinlerab2b9c32018-03-08 16:08:32 -060025my $force = 0;
Gunnar Millsbff56842017-09-25 15:49:16 -050026my $serverwizFile = "";
Matt Spinlerab2b9c32018-03-08 16:08:32 -060027my $debug = 0;
Gunnar Millsbff56842017-09-25 15:49:16 -050028my $outputFile = "";
Gunnar Mills45765332017-09-26 16:19:32 -050029my $settingsFile = "";
Matt Spinlerab2b9c32018-03-08 16:08:32 -060030my $expressionVars = "";
31my %exprVars;
Gunnar Millsbff56842017-09-25 15:49:16 -050032
33# Command line argument parsing
34GetOptions(
35"f" => \$force, # numeric
36"i=s" => \$serverwizFile, # string
37"o=s" => \$outputFile, # string
Gunnar Mills45765332017-09-26 16:19:32 -050038"s=s" => \$settingsFile, # string
Matt Spinlerab2b9c32018-03-08 16:08:32 -060039"v=s" => \$expressionVars, # string
Gunnar Millsbff56842017-09-25 15:49:16 -050040"d" => \$debug,
41)
42or printUsage();
43
Gunnar Mills45765332017-09-26 16:19:32 -050044if (($serverwizFile eq "") or ($outputFile eq "") or ($settingsFile eq "") )
Gunnar Millsbff56842017-09-25 15:49:16 -050045{
46 printUsage();
47}
48
49# API used to access parsed XML data
50my $targetObj = Targets->new;
51if($debug == 1)
52{
53 $targetObj->{debug} = 1;
54}
55
56if($force == 1)
57{
58 $targetObj->{force} = 1;
59}
60
61$targetObj->loadXML($serverwizFile);
62print "Loaded MRW XML: $serverwizFile \n";
63
Gunnar Mills45765332017-09-26 16:19:32 -050064open(my $inFh, '<', $settingsFile) or die "Could not open file '$settingsFile' $!";
65open(my $outFh, '>', $outputFile) or die "Could not open file '$outputFile' $!";
66
Matt Spinlerab2b9c32018-03-08 16:08:32 -060067if (length($expressionVars) > 0)
68{
69 loadVars($expressionVars);
70}
71
Gunnar Mills45765332017-09-26 16:19:32 -050072# Process all the targets in the XML
73foreach my $target (sort keys %{$targetObj->getAllTargets()})
74{
75 # A future improvement could be to specify the MRW target.
76 next if ("SYS" ne $targetObj->getType($target, "TYPE"));
77 # Read the settings YAML replacing any MRW_<variable name> with their
78 # MRW value
79 while (my $row = <$inFh>)
80 {
81 while ($row =~ /MRW_(.*?)\W/g)
82 {
83 my $setting = $1;
84 my $settingValue = $targetObj->getAttribute($target, $setting);
85 $row =~ s/MRW_${setting}/$settingValue/g;
86 }
87 print $outFh $row;
88 }
89 last;
90 close $inFh;
91 close $outFh;
92}
93
Matt Spinlerab2b9c32018-03-08 16:08:32 -060094#Parse the variable=value string passed in from the
95#command line and load it into %exprVars.
96sub loadVars
97{
98 my $varString = shift;
99
100 #Example: "VAR1=VALUE1 VAR2=VALUE2"
101 my @entries = split(' ', $varString);
102
103 for my $entry (@entries)
104 {
105 my ($var, $value) = $entry =~ /(.+)=(.+)/;
106
107 if ((not defined $var) || (not defined $value))
108 {
109 die "Could not parse expression variable string $varString\n";
110 }
111
112 $exprVars{$var} = $value;
113 }
114}
115
Gunnar Millsbff56842017-09-25 15:49:16 -0500116# Usage
117sub printUsage
118{
119 print "
Matt Spinlerab2b9c32018-03-08 16:08:32 -0600120 $0 -i [XML filename] -s [Settings YAML] -o [Output filename] -v [expr vars] [OPTIONS]
Gunnar Mills45765332017-09-26 16:19:32 -0500121
122Required:
123 -i = MRW XML filename
124 -s = The Setting YAML with MRW variables in MRW_<MRW variable name> format
125 -o = YAML output filename
Matt Spinlerab2b9c32018-03-08 16:08:32 -0600126Optional:
127 -v = Variables and values for any [[expression]] evaluation
128 in the form: \"VAR1=VALUE1 VAR2=VALUE2\"
Gunnar Millsbff56842017-09-25 15:49:16 -0500129Options:
130 -f = force output file creation even when errors
131 -d = debug mode
Gunnar Millsbff56842017-09-25 15:49:16 -0500132 \n";
133 exit(1);
134}