Blame SOURCES/0034-RHEL-7-o-rhv-upload-Use-Python-2-instead-of-Python-3.patch

4ebc84
From b0a920f38e0ba2bb4474d4a085d05a8588b1b207 Mon Sep 17 00:00:00 2001
4ebc84
From: "Richard W.M. Jones" <rjones@redhat.com>
4ebc84
Date: Mon, 22 Jul 2019 13:32:54 +0200
4ebc84
Subject: [PATCH] RHEL 7: -o rhv-upload: Use Python 2 instead of Python 3.
4ebc84
4ebc84
[Pino: few additional bits edited by me]
4ebc84
---
4ebc84
 v2v/output_rhv_upload.ml      |  2 +-
4ebc84
 v2v/python_script.ml          |  2 +-
4ebc84
 v2v/rhv-upload-createvm.py    | 20 ++++++++++++---
4ebc84
 v2v/rhv-upload-plugin.py      | 46 +++++++++++++++++++++++------------
4ebc84
 v2v/rhv-upload-precheck.py    | 22 +++++++++++++----
4ebc84
 v2v/test-v2v-python-syntax.sh |  2 +-
4ebc84
 6 files changed, 67 insertions(+), 27 deletions(-)
4ebc84
4ebc84
diff --git a/v2v/output_rhv_upload.ml b/v2v/output_rhv_upload.ml
4ebc84
index 3d6d99008..051ce086f 100644
4ebc84
--- a/v2v/output_rhv_upload.ml
4ebc84
+++ b/v2v/output_rhv_upload.ml
4ebc84
@@ -135,7 +135,7 @@ class output_rhv_upload output_alloc output_conn
4ebc84
       error (f_"nbdkit is not new enough, you need to upgrade to nbdkit ≥ 1.1.16")
4ebc84
   in
4ebc84
 
