Repository :
http://git.fedorahosted.org/cgit/cura-tools.git
On branch : master
---------------------------------------------------------------
commit df4399b3103c01550487f9651b50f7e92ce27921
Author: Peter Hatina <phatina(a)redhat.com>
Date: Tue Dec 2 11:17:35 2014 +0100
make wsman support optional
---------------------------------------------------------------
cli/lmi/shell/LMIConnection.py | 21 +++++++++++++++------
cli/lmi/shell/LMIWSMANClient.py | 17 ++++++++++++++++-
2 files changed, 31 insertions(+), 7 deletions(-)
diff --git a/cli/lmi/shell/LMIConnection.py b/cli/lmi/shell/LMIConnection.py
index 197cc12..4108549 100644
--- a/cli/lmi/shell/LMIConnection.py
+++ b/cli/lmi/shell/LMIConnection.py
@@ -167,10 +167,16 @@ def connect(uri, username="", password="",
interactive=False, use_cache=True,
sys.stdout.write("\n")
return None
- connection = LMIConnection(
- uri, username, password, interactive=interactive,
- use_cache=use_cache, key_file=key_file, cert_file=cert_file,
- verify_server_cert=verify_server_cert)
+ try:
+ # Try to create a LMIConnection. We may fail to construct such object,
+ # if WSMAN isn't supported (package pywsman not available).
+ connection = LMIConnection(
+ uri, username, password, interactive=interactive,
+ use_cache=use_cache, key_file=key_file, cert_file=cert_file,
+ verify_server_cert=verify_server_cert)
+ except Exception as e:
+ lmi_raise_or_dump_exception(e)
+ return
# Verify the connection
rval, _, errorstr = connection.connect()
@@ -241,8 +247,11 @@ class LMIConnection(object):
"""
Disconnects and frees :py:class:`.LMICIMXMLClient` object.
"""
- self._client.disconnect()
- del self._client
+ try:
+ self._client.disconnect()
+ del self._client
+ except AttributeError:
+ pass
def __repr__(self):
"""
diff --git a/cli/lmi/shell/LMIWSMANClient.py b/cli/lmi/shell/LMIWSMANClient.py
index cfa794c..056253a 100644
--- a/cli/lmi/shell/LMIWSMANClient.py
+++ b/cli/lmi/shell/LMIWSMANClient.py
@@ -13,7 +13,11 @@
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <
http://www.gnu.org/licenses/>.
-import pywsman
+try:
+ import pywsman
+ HAVE_PYWSMAN = True
+except ImportError:
+ HAVE_PYWSMAN = False
import types
from lmi.shell.compat import *
@@ -22,8 +26,11 @@ from lmi.shell.LMIDecorators import lmi_wrap_cim_exceptions
from lmi.shell.LMIDecorators import lmi_wrap_cim_exceptions_rval
from lmi.shell.LMIExceptions import CIMError
+from lmi.shell.LMIExceptions import ConnectionError
from lmi.shell.LMIExceptions import LMINotSupported
+from lmi.shell.LMIShellLogger import lmi_get_logger
+
from lmi.shell.LMIReturnValue import LMIReturnValue
from lmi.shell.LMIUtil import lmi_instance_to_path
@@ -47,6 +54,8 @@ SCHEMES = {
"DCIM": "http://schemas.dell.com/wbem/wscim/1/cim-schema/2"
}
+logger = lmi_get_logger()
+
class LMIWSMANClient(object):
"""
@@ -76,6 +85,12 @@ class LMIWSMANClient(object):
def __init__(self, uri, username="", password="",
interactive=False,
verify_server_cert=True, key_file=None, cert_file=None):
+ if not HAVE_PYWSMAN:
+ logger.error("WSMAN not supported")
+ raise ConnectionError(
+ wbem.CIM_ERR_NOT_SUPPORTED,
+ "WSMAN not supported")
+
if not uri.startswith("http://") and \
not uri.startswith("https://"):
uri = "https://" + uri