More robust multiapi script
@@ -3,6 +3,7 @@ import os
import pkgutil
import re
import sys
+import types
from pathlib import Path
from unittest.mock import MagicMock, patch
@@ -10,27 +11,17 @@ try:
import msrestazure
except: # Install msrestazure. Would be best to mock it, since we don't need it, but all scenarios I know are fine with a pip install for now
import subprocess
- subprocess.call("pip install msrestazure", shell=True) # Use shell to use venv if available
+ subprocess.call(sys.executable + " -m pip install msrestazure", shell=True) # Use shell to use venv if available
try:
- import azure.profiles
- import azure.profiles.multiapiclient
+ import azure.common
except:
- patch_profiles = patch.dict('sys.modules', {
- "azure.profiles": MagicMock(),
- "azure.profiles.multiapiclient": MagicMock()}
- )
- patch_profiles.start()
-
-# try:
-# import azure.profiles
-# except: # Install azure-common. Would be best to mock it, since we don't need it, but all scenarios I know are fine with a pip install for now
-# import subprocess
-# subprocess.call("pip install -e azure-common", shell=True) # Use shell to use venv if available
-
- # The following DOES not work, since it bypasses venv for some reason
- # import pip
- # pip.main(["install", "msrestazure"])
+ sdk_root = Path(__file__).parents[1]
+ sys.path.append(str((sdk_root / "azure-common").resolve()))
+ import azure.common
+
+import pkg_resources
+pkg_resources.declare_namespace('azure')
_GENERATE_MARKER = "############ Generated from here ############\n"
@@ -49,7 +40,9 @@ def get_versionned_modules(package_name, module_name, sdk_root=None):
if not sdk_root:
sdk_root = Path(__file__).parents[1]
- sys.path.append(str((sdk_root / package_name).resolve()))
+ azure.__path__.append(str((sdk_root / package_name / "azure").resolve()))
+ # Doesn't work with namespace package
+ # sys.path.append(str((sdk_root / package_name).resolve()))
module_to_generate = importlib.import_module(module_name)
return [(label, importlib.import_module('.'+label, module_to_generate.__name__))
for (_, label, ispkg) in pkgutil.iter_modules(module_to_generate.__path__)
