From: David Woodhouse Date: Fri, 1 Feb 2019 11:09:21 +0000 (+0000) Subject: Fix html.py to work with Python 3.x X-Git-Tag: v8.03~26 X-Git-Url: https://www.infradead.org/git/?a=commitdiff_plain;h=938fcd7530df91c9c99f2ec83fb297a6d46e15f4;p=users%2Fdwmw2%2Fopenconnect.git Fix html.py to work with Python 3.x Fix up running in non-UTF-8 environments too. Signed-off-by: David Woodhouse --- diff --git a/configure.ac b/configure.ac index 136770be..dc78bbc5 100644 --- a/configure.ac +++ b/configure.ac @@ -936,18 +936,8 @@ AC_CHECK_HEADER([endian.h], [AC_DEFINE([ENDIAN_HDR], [])])])]) 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 diff --git a/www/html.py b/www/html.py index c23b3a77..1710ea43 100755 --- a/www/html.py +++ b/www/html.py @@ -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(" 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