# Buggy code
def _process_requirements(requirements, cmd, cwd, saltenv, user):
    """
    Process the requirements argument
    """
    cleanup_requirements = []

    if requirements is not None:
        if isinstance(requirements, string_types):
            requirements = [r.strip() for r in requirements.split(",")]
        elif not isinstance(requirements, list):
            raise TypeError("requirements must be a string or list")

        treq = None

        for requirement in requirements:
            logger.debug("TREQ IS: %s", str(treq))
            if requirement.startswith("salt://"):
                cached_requirements = _get_cached_requirements(requirement, saltenv)
                if not cached_requirements:
                    ret = {
                        "result": False,
                        "comment": "pip requirements file '{0}' not found".format(
                            requirement
                        ),
                    }
                    return None, ret
                requirement = cached_requirements

            if user:
                # Need to make a temporary copy since the user will, most
                # likely, not have the right permissions to read the file

                if not treq:
                    treq = tempfile.mkdtemp()

                __salt__["file.chown"](treq, user, None)

                current_directory = None

                if not current_directory:
                    current_directory = os.path.abspath(os.curdir)

                logger.info(
                    "_process_requirements from directory," + "%s -- requirement: %s",
                    cwd,
                    requirement,
                )

                if cwd is None:
                    r = requirement
                    c = cwd

                    requirement_abspath = os.path.abspath(requirement)
                    cwd = os.path.dirname(requirement_abspath)
                    requirement = os.path.basename(requirement)

                    logger.debug(
                        "\n\tcwd: %s -> %s\n\trequirement: %s -> %s\n",
                        c,
                        cwd,
                        r,
                        requirement,
                    )

                os.chdir(cwd)

                reqs = _resolve_requirements_chain(requirement)

                os.chdir(current_directory)

                logger.info("request files: {0}".format(str(reqs)))

                for req_file in reqs:
                    req_filename = os.path.basename(req_file)

                    logger.debug(
                        "TREQ N CWD: %s -- %s -- for %s",
                        str(treq),
                        str(cwd),
                        str(req_filename),
                    )
                    source_path = os.path.join(cwd, req_filename)
                    target_path = os.path.join(treq, req_filename)

                    logger.debug("S: %s", source_path)
                    logger.debug("T: %s", target_path)

                    target_base = os.path.dirname(target_path)

                    if not os.path.exists(target_base):
                        os.makedirs(target_base, mode=0o755)
                        __salt__["file.chown"](target_base, user, None)

                    if not os.path.exists(target_path):
                        logger.debug("Copying %s to %s", source_path, target_path)
                        __salt__["file.copy"](source_path, target_path)

                    logger.debug(
                        "Changing ownership of requirements file '{0}' to "
                        "user '{1}'".format(target_path, user)
                    )

                    __salt__["file.chown"](target_path, user, None)

            req_args = os.path.join(treq, requirement) if treq else requirement
            cmd.extend(["--requirement", req_args])

        cleanup_requirements.append(treq)

    logger.debug("CLEANUP_REQUIREMENTS: %s", str(cleanup_requirements))
    return cleanup_requirements, None


# Traceback (CommandExecutionError)
An exception occurred in this state: Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/salt/state.py", line 1703, in call
**cdata['kwargs'])
File "/usr/lib/python2.7/dist-packages/salt/loader.py", line 1649, in wrapper
return f(*args, **kwargs)
File "/usr/lib/python2.7/dist-packages/salt/states/virtualenv_mod.py", line 273, in managed
env_vars=env_vars
File "/usr/lib/python2.7/dist-packages/salt/modules/pip.py", line 603, in install
user=user
File "/usr/lib/python2.7/dist-packages/salt/modules/pip.py", line 312, in _process_requirements
__salt__['file.copy'](source_path, target_path)
File "/usr/lib/python2.7/dist-packages/salt/modules/file.py", line 2890, in copy
raise CommandExecutionError('No such file or directory \'{0}\''.format(src))
CommandExecutionError: No such file or directory '/tmp/test-venv/test.txt'

# Fixed code
def _process_requirements(requirements, cmd, cwd, saltenv, user):
    """
    Process the requirements argument
    """
    cleanup_requirements = []

    if requirements is not None:
        if isinstance(requirements, string_types):
            requirements = [r.strip() for r in requirements.split(",")]
        elif not isinstance(requirements, list):
            raise TypeError("requirements must be a string or list")

        treq = None

        for requirement in requirements:
            logger.debug("TREQ IS: %s", str(treq))
            if requirement.startswith("salt://"):
                cached_requirements = _get_cached_requirements(requirement, saltenv)
                if not cached_requirements:
                    ret = {
                        "result": False,
                        "comment": "pip requirements file '{0}' not found".format(
                            requirement
                        ),
                    }
                    return None, ret
                requirement = cached_requirements

            if user:
                # Need to make a temporary copy since the user will, most
                # likely, not have the right permissions to read the file

                if not treq:
                    treq = tempfile.mkdtemp()

                __salt__["file.chown"](treq, user, None)

                current_directory = None

                if not current_directory:
                    current_directory = os.path.abspath(os.curdir)

                logger.info(
                    "_process_requirements from directory," + "%s -- requirement: %s",
                    cwd,
                    requirement,
                )

                if cwd is None:
                    r = requirement
                    c = cwd

                    requirement_abspath = os.path.abspath(requirement)
                    cwd = os.path.dirname(requirement_abspath)
                    requirement = os.path.basename(requirement)

                    logger.debug(
                        "\n\tcwd: %s -> %s\n\trequirement: %s -> %s\n",
                        c,
                        cwd,
                        r,
                        requirement,
                    )

                os.chdir(cwd)

                reqs = _resolve_requirements_chain(requirement)

                os.chdir(current_directory)

                logger.info("request files: {0}".format(str(reqs)))

                for req_file in reqs:
                    if not os.path.isabs(req_file):
                        req_file = os.path.join(cwd, req_file)

                    logger.debug(
                        "TREQ N CWD: %s -- %s -- for %s",
                        str(treq),
                        str(cwd),
                        str(req_file),
                    )
                    target_path = os.path.join(treq, os.path.basename(req_file))

                    logger.debug("S: %s", req_file)
                    logger.debug("T: %s", target_path)

                    target_base = os.path.dirname(target_path)

                    if not os.path.exists(target_base):
                        os.makedirs(target_base, mode=0o755)
                        __salt__["file.chown"](target_base, user, None)

                    if not os.path.exists(target_path):
                        logger.debug("Copying %s to %s", req_file, target_path)
                        __salt__["file.copy"](req_file, target_path)

                    logger.debug(
                        "Changing ownership of requirements file '{0}' to "
                        "user '{1}'".format(target_path, user)
                    )

                    __salt__["file.chown"](target_path, user, None)

            req_args = os.path.join(treq, requirement) if treq else requirement
            cmd.extend(["--requirement", req_args])

        cleanup_requirements.append(treq)

    logger.debug("CLEANUP_REQUIREMENTS: %s", str(cleanup_requirements))
    return cleanup_requirements, None