Protection of pip.exe from modification on Windows

    On Windows, any operation modifying pip should be run as:
        python -m pip ...
485379
def protect_pip_from_modification_on_windows(modifying_pip):
    
    pip_names = [
        "pip.exe",
        "pip{}.exe".format(sys.version_info[0]),
        "pip{}.{}.exe".format(*sys.version_info[:2])
    ]

    # See https://github.com/pypa/pip/issues/1299 for more discussion
    should_show_use_python_msg = (
        modifying_pip and
        WINDOWS and
        os.path.basename(sys.argv[0]) in pip_names
    )

    if should_show_use_python_msg:
        new_command = [
            sys.executable, "-m", "pip"
        ] + sys.argv[1:]
        raise CommandError(
            'To modify pip, please run the following command:\n{}'
            .format(" ".join(new_command))
        )
525239
def download_pip(env, requirements):
    """"""
    if config.installation_cache_folder() is None:
        raise EnvironmentSetupError("Local installation cache folder not "
            "defined but required for downloading a pip installation.")
    # Installation cache folder needs to be explicitly created for setuptools
    # to be able to copy its downloaded installation files into it. Seen using
    # Python 2.4.4 & setuptools 1.4.
    _create_installation_cache_folder_if_needed()
    try:
        env.execute(["-m", "easy_install", "--zip-ok", "--multi-version",
            "--always-copy", "--exclude-scripts", "--install-dir",
            config.installation_cache_folder()] + requirements)
        zip_eggs_in_folder(config.installation_cache_folder())
    except (KeyboardInterrupt, SystemExit):
        raise
    except Exception:
        raise EnvironmentSetupError("pip download failed.")
886559
def win_verify_env(
        path,
        dirs,
        permissive=False,
        pki_dir='',
        skip_extra=False):
    '''
    
    '''
    import salt.utils.win_functions
    import salt.utils.win_dacl
    import salt.utils.path

    # Make sure the file_roots is not set to something unsafe since permissions
    # on that directory are reset

    # `salt.utils.path.safe_path` will consider anything inside `C:\Windows` to
    # be unsafe. In some instances the test suite uses
    # `C:\Windows\Temp\salt-tests-tmpdir\rootdir` as the file_roots. So, we need
    # to consider anything in `C:\Windows\Temp` to be safe
    system_root = os.environ.get('SystemRoot', r'C:\Windows')
    allow_path = '\\'.join([system_root, 'TEMP'])
    if not salt.utils.path.safe_path(path=path, allow_path=allow_path):
        raise CommandExecutionError(
            '`file_roots` set to a possibly unsafe location: {0}'.format(path)
        )

    # Create the root path directory if missing
    if not os.path.isdir(path):
        os.makedirs(path)

    # Set permissions to the root path directory
    current_user = salt.utils.win_functions.get_current_user()
    if salt.utils.win_functions.is_admin(current_user):
        try:
            # Make the Administrators group owner
            # Use the SID to be locale agnostic
            salt.utils.win_dacl.set_owner(path, 'S-1-5-32-544')

        except CommandExecutionError:
            msg = 'Unable to securely set the owner of "{0}".'.format(path)
            if is_console_configured():
                log.critical(msg)
            else:
                sys.stderr.write("CRITICAL: {0}\n".format(msg))

        if not permissive:
            try:
                # Get a clean dacl by not passing an obj_name
                dacl = salt.utils.win_dacl.dacl()

                # Add aces to the dacl, use the GUID (locale non-specific)
                # Administrators Group
                dacl.add_ace('S-1-5-32-544', 'grant', 'full_control',
                             'this_folder_subfolders_files')
                # System
                dacl.add_ace('S-1-5-18', 'grant', 'full_control',
                             'this_folder_subfolders_files')
                # Owner
                dacl.add_ace('S-1-3-4', 'grant', 'full_control',
                             'this_folder_subfolders_files')

                # Save the dacl to the object
                dacl.save(path, True)

            except CommandExecutionError:
                msg = 'Unable to securely set the permissions of ' \
                      '"{0}".'.format(path)
                if is_console_configured():
                    log.critical(msg)
                else:
                    sys.stderr.write("CRITICAL: {0}\n".format(msg))

    # Create the directories
    for dir_ in dirs:
        if not dir_:
            continue
        if not os.path.isdir(dir_):
            try:
                os.makedirs(dir_)
            except OSError as err:
                msg = 'Failed to create directory path "{0}" - {1}\n'
                sys.stderr.write(msg.format(dir_, err))
                sys.exit(err.errno)

        # The PKI dir gets its own permissions
        if dir_ == pki_dir:
            try:
                # Make Administrators group the owner
                salt.utils.win_dacl.set_owner(path, 'S-1-5-32-544')

                # Give Admins, System and Owner permissions
                # Get a clean dacl by not passing an obj_name
                dacl = salt.utils.win_dacl.dacl()

                # Add aces to the dacl, use the GUID (locale non-specific)
                # Administrators Group
                dacl.add_ace('S-1-5-32-544', 'grant', 'full_control',
                             'this_folder_subfolders_files')
                # System
                dacl.add_ace('S-1-5-18', 'grant', 'full_control',
                             'this_folder_subfolders_files')
                # Owner
                dacl.add_ace('S-1-3-4', 'grant', 'full_control',
                             'this_folder_subfolders_files')

                # Save the dacl to the object
                dacl.save(dir_, True)

            except CommandExecutionError:
                msg = 'Unable to securely set the permissions of "{0}".'
                msg = msg.format(dir_)
                if is_console_configured():
                    log.critical(msg)
                else:
                    sys.stderr.write("CRITICAL: {0}\n".format(msg))

    if skip_extra is False:
        # Run the extra verification checks
        zmq_version()
