2017-10-11 05:09:30 -05:00
|
|
|
#
|
|
|
|
# Copyright (C) 2017 FreeIPA Contributors see COPYING for license
|
|
|
|
#
|
2018-04-05 02:21:16 -05:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
2017-10-11 05:09:30 -05:00
|
|
|
|
|
|
|
import importlib
|
|
|
|
import sys
|
|
|
|
|
2018-08-29 05:43:03 -05:00
|
|
|
from ipaplatform.osinfo import osinfo
|
2017-10-11 05:09:30 -05:00
|
|
|
|
|
|
|
|
2018-09-26 04:59:50 -05:00
|
|
|
class IpaMetaImporter:
|
2017-10-11 05:09:30 -05:00
|
|
|
modules = {
|
|
|
|
'ipaplatform.constants',
|
|
|
|
'ipaplatform.paths',
|
|
|
|
'ipaplatform.services',
|
|
|
|
'ipaplatform.tasks'
|
|
|
|
}
|
|
|
|
|
2018-08-29 05:43:03 -05:00
|
|
|
def __init__(self, platform):
|
|
|
|
self.platform = platform
|
2017-10-11 05:09:30 -05:00
|
|
|
|
|
|
|
def find_module(self, fullname, path=None):
|
|
|
|
"""Meta importer hook"""
|
|
|
|
if fullname in self.modules:
|
|
|
|
return self
|
|
|
|
return None
|
|
|
|
|
|
|
|
def load_module(self, fullname):
|
|
|
|
"""Meta importer hook"""
|
|
|
|
suffix = fullname.split('.', 1)[1]
|
|
|
|
alias = 'ipaplatform.{}.{}'.format(self.platform, suffix)
|
|
|
|
platform_mod = importlib.import_module(alias)
|
|
|
|
base_mod = sys.modules.get(fullname)
|
|
|
|
if base_mod is not None:
|
|
|
|
# module has been imported before, update its __dict__
|
|
|
|
base_mod.__dict__.update(platform_mod.__dict__)
|
|
|
|
for key in list(base_mod.__dict__):
|
|
|
|
if not hasattr(platform_mod, key):
|
|
|
|
delattr(base_mod, key)
|
|
|
|
else:
|
|
|
|
sys.modules[fullname] = platform_mod
|
|
|
|
return platform_mod
|
|
|
|
|
|
|
|
|
2018-08-29 05:43:03 -05:00
|
|
|
metaimporter = IpaMetaImporter(osinfo.platform)
|
2017-10-11 05:09:30 -05:00
|
|
|
sys.meta_path.insert(0, metaimporter)
|
|
|
|
|
|
|
|
fixup_module = metaimporter.load_module
|