<commit_before># --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

import re
from os.path import isdir

from knack.util import CLIError
from knack.log import get_logger

from ._utils import user_confirmation
from ._docker_utils import get_access_credentials, request_data_from_registry


logger = get_logger(__name__)


# https://github.com/kubernetes/helm/blob/b6660cd5c9a9ee35ff24034fcc223b7eaeed3ee2/pkg/tiller/release_server.go#L80
VALID_NAME = '^(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])+(.tgz)(.prov)?$'


def acr_helm_list(cmd,
                  registry_name,
                  repository='repo',
                  resource_group_name=None,
                  username=None,
                  password=<PASSWORD>):
    login_server, username, password = get_access_credentials(
        cli_ctx=cmd.cli_ctx,
        registry_name=registry_name,
        resource_group_name=resource_group_name,
        username=username,
        password=password,
        artifact_repository=repository,
        permission='pull')

    return request_data_from_registry(
        http_method='get',
        login_server=login_server,
        path=_get_charts_path(repository),
        username=username,
        password=password)[0]


def acr_helm_show(cmd,
                  registry_name,
                  chart,
                  version=None,
                  repository='repo',
                  resource_group_name=None,
                  username=None,
                  password=<PASSWORD>):
    login_server, username, password = get_access_credentials(
        cli_ctx=cmd.cli_ctx,
        registry_name=registry_name,
        resource_group_name=resource_group_name,
        username=username,
        password=password,
        artifact_repository=repository,
        permission='pull')

    return request_data_from_registry(
        http_method='get',
        login_server=login_server,
        path=_get_charts_path(repository, chart, version),
        username=username,
        password=password)[0]


def acr_helm_delete(cmd,
                    registry_name,
                    chart,
                    version=None,
                    repository='repo',
                    resource_group_name=None,
                    username=None,
                    password=<PASSWORD>,
                    prov=False,
                    yes=False):
    if version:
        message = "This operation will delete the chart package '{}'".format(
            _get_chart_package_name(chart, version, prov))
    else:
        message = "This operation will delete all versions of the chart '{}'".format(chart)
    user_confirmation("{}.\nAre you sure you want to continue?".format(message), yes)

    login_server, username, password = get_access_credentials(
        cli_ctx=cmd.cli_ctx,
        registry_name=registry_name,
        resource_group_name=resource_group_name,
        username=username,
        password=password,
        artifact_repository=repository,
        permission='*')

    return request_data_from_registry(
        http_method='delete',
        login_server=login_server,
        path=_get_blobs_path(repository, chart, version, prov) if version else _get_charts_path(repository, chart),
        username=username,
        password=password)[0]


def acr_helm_push(cmd,
                  registry_name,
                  chart_package,
                  repository='repo',
                  force=False,
                  resource_group_name=None,
                  username=None,
                  password=<PASSWORD>):
    if isdir(chart_package):
        raise CLIError("Please run 'helm package {}' to generate a chart package first.".format(chart_package))

    if not re.match(VALID_NAME, chart_package):
        raise CLIError("Invalid helm package name '{}'. Is it a '*.tgz' or '*.tgz.prov' file?".format(chart_package))

    login_server, username, password = get_access_credentials(
        cli_ctx=cmd.cli_ctx,
        registry_name=registry_name,
        resource_group_name=resource_group_name,
        username=username,
        password=password,
        artifact_repository=repository,
        permission='*')

    try:
        with open(chart_package, 'rb') as input_file:
            return request_data_from_registry(
                http_method='patch' if force else 'put',
                login_server=login_server,
                path=_get_blobs_path(repository, chart_package),
                username=username,
                password=password,
                data_payload=input_file,
                retry_times=1)[0]
    except OSError as e:
        raise CLIError(e)