1141670
def patch_modules():
    
    old_call = subprocess.call

    def dummy_call(*args, **kwargs):
        with tempfile.TemporaryFile("wb+") as f:
            kwargs["stdout"] = f
            kwargs["stderr"] = f
            rv = subprocess.Popen(*args, **kwargs).wait()
            f.seek(0)
            click.echo(f.read().decode("utf-8", "replace").rstrip())
        return rv

    subprocess.call = dummy_call

    try:
        yield
    finally:
        subprocess.call = old_call
536272
def sync(to_install, to_uninstall, verbose=False, dry_run=False, install_flags=None):
    
    if not to_uninstall and not to_install:
        click.echo("Everything up-to-date")

    pip_flags = []
    if not verbose:
        pip_flags += ['-q']

    if to_uninstall:
        if dry_run:
            click.echo("Would uninstall:")
            for pkg in to_uninstall:
                click.echo("  {}".format(pkg))
        else:
            check_call([sys.executable, '-m', 'pip', 'uninstall', '-y'] + pip_flags + sorted(to_uninstall))

    if to_install:
        if install_flags is None:
            install_flags = []
        if dry_run:
            click.echo("Would install:")
            for ireq in to_install:
                click.echo("  {}".format(format_requirement(ireq)))
        else:
            # prepare requirement lines
            req_lines = []
            for ireq in sorted(to_install, key=key_from_ireq):
                ireq_hashes = get_hashes_from_ireq(ireq)
                req_lines.append(format_requirement(ireq, hashes=ireq_hashes))

            # save requirement lines to a temporary file
            tmp_req_file = tempfile.NamedTemporaryFile(mode='wt', delete=False)
            tmp_req_file.write('\n'.join(req_lines))
            tmp_req_file.close()

            try:
                check_call(
                    [sys.executable, '-m', 'pip', 'install', '-r', tmp_req_file.name] + pip_flags + install_flags
                )
            finally:
                os.unlink(tmp_req_file.name)

    return 0
