def setup_pack_virtualenv(
    pack_name,
    update=False,
    logger=None,
    include_pip=True,
    include_setuptools=True,
    include_wheel=True,
    proxy_config=None,
    no_download=True,
    force_owner_group=True,
    inject_parent_virtualenv_sites=True,
):

    """
    Setup virtual environment for the provided pack.

    :param pack_name: Name of the pack to setup the virtualenv for.
    :type pack_name: ``str``

    :param update: True to update dependencies inside the virtual environment.
    :type update: ``bool``

    :param logger: Optional logger instance to use. If not provided it defaults to the module
                   level logger.

    :param no_download: Do not download and install latest version of pre-installed packages such
                        as pip and setuptools.
    :type no_download: ``bool``
    """
    logger = logger or LOG

    if not re.match(PACK_REF_WHITELIST_REGEX, pack_name):
        raise ValueError('Invalid pack name "%s"' % (pack_name))

    if pack_name in RESERVED_PACK_LIST:
        raise ValueError(
            f"Pack name {pack_name} is a reserved name, and cannot be used"
        )

    base_virtualenvs_path = os.path.join(cfg.CONF.system.base_path, "virtualenvs/")
    virtualenv_path = os.path.join(base_virtualenvs_path, quote_unix(pack_name))

    # Ensure pack directory exists in one of the search paths
    pack_path = get_pack_directory(pack_name=pack_name)

    logger.debug('Setting up virtualenv for pack "%s" (%s)' % (pack_name, pack_path))

    if not pack_path:
        packs_base_paths = get_packs_base_paths()
        search_paths = ", ".join(packs_base_paths)
        msg = 'Pack "%s" is not installed. Looked in: %s' % (pack_name, search_paths)
        raise Exception(msg)

    # 1. Create virtualenv if it doesn't exist
    if not update or not os.path.exists(virtualenv_path):
        # 0. Delete virtual environment if it exists
        remove_virtualenv(virtualenv_path=virtualenv_path, logger=logger)

        # 1. Create virtual environment
        logger.debug(
            'Creating virtualenv for pack "%s" in "%s"' % (pack_name, virtualenv_path)
        )
        create_virtualenv(
            virtualenv_path=virtualenv_path,
            logger=logger,
            include_pip=include_pip,
            include_setuptools=include_setuptools,
            include_wheel=include_wheel,
            no_download=no_download,
        )

    # 2. Inject the st2 site-packages dir to enable .pth file loading
    if inject_parent_virtualenv_sites:
        logger.debug("Injecting st2 venv site-packages via .pth file")
        inject_st2_pth_into_virtualenv(
            virtualenv_path=virtualenv_path,
            logger=logger,
        )

    # 3. Install base requirements which are common to all the packs
    logger.debug("Installing base requirements")
    for requirement in BASE_PACK_REQUIREMENTS:
        install_requirement(
            virtualenv_path=virtualenv_path,
            requirement=requirement,
            proxy_config=proxy_config,
            logger=logger,
        )

    # 4. Install pack-specific requirements
    requirements_file_path = os.path.join(pack_path, "requirements.txt")
    has_requirements = os.path.isfile(requirements_file_path)

    if has_requirements:
        logger.debug(
            'Installing pack specific requirements from "%s"' % (requirements_file_path)
        )
        install_requirements(
            virtualenv_path=virtualenv_path,
            requirements_file_path=requirements_file_path,
            proxy_config=proxy_config,
            logger=logger,
        )
    else:
        logger.debug("No pack specific requirements found")

    # 5. Set the owner group
    if force_owner_group:
        apply_pack_owner_group(pack_path=virtualenv_path)

    action = "updated" if update else "created"
    logger.debug(
        'Virtualenv for pack "%s" successfully %s in "%s"'
        % (pack_name, action, virtualenv_path)
    )

    Setup virtual environment for the provided pack.

    :param pack_name: Name of the pack to setup the virtualenv for.
    :type pack_name: ``str``

    :param update: True to update dependencies inside the virtual environment.
    :type update: ``bool``

    :param logger: Optional logger instance to use. If not provided it defaults to the module
                   level logger.

    :param no_download: Do not download and install latest version of pre-installed packages such
                        as pip and setuptools.
    :type no_download: ``bool``
    
st2common/st2common/util/virtualenvs.py