Exemplo n.º 1
0
def install_extensions(venv_path, extensions):
    activate_path = os.path.join(
        venv_path, 'Scripts',
        'activate') if const.IS_WINDOWS else 'source ' + os.path.join(
            venv_path, const.UN_BIN, const.UN_ACTIVATE)
    delimiter = ' && ' if const.IS_WINDOWS else '; '
    executable = None if const.IS_WINDOWS else '/bin/bash'
    all_ext = azdev.operations.extensions.list_extensions()
    if extensions == ['*']:
        display("\nInstalling all extensions")
        for i in all_ext:
            shell_cmd(activate_path + delimiter + const.PIP_E_CMD + i['path'],
                      executable=executable)
        extensions = False
    else:
        display("\nInstalling the following extensions: " + str(extensions))
        extensions = set(extensions)
    k = 0
    while k < len(all_ext) and extensions:
        if all_ext[k]['name'] in extensions:
            shell_cmd(activate_path + delimiter + const.PIP_E_CMD +
                      all_ext[k]['path'],
                      executable=executable)
            extensions.remove(all_ext[k]['name'])
        k += 1
    if extensions:
        raise CLIError(
            "The following extensions were not found. Ensure you have added "
            "the repo using `--repo/-r PATH`.\n    {}".format(
                '\n    '.join(extensions)))
Exemplo n.º 2
0
def _generate_extension(ext_name, repo_path, swagger_readme_file_path, use):
    heading('Start generating extension {}.'.format(ext_name))
    # check if npm is installed
    try:
        shell_cmd('npm --version', stdout=subprocess.DEVNULL, raise_ex=False)
    except CLIError as ex:
        raise CLIError('{}\nPlease install npm.'.format(ex))
    display('Installing autorest...\n')
    if const.IS_WINDOWS:
        try:
            shell_cmd('npm install -g autorest', raise_ex=False)
        except CLIError as ex:
            raise CLIError("Failed to install autorest.\n{}".format(ex))
    else:
        try:
            shell_cmd('npm install -g autorest',
                      stderr=subprocess.DEVNULL,
                      raise_ex=False)
        except CLIError as ex:
            path = os.environ['PATH']
            # check if npm is installed through nvm
            if os.environ.get('NVM_DIR'):
                raise ex
            # check if user using specific node version and manually add it to the os env PATH
            node_version = shell_cmd('node --version',
                                     capture_output=True).result
            if 'node/' + node_version + '/bin' in path:
                raise ex
            # create a new directory for npm global installations, to avoid using sudo in installing autorest
            npm_path = os.path.join(os.environ['HOME'], '.npm-packages')
            if not os.path.isdir(npm_path):
                os.mkdir(npm_path)
            npm_prefix = shell_cmd('npm prefix -g', capture_output=True).result
            shell_cmd('npm config set prefix ' + npm_path)
            os.environ['PATH'] = path + ':' + os.path.join(npm_path, 'bin')
            os.environ['MANPATH'] = os.path.join(npm_path, 'share', 'man')
            shell_cmd('npm install -g autorest')
            shell_cmd('npm config set prefix ' + npm_prefix)
    # update autorest core
    shell_cmd('autorest --latest')
    if not use:
        cmd = 'autorest --az --azure-cli-extension-folder={} {}'.format(
            repo_path, swagger_readme_file_path)
    else:
        cmd = 'autorest --az --azure-cli-extension-folder={} {} --use={}'.format(
            repo_path, swagger_readme_file_path, use)
    shell_cmd(cmd, message=True)
Exemplo n.º 3
0
def install_cli(cli_path, venv_path):
    src_path = os.path.join(cli_path, 'src')
    activate_path = (os.path.join(venv_path, 'Scripts', 'activate')
                     if const.IS_WINDOWS else 'source ' +
                     os.path.join(venv_path, const.UN_BIN, const.UN_ACTIVATE))
    delimiter = ' && ' if const.IS_WINDOWS else '; '
    executable = None if const.IS_WINDOWS else '/bin/bash'
    display("\nvenv activate path is " + str(activate_path))
    shell_cmd(activate_path + delimiter +
              'pip install --ignore-installed azure-common',
              stdout=subprocess.DEVNULL,
              stderr=subprocess.DEVNULL,
              raise_ex=False,
              executable=executable)
    display("\nInstalling telemetry ")
    shell_cmd(activate_path + delimiter + const.PIP_E_CMD +
              os.path.join(src_path, 'azure-cli-telemetry'),
              stdout=subprocess.DEVNULL,
              raise_ex=False,
              stderr=subprocess.DEVNULL,
              executable=executable)
    display("\nInstalling core ")
    shell_cmd(activate_path + delimiter + const.PIP_E_CMD +
              os.path.join(src_path, 'azure-cli-core'),
              stdout=subprocess.DEVNULL,
              raise_ex=False,
              stderr=subprocess.DEVNULL,
              executable=executable)
    shell_cmd(activate_path + delimiter + const.PIP_E_CMD +
              os.path.join(src_path, 'azure-cli-testsdk'),
              stdout=subprocess.DEVNULL,
              stderr=subprocess.DEVNULL,
              raise_ex=False,
              executable=executable)
    display("\nInstalling cli ")
    shell_cmd(activate_path + delimiter + const.PIP_E_CMD +
              os.path.join(src_path, 'azure-cli'),
              raise_ex=False,
              executable=executable)
    req_file = 'requirements.py3.{}.txt'.format(
        platform.system().lower() if const.IS_WINDOWS else platform.system())
    req_file = "{}/src/azure-cli/{}".format(cli_path, req_file)
    display("Installing " + req_file)
    shell_cmd(activate_path + delimiter + const.PIP_R_CMD + req_file,
              raise_ex=False,
              executable=executable)
