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

e9bfca
From 587edb5852a7fafffa468f952ef13efd5958bcb8 Mon Sep 17 00:00:00 2001
e9bfca
From: "Richard W.M. Jones" <rjones@redhat.com>
e9bfca
Date: Fri, 9 Mar 2018 12:15:44 +0000
e9bfca
Subject: [PATCH] RHEL 7: -o rhv-upload: Use Python 2 instead of Python 3.
e9bfca
e9bfca
---
e9bfca
 v2v/output_rhv_upload.ml      | 23 +++++++++++------------
e9bfca
 v2v/rhv-upload-createvm.py    | 20 ++++++++++++++++----
e9bfca
 v2v/rhv-upload-plugin.py      | 35 ++++++++++++++++++++++++-----------
e9bfca
 v2v/rhv-upload-precheck.py    | 22 +++++++++++++++++-----
e9bfca
 v2v/test-v2v-python-syntax.sh |  2 +-
e9bfca
 5 files changed, 69 insertions(+), 33 deletions(-)
e9bfca
e9bfca
diff --git a/v2v/output_rhv_upload.ml b/v2v/output_rhv_upload.ml
e9bfca
index 32c2f8f64..9f87244ae 100644
e9bfca
--- a/v2v/output_rhv_upload.ml
e9bfca
+++ b/v2v/output_rhv_upload.ml
e9bfca
@@ -78,7 +78,6 @@ let parse_output_options options =
e9bfca
 
e9bfca
   { rhv_cafile; rhv_cluster; rhv_direct; rhv_verifypeer }
e9bfca
 
e9bfca
-let python3 = "python3" (* Defined by PEP 394 *)
e9bfca
 let pidfile_timeout = 30
e9bfca
 let finalization_timeout = 5*60
e9bfca
 
e9bfca
@@ -120,15 +119,15 @@ class output_rhv_upload output_alloc output_conn
e9bfca
 
e9bfca
   (* Check that the Python binary is available. *)
e9bfca
   let error_unless_python_binary_on_path () =
e9bfca
-    try ignore (which python3)
e9bfca
+    try ignore (which "python")
e9bfca
     with Executable_not_found _ ->
e9bfca
       error (f_"no python binary called ‘%s’ can be found on the $PATH")
e9bfca
-            python3
e9bfca
+            "python"
e9bfca
   in
e9bfca
 
e9bfca
   (* Check that the 'ovirtsdk4' Python module is available. *)
e9bfca
   let error_unless_ovirtsdk4_module_available () =
e9bfca
-    let res = run_command [ python3; "-c"; "import ovirtsdk4" ] in
e9bfca
+    let res = run_command [ "python"; "-c"; "import ovirtsdk4" ] in
e9bfca
     if res <> 0 then
e9bfca
       error (f_"the Python module ‘ovirtsdk4’ could not be loaded, is it installed?  See previous messages for problems.")
e9bfca
   in
e9bfca
@@ -150,14 +149,14 @@ class output_rhv_upload output_alloc output_conn
e9bfca
       error (f_"nbdkit is not new enough, you need to upgrade to nbdkit ≥ 1.1.16")
e9bfca
   in
e9bfca
 
e9bfca
-  (* Check that the python3 plugin is installed and working
e9bfca
+  (* Check that the python plugin is installed and working
e9bfca
    * and can load the plugin script.
e9bfca
    *)
e9bfca
-  let error_unless_nbdkit_python3_working () =
e9bfca
+  let error_unless_nbdkit_python_working () =
e9bfca
     let cmd = sprintf "nbdkit %s %s --dump-plugin >/dev/null"
e9bfca
-                      python3 (quote plugin) in
e9bfca
+                      "python" (quote plugin) in
e9bfca
     if Sys.command cmd <> 0 then
e9bfca
-      error (f_"nbdkit Python 3 plugin is not installed or not working.  It is required if you want to use ‘-o rhv-upload’.
e9bfca
+      error (f_"nbdkit Python plugin is not installed or not working.  It is required if you want to use ‘-o rhv-upload’.
e9bfca
 
e9bfca
 See also \"OUTPUT TO RHV\" in the virt-v2v(1) manual.")
e9bfca
   in
e9bfca
@@ -214,7 +213,7 @@ See also \"OUTPUT TO RHV\" in the virt-v2v(1) manual.")
e9bfca
       "--newstyle";             (* use newstyle NBD protocol *)
e9bfca
       "--exportname"; "/";
e9bfca
 
e9bfca
-      "python3";                (* use the nbdkit Python 3 plugin *)
e9bfca
+      "python";                 (* use the nbdkit Python plugin *)
e9bfca
       plugin;                   (* Python plugin script *)
e9bfca
     ] in
