-
Notifications
You must be signed in to change notification settings - Fork 129
Expand file tree
/
Copy path__init__.py
More file actions
187 lines (158 loc) · 8.3 KB
/
__init__.py
File metadata and controls
187 lines (158 loc) · 8.3 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
# -----------------------------------------------------------------------------
# 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
import json
import os
import time
from deepdiff import DeepDiff
from knack.log import get_logger
from azdev.utilities import display, require_azure_cli, heading, get_path_table, filter_by_git_diff, cmd
from .custom import MetaChangeDetects, DiffExportFormat
from .util import export_meta_changes_to_json, gen_commands_meta, get_commands_meta, \
extrct_module_name_from_meta_file, export_meta_changes_to_csv
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
# 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": command.help,
"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)
}
module_loader = command_loader.cmd_to_loader_map[command_name]
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)
gen_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):
if not os.path.exists(base_meta_file):
raise Exception("base meta file needed")
if not os.path.exists(diff_meta_file):
raise Exception("diff meta file needed")
start = time.time()
with open(base_meta_file, "r") as g:
command_tree_before = json.load(g)
with open(diff_meta_file, "r") as g:
command_tree_after = json.load(g)
stop = time.time()
logger.info('Command meta files loaded in %i sec', stop - start)
diff = DeepDiff(command_tree_before, command_tree_after)
if not diff:
display(f"No meta diffs from {diff_meta_file} to {base_meta_file}")
return export_meta_changes_to_json(None, output_file)
else:
detected_changes = MetaChangeDetects(diff, command_tree_before, command_tree_after)
detected_changes.check_deep_diffs()
result = detected_changes.export_meta_changes(only_break, output_type)
return export_meta_changes_to_json(result, output_file)
def cmp_command_meta_of_versions(base_version, diff_version, only_break=False, version_diff_file=None):
version_diffs = []
base_version_meta_path = "azure-cli-" + base_version
base_meta_download_cmd = """az storage blob download-batch --account-name versionmeta -s $web --pattern """ \
+ base_version_meta_path + """/* -d . """
display(f"Downloading {base_version} meta data using '{base_meta_download_cmd}'")
cmd(base_meta_download_cmd, show_stderr=True)
if not os.path.exists(os.getcwd() + "/" + base_version_meta_path):
display(f"No meta downloaded from blob for {base_version}, please check blob data")
return export_meta_changes_to_csv(version_diffs, version_diff_file)
diff_version_meta_path = "azure-cli-" + diff_version
diff_meta_download_cmd = """az storage blob download-batch --account-name versionmeta -s $web --pattern """ \
+ diff_version_meta_path + """/* -d . """
display(f"Downloading {diff_version} meta data using '{diff_meta_download_cmd}'")
cmd(diff_meta_download_cmd, show_stderr=True)
if not os.path.exists(os.getcwd() + "/" + diff_version_meta_path):
display(f"No meta downloaded from blob for {diff_version}, please check blob data")
return export_meta_changes_to_csv(version_diffs, version_diff_file)
for base_meta_file in os.listdir(os.getcwd() + "/" + base_version_meta_path):
module_name = extrct_module_name_from_meta_file(base_meta_file)
if not module_name:
continue
diff_meta_file = os.path.join(os.getcwd(), diff_version_meta_path, base_meta_file)
if not os.path.exists(diff_meta_file):
display(f"Module {module_name} removed for {diff_version}")
continue
with open(os.path.join(base_version_meta_path, base_meta_file), "r") as g:
command_tree_before = json.load(g)
with open(diff_meta_file, "r") as g:
command_tree_after = json.load(g)
diff = DeepDiff(command_tree_before, command_tree_after)
if not diff:
display(f"No meta diffs from version: {diff_version}/{base_meta_file} for module: {module_name}")
continue
detected_changes = MetaChangeDetects(diff, command_tree_before, command_tree_after)
detected_changes.check_deep_diffs()
diff_objs = detected_changes.export_meta_changes(only_break, "dict")
mod_obj = {"module": module_name}
for obj in diff_objs:
obj.update(mod_obj)
version_diffs.append(obj)
return export_meta_changes_to_csv(version_diffs, version_diff_file)