summaryrefslogtreecommitdiffstats
path: root/python
diff options
context:
space:
mode:
Diffstat (limited to 'python')
-rw-r--r--python/samba/__init__.py35
1 files changed, 30 insertions, 5 deletions
diff --git a/python/samba/__init__.py b/python/samba/__init__.py
index cd2a309fc0..0cbdec7800 100644
--- a/python/samba/__init__.py
+++ b/python/samba/__init__.py
@@ -314,7 +314,8 @@ def valid_netbios_name(name):
return True
-def import_bundled_package(modulename, location):
+def import_bundled_package(modulename, location, source_tree_container,
+ namespace):
"""Import the bundled version of a package.
:note: This should only be called if the system version of the package
@@ -322,14 +323,35 @@ def import_bundled_package(modulename, location):
:param modulename: Module name to import
:param location: Location to add to sys.path (can be relative to
- ${srcdir}/lib)
+ ${srcdir}/${source_tree_container})
+ :param source_tree_container: Directory under source root that
+ contains the bundled third party modules.
+ :param namespace: Namespace to import module from, when not in source tree
"""
if in_source_tree():
- sys.path.insert(0, os.path.join(source_tree_topdir(), "lib", location))
+ extra_path = os.path.join(source_tree_topdir(), source_tree_container,
+ location)
+ if not extra_path in sys.path:
+ sys.path.insert(0, extra_path)
sys.modules[modulename] = __import__(modulename)
else:
sys.modules[modulename] = __import__(
- "samba.external.%s" % modulename, fromlist=["samba.external"])
+ "%s.%s" % (namespace, modulename), fromlist=[namespace])
+
+
+def ensure_third_party_module(modulename, location):
+ """Add a location to sys.path if a third party dependency can't be found.
+
+ :param modulename: Module name to import
+ :param location: Location to add to sys.path (can be relative to
+ ${srcdir}/third_party)
+ """
+ try:
+ __import__(modulename)
+ except ImportError:
+ import_bundled_package(modulename, location,
+ source_tree_container="third_party",
+ namespace="samba.third_party")
def ensure_external_module(modulename, location):
@@ -339,10 +361,13 @@ def ensure_external_module(modulename, location):
:param location: Location to add to sys.path (can be relative to
${srcdir}/lib)
"""
+ # This is deprecated - please use ensure_third_party_module for
+ # new modules instead, and put them in third_party/.
try:
__import__(modulename)
except ImportError:
- import_bundled_package(modulename, location)
+ import_bundled_package(modulename, location,
+ source_tree_container="lib", namespace="samba.external")
def dn_from_dns_name(dnsdomain):