Skip to content

Commit

Permalink
add extension show (#456)
Browse files Browse the repository at this point in the history
* add extension show
* fix extension index name
  • Loading branch information
AllyW committed Jul 3, 2024
1 parent 8f5eb98 commit 78af72c
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 3 deletions.
5 changes: 5 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion azdev/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
# license information.
# -----------------------------------------------------------------------------

__VERSION__ = '0.1.70'
__VERSION__ = '0.1.71'
1 change: 1 addition & 0 deletions azdev/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
3 changes: 3 additions & 0 deletions azdev/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
30 changes: 30 additions & 0 deletions azdev/operations/extensions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
13 changes: 13 additions & 0 deletions azdev/operations/extensions/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 12 additions & 2 deletions azdev/operations/extensions/version_upgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand Down
3 changes: 3 additions & 0 deletions azdev/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.')

Expand Down

0 comments on commit 78af72c

Please sign in to comment.