-
Notifications
You must be signed in to change notification settings - Fork 129
Expand file tree
/
Copy path__init__.py
More file actions
196 lines (151 loc) · 7.7 KB
/
__init__.py
File metadata and controls
196 lines (151 loc) · 7.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# -----------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# -----------------------------------------------------------------------------
# pylint: disable=no-else-return, too-many-nested-blocks, too-many-locals, too-many-branches
import time
import os
import json
from knack.log import get_logger
import azure_cli_diff_tool
from azdev.utilities import display, require_azure_cli, heading, get_path_table, filter_by_git_diff, \
calc_selected_mod_names
from .custom import DiffExportFormat, get_commands_meta, STORED_DEPRECATION_KEY, command_example_diff
from .util import export_commands_meta, dump_command_tree, add_to_command_tree
from ..statistics import _create_invoker_and_load_cmds, _get_command_source, \
_command_codegen_info # pylint: disable=protected-access
from ..statistics.util import filter_modules
logger = get_logger(__name__)
def diff_export_format_choices():
return [form.value for form in DiffExportFormat]
# pylint: disable=too-many-statements
def export_command_meta(modules=None, git_source=None, git_target=None, git_repo=None,
with_help=False, with_example=False,
meta_output_path=None):
require_azure_cli()
# allow user to run only on CLI or extensions
cli_only = modules == ['CLI']
ext_only = modules == ['EXT']
if cli_only or ext_only:
modules = None
selected_modules = get_path_table(include_only=modules)
if cli_only:
selected_modules['ext'] = {}
if ext_only:
selected_modules['core'] = {}
selected_modules['mod'] = {}
# filter down to only modules that have changed based on git diff
selected_modules = filter_by_git_diff(selected_modules, git_source, git_target, git_repo)
if not any(selected_modules.values()):
logger.warning('No commands selected to check.')
selected_mod_names = list(selected_modules['mod'].keys())
selected_mod_names += list(selected_modules['ext'].keys())
selected_mod_names += list(selected_modules['core'].keys())
if selected_mod_names:
display('Modules selected: {}\n'.format(', '.join(selected_mod_names)))
heading('Export Command Table Meta')
start = time.time()
display('Initializing with loading command table...')
from azure.cli.core import get_default_cli # pylint: disable=import-error
az_cli = get_default_cli()
# load commands, args, and help
_create_invoker_and_load_cmds(az_cli)
stop = time.time()
logger.info('Commands loaded in %i sec', stop - start)
display('Commands loaded in {} sec'.format(stop - start))
command_loader = az_cli.invocation.commands_loader
from azure.cli.core.file_util import get_all_help
help_info = {}
if with_help or with_example:
help_files = get_all_help(az_cli)
for help_item in help_files:
if not help_item.command:
continue
help_info[help_item.command] = help_item
# trim command table to selected_modules
command_loader = filter_modules(command_loader, modules=selected_mod_names)
if not command_loader.command_table:
logger.warning('No commands selected to check.')
commands_info = []
for command_name, command in command_loader.command_table.items():
command_info = {
"name": command_name,
"source": _get_command_source(command_name, command),
"is_aaz": False,
"help": help_info[command_name] if command_name in help_info else None,
"confirmation": command.confirmation is True,
"arguments": [],
"az_arguments_schema": None,
"supports_no_wait": command.supports_no_wait,
"is_preview": command.command_kwargs.get("is_preview", False)
}
if hasattr(command, "deprecate_info"):
for info_key in STORED_DEPRECATION_KEY:
if hasattr(command.deprecate_info, info_key) and getattr(command.deprecate_info, info_key):
if command_info.get("deprecate_info", None) is None:
command_info["deprecate_info"] = {}
command_info["deprecate_info"][info_key] = getattr(command.deprecate_info, info_key)
module_loader = command_loader.cmd_to_loader_map[command_name]
for loader in module_loader:
loader.skip_applicability = True
codegen_info = _command_codegen_info(command_name, command, module_loader)
if codegen_info:
command_info['codegen_version'] = codegen_info['version']
command_info['codegen_type'] = codegen_info['type']
if codegen_info['version'] == "v2":
command_info['is_aaz'] = True
command_loader.load_arguments(command_name)
if command.arguments is None:
logger.warning('No arguments generated from %i.', command_name)
else:
command_info['arguments'] = command.arguments
if command_info["is_aaz"]:
try:
command_info['az_arguments_schema'] = command._args_schema # pylint: disable=protected-access
except AttributeError:
pass
commands_info.append(command_info)
commands_meta = get_commands_meta(command_loader.command_group_table, commands_info, with_help, with_example)
export_commands_meta(commands_meta, meta_output_path)
display(f"Total Commands: {len(commands_info)} from {', '.join(selected_mod_names)} have been generated.")
def cmp_command_meta(base_meta_file, diff_meta_file, only_break=False, output_type="text", output_file=None):
return azure_cli_diff_tool.meta_diff(base_meta_file, diff_meta_file, only_break, output_type, output_file)
def export_command_tree(modules, output_file=None):
require_azure_cli()
selected_mod_names = calc_selected_mod_names(modules)
if selected_mod_names:
display('Modules selected: {}\n'.format(', '.join(selected_mod_names)))
heading('Export Command Tree')
start = time.time()
display('Initializing with loading command table...')
from azure.cli.core import get_default_cli # pylint: disable=import-error
az_cli = get_default_cli()
# load commands, args, and help
_create_invoker_and_load_cmds(az_cli)
stop = time.time()
logger.info('Commands loaded in %i sec', stop - start)
display('Commands loaded in {} sec'.format(stop - start))
command_loader = az_cli.invocation.commands_loader
# trim command table to selected_modules
command_loader = filter_modules(command_loader, modules=selected_mod_names)
if not command_loader.command_table:
logger.warning('No commands selected to check.')
command_tree = {}
for command_name, command in command_loader.command_table.items():
module_source = _get_command_source(command_name, command)['module']
# The command tree is a tree structure like our azExtCmdTree: https://aka.ms/azExtCmdTree
add_to_command_tree(command_tree, command_name, module_source)
dump_command_tree(command_tree, output_file)
def meta_command_example_diff(base_meta_file, diff_meta_file):
if not os.path.exists(base_meta_file) and not os.path.exists(diff_meta_file):
raise Exception("base and/or diff meta file needed")
command_tree_before = {}
command_tree_after = {}
if os.path.exists(base_meta_file):
with open(base_meta_file, "r") as g:
command_tree_before = json.load(g)
if os.path.exists(diff_meta_file):
with open(diff_meta_file, "r") as g:
command_tree_after = json.load(g)
return command_example_diff(command_tree_before, command_tree_after)