1
1
import importlib
2
+ import logging
2
3
import os
3
4
import pkgutil
4
5
import re
25
26
26
27
_GENERATE_MARKER = "############ Generated from here ############\n "
27
28
29
+ _LOGGER = logging .getLogger (__name__ )
30
+
31
+
28
32
def parse_input (input_parameter ):
29
33
"""From a syntax like package_name#submodule, build a package name
30
34
and complete module name.
@@ -48,26 +52,53 @@ def get_versionned_modules(package_name, module_name, sdk_root=None):
48
52
for (_ , label , ispkg ) in pkgutil .iter_modules (module_to_generate .__path__ )
49
53
if label .startswith ("v20" ) and ispkg ]
50
54
55
+ def extract_api_version_from_code (function ):
56
+ """Will extract from __code__ the API version. Should be use if you use this is an operation group with no constant api_version.
57
+ """
58
+ try :
59
+ if "api_version" in function .__code__ .co_varnames :
60
+ return function .__code__ .co_consts [1 ]
61
+ except Exception :
62
+ pass
63
+
51
64
def build_operation_meta (versionned_modules ):
52
65
version_dict = {}
53
66
mod_to_api_version = {}
54
67
for versionned_label , versionned_mod in versionned_modules :
55
- extracted_api_version = None
68
+ extracted_api_versions = set ()
56
69
client_doc = versionned_mod .__dict__ [versionned_mod .__all__ [0 ]].__doc__
57
70
operations = list (re .finditer (r':ivar (?P<attr>[a-z_]+): \w+ operations\n\s+:vartype (?P=attr): .*.operations.(?P<clsname>\w+)\n' , client_doc ))
58
71
for operation in operations :
59
72
attr , clsname = operation .groups ()
60
73
version_dict .setdefault (attr , []).append ((versionned_label , clsname ))
61
- if not extracted_api_version :
62
- # Create a fake operation group to extract easily the real api version
63
- try :
64
- extracted_api_version = versionned_mod .operations .__dict__ [clsname ](None , None , None , None ).api_version
65
- except Exception :
66
- # Should not happen. I guess it mixed operation groups like VMSS Network...
67
- pass
68
- if not extracted_api_version :
69
- sys .exit ("Was not able to extract api_version of %s" % versionned_label )
70
- mod_to_api_version [versionned_label ] = extracted_api_version
74
+
75
+ # Create a fake operation group to extract easily the real api version
76
+ extracted_api_version = None
77
+ try :
78
+ extracted_api_version = versionned_mod .operations .__dict__ [clsname ](None , None , None , None ).api_version
79
+ except Exception :
80
+ # Should not happen. I guess it mixed operation groups like VMSS Network...
81
+ for func_name , function in versionned_mod .operations .__dict__ [clsname ].__dict__ .items ():
82
+ if not func_name .startswith ("__" ):
83
+ extracted_api_version = extract_api_version_from_code (function )
84
+ if extracted_api_version :
85
+ extracted_api_versions .add (extracted_api_version )
86
+
87
+ if not extracted_api_versions :
88
+ sys .exit ("Was not able to extract api_version of {}" .format (versionned_label ))
89
+ if len (extracted_api_versions ) >= 2 :
90
+ # Mixed operation group, try to figure out what we want to use
91
+ final_api_version = None
92
+ _LOGGER .warning ("Found too much API version: {} in label {}" .format (extracted_api_versions , versionned_label ))
93
+ for candidate_api_version in extracted_api_versions :
94
+ if "v{}" .format (candidate_api_version .replace ("-" , "_" )) == versionned_label :
95
+ final_api_version = candidate_api_version
96
+ _LOGGER .warning ("Guessing you want {} based on label {}" .format (final_api_version , versionned_label ))
97
+ break
98
+ else :
99
+ sys .exit ("Unble to match {} to label {}" .format (extracted_api_versions , versionned_label ))
100
+ extracted_api_versions = {final_api_version }
101
+ mod_to_api_version [versionned_label ] = extracted_api_versions .pop ()
71
102
72
103
# latest: api_version=mod_to_api_version[versions[-1][0]]
73
104
@@ -160,6 +191,8 @@ def _models_dict(cls, api_version):
160
191
"""
161
192
162
193
if __name__ == "__main__" :
194
+ logging .basicConfig (level = logging .INFO )
195
+
163
196
package_name , module_name = parse_input (sys .argv [1 ])
164
197
versionned_modules = get_versionned_modules (package_name , module_name )
165
198
version_dict , mod_to_api_version = build_operation_meta (versionned_modules )
0 commit comments