431873
def is_installable_file(path):
    # type: (PipfileType) -> bool
    """"""
    from packaging import specifiers

    if isinstance(path, Mapping):
        path = convert_entry_to_path(path)

    # If the string starts with a valid specifier operator, test if it is a valid
    # specifier set before making a path object (to avoid breaking windows)
    if any(path.startswith(spec) for spec in "!=<>~"):
        try:
            specifiers.SpecifierSet(path)
        # If this is not a valid specifier, just move on and try it as a path
        except specifiers.InvalidSpecifier:
            pass
        else:
            return False

    parsed = urlparse(path)
    is_local = (
        not parsed.scheme
        or parsed.scheme == "file"
        or (len(parsed.scheme) == 1 and os.name == "nt")
    )
    if parsed.scheme and parsed.scheme == "file":
        path = vistir.compat.fs_decode(vistir.path.url_to_path(path))
    normalized_path = vistir.path.normalize_path(path)
    if is_local and not os.path.exists(normalized_path):
        return False

    is_archive = pip_shims.shims.is_archive_file(normalized_path)
    is_local_project = os.path.isdir(normalized_path) and is_installable_dir(
        normalized_path
    )
    if is_local and is_local_project or is_archive:
        return True

    if not is_local and pip_shims.shims.is_archive_file(parsed.path):
        return True

    return False
329226
def mktmpenv_cmd(argv):
    """"""
    parser = mkvirtualenv_argparser()
    env = '.'
    while (workon_home / env).exists():
        env = hex(random.getrandbits(64))[2:-1]

    args, rest = parser.parse_known_args(argv)

    mkvirtualenv(env, args.python, args.packages, requirements=args.requirements,
                 rest=rest)
    print('This is a temporary environment. It will be deleted when you exit')
    try:
        if args.activate:
            # only used for testing on windows
            shell(env)
    finally:
        return rmvirtualenvs([env])
868239
def configure_processes(agent_metadata_map, logger):
    

    if not optional_packages_installed:
        logger.warning("\n#### WARNING ####\n"
                       "You are missing some optional packages which will become mandatory in the future!\n"
                       "Please run `pip install -r requirements.txt` to enjoy optimal functionality "
                       "and future-proof yourself!\n")

    if not optional_packages_installed:
        return

    team_pids_map = {}

    for player_index, data in agent_metadata_map.items():
        team = data.team
        if team not in team_pids_map:
            team_pids_map[team] = set()
        team_pids_map[team].update(data.pids)

    shared_pids = set()
    cpu_count = psutil.cpu_count()
    cpus_per_team = cpu_count // 3

    if len(team_pids_map) >= 2 and cpus_per_team > 0:
        # Sort into three sets of pids: team 0 exclusives, team 1 exclusives, and shared pids
        # All pids will be assigned high priority
        # Team exclusive pids will be bound to a subset of cpus so they can't adversely affect the opposite team.

        for team, team_set in team_pids_map.items():
            if not shared_pids:
                shared_pids.update(team_set)
            else:
                shared_pids.intersection_update(team_set)

        for team, team_set in team_pids_map.items():
            team_set -= shared_pids

        for team, team_pids in team_pids_map.items():
            team_cpu_offset = cpus_per_team * team
            team_cpus = list(range(cpu_count - cpus_per_team - team_cpu_offset, cpu_count - team_cpu_offset))
            for pid in team_pids:
                p = psutil.Process(pid)
                p.cpu_affinity(team_cpus)  # Restrict the process to run on the cpus assigned to the team
                p.nice(psutil.HIGH_PRIORITY_CLASS)  # Allow the process to run at high priority
    else:
        # Consider everything a shared pid, because we are not in a position to split up cpus.
        for team, team_set in team_pids_map.items():
            shared_pids.update(team_set)

    for pid in shared_pids:
        p = psutil.Process(pid)  # Allow the process to run at high priority
        p.nice(psutil.HIGH_PRIORITY_CLASS)
1319263
def save_pip(self, out_dir):
        """"""
        try:
            import pkg_resources

            installed_packages = [d for d in iter(pkg_resources.working_set)]
            installed_packages_list = sorted(
                ["%s==%s" % (i.key, i.version) for i in installed_packages]
            )
            with open(os.path.join(out_dir, 'requirements.txt'), 'w') as f:
                f.write("\n".join(installed_packages_list))
        except Exception as e:
            logger.error("Error saving pip packages")
