Make os-win Py3 compatible

This patch updates the os-win library, making it Py3 compatible.

Change-Id: I0f8e5b524afc89d4af2aa82d56b22030191507bc
This commit is contained in:
Lucian Petrut 2015-11-13 14:38:48 +02:00
parent ac42cee6e3
commit 3fd7890771
16 changed files with 45 additions and 43 deletions

View File

@ -37,8 +37,8 @@ source_suffix = '.rst'
master_doc = 'index'
# General information about the project.
project = u'os-win'
copyright = u'2015, Cloudbase Solutions Srl'
project = 'os-win'
copyright = '2015, Cloudbase Solutions Srl'
# If true, '()' will be appended to :func: etc. cross-reference text.
add_function_parentheses = True
@ -67,8 +67,8 @@ htmlhelp_basename = '%sdoc' % project
latex_documents = [
('index',
'%s.tex' % project,
u'%s Documentation' % project,
u'OpenStack Foundation', 'manual'),
'%s Documentation' % project,
'OpenStack Foundation', 'manual'),
]
# Example configuration for intersphinx: refer to the Python standard library.

View File

@ -276,7 +276,7 @@ class VMUtilsTestCase(base.BaseTestCase):
def test_get_free_controller_slot_exception(self):
fake_drive = mock.MagicMock()
type(fake_drive).AddressOnParent = mock.PropertyMock(
side_effect=range(constants.SCSI_CONTROLLER_SLOTS_NUMBER))
side_effect=list(range(constants.SCSI_CONTROLLER_SLOTS_NUMBER)))
with mock.patch.object(self._vmutils,
'get_attached_disks') as fake_get_attached_disks:

View File

@ -34,7 +34,7 @@ class IOThreadTestCase(base.BaseTestCase):
self._FAKE_SRC, self._FAKE_DEST, self._FAKE_MAX_BYTES)
super(IOThreadTestCase, self).setUp()
@mock.patch('__builtin__.open')
@mock.patch.object(six.moves.builtins, 'open')
@mock.patch('os.rename')
@mock.patch('os.path.exists')
@mock.patch('os.remove')

View File

@ -64,7 +64,7 @@ class BaseISCSIInitiatorUtilsTestCase(base.BaseTestCase):
mock_computer]
with mock.patch.object(base_iscsi_utils,
'_winreg', create=True) as mock_winreg:
'winreg', create=True) as mock_winreg:
mock_winreg.OpenKey = winreg_method
mock_winreg.QueryValueEx = mock.MagicMock(return_value=[expected])

View File

@ -656,8 +656,9 @@ class VHDUtilsTestCase(base.BaseTestCase):
def test_get_vhdx_current_header(self):
# The current header has the maximum sequence number.
fake_seq_numbers = ['\x01\x00\x00\x00\x00\x00\x00\x00',
'\x02\x00\x00\x00\x00\x00\x00\x00']
fake_seq_numbers = [
bytearray(b'\x01\x00\x00\x00\x00\x00\x00\x00'),
bytearray(b'\x02\x00\x00\x00\x00\x00\x00\x00')]
mock_handle = self._get_mock_file_handle(*fake_seq_numbers)
offset = self._vhdutils._get_vhdx_current_header_offset(mock_handle)
@ -667,7 +668,7 @@ class VHDUtilsTestCase(base.BaseTestCase):
@mock.patch.object(vhdutils.VHDUtils, '_get_vhdx_current_header_offset')
def test_get_log_size(self, mock_get_vhdx_curr_hd_offset):
fake_curr_header_offset = vdisk_const.VHDX_HEADER_OFFSETS[0]
fake_log_sz = '\x01\x00\x00\x00'
fake_log_sz = bytearray(b'\x01\x00\x00\x00')
mock_get_vhdx_curr_hd_offset.return_value = fake_curr_header_offset
mock_handle = self._get_mock_file_handle(fake_log_sz)
@ -677,8 +678,8 @@ class VHDUtilsTestCase(base.BaseTestCase):
self.assertEqual(log_size, 1)
def test_get_vhdx_metadata_size(self):
fake_md_offset = '\x01\x00\x00\x00\x00\x00\x00\x00'
fake_md_sz = '\x01\x00\x00\x00'
fake_md_offset = bytearray(b'\x01\x00\x00\x00\x00\x00\x00\x00')
fake_md_sz = bytearray(b'\x01\x00\x00\x00')
mock_handle = self._get_mock_file_handle(fake_md_offset,
fake_md_sz)

