Skip to content

Commit 8765a1d

Browse files
authored
Merge pull request #433 from necusjz/enhance-verify-perf
Enhance performance of `aaz-dev command-model verify`
2 parents 4de9f14 + 8de427f commit 8765a1d

File tree

1 file changed

+53
-27
lines changed

1 file changed

+53
-27
lines changed

src/aaz_dev/command/api/_cmds.py

Lines changed: 53 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -167,40 +167,75 @@ def generate_command_models_from_swagger(swagger_tag, workspace_path=None):
167167
help="Path of `aaz` repository."
168168
)
169169
def verify():
170-
def verify_command(file_path, node):
170+
def verify_resource(model, path):
171+
if "commandGroups" not in model:
172+
return
173+
174+
for grp in model["commandGroups"]:
175+
base_path = os.path.join(path, *grp["name"].split())
176+
if not os.path.exists(base_path):
177+
raise FileNotFoundError(base_path)
178+
179+
for cmd in grp.get("commands", []):
180+
file_path = os.path.join(base_path, f"_{cmd['name']}.md")
181+
if not os.path.isfile(file_path):
182+
raise FileNotFoundError(file_path)
183+
184+
verify_resource(grp, base_path)
185+
186+
def verify_command(file_path):
171187
with open(file_path, "r", encoding="utf-8") as fp:
172188
content = fp.read()
173189

190+
base_path = os.path.dirname(file_path)
191+
curr_grp = " ".join(os.path.relpath(base_path, aaz.commands_folder).split(os.sep))
192+
curr_cmd = os.path.splitext(os.path.basename(file_path))[0][1:]
193+
174194
paths = re.findall(r"]\(([^)]+)\)", content)
175195
for path in paths:
176196
json_path = os.path.join(Config.AAZ_PATH, os.path.splitext(path)[0][1:] + ".json")
177197
json_path = os.path.normpath(json_path)
198+
199+
if json_path in model_set:
200+
continue
201+
178202
if not os.path.exists(json_path):
179203
raise Exception(f"{json_path} defined in {file_path} is missing.")
180204

181205
with open(json_path, "r", encoding="utf-8", errors="ignore") as fp:
182206
model = json.load(fp)
183-
group, command = " ".join(node.names[:-1]), node.names[-1]
184-
for g in model["commandGroups"]:
185-
if g["name"] == group:
186-
if not any(cmd["name"] == command for cmd in g["commands"]):
187-
raise Exception(f"There is no {command} command info in {json_path}.")
188207

189-
break
208+
try:
209+
verify_resource(model, aaz.commands_folder)
190210

191-
model_set.add(json_path)
211+
except FileNotFoundError as e:
212+
raise Exception(f"Cannot find {e} defined in {json_path}.")
192213

193-
tmpl = get_templates()["command"]
194-
if not tmpl.render(command=node) == content:
195-
raise Exception(f"{file_path} cannot be rendered correctly.")
214+
target = curr_grp
215+
while target:
216+
try:
217+
for grp in model["commandGroups"]:
218+
if target.startswith(grp["name"]):
219+
target = target[len(grp["name"]):].strip()
220+
model = grp
221+
222+
break
223+
224+
except KeyError:
225+
raise Exception(f"{curr_grp} has no corresponding definition in {json_path}.")
226+
227+
commands = model["commands"]
228+
if not any(cmd["name"] == curr_cmd for cmd in commands):
229+
raise Exception(f"There is no {curr_cmd} command info in {json_path}.")
230+
231+
model_set.add(json_path)
196232

197233
model_set = set()
198234
aaz = AAZSpecsManager()
199-
stack = [(aaz.commands_folder, aaz.tree.root)] # root nodes
235+
stack = [aaz.commands_folder]
200236

201237
while stack:
202-
curr_path, curr_node = stack.pop()
203-
logger.info(f"Checking {curr_path}")
238+
curr_path = stack.pop()
204239
if os.path.isdir(curr_path):
205240
readme_path = os.path.join(curr_path, "readme.md")
206241
if not os.path.exists(readme_path):
@@ -227,13 +262,9 @@ def verify_command(file_path, node):
227262
diff = cmd_set - items or items - cmd_set
228263
raise Exception(f"Command info {diff} doesn't match in {readme_path}.")
229264

230-
groups = set(curr_node.commands.keys())
231-
if groups != items:
232-
diff = groups - items or items - groups
233-
raise Exception(f"Command info {diff} in tree.json doesn't match in {readme_path}.")
234-
235265
for file in files:
236-
verify_command(os.path.join(curr_path, file), curr_node.commands[file[1:-3]])
266+
verify_command(os.path.join(curr_path, file))
267+
237268
else:
238269
if len(items) != len(set(items)):
239270
raise Exception(f"{readme_path} has duplicate command group names.")
@@ -245,15 +276,10 @@ def verify_command(file_path, node):
245276
diff = folders - items or items - folders
246277
raise Exception(f"Command group info {diff} doesn't match in {readme_path}.")
247278

248-
groups = set(curr_node.command_groups.keys())
249-
if groups != set(items):
250-
diff = groups - items or items - groups
251-
raise Exception(f"Command group info {diff} in tree.json doesn't match in {readme_path}.")
252-
253279
for folder in folders:
254-
stack.append((os.path.join(curr_path, folder), curr_node.command_groups[folder]))
280+
stack.append(os.path.join(curr_path, folder))
255281

256-
for root, dirs, files in os.walk(aaz.resources_folder):
282+
for root, _, files in os.walk(aaz.resources_folder):
257283
for file in files:
258284
if not file.endswith(".json") or file.startswith("client"): # support data-plane
259285
continue

0 commit comments

Comments
 (0)