def acr_helm_repo_add(cmd,
                      registry_name,
                      repository='repo',
                      resource_group_name=None,
                      username=None,<commit_msg>[ACR] Support pushing helm packages with path reference (#7358)<commit_after># --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

import re
from os.path import isdir, basename

from knack.util import CLIError
from knack.log import get_logger

from ._utils import user_confirmation
from ._docker_utils import get_access_credentials, request_data_from_registry


logger = get_logger(__name__)


# https://github.com/kubernetes/helm/blob/b6660cd5c9a9ee35ff24034fcc223b7eaeed3ee2/pkg/tiller/release_server.go#L80
VALID_NAME = '^(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])+(.tgz)(.prov)?$'


def acr_helm_list(cmd,
                  registry_name,
                  repository='repo',
                  resource_group_name=None,
                  username=None,
                  password=<PASSWORD>):
    login_server, username, password = get_access_credentials(
        cli_ctx=cmd.cli_ctx,
        registry_name=registry_name,
        resource_group_name=resource_group_name,
        username=username,
        password=password,
        artifact_repository=repository,
        permission='pull')

    return request_data_from_registry(
        http_method='get',
        login_server=login_server,
        path=_get_charts_path(repository),
        username=username,
        password=password)[0]


def acr_helm_show(cmd,
                  registry_name,
                  chart,
                  version=None,
                  repository='repo',
                  resource_group_name=None,
                  username=None,
                  password=<PASSWORD>):
    login_server, username, password = get_access_credentials(
        cli_ctx=cmd.cli_ctx,
        registry_name=registry_name,
        resource_group_name=resource_group_name,
        username=username,
        password=password,
        artifact_repository=repository,
        permission='pull')

    return request_data_from_registry(
        http_method='get',
        login_server=login_server,
        path=_get_charts_path(repository, chart, version),
        username=username,
        password=password)[0]


def acr_helm_delete(cmd,
                    registry_name,
                    chart,
                    version=None,
                    repository='repo',
                    resource_group_name=None,
                    username=None,
                    password=<PASSWORD>,
                    prov=False,
                    yes=False):
    if version:
        message = "This operation will delete the chart package '{}'".format(
            _get_chart_package_name(chart, version, prov))
    else:
        message = "This operation will delete all versions of the chart '{}'".format(chart)
    user_confirmation("{}.\nAre you sure you want to continue?".format(message), yes)

    login_server, username, password = get_access_credentials(
        cli_ctx=cmd.cli_ctx,
        registry_name=registry_name,
        resource_group_name=resource_group_name,
        username=username,
        password=password,
        artifact_repository=repository,
        permission='*')

    return request_data_from_registry(
        http_method='delete',
        login_server=login_server,
        path=_get_blobs_path(repository, chart, version, prov) if version else _get_charts_path(repository, chart),
        username=username,
        password=password)[0]


def acr_helm_push(cmd,
                  registry_name,
                  chart_package,
                  repository='repo',
                  force=False,
                  resource_group_name=None,
                  username=None,
                  password=<PASSWORD>):
    if isdir(chart_package):
        raise CLIError("Please run 'helm package {}' to generate a chart package first.".format(chart_package))

    chart_name = basename(chart_package)
    if not re.match(VALID_NAME, chart_name):
        raise CLIError("Invalid helm package name '{}'. Is it a '*.tgz' or '*.tgz.prov' file?".format(chart_package))

    login_server, username, password = get_access_credentials(
        cli_ctx=cmd.cli_ctx,
        registry_name=registry_name,
        resource_group_name=resource_group_name,
        username=username,
        password=password,
        artifact_repository=repository,
        permission='*')

    try:
        with open(chart_package, 'rb') as input_file:
            return request_data_from_registry(
                http_method='patch' if force else 'put',
                login_server=login_server,
                path=_get_blobs_path(repository, chart_name),
                username=username,
                password=password,
                data_payload=input_file,
                retry_times=1)[0]
    except OSError as e:
        raise CLIError(e)


def acr_helm_repo_add(cmd,
                      registry_name,
                      repository='repo',
                      resource_group_name=None,
                      username=None,
{'fraction_non_alphanumeric': 0.11785748815624093, 'fraction_numerical': 0.006767862322343614, 'mean_word_length': 1.9852813852813853, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5630266', 'n_tokens_mistral': 2839, 'n_tokens_neox': 2666, 'n_words': 522}