e9bfca
     let args = if verbose () then args @ ["--verbose"] else args in
e9bfca
@@ -232,7 +231,7 @@ object
e9bfca
     error_unless_python_binary_on_path ();
e9bfca
     error_unless_ovirtsdk4_module_available ();
e9bfca
     error_unless_nbdkit_working ();
e9bfca
-    error_unless_nbdkit_python3_working ();
e9bfca
+    error_unless_nbdkit_python_working ();
e9bfca
     if have_selinux then
e9bfca
       error_unless_nbdkit_compiled_with_selinux ()
e9bfca
 
e9bfca
@@ -261,7 +260,7 @@ object
e9bfca
     with_open_out
e9bfca
       json_param_file
e9bfca
       (fun chan -> output_string chan (JSON.string_of_doc json_params));
e9bfca
-    if run_command [ python3; precheck; json_param_file ] <> 0 then
e9bfca
+    if run_command [ "python"; precheck; json_param_file ] <> 0 then
e9bfca
       error (f_"failed server prechecks, see earlier errors");
e9bfca
 
e9bfca
     (* Create an nbdkit instance for each disk and set the
e9bfca
@@ -410,7 +409,7 @@ If the messages above are not sufficient to diagnose the problem then add the 
e9bfca
 
e9bfca
     let ovf_file = tmpdir // "vm.ovf" in
e9bfca
     with_open_out ovf_file (fun chan -> output_string chan ovf);
e9bfca
-    if run_command [ python3; createvm; json_param_file; ovf_file ] <> 0 then
e9bfca
+    if run_command [ "python"; createvm; json_param_file; ovf_file ] <> 0 then
e9bfca
       error (f_"failed to create virtual machine, see earlier errors")
e9bfca
 
e9bfca
 end
e9bfca
diff --git a/v2v/rhv-upload-createvm.py b/v2v/rhv-upload-createvm.py
e9bfca
index 1d0e8c95d..3f2fae4c6 100644
e9bfca
--- a/v2v/rhv-upload-createvm.py
e9bfca
+++ b/v2v/rhv-upload-createvm.py
e9bfca
@@ -1,5 +1,6 @@
e9bfca
 # -*- python -*-
e9bfca
-# oVirt or RHV upload create VM used by ‘virt-v2v -o rhv-upload’
e9bfca
+# coding: utf-8
e9bfca
+# oVirt or RHV upload create VM used by 'virt-v2v -o rhv-upload'
e9bfca
 # Copyright (C) 2018 Red Hat Inc.
e9bfca
 #
e9bfca
 # This program is free software; you can redistribute it and/or modify
e9bfca
@@ -21,8 +22,8 @@ import logging
e9bfca
 import sys
e9bfca
 import time
e9bfca
 
e9bfca
-from http.client import HTTPSConnection
e9bfca
-from urllib.parse import urlparse
e9bfca
+from httplib import HTTPSConnection
e9bfca
+from urlparse import urlparse
e9bfca
 
e9bfca
 import ovirtsdk4 as sdk
e9bfca
 import ovirtsdk4.types as types
e9bfca
@@ -37,8 +38,19 @@ if len(sys.argv) != 3:
e9bfca
     raise RuntimeError("incorrect number of parameters")
e9bfca
 
e9bfca
 # Parameters are passed in via a JSON document.
e9bfca
+# https://stackoverflow.com/a/13105359
e9bfca
+def byteify(input):
e9bfca
+    if isinstance(input, dict):
e9bfca
+        return {byteify(key): byteify(value)
e9bfca
+                for key, value in input.iteritems()}
e9bfca
+    elif isinstance(input, list):
e9bfca
+        return [byteify(element) for element in input]
e9bfca
+    elif isinstance(input, unicode):
e9bfca
+        return input.encode('utf-8')
e9bfca
+    else:
e9bfca
+        return input
e9bfca
 with open(sys.argv[1], 'r') as fp:
e9bfca
-    params = json.load(fp)
e9bfca
+    params = byteify(json.load(fp))
e9bfca
 
e9bfca
 # What is passed in is a password file, read the actual password.
e9bfca
 with open(params['output_password'], 'r') as fp:
e9bfca
diff --git a/v2v/rhv-upload-plugin.py b/v2v/rhv-upload-plugin.py
e9bfca
index 41cb29992..3d4ed1270 100644
e9bfca
--- a/v2v/rhv-upload-plugin.py
e9bfca
+++ b/v2v/rhv-upload-plugin.py
e9bfca
@@ -1,5 +1,6 @@
e9bfca
 # -*- python -*-
e9bfca
-# oVirt or RHV upload nbdkit plugin used by ‘virt-v2v -o rhv-upload’
e9bfca
+# coding: utf-8
e9bfca
+# oVirt or RHV upload nbdkit plugin used by 'virt-v2v -o rhv-upload'
e9bfca
 # Copyright (C) 2018 Red Hat Inc.
e9bfca
 #
e9bfca
 # This program is free software; you can redistribute it and/or modify
e9bfca
@@ -16,7 +17,7 @@
e9bfca
 # with this program; if not, write to the Free Software Foundation, Inc.,
e9bfca
 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
e9bfca
 
e9bfca
-import builtins
e9bfca
+from __builtin__ import open as builtin_open
e9bfca
 import json
e9bfca
 import logging
e9bfca
 import socket
e9bfca
@@ -24,8 +25,8 @@ import ssl
e9bfca
 import sys
e9bfca
 import time
e9bfca
 
e9bfca
-from http.client import HTTPSConnection, HTTPConnection
e9bfca
-from urllib.parse import urlparse
e9bfca
+from httplib import HTTPSConnection, HTTPConnection
e9bfca
+from urlparse import urlparse
e9bfca
 
e9bfca
 import ovirtsdk4 as sdk
e9bfca
 import ovirtsdk4.types as types
e9bfca
@@ -37,14 +38,25 @@ timeout = 5*60
e9bfca
 # Parameters are passed in via a JSON doc from the OCaml code.
e9bfca
 # Because this Python code ships embedded inside virt-v2v there
e9bfca
 # is no formal API here.
e9bfca
+# https://stackoverflow.com/a/13105359
e9bfca
+def byteify(input):
e9bfca
+    if isinstance(input, dict):
e9bfca
+        return {byteify(key): byteify(value)
e9bfca
+                for key, value in input.iteritems()}
e9bfca
+    elif isinstance(input, list):
e9bfca
+        return [byteify(element) for element in input]
e9bfca
+    elif isinstance(input, unicode):
e9bfca
+        return input.encode('utf-8')
e9bfca
+    else:
e9bfca
+        return input
e9bfca
 params = None
e9bfca
 
e9bfca
 def config(key, value):
e9bfca
     global params
e9bfca
 
e9bfca
     if key == "params":
e9bfca
-        with builtins.open(value, 'r') as fp:
e9bfca
-            params = json.load(fp)
e9bfca
+        with builtin_open(value, 'r') as fp:
e9bfca
+            params = byteify(json.load(fp))
e9bfca
     else:
e9bfca
         raise RuntimeError("unknown configuration key '%s'" % key)
e9bfca
 
e9bfca
@@ -54,13 +66,14 @@ def config_complete():
e9bfca
 
e9bfca
 def debug(s):
e9bfca
     if params['verbose']:
e9bfca
-        print(s, file=sys.stderr)
e9bfca
+        sys.stderr.write(s)
e9bfca
+        sys.stderr.write("\n")
e9bfca
         sys.stderr.flush()
e9bfca
 
e9bfca
 def find_host(connection):
e9bfca
     """Return the current host object or None."""
e9bfca
     try:
e9bfca
-        with builtins.open("/etc/vdsm/vdsm.id") as f:
e9bfca
+        with builtin_open("/etc/vdsm/vdsm.id") as f:
e9bfca
             vdsm_id = f.readline().strip()
e9bfca
     except Exception as e:
e9bfca
         # This is most likely not an oVirt host.
e9bfca
@@ -110,7 +123,7 @@ def open(readonly):
e9bfca
     username = parsed.username or "admin@internal"
e9bfca
 
e9bfca
     # Read the password from file.
e9bfca
-    with builtins.open(params['output_password'], 'r') as fp:
e9bfca
+    with builtin_open(params['output_password'], 'r') as fp:
e9bfca
         password = fp.read()
e9bfca
     password = password.rstrip()
e9bfca
 
e9bfca
@@ -232,7 +245,7 @@ def open(readonly):
e9bfca
         # New imageio never needs authentication.
e9bfca
         needs_auth = False
e9bfca
 
e9bfca
-        j = json.loads(data)
e9bfca
+        j = byteify(json.loads(data))
e9bfca
         can_flush = "flush" in j['features']
e9bfca
         can_trim = "trim" in j['features']
e9bfca
         can_zero = "zero" in j['features']
e9bfca
@@ -514,7 +527,7 @@ def close(h):
e9bfca
             pass
e9bfca
 
e9bfca
         # Write the disk ID file.  Only do this on successful completion.
e9bfca
-        with builtins.open(params['diskid_file'], 'w') as fp:
e9bfca
+        with builtin_open(params['diskid_file'], 'w') as fp:
e9bfca
             fp.write(disk.id)
e9bfca
 
e9bfca
     except:
e9bfca
diff --git a/v2v/rhv-upload-precheck.py b/v2v/rhv-upload-precheck.py
e9bfca
index 2798a29dd..5b650899f 100644
e9bfca
--- a/v2v/rhv-upload-precheck.py
e9bfca
+++ b/v2v/rhv-upload-precheck.py
e9bfca
@@ -1,5 +1,6 @@
e9bfca
 # -*- python -*-
e9bfca
-# oVirt or RHV pre-upload checks used by ‘virt-v2v -o rhv-upload’
e9bfca
+# coding: utf-8
e9bfca
+# oVirt or RHV pre-upload checks used by 'virt-v2v -o rhv-upload'
e9bfca
 # Copyright (C) 2018 Red Hat Inc.
e9bfca
 #
e9bfca
 # This program is free software; you can redistribute it and/or modify
e9bfca
@@ -21,8 +22,8 @@ import logging
e9bfca
 import sys
e9bfca
 import time
e9bfca
 
e9bfca
-from http.client import HTTPSConnection
e9bfca
-from urllib.parse import urlparse
e9bfca
+from httplib import HTTPSConnection
e9bfca
+from urlparse import urlparse
e9bfca
 
e9bfca
 import ovirtsdk4 as sdk
e9bfca
 import ovirtsdk4.types as types
e9bfca
@@ -36,8 +37,19 @@ if len(sys.argv) != 2:
e9bfca
     raise RuntimeError("incorrect number of parameters")
e9bfca
 
e9bfca
 # Parameters are passed in via a JSON document.
e9bfca
+# https://stackoverflow.com/a/13105359
e9bfca
+def byteify(input):
e9bfca
+    if isinstance(input, dict):
e9bfca
+        return {byteify(key): byteify(value)
e9bfca
+                for key, value in input.iteritems()}
e9bfca
+    elif isinstance(input, list):
e9bfca
+        return [byteify(element) for element in input]
e9bfca
+    elif isinstance(input, unicode):
e9bfca
+        return input.encode('utf-8')
e9bfca
+    else:
e9bfca
+        return input
e9bfca
 with open(sys.argv[1], 'r') as fp:
e9bfca
-    params = json.load(fp)
e9bfca
+    params = byteify(json.load(fp))
e9bfca
 
e9bfca
 # What is passed in is a password file, read the actual password.
e9bfca
 with open(params['output_password'], 'r') as fp:
e9bfca
@@ -67,7 +79,7 @@ vms = vms_service.list(
e9bfca
 )
e9bfca
 if len(vms) > 0:
e9bfca
     vm = vms[0]
e9bfca
-    raise RuntimeError("VM already exists with name ‘%s’, id ‘%s’" %
e9bfca
+    raise RuntimeError("VM already exists with name '%s', id '%s'" %
e9bfca
                        (params['output_name'], vm.id))
e9bfca
 
e9bfca
 # Otherwise everything is OK, exit with no error.
e9bfca
diff --git a/v2v/test-v2v-python-syntax.sh b/v2v/test-v2v-python-syntax.sh
e9bfca
index b167f4610..8d5924e3c 100755
e9bfca
--- a/v2v/test-v2v-python-syntax.sh
e9bfca
+++ b/v2v/test-v2v-python-syntax.sh
e9bfca
@@ -25,7 +25,7 @@ skip_if_skipped
e9bfca
 files="rhv-upload-createvm.py rhv-upload-plugin.py rhv-upload-precheck.py"
e9bfca
 
e9bfca
 # Base version of Python.
e9bfca
-python=python3
e9bfca
+python=python
e9bfca
 
e9bfca
 # Checks the files are syntactically correct, but not very much else.
e9bfca
 for f in $files; do
e9bfca
-- 
8ff76f
2.20.1
e9bfca