Exemplo n.º 4
0
def setup(cli_path=None,
          ext_repo_path=None,
          ext=None,
          deps=None,
          set_env=None,
          copy=None,
          use_global=None):
    _check_env(set_env)

    _check_shell()

    heading('Azure CLI Dev Setup')

    # cases for handling legacy install
    if not any([cli_path, ext_repo_path]) or cli_path == "pypi":
        display(
            "WARNING: Installing azdev in legacy mode. Run with atleast -c "
            "to install the latest azdev wihout \"pypi\"\n")
        return _handle_legacy(cli_path, ext_repo_path, ext, deps, time.time())
    if 'CONDA_PREFIX' in os.environ:
        raise CLIError('CONDA virutal enviroments are not supported outside'
                       ' of interactive mode or when -c and -r are provided')

    if not cli_path:
        cli_path = _handle_no_cli_path()

    _validate_input(cli_path, ext_repo_path, set_env, copy, use_global, ext)
    _check_paths(cli_path, ext_repo_path)

    if set_env:
        shell_cmd((const.VENV_CMD if const.IS_WINDOWS else const.VENV_CMD3) +
                  set_env,
                  raise_ex=False)
        azure_path = os.path.join(os.path.abspath(os.getcwd()), set_env)
    else:
        azure_path = os.environ.get('VIRTUAL_ENV')

    dot_azure_config = os.path.join(azure_path, '.azure')
    dot_azdev_config = os.path.join(azure_path, '.azdev')

    # clean up venv dirs if they already existed
    # and this is a reinstall/new setup
    if os.path.isdir(dot_azure_config):
        shutil.rmtree(dot_azure_config)
    if os.path.isdir(dot_azdev_config):
        shutil.rmtree(dot_azdev_config)

    global_az_config = os.path.expanduser(os.path.join('~', '.azure'))
    global_azdev_config = os.path.expanduser(os.path.join('~', '.azdev'))
    azure_config_path = os.path.join(dot_azure_config, const.CONFIG_NAME)
    azdev_config_path = os.path.join(dot_azdev_config, const.CONFIG_NAME)

    if os.path.isdir(global_az_config) and copy:
        shutil.copytree(global_az_config, dot_azure_config)
        if os.path.isdir(global_azdev_config):
            shutil.copytree(global_azdev_config, dot_azdev_config)
        else:
            os.mkdir(dot_azdev_config)
            file = open(azdev_config_path, "w")
            file.close()
    elif not use_global and not copy:
        os.mkdir(dot_azure_config)
        os.mkdir(dot_azdev_config)
        file_az, file_dev = open(azure_config_path,
                                 "w"), open(azdev_config_path, "w")
        file_az.close()
        file_dev.close()
    elif os.path.isdir(global_az_config):
        dot_azure_config, dot_azdev_config = global_az_config, global_azdev_config
        azure_config_path = os.path.join(dot_azure_config, const.CONFIG_NAME)
    else:
        raise CLIError(
            "Global AZ config is not set up, yet it was specified to be used.")

    # set env vars for get azure config and get azdev config
    os.environ['AZURE_CONFIG_DIR'], os.environ[
        'AZDEV_CONFIG_DIR'] = dot_azure_config, dot_azdev_config
    config = get_azure_config()
    if not config.get('cloud', 'name', None):
        config.set_value('cloud', 'name', 'AzureCloud')
    if ext_repo_path:
        config.set_value(const.EXT_SECTION, const.AZ_DEV_SRC,
                         os.path.abspath(ext_repo_path))
    venv.edit_activate(azure_path, dot_azure_config, dot_azdev_config)
    if cli_path:
        config.set_value('clipath', const.AZ_DEV_SRC,
                         os.path.abspath(cli_path))
        venv.install_cli(os.path.abspath(cli_path), azure_path)
    config = get_azdev_config()
    config.set_value(
        'ext', 'repo_paths',
        os.path.abspath(ext_repo_path) if ext_repo_path else '_NONE_')
    config.set_value('cli', 'repo_path', os.path.abspath(cli_path))
    _copy_config_files()
    if ext and ext_repo_path:
        venv.install_extensions(azure_path, ext)

    if not set_env:
        heading(
            "The setup was successful! Please run or re-run the virtual environment activation script."
        )
    else:
        heading("The setup was successful!")
    return None