]> www.infradead.org Git - users/dwmw2/openconnect.git/commitdiff
Fix html.py to work with Python 3.x
authorDavid Woodhouse <dwmw2@infradead.org>
Fri, 1 Feb 2019 11:09:21 +0000 (11:09 +0000)
committerDavid Woodhouse <dwmw2@infradead.org>
Fri, 1 Feb 2019 11:09:23 +0000 (11:09 +0000)
Fix up running in non-UTF-8 environments too.

Signed-off-by: David Woodhouse <dwmw2@infradead.org>
configure.ac
www/html.py

index 136770be0eb3c7cd7c7e87dfbdf5356721906c88..dc78bbc5639c8150fccc2eceff9477a1966f7ae7 100644 (file)
@@ -936,18 +936,8 @@ AC_CHECK_HEADER([endian.h],
             [AC_DEFINE([ENDIAN_HDR], [<sys/isa_defs.h>])])])])
 
 build_www=yes
-AC_PATH_PROGS(PYTHON, [python2 python], [], $PATH:/bin:/usr/bin)
-if (test -n "${ac_cv_path_PYTHON}"); then
-   AC_MSG_CHECKING([that python is version 2.x])
-   if $PYTHON --version 2>&1 | grep "Python 2\." > /dev/null; then
-      AC_MSG_RESULT([yes])
-      AC_SUBST(PYTHON, ${ac_cv_path_PYTHON})
-   else
-      AC_MSG_RESULT([no])
-      AC_MSG_NOTICE([Python is not v2.x; not building HTML pages])
-      build_www=no
-   fi
-else
+AC_PATH_PROGS(PYTHON, [python3 python2 python], [], $PATH:/bin:/usr/bin)
+if test -z "${ac_cv_path_PYTHON}"; then
    AC_MSG_NOTICE([Python not found; not building HTML pages])
    build_www=no
 fi
index c23b3a77d97cbca2425e25ccb4417f968a315428..1710ea4311dbc8c7e81fd7a4410eb0fd40fdb31c 100755 (executable)
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
 #
 # Simple XML to HTML converter.
 # 
@@ -19,21 +19,24 @@ import smtplib
 import socket
 import time
 import xml.sax
-import commands
 import codecs
 
-reload(sys)
-sys.setdefaultencoding('utf-8')
+if sys.version_info >= (3,0):
+        sys.stdout = codecs.getwriter("utf-8")(sys.stdout.detach())
+else:
+        reload(sys)
+        sys.setdefaultencoding('utf-8')
+        sys.stdout = codecs.getwriter("utf-8")(sys.stdout)
 
 lookupdir = ''
 
 # Print the usage information
 def usage():
-       print "USAGE:"
-       print "html.py <-f -h file.xml>"
-       print "  -d DIR    use DIR as base directory for opening files"
-       print "  -f        write output to file.html (default is stdout)"
-       print "  -h        help"
+       print ("USAGE:")
+       print ("html.py <-f -h file.xml>")
+       print ("  -d DIR    use DIR as base directory for opening files")
+       print ("  -f        write output to file.html (default is stdout)")
+       print ("  -h        help")
        return
 
 
@@ -98,9 +101,9 @@ class docHandler(xml.sax.ContentHandler):
                        return
                elif name == "INCLUDE":
                        try:
-                               fd = open(attrs.get('file'), 'r')
+                               fd = codecs.open(attrs.get('file'), 'r', 'utf-8')
                        except:
-                               fd = open(lookupdir + attrs.get('file'), 'r')
+                               fd = codecs.open(lookupdir + attrs.get('file'), 'r', 'utf-8')
                        lines = fd.readlines()
                        fd.close()
                        for line in lines:
@@ -127,7 +130,7 @@ class docHandler(xml.sax.ContentHandler):
                
                elif name == "br":
                        writeHtml("<br")
-                       if attrs.getLength > 0:
+                       if attrs.getLength() > 0:
                                names = attrs.getNames()
                                for name in names:
                                        writeHtml(" " + name + "=\"" + attrs.get(name) + "\"")
@@ -135,7 +138,7 @@ class docHandler(xml.sax.ContentHandler):
                        
                else:
                        writeHtml("<" + name)
-                       if attrs.getLength > 0:
+                       if attrs.getLength() > 0:
                                names = attrs.getNames()
                                for name in names:
                                        writeHtml(" " + name + "=\"" + attrs.get(name) + "\"")
@@ -198,9 +201,9 @@ def parseConfig(file):
        parser.setErrorHandler(eh)
 
        try:
-               fd = open(file, 'r')
+               fd = codecs.open(file, 'r', 'utf-8')
        except:
-               fd = open(lookupdir + file, 'r')
+               fd = codecs.open(lookupdir + file, 'r', 'utf-8')
 
        # Parse the file
        parser.parse(fd)
@@ -214,10 +217,10 @@ writefile = 0
 
 try:
        (options, arguments) = getopt.getopt(sys.argv[1:],'fhd:')
-except getopt.GetoptError, ex:
+except getopt.GetoptError as ex:
        print
-       print "ERROR:"
-       print ex.msg
+       print ("ERROR:")
+       print (ex.msg)
        usage()
        sys.exit(1)
        pass
@@ -240,7 +243,7 @@ idx = len(replace)
 replace[idx:] = [lookupdir]
 
 if not arguments:
-       print "No source file specified"
+       print ("No source file specified")
        usage()
        sys.exit(1)
        pass