Ticket #13293: perl-config-parser-v0.patch

File perl-config-parser-v0.patch, 5.2 KB (added by Bill Meek, 17 months ago)
  • mythtv/bindings/perl/MythTV.pm

    diff --git a/mythtv/bindings/perl/MythTV.pm b/mythtv/bindings/perl/MythTV.pm
    index dd61d3b05d..6375bafe4a 100644
    a b  
    88#
    99
    1010# Version
    11     $VERSION = '.28git';
     11    $VERSION = 'v30git';
    1212
    1313# Load sub libraries
    1414    use IO::Socket::INET::MythTV;
    package MythTV; 
    2424    use HTTP::Request;
    2525    use LWP::UserAgent;
    2626    use POSIX;
     27    use XML::Simple qw(:strict);
     28    #use XML::LibXML;
     29    #use Data::Dumper;
    2730
    2831# Load the UPNP libraries if we have them, but die nicely if we don't.
    2932    BEGIN {
    package MythTV; 
    170173        ? $ENV{'MYTHCONFDIR'}
    171174        : "$homedir/.mythtv"
    172175        ;
    173     unless (-T "$conf/config.xml") {
     176    my $config_file = "$conf/config.xml";
     177    unless (-T "$config_file") {
     178
    174179        unless ($has_upnp) {
    175180            die "Please install Net::UPnP or copy ~/.mythtv/config.xml\n"
    176                ."from a working MythTV installation to $conf/config.xml.\n";
     181               ."from a working MythTV installation to $config_file.\n";
    177182        }
    178183    # Try to create the config directory first, so we can die sooner if it fails.
    179184        unless (-d $conf) {
    package MythTV; 
    243248            #print $dev->getfriendlyname(), "\n";
    244249        }
    245250    # Save the config.xml
    246         open CONF, ">$conf/config.xml" or die "Can't write to $conf/config.xml:  $!\n";
     251        open CONF, ">$config_file" or die "Can't write to $config_file:  $!\n";
    247252        print CONF <<EOF;
    248253<Configuration>
    249254  <UPnP>
    EOF 
    286291                       'db_name'  => 'mythconverg',
    287292                       'upnp_pin' => '',
    288293                      );
    289     open(CONF, "$conf/config.xml") or die "Unable to read $conf/config.xml:  $!\n";
    290     while (my $line = <CONF>) {
    291     # Search through the XML data
    292         if ($line =~ m#<SecurityPin>(.*?)</SecurityPin>#) {
    293             $mysql_conf{'upnp_pin'} = $1;
    294         }
    295         elsif (($line =~ m#<Host>(.*?)</Host>#) ||
    296                ($line =~ m#<DBHostName>(.*?)</DBHostName>#)) {
    297             $mysql_conf{'db_host'}  = $1;
    298         }
    299         elsif (($line =~ m#<UserName>(.*?)</UserName>#) ||
    300                ($line =~ m#<DBUserName>(.*?)</DBUserName>#)) {
    301             $mysql_conf{'db_user'}  = $1;
    302         }
    303         elsif (($line =~ m#<Password>(.*?)</Password>#) ||
    304                ($line =~ m#<DBPassword>(.*?)</DBPassword>#)) {
    305             $mysql_conf{'db_pass'}  = $1;
    306             $mysql_conf{'db_pass'} =~ s/&amp;/&/sg;
    307             $mysql_conf{'db_pass'} =~ s/&gt;/>/sg;
    308             $mysql_conf{'db_pass'} =~ s/&lt;/</sg;
    309         }
    310         elsif (($line =~ m#<DatabaseName>(.*?)</DatabaseName>#) ||
    311                ($line =~ m#<DBName>(.*?)</DBName>#)) {
    312             $mysql_conf{'db_name'}  = $1;
    313         }
    314         elsif (($line =~ m#<Port>(\d*?)</Port>#) ||
    315                ($line =~ m#<DBPort>(.*?)</DBPort>#)) {
    316             $mysql_conf{'db_port'}  = $1;
    317         }
    318     # Hostname override.  Not sure if this is still valid or not
    319         elsif ($line =~ m#<LocalHostName>(.*?)</LocalHostName>#) {
    320             $profileoverride = $1;
    321             if ($profileoverride =~ /^\s*$/) {
    322                 print STDERR "Warning: LocalHostName tag is empty. Ignoring.\n";
    323             } elsif ($profileoverride ne "my-unique-identifier-goes-here") {
    324                 $mysql_conf{'hostname'} = $profileoverride;
    325             }
     294
     295    my $xml = new XML::Simple;
     296    my $config = $xml->XMLin($config_file, ForceArray => 0, KeyAttr => [ ]);
     297
     298    my $cfg_err = 0;
     299
     300    foreach my $cfg_element ('Host', 'UserName', 'Password', 'DatabaseName', 'Port') {
     301        if (ref $config->{'Database'}->{$cfg_element} eq 'ARRAY') {
     302            print STDERR "<$cfg_element> appers more than once in $config_file\n";
     303            $cfg_err++;
     304        }
     305        if (!length $config->{'Database'}->{$cfg_element}) {
     306            print STDERR "<$cfg_element> is missing from $config_file\n";
     307            $cfg_err++;
    326308        }
    327309    }
    328     close CONF;
    329310
    330 # Cleanup
     311    if ($cfg_err > 0) {
     312        return;
     313    }
     314
     315    $mysql_conf{'db_host'} = $config->{'Database'}->{'Host'};
     316    $mysql_conf{'db_user'} = $config->{'Database'}->{'UserName'};
     317    $mysql_conf{'db_pass'} = $config->{'Database'}->{'Password'};
     318    $mysql_conf{'db_name'} = $config->{'Database'}->{'DatabaseName'};
     319    $mysql_conf{'db_port'} = $config->{'Database'}->{'Port'};
     320    $mysql_conf{'hostname'} = $config->{'LocalHostName'};
     321
     322=pod # XMLLib:
     323    my $config = eval {
     324        XML::LibXML->load_xml(location => $config_file);
     325    };
     326    if($@) {
     327        print STDERR "XML format error in '$config_file':\n$@";
     328        return;
     329    }
     330
     331    foreach my $credentials ($config->findnodes('//Database')) {
     332        $mysql_conf{'db_host'} = $credentials->findvalue('./Host');
     333        $mysql_conf{'db_user'} = $credentials->findvalue('./UserName');
     334        $mysql_conf{'db_pass'} = $credentials->findvalue('./Password');
     335        $mysql_conf{'db_name'} = $credentials->findvalue('./DatabaseName');
     336        $mysql_conf{'db_port'} = $credentials->findvalue('./Port');
     337    }
     338    $mysql_conf{'hostname'} = $config->findnodes('Configuration/LocalHostName');
     339=cut
     340
     341    #print Dumper($config);
     342
     343# Default DB port
    331344    $mysql_conf{'db_port'} ||= 3306;
    332345
    333346# Constructor