From: "Greg A. Woods" <[email protected]>

This script is an abomination, and should not be necessary.  I have
temporarily re-implemented it in C as an option to the snmp-ups driver
itself.  Eventually nut-scanner should link directly with the *-mib.c
modules instead.
---
 tools/nut-snmpinfo.py |   70 +++++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 59 insertions(+), 11 deletions(-)

diff --git a/tools/nut-snmpinfo.py b/tools/nut-snmpinfo.py
index 89cc359..8b26ed1 100755
--- a/tools/nut-snmpinfo.py
+++ b/tools/nut-snmpinfo.py
@@ -1,4 +1,5 @@
 #!/usr/bin/env python
+#
 #   Copyright (C) 2011 - Frederic Bohe <[email protected]>
 #
 #   This program is free software; you can redistribute it and/or modify
@@ -15,24 +16,58 @@
 #   along with this program; if not, write to the Free Software
 #   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
-# This program extracts all SNMP information related to NUT snmp-ups
-# drivers.
+# NOTICE!!!
+#
+# This script has been deprecated.  It remains here only in hopes of
+# helping anyone trying to set up cross-compilation.  It is not
+# regularly tested, and it may not work!  See the comments about how
+# it could be improved.
+
+# This massive mess of trouble attempts to extract some SNMP
+# information from the MIB-defining source code modules of the NUT
+# snmp-ups driver so that it can be encoded into the nut-scanner
+# program.
 
+import sys
+import getpass
+import time
 import glob
 import re
 
 output_file_name="./nut-scanner/nutscan-snmp.h"
 output_file = open(output_file_name,'w')
 
-#expand #define constant
+# expand a #define constant, recursively
+#
+# XXX this is really gross and ugly, and should be done by cpp
+# instead!!!
+#
+# some Makefile should list each file documenting a MIB and a rule
+# should be defined therein to create a *.i file from each of those
+# files using $(CPP), like this:
+#
+#      .c.i:
+#              $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) 
$(CPPFLAGS) -o $@ $<
+#
+# Then this script would merely look for the definition of the varible
+# with the type "mib2nut_info_t" in the resulting *.i files and decode
+# that as necessary
+#
+# Even better than trying to parse C struct initializers would be a
+# wee C program, or perhaps a hidden option to snmp-ups itself, that
+# would do this direct from the data defined in these mib2nut_info_t
+# variables.
+#
 def expand_define(filename,constant):
        ret_line = ""
        f = open(filename, 'r')
        for line in f:
-               if constant in line and "#define" in line:
-                       line_without_carriage_return  = re.sub("[\n\r]", "", 
line)
+               nocomment = re.sub("[   ]*/\*.*\*/[     ]*", "", line)
+               if constant in nocomment and "#define" in nocomment:
+                       line_without_carriage_return  = re.sub("[\n\r]", "", 
nocomment)
                        line_with_single_blank = re.sub("[ \t]+", " ", 
line_without_carriage_return)
-                       define_line = line_with_single_blank.split(" ");
+                       define_line = line_with_single_blank.split(" ")
+                       #print define_line
                        #define_line[0] = "#define"
                        #define_line[1] = const name
                        #define_line[2...] = const value (may be other const 
name)
@@ -44,11 +79,15 @@ def expand_define(filename,constant):
                                                clean_elem = re.sub("\"", "", 
elem)
                                                ret_line = ret_line + clean_elem
                                        else:
-                                               ret_line = ret_line + 
expand_define(filename,elem);
+                                               #print "recursing into " + 
filename + " for " + elem
+                                               ret_line = ret_line + 
expand_define(filename, elem)
+       f.close()
+
        return ret_line
 
 
 output_file.write( "/* nutscan-snmp\n" )
+output_file.write( " *\n" )
 output_file.write( " *  Copyright (C) 2011 - Frederic Bohe 
<[email protected]>\n" )
 output_file.write( " *\n" )
 output_file.write( " *  This program is free software; you can redistribute it 
and/or modify\n" )
@@ -65,6 +104,10 @@ output_file.write( " *  You should have received a copy of 
the GNU General Publi
 output_file.write( " *  along with this program; if not, write to the Free 
Software\n" )
 output_file.write( " *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, 
MA 02111-1307 USA\n" )
 output_file.write( " */\n" )
+output_file.write( "/*\n" )
+output_file.write( " * WARNING!!!  This file was generated by: " + sys.argv[0] 
+ "\n" )
+output_file.write( " * (run by: " + getpass.getuser() + " on: " + time.ctime() 
+ ")\n" )
+output_file.write( " */\n" )
 output_file.write( "\n" )
 output_file.write( "#ifndef DEVSCAN_SNMP_H\n" )
 output_file.write( "#define DEVSCAN_SNMP_H\n" )
@@ -78,6 +121,9 @@ output_file.write( "\n" )
 output_file.write( "/* SNMP IDs device table */\n" )
 output_file.write( "static snmp_device_id_t snmp_device_table[] = {\n" )
 
+
+# Holy ugly parsing Batman!
+#
 for filename in glob.glob('../drivers/*-mib.c'):
        list_of_line = open(filename,'r').read().split(';')
        for line in list_of_line:
@@ -99,7 +145,9 @@ for filename in glob.glob('../drivers/*-mib.c'):
                        else:
                                source_sysoid = "NULL"
 
-                       #decode source_oid
+                       #print "found OID '" + source_oid + "', and sysOID '" + 
source_sysoid + "' in " + filename
+
+                       # decode source_oid
                        line = source_oid.lstrip(" ")
                        line2 = line.split(" ")
 
@@ -109,9 +157,9 @@ for filename in glob.glob('../drivers/*-mib.c'):
                                        clean_elem = re.sub("\"", "", elem)
                                        oid = oid+clean_elem
                                else:
-                                       oid = oid + 
expand_define(filename,elem);
+                                       oid = oid + expand_define(filename, 
elem);
 
-                       #decode source_sysoid
+                       # decode source_sysoid
                        line = source_sysoid.lstrip(" ")
                        line = line.rstrip(" ")
                        line2 = line.split(" ")
@@ -122,7 +170,7 @@ for filename in glob.glob('../drivers/*-mib.c'):
                                        clean_elem = re.sub("\"", "", elem)
                                        sysoid = sysoid+clean_elem
                                else:
-                                       sysoid = sysoid + 
expand_define(filename,elem);
+                                       sysoid = sysoid + 
expand_define(filename, elem);
 
                        if sysoid == "":
                                sysoid = "NULL"
-- 
1.7.9.2


_______________________________________________
Nut-upsdev mailing list
[email protected]
http://lists.alioth.debian.org/cgi-bin/mailman/listinfo/nut-upsdev

Reply via email to