4ebc84
-  (* Check that the python3 plugin is installed and working
4ebc84
+  (* Check that the python plugin is installed and working
4ebc84
    * and can load the plugin script.
4ebc84
    *)
4ebc84
   let error_unless_nbdkit_python_plugin_working () =
4ebc84
diff --git a/v2v/python_script.ml b/v2v/python_script.ml
4ebc84
index 3159373a1..fa052d697 100644
4ebc84
--- a/v2v/python_script.ml
4ebc84
+++ b/v2v/python_script.ml
4ebc84
@@ -24,7 +24,7 @@ open Unix_utils
4ebc84
 
4ebc84
 open Common_gettext.Gettext
4ebc84
 
4ebc84
-let python = "python3"          (* Defined by PEP 394 *)
4ebc84
+let python = "python"
4ebc84
 
4ebc84
 type script = {
4ebc84
   tmpdir : string;              (* Temporary directory. *)
4ebc84
diff --git a/v2v/rhv-upload-createvm.py b/v2v/rhv-upload-createvm.py
4ebc84
index 1d0e8c95d..3f2fae4c6 100644
4ebc84
--- a/v2v/rhv-upload-createvm.py
4ebc84
+++ b/v2v/rhv-upload-createvm.py
4ebc84
@@ -1,5 +1,6 @@
4ebc84
 # -*- python -*-
4ebc84
-# oVirt or RHV upload create VM used by ‘virt-v2v -o rhv-upload’
4ebc84
+# coding: utf-8
4ebc84
+# oVirt or RHV upload create VM used by 'virt-v2v -o rhv-upload'
4ebc84
 # Copyright (C) 2018 Red Hat Inc.
4ebc84
 #
4ebc84
 # This program is free software; you can redistribute it and/or modify
4ebc84
@@ -21,8 +22,8 @@ import logging
4ebc84
 import sys
4ebc84
 import time
4ebc84
 
4ebc84
-from http.client import HTTPSConnection
4ebc84
-from urllib.parse import urlparse
4ebc84
+from httplib import HTTPSConnection
4ebc84
+from urlparse import urlparse
4ebc84
 
4ebc84
 import ovirtsdk4 as sdk
4ebc84
 import ovirtsdk4.types as types
4ebc84
@@ -37,8 +38,19 @@ if len(sys.argv) != 3:
4ebc84
     raise RuntimeError("incorrect number of parameters")
4ebc84
 
4ebc84
 # Parameters are passed in via a JSON document.
4ebc84
+# https://stackoverflow.com/a/13105359
4ebc84
+def byteify(input):
4ebc84
+    if isinstance(input, dict):
4ebc84
+        return {byteify(key): byteify(value)
4ebc84
+                for key, value in input.iteritems()}
4ebc84
+    elif isinstance(input, list):
4ebc84
+        return [byteify(element) for element in input]
4ebc84
+    elif isinstance(input, unicode):
4ebc84
+        return input.encode('utf-8')
4ebc84
+    else:
4ebc84
+        return input
4ebc84
 with open(sys.argv[1], 'r') as fp:
4ebc84
-    params = json.load(fp)
4ebc84
+    params = byteify(json.load(fp))
4ebc84
 
4ebc84
 # What is passed in is a password file, read the actual password.
4ebc84
 with open(params['output_password'], 'r') as fp:
4ebc84
diff --git a/v2v/rhv-upload-plugin.py b/v2v/rhv-upload-plugin.py
4ebc84
index a631d54d6..adace732b 100644
4ebc84
--- a/v2v/rhv-upload-plugin.py
4ebc84
+++ b/v2v/rhv-upload-plugin.py
4ebc84
@@ -1,5 +1,6 @@
4ebc84
 # -*- python -*-
4ebc84
-# oVirt or RHV upload nbdkit plugin used by ‘virt-v2v -o rhv-upload’
4ebc84
+# coding: utf-8
4ebc84
+# oVirt or RHV upload nbdkit plugin used by 'virt-v2v -o rhv-upload'
4ebc84
 # Copyright (C) 2018 Red Hat Inc.
4ebc84
 #
4ebc84
 # This program is free software; you can redistribute it and/or modify
4ebc84
@@ -16,16 +17,17 @@
4ebc84
 # with this program; if not, write to the Free Software Foundation, Inc.,
4ebc84
 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
4ebc84
 
4ebc84
-import builtins
4ebc84
+from __builtin__ import open as builtin_open
4ebc84
 import json
4ebc84
 import logging
4ebc84
 import socket
4ebc84
 import ssl
4ebc84
 import sys
4ebc84
 import time
4ebc84
+import errno
4ebc84
 
4ebc84
-from http.client import HTTPSConnection, HTTPConnection
4ebc84
-from urllib.parse import urlparse
4ebc84
+from httplib import HTTPSConnection, HTTPConnection
4ebc84
+from urlparse import urlparse
4ebc84
 
4ebc84
 import ovirtsdk4 as sdk
4ebc84
 import ovirtsdk4.types as types
4ebc84
@@ -37,14 +39,25 @@ timeout = 5*60
4ebc84
 # Parameters are passed in via a JSON doc from the OCaml code.
4ebc84
 # Because this Python code ships embedded inside virt-v2v there
4ebc84
 # is no formal API here.
4ebc84
+# https://stackoverflow.com/a/13105359
4ebc84
+def byteify(input):
4ebc84
+    if isinstance(input, dict):
4ebc84
+        return {byteify(key): byteify(value)
4ebc84
+                for key, value in input.iteritems()}
4ebc84
+    elif isinstance(input, list):
4ebc84
+        return [byteify(element) for element in input]
4ebc84
+    elif isinstance(input, unicode):
4ebc84
+        return input.encode('utf-8')
4ebc84
+    else:
4ebc84
+        return input
4ebc84
 params = None
4ebc84
 
4ebc84
 def config(key, value):
4ebc84
     global params
4ebc84
 
4ebc84
     if key == "params":
4ebc84
-        with builtins.open(value, 'r') as fp:
4ebc84
-            params = json.load(fp)
4ebc84
+        with builtin_open(value, 'r') as fp:
4ebc84
+            params = byteify(json.load(fp))
4ebc84
     else:
4ebc84
         raise RuntimeError("unknown configuration key '%s'" % key)
4ebc84
 
4ebc84
@@ -54,13 +67,14 @@ def config_complete():
4ebc84
 
4ebc84
 def debug(s):
4ebc84
     if params['verbose']:
4ebc84
-        print(s, file=sys.stderr)
4ebc84
+        sys.stderr.write(s)
4ebc84
+        sys.stderr.write("\n")
4ebc84
         sys.stderr.flush()
4ebc84
 
4ebc84
 def find_host(connection):
4ebc84
     """Return the current host object or None."""
4ebc84
     try:
4ebc84
-        with builtins.open("/etc/vdsm/vdsm.id") as f:
4ebc84
+        with builtin_open("/etc/vdsm/vdsm.id") as f:
4ebc84
             vdsm_id = f.readline().strip()
4ebc84
     except Exception as e:
4ebc84
         # This is most likely not an oVirt host.
4ebc84
@@ -111,7 +125,7 @@ def open(readonly):
4ebc84
     username = parsed.username or "admin@internal"
4ebc84
 
4ebc84
     # Read the password from file.
4ebc84
-    with builtins.open(params['output_password'], 'r') as fp:
4ebc84
+    with builtin_open(params['output_password'], 'r') as fp:
4ebc84
         password = fp.read()
4ebc84
     password = password.rstrip()
4ebc84
 
4ebc84
@@ -244,7 +258,7 @@ def open(readonly):
4ebc84
         # New imageio never needs authentication.
4ebc84
         needs_auth = False
4ebc84
 
4ebc84
-        j = json.loads(data)
4ebc84
+        j = byteify(json.loads(data))
4ebc84
         can_flush = "flush" in j['features']
4ebc84
         can_trim = "trim" in j['features']
4ebc84
         can_zero = "zero" in j['features']
4ebc84
@@ -369,8 +383,9 @@ def pwrite(h, buf, offset):
4ebc84
 
4ebc84
     try:
4ebc84
         http.send(buf)
4ebc84
-    except BrokenPipeError:
4ebc84
-        pass
4ebc84
+    except EnvironmentError as e:
4ebc84
+        if e.errno != errno.EPIPE:
4ebc84
+            raise
4ebc84
 
4ebc84
     r = http.getresponse()
4ebc84
     if r.status != 200:
4ebc84
@@ -432,8 +447,9 @@ def emulate_zero(h, count, offset):
4ebc84
                 http.send(buf)
4ebc84
                 count -= len(buf)
4ebc84
             http.send(memoryview(buf)[:count])
4ebc84
-        except BrokenPipeError:
4ebc84
-            pass
4ebc84
+        except EnvironmentError as e:
4ebc84
+            if e.errno != errno.EPIPE:
4ebc84
+                raise
4ebc84
 
4ebc84
         r = http.getresponse()
4ebc84
         if r.status != 200:
4ebc84
@@ -540,7 +556,7 @@ def close(h):
4ebc84
             raise RuntimeError("transfer failed: disk %s not found" % disk_id)
4ebc84
 
4ebc84
         # Write the disk ID file.  Only do this on successful completion.
4ebc84
-        with builtins.open(params['diskid_file'], 'w') as fp:
4ebc84
+        with builtin_open(params['diskid_file'], 'w') as fp:
4ebc84
             fp.write(disk.id)
4ebc84
 
4ebc84
     except:
4ebc84
diff --git a/v2v/rhv-upload-precheck.py b/v2v/rhv-upload-precheck.py
4ebc84
index 2798a29dd..5b650899f 100644
4ebc84
--- a/v2v/rhv-upload-precheck.py
4ebc84
+++ b/v2v/rhv-upload-precheck.py
4ebc84
@@ -1,5 +1,6 @@
4ebc84
 # -*- python -*-
4ebc84
-# oVirt or RHV pre-upload checks used by ‘virt-v2v -o rhv-upload’
4ebc84
+# coding: utf-8
4ebc84
+# oVirt or RHV pre-upload checks used by 'virt-v2v -o rhv-upload'
4ebc84
 # Copyright (C) 2018 Red Hat Inc.
4ebc84
 #
4ebc84
 # This program is free software; you can redistribute it and/or modify
4ebc84
@@ -21,8 +22,8 @@ import logging
4ebc84
 import sys
4ebc84
 import time
4ebc84
 
4ebc84
-from http.client import HTTPSConnection
4ebc84
-from urllib.parse import urlparse
4ebc84
+from httplib import HTTPSConnection
4ebc84
+from urlparse import urlparse
4ebc84
 
4ebc84
 import ovirtsdk4 as sdk
4ebc84
 import ovirtsdk4.types as types
4ebc84
@@ -36,8 +37,19 @@ if len(sys.argv) != 2:
4ebc84
     raise RuntimeError("incorrect number of parameters")
4ebc84
 
4ebc84
 # Parameters are passed in via a JSON document.
4ebc84
+# https://stackoverflow.com/a/13105359
4ebc84
+def byteify(input):
4ebc84
+    if isinstance(input, dict):
4ebc84
+        return {byteify(key): byteify(value)
4ebc84
+                for key, value in input.iteritems()}
4ebc84
+    elif isinstance(input, list):
4ebc84
+        return [byteify(element) for element in input]
4ebc84
+    elif isinstance(input, unicode):
4ebc84
+        return input.encode('utf-8')
4ebc84
+    else:
4ebc84
+        return input
4ebc84
 with open(sys.argv[1], 'r') as fp:
4ebc84
-    params = json.load(fp)
4ebc84
+    params = byteify(json.load(fp))
4ebc84
 
4ebc84
 # What is passed in is a password file, read the actual password.
4ebc84
 with open(params['output_password'], 'r') as fp:
4ebc84
@@ -67,7 +79,7 @@ vms = vms_service.list(
4ebc84
 )
4ebc84
 if len(vms) > 0:
4ebc84
     vm = vms[0]
4ebc84
-    raise RuntimeError("VM already exists with name ‘%s’, id ‘%s’" %
4ebc84
+    raise RuntimeError("VM already exists with name '%s', id '%s'" %
4ebc84
                        (params['output_name'], vm.id))
4ebc84
 
4ebc84
 # Otherwise everything is OK, exit with no error.
4ebc84
diff --git a/v2v/test-v2v-python-syntax.sh b/v2v/test-v2v-python-syntax.sh
4ebc84
index b167f4610..8d5924e3c 100755
4ebc84
--- a/v2v/test-v2v-python-syntax.sh
4ebc84
+++ b/v2v/test-v2v-python-syntax.sh
4ebc84
@@ -25,7 +25,7 @@ skip_if_skipped
4ebc84
 files="rhv-upload-createvm.py rhv-upload-plugin.py rhv-upload-precheck.py"
4ebc84
 
4ebc84
 # Base version of Python.
4ebc84
-python=python3
4ebc84
+python=python
4ebc84
 
4ebc84
 # Checks the files are syntactically correct, but not very much else.
4ebc84
 for f in $files; do
4ebc84
-- 
4ebc84
2.21.0
4ebc84