From 78af72c8c698d1de3219871a5c60d610ecbf9dd2 Mon Sep 17 00:00:00 2001 From: AllyW Date: Wed, 3 Jul 2024 12:01:15 +0800 Subject: [PATCH] add extension show (#456) * add extension show * fix extension index name --- HISTORY.rst | 5 ++++ azdev/__init__.py | 2 +- azdev/commands.py | 1 + azdev/help.py | 3 ++ azdev/operations/extensions/__init__.py | 30 +++++++++++++++++++ azdev/operations/extensions/util.py | 13 ++++++++ .../operations/extensions/version_upgrade.py | 14 +++++++-- azdev/params.py | 3 ++ 8 files changed, 68 insertions(+), 3 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index 484b7f50..546d1e90 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -2,6 +2,11 @@ Release History =============== +0.1.71 +++++++ +* `azdev extension show`: Show detailed extension info that installed in your development environment. +* `azdev extension cal-next-version`: Fix last stable version parser from index.json. + 0.1.70 ++++++ * Fix cmdcov issue(#455): remove tested_command.txt reference. diff --git a/azdev/__init__.py b/azdev/__init__.py index db461ad3..0c5c0184 100644 --- a/azdev/__init__.py +++ b/azdev/__init__.py @@ -4,4 +4,4 @@ # license information. # ----------------------------------------------------------------------------- -__VERSION__ = '0.1.70' +__VERSION__ = '0.1.71' diff --git a/azdev/commands.py b/azdev/commands.py index e4cbf939..82829020 100644 --- a/azdev/commands.py +++ b/azdev/commands.py @@ -67,6 +67,7 @@ def operation_group(name): g.command('publish', 'publish_extensions') g.command('update-index', 'update_extension_index') g.command('cal-next-version', 'cal_next_version') + g.command('show', 'show_extension') with CommandGroup(self, 'extension repo', operation_group('extensions')) as g: g.command('add', 'add_extension_repo') diff --git a/azdev/help.py b/azdev/help.py index 3b31635c..ee889e11 100644 --- a/azdev/help.py +++ b/azdev/help.py @@ -258,6 +258,9 @@ short-summary: List what extensions are currently visible to your development environment. """ +helps['extension show'] = """ + short-summary: Show detailed extension info that installed in your development environment. +""" helps['extension publish'] = """ short-summary: Build and publish an extension to a storage account. diff --git a/azdev/operations/extensions/__init__.py b/azdev/operations/extensions/__init__.py index 8ef6d1f4..48368d98 100644 --- a/azdev/operations/extensions/__init__.py +++ b/azdev/operations/extensions/__init__.py @@ -138,6 +138,36 @@ def list_extensions(): return results +def show_extension(mod_name): + ext_paths = get_ext_repo_paths() + ext_mod_paths = [os.path.join(ext, "src", mod_name) for ext in ext_paths] + mod_install_path = find_files(ext_mod_paths, '*.*-info') + + if not mod_install_path: + raise CLIError('extension not installed using azdev: {}'.format(mod_name)) + + if len(mod_install_path) > 1: + raise CLIError('extension {} duplicated, please specify extension name'.format(mod_name)) + + mod_install_dir = os.path.dirname(mod_install_path[0]) + long_name = os.path.basename(mod_install_dir) + assert long_name == mod_name + mod_info = { + "name": mod_name, + "path": mod_install_dir + } + # extract pkg name from egg-info or dist-info folders + logger.debug("Extracting pkg info from %s...", mod_install_path[0]) + meta_files = ["PKG-INFO", "METADATA"] + pkg_info_path = [os.path.join(mod_install_path[0], meta) for meta in meta_files + if os.path.isfile(os.path.join(mod_install_path[0], meta))] + from .util import get_pkg_info_from_pkg_metafile + for pkg_info_file in pkg_info_path: + pkg_info = get_pkg_info_from_pkg_metafile(pkg_info_file) + mod_info.update(pkg_info) + return mod_info + + def _get_sha256sum(a_file): import hashlib sha256 = hashlib.sha256() diff --git a/azdev/operations/extensions/util.py b/azdev/operations/extensions/util.py index 88c0fffb..ead988fd 100644 --- a/azdev/operations/extensions/util.py +++ b/azdev/operations/extensions/util.py @@ -81,3 +81,16 @@ def get_whl_from_url(url, filename, tmp_dir, whl_cache=None): f.write(chunk) whl_cache[url] = ext_file return ext_file + + +def get_pkg_info_from_pkg_metafile(pkg_info_file): + mod_info = {} + with open(pkg_info_file, "r", encoding="utf-8") as f: + for line in f: + if line.startswith("Name:"): + pkg_name = line.split(":")[-1].strip() + mod_info["pkg_name"] = pkg_name + if line.startswith("Version:"): + pkg_version = line.split(":")[-1].strip() + mod_info["pkg_version"] = pkg_version + return mod_info diff --git a/azdev/operations/extensions/version_upgrade.py b/azdev/operations/extensions/version_upgrade.py index 2fd8dd04..dd784784 100644 --- a/azdev/operations/extensions/version_upgrade.py +++ b/azdev/operations/extensions/version_upgrade.py @@ -9,6 +9,7 @@ # https://github.com/Azure/azure-cli/blob/release/doc/extensions/versioning_guidelines.md from packaging.version import parse +from knack.util import CLIError from azure_cli_diff_tool.utils import DiffLevel from azdev.operations.constant import (PREVIEW_INIT_SUFFIX, VERSION_MAJOR_TAG, VERSION_MINOR_TAG, VERSION_PATCH_TAG, VERSION_STABLE_TAG, VERSION_PREVIEW_TAG, VERSION_PRE_TAG, @@ -70,6 +71,8 @@ def __init__(self, module_name, current_version, is_preview, is_experimental, self.init_version_pre_tag() self.next_version = ModuleVersion(self.version) self.last_stable_major = float('inf') + self.pkg_name = self.module_name + self.parse_pkg_name() self.parse_last_stable_major() def norm_versions(self): @@ -179,15 +182,22 @@ def find_max_version(version_datas): has_stable = True return has_stable, max_stable_major + def parse_pkg_name(self): + from azdev.operations.extensions import show_extension + try: + self.pkg_name = show_extension(self.module_name)["pkg_name"] + except CLIError: + pass + def parse_last_stable_major(self): import requests try: response = requests.get(CLI_EXTENSION_INDEX_URL) extension_data = response.json() - if self.module_name not in extension_data["extensions"]: + if self.pkg_name not in extension_data["extensions"]: return has_stable, max_stable_major = self.find_max_version( - extension_data["extensions"][self.module_name]) + extension_data["extensions"][self.pkg_name]) if has_stable: self.last_stable_major = max_stable_major else: diff --git a/azdev/params.py b/azdev/params.py index b34763d8..2a9d54b6 100644 --- a/azdev/params.py +++ b/azdev/params.py @@ -178,6 +178,9 @@ def load_arguments(self, _): c.argument('next_version_pre_tag', help='next version is stable or preview, if not provided, use current stable/preview tag') c.argument('next_version_segment_tag', help='used to modify actual major/minor/patch/pre, if provided, increment version as provided') + with ArgumentsContext(self, 'extension show') as c: + c.argument('mod_name', required=True, help='installed extension module name') + with ArgumentsContext(self, 'cli create') as c: c.positional('mod_name', help='Name of the module to create.')