2016-06-30 08:51:29 -05:00
|
|
|
#
|
|
|
|
# Copyright (C) 2016 FreeIPA Contributors see COPYING for license
|
|
|
|
#
|
|
|
|
|
|
|
|
import importlib
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import sys
|
|
|
|
|
|
|
|
import six
|
|
|
|
|
|
|
|
from ipaclient.frontend import ClientCommand, ClientMethod
|
|
|
|
from ipalib.frontend import Object
|
2016-11-21 03:24:17 -06:00
|
|
|
from ipapython.ipautil import APIVersion
|
2016-06-30 08:51:29 -05:00
|
|
|
|
|
|
|
if six.PY3:
|
|
|
|
unicode = str
|
|
|
|
|
|
|
|
|
|
|
|
class CompatCommand(ClientCommand):
|
|
|
|
@property
|
|
|
|
def forwarded_name(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
|
|
|
|
class CompatMethod(ClientMethod, CompatCommand):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class CompatObject(Object):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2016-08-22 06:34:30 -05:00
|
|
|
def get_package(server_info, client):
|
2016-06-30 08:51:29 -05:00
|
|
|
try:
|
2016-07-26 06:35:22 -05:00
|
|
|
server_version = server_info['version']
|
2016-06-30 08:51:29 -05:00
|
|
|
except KeyError:
|
2016-08-22 06:34:30 -05:00
|
|
|
is_valid = False
|
|
|
|
else:
|
|
|
|
is_valid = server_info.is_valid()
|
|
|
|
|
|
|
|
if not is_valid:
|
2016-07-26 06:35:22 -05:00
|
|
|
if not client.isconnected():
|
|
|
|
client.connect(verbose=False)
|
|
|
|
env = client.forward(u'env', u'api_version', version=u'2.0')
|
2016-06-30 08:51:29 -05:00
|
|
|
try:
|
2016-07-26 06:35:22 -05:00
|
|
|
server_version = env['result']['api_version']
|
2016-06-30 08:51:29 -05:00
|
|
|
except KeyError:
|
2016-08-16 09:49:57 -05:00
|
|
|
ping = client.forward(u'ping', version=u'2.0')
|
2016-07-26 06:35:22 -05:00
|
|
|
try:
|
|
|
|
match = re.search(u'API version (2\.[0-9]+)', ping['summary'])
|
|
|
|
except KeyError:
|
|
|
|
match = None
|
|
|
|
if match is not None:
|
|
|
|
server_version = match.group(1)
|
|
|
|
else:
|
|
|
|
server_version = u'2.0'
|
|
|
|
server_info['version'] = server_version
|
2016-08-22 06:34:30 -05:00
|
|
|
server_info.update_validity()
|
|
|
|
|
2016-11-21 03:24:17 -06:00
|
|
|
server_version = APIVersion(server_version)
|
2016-06-30 08:51:29 -05:00
|
|
|
|
|
|
|
package_names = {}
|
|
|
|
base_name = __name__.rpartition('.')[0]
|
|
|
|
base_dir = os.path.dirname(__file__)
|
|
|
|
for name in os.listdir(base_dir):
|
|
|
|
package_dir = os.path.join(base_dir, name)
|
|
|
|
if name.startswith('2_') and os.path.isdir(package_dir):
|
2016-11-21 03:24:17 -06:00
|
|
|
package_version = APIVersion(name.replace('_', '.'))
|
2016-06-30 08:51:29 -05:00
|
|
|
package_names[package_version] = '{}.{}'.format(base_name, name)
|
|
|
|
|
|
|
|
package_version = None
|
2016-11-21 03:24:17 -06:00
|
|
|
for version in sorted(package_names):
|
|
|
|
if package_version is None or package_version < version:
|
2016-06-30 08:51:29 -05:00
|
|
|
package_version = version
|
2016-11-21 03:24:17 -06:00
|
|
|
if version >= server_version:
|
2016-06-30 08:51:29 -05:00
|
|
|
break
|
|
|
|
|
|
|
|
package_name = package_names[package_version]
|
|
|
|
try:
|
|
|
|
package = sys.modules[package_name]
|
|
|
|
except KeyError:
|
|
|
|
package = importlib.import_module(package_name)
|
|
|
|
|
|
|
|
return package
|