133678
def _is_master_running(self):
        '''
        
        '''
        # Windows doesn't have IPC. Assume the master is running.
        # At worse, it will error 500.
        if salt.utils.platform.is_windows():
            return True

        if self.opts['transport'] == 'tcp':
            ipc_file = 'publish_pull.ipc'
        else:
            ipc_file = 'workers.ipc'
        return os.path.exists(os.path.join(
            self.opts['sock_dir'],
            ipc_file))
612259
def enabled_actions_for_env(env):
    """"""
    def enabled(config_value, required):
        if config_value is Config.TriBool.No:
            return False
        if config_value is Config.TriBool.Yes:
            return True
        assert config_value is Config.TriBool.IfNeeded
        return bool(required)

    # Some old Python versions do not support HTTPS downloads and therefore can
    # not download installation packages from PyPI. To run setuptools or pip
    # based installations on such Python versions, all the required
    # installation packages need to be downloaded locally first using a
    # compatible Python version (e.g. Python 2.4.4 for Python 2.4.3) and then
    # installed locally.
    download_supported = not ((2, 4, 3) <= env.sys_version_info < (2, 4, 4))

    local_install = config.installation_cache_folder() is not None

    actions = set()

    pip_required = False
    run_pip_based_installations = enabled(config.install_environments, True)
    if run_pip_based_installations:
        actions.add("run pip based installations")
        pip_required = True
    if download_supported and enabled(config.download_installations,
            local_install and run_pip_based_installations):
        actions.add("download pip based installations")
        pip_required = True

    setuptools_required = False
    run_pip_installation = enabled(config.install_environments, pip_required)
    if run_pip_installation:
        actions.add("run pip installation")
        setuptools_required = True
    if download_supported and enabled(config.download_installations,
            local_install and run_pip_installation):
        actions.add("download pip installation")
        setuptools_required = True

    if enabled(config.setup_setuptools, setuptools_required):
        actions.add("setup setuptools")

    return actions
712873
def remove_armstrong():
    """"""

    from pip.util import get_installed_distributions
    pkgs = get_installed_distributions(local_only=True, include_editables=True)
    apps = [pkg for pkg in pkgs
            if pkg.key.startswith('armstrong') and pkg.key != 'armstrong.dev']

    for app in apps:
        run("pip uninstall -y %s" % app.key)

    if apps:
        print(
            "Note: this hasn't removed other dependencies installed by "
            "these components. There's no substitute for a fresh virtualenv.")
    else:
        print("No Armstrong components to remove.")
913780
def _run_with_different_python(executable):
    
    args = [arg for arg in sys.argv if arg != VIRTUALENV_OPTION]
    args.insert(0, executable)
    print("Running bootstrap.py with {0}".format(executable))
    exit(subprocess.call(args))
696817
def pip_install(self, reqs):
        """"""
        if not reqs:
            return
        log.info('Calling pip to install %s', reqs)
        check_call([
            sys.executable, '-m', 'pip', 'install', '--ignore-installed',
            '--prefix', self.path] + list(reqs))
806300
def _pip_safe_ssl(cmds, anaconda_dir):
    
    try:
        for cmd in cmds:
            subprocess.check_call(cmd)
    except subprocess.CalledProcessError:
        _set_pip_ssl(anaconda_dir)
        for cmd in cmds:
            subprocess.check_call(cmd)
35761
def safe_py_code(code):
    '''
    
    '''
    bads = (
            'import',
            ';',
            'subprocess',
            'eval',
            'open',
            'file',
            'exec',
            'input')
    for bad in bads:
        if code.count(bad):
            return False
    return True
259386
def del_windows_env_var(key):
    

    if not isinstance(key, text_type):
        raise TypeError("%r not of type %r" % (key, text_type))

    status = winapi.SetEnvironmentVariableW(key, None)
    if status == 0:
        raise ctypes.WinError()