diff --git a/tools/azure-sdk-tools/packaging_tools/generate_utils.py b/tools/azure-sdk-tools/packaging_tools/generate_utils.py index 049d45906c8a..987fed04a3dc 100644 --- a/tools/azure-sdk-tools/packaging_tools/generate_utils.py +++ b/tools/azure-sdk-tools/packaging_tools/generate_utils.py @@ -6,7 +6,7 @@ from azure_devtools.ci_tools.git_tools import get_add_diff_file_list from pathlib import Path -from subprocess import check_call, getoutput +from subprocess import check_call from typing import List, Dict, Any from glob import glob import yaml @@ -329,9 +329,21 @@ def format_samples(sdk_code_path) -> None: _LOGGER.info(f"format generated_samples successfully") +def get_npm_package_version(package: str) -> Dict[any, any]: + temp_file = "python_temp.json" + check_call(f"npm list {package} -json > {temp_file}", shell=True) + with open(temp_file, "r") as file_in: + data = json.load(file_in) + if "dependencies" not in data: + _LOGGER.info(f"can not find {package}: {data}") + return {} + + return data["dependencies"] + def gen_cadl(cadl_relative_path: str, spec_folder: str) -> Dict[str, Any]: # update config file cadl_python = "@azure-tools/cadl-python" + autorest_python = "@autorest/python" project_yaml_path = Path(spec_folder) / cadl_relative_path / "cadl-project.yaml" with open(project_yaml_path, "r") as file_in: project_yaml = yaml.safe_load(file_in) @@ -358,10 +370,10 @@ def gen_cadl(cadl_relative_path: str, spec_folder: str) -> Dict[str, Any]: if Path(output_path / "output.yaml").exists(): os.remove(Path(output_path / "output.yaml")) - # get version of @azure-tools/cadl-python used in generation - cadl_python_version = getoutput(f"npm view {cadl_python} version").split('\n')[-1] + # get version of codegen used in generation + npm_package_verstion = get_npm_package_version(autorest_python) # return to original folder os.chdir(origin_path) - return {cadl_python: cadl_python_version} \ No newline at end of file + return npm_package_verstion \ No newline at end of file