def remove_extension(extensions):

    ext_paths = get_ext_repo_paths()
    installed_paths = find_files(ext_paths, '*.*-info')
    paths_to_remove = []
    names_to_remove = []
    if extensions == ['*']:
        paths_to_remove = [os.path.dirname(path) for path in installed_paths]
        names_to_remove = [
            os.path.basename(os.path.dirname(path)) for path in installed_paths
        ]
    else:
        for path in installed_paths:
            folder = os.path.dirname(path)
            long_name = os.path.basename(folder)
            if long_name in extensions:
                paths_to_remove.append(folder)
                names_to_remove.append(long_name)
                extensions.remove(long_name)
        # raise error if any extension not installed
        if extensions:
            raise CLIError('extension(s) not installed: {}'.format(
                ' '.join(extensions)))

    # removes any links that may have been added to site-packages.
    for ext in names_to_remove:
        pip_cmd('uninstall {} -y'.format(ext))

    for path in paths_to_remove:
        for d in os.listdir(path):
            # delete the egg-info and dist-info folders to make the extension invisible to the CLI and azdev
            if d.endswith('egg-info') or d.endswith('dist-info'):
                path_to_remove = os.path.join(path, d)
                display("Removing '{}'...".format(path_to_remove))
                shutil.rmtree(path_to_remove)
def create_extension(ext_name='test',
                     repo_name='azure-cli-extensions',
                     display_name=None,
                     display_name_plural=None,
                     required_sdk=None,
                     client_name=None,
                     operation_name=None,
                     sdk_property=None,
                     not_preview=False,
                     github_alias=None,
                     local_sdk=None):
    repo_path = None
    repo_paths = get_ext_repo_paths()
    repo_path = next((x for x in repo_paths if x.endswith(repo_name)), None)

    if not repo_path:
        raise CLIError(
            'Unable to find `{}` repo. Have you cloned it and added '
            'with `azdev extension repo add`?'.format(repo_name))

    _create_package(EXTENSION_PREFIX, os.path.join(repo_path, 'src'), True,
                    ext_name, display_name, display_name_plural, required_sdk,
                    client_name, operation_name, sdk_property, not_preview,
                    local_sdk)
    _add_to_codeowners(repo_path, EXTENSION_PREFIX, ext_name, github_alias)

    _display_success_message(EXTENSION_PREFIX + ext_name, ext_name)
def build_extensions(extensions, dist_dir='dist'):
    ext_paths = get_ext_repo_paths()
    all_extensions = find_files(ext_paths, 'setup.py')

    paths_to_build = []
    for path in all_extensions:
        folder = os.path.dirname(path)
        long_name = os.path.basename(folder)
        if long_name in extensions:
            paths_to_build.append(folder)
            extensions.remove(long_name)
    # raise error if any extension wasn't found
    if extensions:
        raise CLIError('extension(s) not found: {}'.format(
            ' '.join(extensions)))

    original_cwd = os.getcwd()
    dist_dir = os.path.join(original_cwd, dist_dir)
    for path in paths_to_build:
        os.chdir(path)
        command = 'setup.py bdist_wheel -b bdist -d {}'.format(dist_dir)
        result = py_cmd(command,
                        "Building extension '{}'...".format(path),
                        is_module=False)
        if result.error:
            raise result.error  # pylint: disable=raising-bad-type
    os.chdir(original_cwd)
def add_extension(extensions):

    ext_paths = get_ext_repo_paths()
    all_extensions = find_files(ext_paths, 'setup.py')

    if extensions == ['*']:
        paths_to_add = [
            os.path.dirname(path) for path in all_extensions
            if 'site-packages' not in path
        ]
    else:
        paths_to_add = []
        for path in all_extensions:
            folder = os.path.dirname(path)
            long_name = os.path.basename(folder)
            if long_name in extensions:
                paths_to_add.append(folder)
                extensions.remove(long_name)
        # raise error if any extension wasn't found
        if extensions:
            raise CLIError('extension(s) not found: {}'.format(
                ' '.join(extensions)))

    for path in paths_to_add:
        result = pip_cmd('install -e {}'.format(path),
                         "Adding extension '{}'...".format(path))
        if result.error:
            raise result.error  # pylint: disable=raising-bad-type
