Richard W.M. Jones
2018-Mar-12 12:56 UTC
[Libguestfs] [PATCH RHEL 7] RHEL 7: -o rhv-upload: Use Python 2 instead of Python
For interest only, here is the patch required to make -o rhv-upload work with Python 2 (for RHEL 7). I don't think we want this upstream. A couple of remarks: * It's supposed to be possible to add ‘coding: utf-8’ to the top of .py files to make Python 2 accept that the file is UTF-8 (otherwise it gives an error on loading). However I added this and it didn't appear to make any difference, I still had to replace the Unicode quotes with ASCII ones, so I don't know what's going on there. * The ‘byteify’ function is a bit of a hack, but required here because the Python 2 json library returns unicode objects where all the rest of the libraries are expecting str. Not needed in Python 3 of course. Rich.
Richard W.M. Jones
2018-Mar-12 12:56 UTC
[Libguestfs] [PATCH RHEL 7] RHEL 7: -o rhv-upload: Use Python 2 instead of Python 3.
From: "Richard W.M. Jones" <rjones@redhat.com> --- v2v/output_rhv_upload.ml | 17 ++++++++--------- v2v/rhv-upload-createvm.py | 20 ++++++++++++++++---- v2v/rhv-upload-plugin.py | 28 ++++++++++++++++++++-------- v2v/rhv-upload-precheck.py | 22 +++++++++++++++++----- 4 files changed, 61 insertions(+), 26 deletions(-) diff --git a/v2v/output_rhv_upload.ml b/v2v/output_rhv_upload.ml index 7530c0b..cf730bd 100644 --- a/v2v/output_rhv_upload.ml +++ b/v2v/output_rhv_upload.ml @@ -26,7 +26,6 @@ open Common_gettext.Gettext open Types open Utils -let python3 = "python3" (* Defined by PEP 394 *) let pidfile_timeout = 30 let finalization_timeout = 5*60 @@ -83,14 +82,14 @@ class output_rhv_upload output_alloc output_conn error (f_"nbdkit is not new enough, you need to upgrade to nbdkit ≥ 1.1.16") in - (* Check that the python3 plugin is installed and working + (* Check that the python plugin is installed and working * and can load the plugin script. *) - let error_unless_nbdkit_python3_working () + let error_unless_nbdkit_python_working () let cmd = sprintf "nbdkit %s %s --dump-plugin >/dev/null" - python3 (quote plugin) in + "python" (quote plugin) in if Sys.command cmd <> 0 then - error (f_"nbdkit Python 3 plugin is not installed or not working. It is required if you want to use ‘-o rhv-upload’. + error (f_"nbdkit Python plugin is not installed or not working. It is required if you want to use ‘-o rhv-upload’. See also \"OUTPUT TO RHV\" in the virt-v2v(1) manual.") in @@ -138,7 +137,7 @@ See also \"OUTPUT TO RHV\" in the virt-v2v(1) manual.") "--newstyle"; (* use newstyle NBD protocol *) "--exportname"; "/"; - "python3"; (* use the nbdkit Python 3 plugin *) + "python"; (* use the nbdkit Python plugin *) plugin; (* Python plugin script *) ] in let args = if verbose () then args @ ["--verbose"] else args in @@ -154,7 +153,7 @@ object method precheck () error_unless_nbdkit_working (); - error_unless_nbdkit_python3_working (); + error_unless_nbdkit_python_working (); if have_selinux then error_unless_nbdkit_compiled_with_selinux () @@ -180,7 +179,7 @@ object with_open_out json_param_file (fun chan -> output_string chan (JSON.string_of_doc json_params)); - if run_command [ python3; precheck; json_param_file ] <> 0 then + if run_command [ "python"; precheck; json_param_file ] <> 0 then error (f_"failed server prechecks, see earlier errors"); (* Create an nbdkit instance for each disk and set the @@ -327,7 +326,7 @@ If the messages above are not sufficient to diagnose the problem then add the let ovf_file = tmpdir // "vm.ovf" in with_open_out ovf_file (fun chan -> output_string chan ovf); - if run_command [ python3; createvm; json_param_file; ovf_file ] <> 0 then + if run_command [ "python"; createvm; json_param_file; ovf_file ] <> 0 then error (f_"failed to create virtual machine, see earlier errors") end diff --git a/v2v/rhv-upload-createvm.py b/v2v/rhv-upload-createvm.py index 612e469..bb23544 100644 --- a/v2v/rhv-upload-createvm.py +++ b/v2v/rhv-upload-createvm.py @@ -1,5 +1,6 @@ # -*- python -*- -# oVirt or RHV upload create VM used by ‘virt-v2v -o rhv-upload’ +# coding: utf-8 +# oVirt or RHV upload create VM used by 'virt-v2v -o rhv-upload' # Copyright (C) 2018 Red Hat Inc. # # This program is free software; you can redistribute it and/or modify @@ -23,8 +24,8 @@ import ovirtsdk4.types as types import sys import time -from http.client import HTTPSConnection -from urllib.parse import urlparse +from httplib import HTTPSConnection +from urlparse import urlparse # Parameters are passed in via a JSON doc from the OCaml code. # Because this Python code ships embedded inside virt-v2v there @@ -36,8 +37,19 @@ if len(sys.argv) != 3: raise RuntimeError("incorrect number of parameters") # Parameters are passed in via a JSON document. +# https://stackoverflow.com/a/13105359 +def byteify(input): + if isinstance(input, dict): + return {byteify(key): byteify(value) + for key, value in input.iteritems()} + elif isinstance(input, list): + return [byteify(element) for element in input] + elif isinstance(input, unicode): + return input.encode('utf-8') + else: + return input with open(sys.argv[1], 'r') as fp: - params = json.load(fp) + params = byteify(json.load(fp)) # What is passed in is a password file, read the actual password. with open(params['output_password'], 'r') as fp: diff --git a/v2v/rhv-upload-plugin.py b/v2v/rhv-upload-plugin.py index 5568339..91699bf 100644 --- a/v2v/rhv-upload-plugin.py +++ b/v2v/rhv-upload-plugin.py @@ -1,5 +1,6 @@ # -*- python -*- -# oVirt or RHV upload nbdkit plugin used by ‘virt-v2v -o rhv-upload’ +# coding: utf-8 +# oVirt or RHV upload nbdkit plugin used by 'virt-v2v -o rhv-upload' # Copyright (C) 2018 Red Hat Inc. # # This program is free software; you can redistribute it and/or modify @@ -16,7 +17,7 @@ # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -import builtins +from __builtin__ import open as builtin_open import json import logging import ovirtsdk4 as sdk @@ -25,8 +26,8 @@ import ssl import sys import time -from http.client import HTTPSConnection -from urllib.parse import urlparse +from httplib import HTTPSConnection +from urlparse import urlparse # Timeout to wait for oVirt disks to change status, or the transfer # object to finish initializing [seconds]. @@ -35,14 +36,25 @@ timeout = 5*60 # Parameters are passed in via a JSON doc from the OCaml code. # Because this Python code ships embedded inside virt-v2v there # is no formal API here. +# https://stackoverflow.com/a/13105359 +def byteify(input): + if isinstance(input, dict): + return {byteify(key): byteify(value) + for key, value in input.iteritems()} + elif isinstance(input, list): + return [byteify(element) for element in input] + elif isinstance(input, unicode): + return input.encode('utf-8') + else: + return input params = None def config(key, value): global params if key == "params": - with builtins.open(value, 'r') as fp: - params = json.load(fp) + with builtin_open(value, 'r') as fp: + params = byteify(json.load(fp)) else: raise RuntimeError("unknown configuration key '%s'" % key) @@ -56,7 +68,7 @@ def open(readonly): username = parsed.username or "admin@internal" # Read the password from file. - with builtins.open(params['output_password'], 'r') as fp: + with builtin_open(params['output_password'], 'r') as fp: password = fp.read() password = password.rstrip() @@ -242,7 +254,7 @@ def close(h): transfer_service.finalize() # Write the disk ID file. Only do this on successful completion. - with builtins.open(params['diskid_file'], 'w') as fp: + with builtin_open(params['diskid_file'], 'w') as fp: fp.write(disk.id) # Otherwise if we did fail then we should delete the disk. diff --git a/v2v/rhv-upload-precheck.py b/v2v/rhv-upload-precheck.py index e3fa651..0c02d2f 100644 --- a/v2v/rhv-upload-precheck.py +++ b/v2v/rhv-upload-precheck.py @@ -1,5 +1,6 @@ # -*- python -*- -# oVirt or RHV pre-upload checks used by ‘virt-v2v -o rhv-upload’ +# coding: utf-8 +# oVirt or RHV pre-upload checks used by 'virt-v2v -o rhv-upload' # Copyright (C) 2018 Red Hat Inc. # # This program is free software; you can redistribute it and/or modify @@ -23,8 +24,8 @@ import ovirtsdk4.types as types import sys import time -from http.client import HTTPSConnection -from urllib.parse import urlparse +from httplib import HTTPSConnection +from urlparse import urlparse # Parameters are passed in via a JSON doc from the OCaml code. # Because this Python code ships embedded inside virt-v2v there @@ -35,8 +36,19 @@ if len(sys.argv) != 2: raise RuntimeError("incorrect number of parameters") # Parameters are passed in via a JSON document. +# https://stackoverflow.com/a/13105359 +def byteify(input): + if isinstance(input, dict): + return {byteify(key): byteify(value) + for key, value in input.iteritems()} + elif isinstance(input, list): + return [byteify(element) for element in input] + elif isinstance(input, unicode): + return input.encode('utf-8') + else: + return input with open(sys.argv[1], 'r') as fp: - params = json.load(fp) + params = byteify(json.load(fp)) # What is passed in is a password file, read the actual password. with open(params['output_password'], 'r') as fp: @@ -66,7 +78,7 @@ vms = vms_service.list( ) if len(vms) > 0: vm = vms[0] - raise RuntimeError("VM already exists with name ‘%s’, id ‘%s’" % + raise RuntimeError("VM already exists with name '%s', id '%s'" % (params['output_name'], vm.id)) # Otherwise everything is OK, exit with no error. -- 1.8.3.1
Maybe Matching Threads
- [PATCH v5 0/4] v2v: Add -o rhv-upload output mode.
- [PATCH v6] v2v: Add -o rhv-upload output mode.
- [PATCH v8] v2v: Add -o rhv-upload output mode (RHBZ#1557273).
- [PATCH v9] v2v: Add -o rhv-upload output mode (RHBZ#1557273).
- Re: [PATCH v5 4/4] v2v: Add -o rhv-upload output mode.