View File

@ -13,6 +13,7 @@
# under the License.
import os
import six
import mock
from oslotest import base
@ -74,8 +75,8 @@ class PathUtilsTestCase(base.BaseTestCase):
mock_rmtree.side_effect = [WindowsError(
pathutils.ERROR_DIR_IS_NOT_EMPTY), True]
fake_windows_error = WindowsError
with mock.patch('__builtin__.WindowsError',
fake_windows_error, create=True):
with mock.patch.object(six.moves.builtins, 'WindowsError',
fake_windows_error, create=True):
self._pathutils.rmtree(mock.sentinel.FAKE_PATH)
mock_sleep.assert_called_once_with(1)

View File

@ -118,7 +118,7 @@ class LiveMigrationUtils(object):
scsi_ctrl_path = self._vmutils.get_vm_scsi_controller(vm_name)
scsi_paths = self._vmutils.get_controller_volume_paths(scsi_ctrl_path)
return dict(ide_paths.items() + scsi_paths.items())
return dict(list(ide_paths.items()) + list(scsi_paths.items()))
def _get_remote_disk_data(self, vmutils_remote, disk_paths, dest_host):
remote_iscsi_initiator = iscsi_wmi_utils.ISCSIInitiatorWMIUtils(

View File

@ -144,10 +144,10 @@ class VMUtils(object):
si = summary_info[0]
memory_usage = None
if si.MemoryUsage is not None:
memory_usage = long(si.MemoryUsage)
memory_usage = int(si.MemoryUsage)
up_time = None
if si.UpTime is not None:
up_time = long(si.UpTime)
up_time = int(si.UpTime)
# Nova requires a valid state to be returned. Hyper-V has more
# states than Nova, typically intermediate ones and since there is
@ -203,14 +203,14 @@ class VMUtils(object):
mem_settings = vmsetting.associators(
wmi_result_class=self._MEMORY_SETTING_DATA_CLASS)[0]
max_mem = long(memory_mb)
max_mem = int(memory_mb)
mem_settings.Limit = max_mem
if dynamic_memory_ratio > 1:
mem_settings.DynamicMemoryEnabled = True
# Must be a multiple of 2
reserved_mem = min(
long(max_mem / dynamic_memory_ratio) >> 1 << 1,
int(max_mem / dynamic_memory_ratio) >> 1 << 1,
max_mem)
else:
mem_settings.DynamicMemoryEnabled = False
@ -225,7 +225,7 @@ class VMUtils(object):
def _set_vm_vcpus(self, vm, vmsetting, vcpus_num, limit_cpu_features):
procsetting = vmsetting.associators(
wmi_result_class=self._PROCESSOR_SETTING_DATA_CLASS)[0]
vcpus = long(vcpus_num)
vcpus = int(vcpus_num)
procsetting.VirtualQuantity = vcpus
procsetting.Reservation = vcpus
procsetting.Limit = 100000 # static assignment to 100%

View File

@ -80,8 +80,8 @@ class HostUtils(object):
mem_info = self._conn_cimv2.query("SELECT TotalVisibleMemorySize, "
"FreePhysicalMemory "
"FROM win32_operatingsystem")[0]
return (long(mem_info.TotalVisibleMemorySize),
long(mem_info.FreePhysicalMemory))
return (int(mem_info.TotalVisibleMemorySize),
int(mem_info.FreePhysicalMemory))
def get_volume_info(self, drive):
"""Returns a tuple with total size and free space
@ -91,11 +91,11 @@ class HostUtils(object):
"FROM win32_logicaldisk "
"WHERE DeviceID='%s'"
% drive)[0]
return (long(logical_disk.Size), long(logical_disk.FreeSpace))
return (int(logical_disk.Size), int(logical_disk.FreeSpace))
def check_min_windows_version(self, major, minor, build=0):
version_str = self.get_windows_version()
return map(int, version_str.split('.')) >= [major, minor, build]
return list(map(int, version_str.split('.'))) >= [major, minor, build]
def get_windows_version(self):
return self._conn_cimv2.Win32_OperatingSystem()[0].Version

View File

@ -428,27 +428,27 @@ class NetworkUtilsR2(NetworkUtils):
if a.Action == sg_rule.Action]
if not existent_acls:
if sg_rule.Action == self._ACL_ACTION_DENY:
return range(1, 1 + num_rules)
return list(range(1, 1 + num_rules))
else:
return range(self._MAX_WEIGHT - 1,
self._MAX_WEIGHT - 1 - num_rules, - 1)
return list(range(self._MAX_WEIGHT - 1,
self._MAX_WEIGHT - 1 - num_rules, - 1))
# there are existent ACLs.
weights = [a.Weight for a in existent_acls]
if sg_rule.Action == self._ACL_ACTION_DENY:
return [i for i in range(1, self._REJECT_ACLS_COUNT + 1)
return [i for i in list(range(1, self._REJECT_ACLS_COUNT + 1))
if i not in weights][:num_rules]
min_weight = min(weights)
last_weight = min_weight - num_rules - 1
if last_weight > self._REJECT_ACLS_COUNT:
return range(min_weight - 1, last_weight, - 1)
return list(range(min_weight - 1, last_weight, - 1))
# not enough weights. Must search for available weights.
# if it is this case, num_rules is a small number.
current_weight = self._MAX_WEIGHT - 1
new_weights = []
for i in range(num_rules):
for i in list(range(num_rules)):
while current_weight in weights:
current_weight -= 1
new_weights.append(current_weight)

View File

@ -43,8 +43,8 @@ class PathUtils(object):
def open(self, path, mode):
"""Wrapper on __builtin__.open used to simplify unit testing."""
import __builtin__
return __builtin__.open(path, mode)
from six.moves import builtins
return builtins.open(path, mode)
def exists(self, path):
return os.path.exists(path)

View File

@ -25,7 +25,7 @@ import re
import sys
if sys.platform == 'win32':
import _winreg
from six.moves import winreg
import wmi
from oslo_log import log as logging
@ -64,11 +64,11 @@ class BaseISCSIInitiatorUtils(object):
keypath = ("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\"
"iSCSI\\Discovery")
try:
key = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, keypath, 0,
_winreg.KEY_ALL_ACCESS)
temp = _winreg.QueryValueEx(key, 'DefaultInitiatorName')
key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, keypath, 0,
winreg.KEY_ALL_ACCESS)
temp = winreg.QueryValueEx(key, 'DefaultInitiatorName')
initiator_name = str(temp[0])
_winreg.CloseKey(key)
winreg.CloseKey(key)
except Exception:
LOG.info(_LI("The ISCSI initiator name can't be found. "
"Choosing the default one"))

View File

@ -69,7 +69,7 @@ class ISCSITargetUtils(object):
wt_portals = self._conn_wmi.WT_Portal()
if available_only:
wt_portals = filter(lambda portal: portal.Listen, wt_portals)
wt_portals = list(filter(lambda portal: portal.Listen, wt_portals))
if not wt_portals and fail_if_none_found:
err_msg = _("No valid iSCSI portal was found.")

View File

@ -405,7 +405,7 @@ class VHDUtils(object):
fs = vdisk_const.VHD_FOOTER_SIZE_DYNAMIC
max_internal_size = (new_vhd_file_size -
(hs + ddhs + fs)) * bs / (bes + bs)
(hs + ddhs + fs)) * bs // (bes + bs)
return max_internal_size
def _get_internal_vhdx_size_by_file_size(self, vhd_path,
@ -437,7 +437,7 @@ class VHDUtils(object):
size = new_vhd_file_size
max_internal_size = (bs * chunk_ratio * (size - hs -
ls - ms - bes - bes / chunk_ratio) / (bs *
ls - ms - bes - bes // chunk_ratio) // (bs *
chunk_ratio + bes * chunk_ratio + bes))
return max_internal_size - (max_internal_size % bs)

View File

@ -17,8 +17,8 @@ from oslo_utils import units
from os_win.utils import constants
VHD_SIGNATURE = 'conectix'
VHDX_SIGNATURE = 'vhdxfile'
VHD_SIGNATURE = b'conectix'
VHDX_SIGNATURE = b'vhdxfile'
VIRTUAL_STORAGE_TYPE_DEVICE_ISO = 1
VIRTUAL_STORAGE_TYPE_DEVICE_VHD = 2

View File

@ -108,7 +108,7 @@ def _get_class(class_type):
'not exist') % class_type)
windows_version = utils.get_windows_version()
build = map(int, windows_version.split('.'))
build = list(map(int, windows_version.split('.')))
windows_version = float("%i.%i" % (build[0], build[1]))
existing_classes = utils_map.get(class_type)