# Mark Huang <mlhuang@cs.princeton.edu>
# Copyright (C) 2006 The Trustees of Princeton University
#
-# $Id: GPG.py,v 1.4 2007/01/10 20:18:52 mlhuang Exp $
-#
import os
-import xmlrpclib
+import xmlrpc.client
import shutil
-from types import StringTypes
-from StringIO import StringIO
-from xml.dom import minidom
-from xml.dom.ext import Canonicalize
+from io import StringIO
from subprocess import Popen, PIPE, call
from tempfile import NamedTemporaryFile, mkdtemp
+from lxml import etree
from PLC.Faults import *
True).
"""
- xml = xmlrpclib.dumps(args, methodname, methodresponse, encoding = 'utf-8', allow_none = 1)
- dom = minidom.parseString(xml)
-
+ xml = xmlrpc.client.dumps(args, methodname, methodresponse, encoding = 'utf-8', allow_none = 1)
+ dom = etree.fromstring(xml)
+ canonical = etree.tostring(dom)
+ # pre-f20 version was using Canonicalize from PyXML
+ # from xml.dom.ext import Canonicalize
# Canonicalize(), though it claims to, does not encode unicode
# nodes to UTF-8 properly and throws an exception unless you write
# the stream to a file object, so just encode it ourselves.
- buf = StringIO()
- Canonicalize(dom, output = buf)
- xml = buf.getvalue().encode('utf-8')
-
- return xml
+ return canonical.encode('utf-8')
def gpg_export(keyring, armor = True):
"""
if armor:
args.append("--armor")
- p = Popen(args, stdin = PIPE, stdout = PIPE, stderr = PIPE)
+ p = Popen(args, stdin = PIPE, stdout = PIPE, stderr = PIPE, close_fds = True)
export = p.stdout.read()
err = p.stderr.read()
rc = p.wait()
shutil.rmtree(homedir)
if rc:
- raise PLCAuthenticationFailure, "GPG export failed with return code %d: %s" % (rc, err)
+ raise PLCAuthenticationFailure("GPG export failed with return code %d: %s" % (rc, err))
return export
"""
# Accept either an opaque string blob or a Python tuple
- if isinstance(args, StringTypes):
+ if isinstance(args, str):
message = args
elif isinstance(args, tuple):
message = canonicalize(args, methodname, methodresponse)
shutil.rmtree(homedir)
if rc:
- raise PLCAuthenticationFailure, "GPG signing failed with return code %d: %s" % (rc, err)
+ raise PLCAuthenticationFailure("GPG signing failed with return code %d: %s" % (rc, err))
return signature
"""
# Accept either an opaque string blob or a Python tuple
- if isinstance(args, StringTypes):
+ if isinstance(args, str):
message = args
else:
message = canonicalize(args, methodname, methodresponse)
keyfile.close()
if rc:
- raise PLCAuthenticationFailure, "GPG verification failed with return code %d: %s" % (rc, err)
+ raise PLCAuthenticationFailure("GPG verification failed with return code %d: %s" % (rc, err))
return message