Exemple #5
0
def create_extension(ext_name,
                     azure_rest_api_specs=const.GITHUB_SWAGGER_REPO_URL,
                     branch=None,
                     use=None):
    if not azure_rest_api_specs.startswith('http') and branch:
        raise CLIError(
            'Cannot specify azure-rest-api-specs repo branch when using local one.'
        )
    if not branch:
        branch = json.load(
            request.urlopen(
                'https://api.github.com/repos/Azure/azure-rest-api-specs')
        ).get('default_branch')
    require_virtual_env()
    repo_paths = get_ext_repo_paths()
    repo_path = next(
        (x for x in repo_paths
         if x.endswith(const.EXT_REPO_NAME) or x.endswith(const.EXT_REPO_NAME +
                                                          '\\')), None)
    if not repo_path:
        raise CLIError(
            'Unable to find `{}` repo. Have you cloned it and added '
            'with `azdev extension repo add`?'.format(const.EXT_REPO_NAME))
    if not os.path.isdir(repo_path):
        raise CLIError("Invalid path {} in .azure config.".format(repo_path))
    swagger_readme_file_path = _get_swagger_readme_file_path(
        ext_name, azure_rest_api_specs, branch)
    _generate_extension(ext_name, repo_path, swagger_readme_file_path, use)
    _add_extension(ext_name, repo_path)

    _display_success_message(ext_name, ext_name)
Exemple #6
0
def update_extension_index(extension):
    import json
    import re
    import tempfile

    from .util import get_ext_metadata, get_whl_from_url

    ext_repos = get_ext_repo_paths()
    index_path = next((x for x in find_files(ext_repos, 'index.json') if 'azure-cli-extensions' in x), None)
    if not index_path:
        raise CLIError("Unable to find 'index.json' in your extension repos. Have "
                       "you cloned 'azure-cli-extensions' and added it to you repo "
                       "sources with `azdev extension repo add`?")

    NAME_REGEX = r'.*/([^/]*)-\d+.\d+.\d+'

    # Get extension WHL from URL
    if not extension.endswith('.whl') or not extension.startswith('https:'):
        raise ValueError('usage error: only URL to a WHL file currently supported.')

    # TODO: extend to consider other options
    ext_path = extension

    # Extract the extension name
    try:
        extension_name = re.findall(NAME_REGEX, ext_path)[0]
        extension_name = extension_name.replace('_', '-')
    except IndexError:
        raise ValueError('unable to parse extension name')

    extensions_dir = tempfile.mkdtemp()
    ext_dir = tempfile.mkdtemp(dir=extensions_dir)
    whl_cache_dir = tempfile.mkdtemp()
    whl_cache = {}
    ext_file = get_whl_from_url(ext_path, extension_name, whl_cache_dir, whl_cache)

    with open(index_path, 'r') as infile:
        curr_index = json.loads(infile.read())

    entry = {
        'downloadUrl': ext_path,
        'sha256Digest': _get_sha256sum(ext_file),
        'filename': ext_path.split('/')[-1],
        'metadata': get_ext_metadata(ext_dir, ext_file, extension_name)
    }

    if extension_name not in curr_index['extensions'].keys():
        logger.info("Adding '%s' to index...", extension_name)
        curr_index['extensions'][extension_name] = [entry]
    else:
        logger.info("Updating '%s' in index...", extension_name)
        curr_index['extensions'][extension_name].append(entry)

    # update index and write back to file
    with open(os.path.join(index_path), 'w') as outfile:
        outfile.write(json.dumps(curr_index, indent=4, sort_keys=True))
def check_license_headers():

    heading('Verify License Headers')

    cli_path = get_cli_repo_path()
    all_paths = [cli_path]
    try:
        ext_repo = get_ext_repo_paths()
        for path in ext_repo:
            all_paths.append(path)
    except CLIError:
        display("No CLI ext path, running check only on modules")

    files_without_header = []
    for path in all_paths:
        py_files = pathlib.Path(path).glob('**' + os.path.sep + '*.py')

        for py_file in py_files:
            py_file = str(py_file)

            if py_file.endswith('azure_bdist_wheel.py'):
                continue

            for ignore_token in _IGNORE_SUBDIRS:
                if ignore_token in py_file:
                    break
            else:
                with open(str(py_file), 'r', encoding='utf-8') as f:
                    file_text = f.read()

                    if not file_text:
                        continue

                    test_results = [
                        LICENSE_HEADER in file_text, WRAPPED_LICENSE_HEADER
                        in file_text, CODEGEN_LICENSE_HEADER in file_text
                    ]
                    if not any(test_results):
                        files_without_header.append(py_file)

    subheading('Results')
    if files_without_header:
        raise CLIError(
            "{}\nError: {} files don't have the required license headers.".
            format('\n'.join(files_without_header), len(files_without_header)))
    display('License headers verified OK.')