Port to Python 3

* Optimize _Backstore constructor to find a free index for the cache.
  Build a temporary set to check if an index is used or not.
* Replace dict.iteritems() with six.iteritems(dict)
* Replace dict.itervalues() with six.itervalues(dict)
* Fix syntax of relative imports
* Get range from six.moves to get xrange() on Python 2 and range() on
  Python 3
* Replace it.next() with next(it)
* Stop using 2to3 in setup.py
* Add Python 3 classifier to setup.py
* Sort also imports
This commit is contained in:
Victor Stinner
2015-05-21 16:57:28 -07:00
parent c6a77cfda9
commit ed0e145ea3
8 changed files with 67 additions and 56 deletions

View File

@@ -21,17 +21,17 @@ if __name__ == "rtslib":
warn("'rtslib' package name for rtslib-fb is deprecated, please"
+ " instead import 'rtslib_fb'", UserWarning, stacklevel=2)
from root import RTSRoot
from utils import RTSLibError, RTSLibBrokenLink, RTSLibNotInCFS
from .root import RTSRoot
from .utils import RTSLibError, RTSLibBrokenLink, RTSLibNotInCFS
from target import LUN, MappedLUN
from target import NodeACL, NetworkPortal, TPG, Target
from target import NodeACLGroup, MappedLUNGroup
from fabric import FabricModule
from .target import LUN, MappedLUN
from .target import NodeACL, NetworkPortal, TPG, Target
from .target import NodeACLGroup, MappedLUNGroup
from .fabric import FabricModule
from tcm import FileIOStorageObject, BlockStorageObject
from tcm import PSCSIStorageObject, RDMCPStorageObject, UserBackedStorageObject
from tcm import StorageObjectFactory
from .tcm import FileIOStorageObject, BlockStorageObject
from .tcm import PSCSIStorageObject, RDMCPStorageObject, UserBackedStorageObject
from .tcm import StorageObjectFactory
__version__ = 'GIT_VERSION'
__author__ = "Jerome Martin <jxm@risingtidesystems.com>"

View File

@@ -107,15 +107,16 @@ Example: self._path = "%s/%s" % (self.configfs_dir, "my_cfs_dir")
'''
import os
from glob import iglob as glob
from node import CFSNode
from utils import fread, fwrite, generate_wwn, normalize_wwn, colonize
from utils import RTSLibError, modprobe, ignored
from target import Target
from utils import _get_auth_attr, _set_auth_attr
from functools import partial
from glob import iglob as glob
import os
import six
from .node import CFSNode
from .utils import fread, fwrite, generate_wwn, normalize_wwn, colonize
from .utils import RTSLibError, modprobe, ignored
from .target import Target
from .utils import _get_auth_attr, _set_auth_attr
version_attributes = {"lio_version", "version"}
discovery_auth_attributes = {"discovery_auth"}
@@ -297,7 +298,7 @@ class _BaseFabricModule(CFSNode):
'''
Setup fabricmodule with settings from fm dict.
'''
for name, value in fm.iteritems():
for name, value in six.iteritems(fm):
if name != 'name':
try:
setattr(self, name, value)
@@ -458,5 +459,5 @@ class FabricModule(object):
@classmethod
def all(cls):
for mod in fabric_modules.itervalues():
for mod in six.itervalues(fabric_modules):
yield mod()

View File

@@ -20,7 +20,7 @@ under the License.
import os
import stat
from utils import fread, fwrite, RTSLibError, RTSLibNotInCFS
from .utils import fread, fwrite, RTSLibError, RTSLibNotInCFS
class CFSNode(object):

View File

@@ -23,12 +23,12 @@ import os
import stat
import json
from node import CFSNode
from target import Target
from fabric import FabricModule
from tcm import so_mapping, StorageObject
from utils import RTSLibError, RTSLibBrokenLink, modprobe, mount_configfs
from utils import dict_remove, set_attributes
from .node import CFSNode
from .target import Target
from .fabric import FabricModule
from .tcm import so_mapping, StorageObject
from .utils import RTSLibError, RTSLibBrokenLink, modprobe, mount_configfs
from .utils import dict_remove, set_attributes
default_save_file = "/etc/target/saveconfig.json"

View File

@@ -21,16 +21,19 @@ under the License.
import re
import os
from glob import iglob as glob
from functools import partial
from os.path import isdir
from six.moves import range
import uuid
from node import CFSNode
from os.path import isdir
from utils import RTSLibError, RTSLibBrokenLink
from utils import fread, fwrite, normalize_wwn, generate_wwn
from utils import dict_remove, set_attributes, set_parameters, ignored
from utils import _get_auth_attr, _set_auth_attr
import tcm
from functools import partial
from .node import CFSNode
from .utils import RTSLibError, RTSLibBrokenLink
from .utils import fread, fwrite, normalize_wwn, generate_wwn
from .utils import dict_remove, set_attributes, set_parameters, ignored
from .utils import _get_auth_attr, _set_auth_attr
from . import tcm
import six
auth_params = ('userid', 'password', 'mutual_userid', 'mutual_password')
@@ -172,7 +175,7 @@ class TPG(CFSNode):
if tag is None:
tags = [tpg.tag for tpg in parent_target.tpgs]
for index in xrange(1048576):
for index in range(1048576):
if index not in tags and index > 0:
tag = index
break
@@ -434,7 +437,7 @@ class TPG(CFSNode):
tpg_obj.enable = tpg.get('enable', True)
dict_remove(tpg, ('luns', 'portals', 'node_acls', 'tag',
'attributes', 'parameters', 'enable'))
for name, value in tpg.iteritems():
for name, value in six.iteritems(tpg):
if value:
try:
setattr(tpg_obj, name, value)
@@ -503,7 +506,7 @@ class LUN(CFSNode):
if lun is None:
luns = [l.lun for l in self.parent_tpg.luns]
for index in xrange(self.MAX_LUN+1):
for index in range(self.MAX_LUN+1):
if index not in luns:
lun = index
break
@@ -958,7 +961,7 @@ class NodeACL(CFSNode):
MappedLUN.setup(tpg_obj, acl_obj, mlun, err_func)
dict_remove(acl, ('attributes', 'mapped_luns', 'node_wwn'))
for name, value in acl.iteritems():
for name, value in six.iteritems(acl):
if value:
try:
setattr(acl_obj, name, value)
@@ -1198,7 +1201,7 @@ class Group(object):
def _get_first_member(self):
try:
return self._mem_func(self).next()
return next(self._mem_func(self))
except StopIteration:
raise IndexError("Group is empty")
@@ -1304,7 +1307,7 @@ class NodeACLGroup(Group):
# if joining a group, take its config
try:
model = self._node_acls.next()
model = next(self._node_acls)
except StopIteration:
pass
else:

View File

@@ -23,12 +23,13 @@ import stat
import re
import glob
import resource
from six.moves import range
from node import CFSNode
from utils import fread, fwrite, RTSLibError, generate_wwn
from utils import convert_scsi_path_to_hctl, convert_scsi_hctl_to_path
from utils import is_dev_in_use, get_blockdev_type
from utils import get_size_for_blk_dev, get_size_for_disk_name
from .node import CFSNode
from .utils import fread, fwrite, RTSLibError, generate_wwn
from .utils import convert_scsi_path_to_hctl, convert_scsi_hctl_to_path
from .utils import is_dev_in_use, get_blockdev_type
from .utils import get_size_for_blk_dev, get_size_for_disk_name
class StorageObject(CFSNode):
@@ -185,9 +186,9 @@ class StorageObject(CFSNode):
listdir = os.listdir
realpath = os.path.realpath
path = self.path
from root import RTSRoot
from target import LUN, TPG, Target
from fabric import target_names_excludes
from .root import RTSRoot
from .target import LUN, TPG, Target
from .fabric import target_names_excludes
for base, fm in ((fm.path, fm) for fm in RTSRoot().fabric_modules if fm.exists):
for tgt_dir in listdir(base):
@@ -899,8 +900,9 @@ class _Backstore(CFSNode):
(self._plugin, name))
else:
# Allocate new index value
for index in xrange(1048576):
if index not in bs_cache.values():
indexes = set(bs_cache.values())
for index in range(1048576):
if index not in indexes:
self._index = index
bs_cache[self._lookup_key] = self._index
break

View File

@@ -18,12 +18,13 @@ License for the specific language governing permissions and limitations
under the License.
'''
import re
import os
import stat
import uuid
import re
import six
import socket
import stat
import subprocess
import uuid
from contextlib import contextmanager
class RTSLibError(Exception):
@@ -455,14 +456,14 @@ def _set_auth_attr(self, value, attribute, ignore=False):
raise
def set_attributes(obj, attr_dict, err_func):
for name, value in attr_dict.iteritems():
for name, value in six.iteritems(attr_dict):
try:
obj.set_attribute(name, value)
except RTSLibError as e:
err_func(str(e))
def set_parameters(obj, param_dict, err_func):
for name, value in param_dict.iteritems():
for name, value in six.iteritems(param_dict):
try:
obj.set_parameter(name, value)
except RTSLibError as e:

View File

@@ -28,5 +28,9 @@ setup (
url = 'http://github.com/agrover/rtslib-fb',
packages = ['rtslib_fb', 'rtslib'],
scripts = ['scripts/targetctl'],
use_2to3 = True,
"classifiers": [
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"License :: OSI Approved :: Apache Software License",
],
)