from pyinfra import host
from pyinfra.operations import apk, apt, files, pip, python, yum

SUDO = True

if host.fact.linux_name in ['Alpine']:
    apk.packages(
        name='Install packages for python virtual environments',
        packages=[
            'gcc',
            'libffi-dev',
            'make',
            'musl-dev',
            'openssl-dev',
            'py3-pynacl',
            'py3-virtualenv',
            'python3-dev',
        ],
    )

if host.fact.linux_name in ['CentOS']:
    yum.packages(
        name='Install pip3 so you can install virtualenv',
        packages='python3-pip',
    )

if host.fact.linux_name in ['Ubuntu']:
    apt.packages(
        name='Install pip3 so you can install virtualenv',
        packages='python3-pip',
        update=True,
    )

if not host.fact.file('/usr/bin/pip'):
    files.link(
        name='Create link /usr/bin/pip that points to /usr/bin/pip3',
        path='/usr/bin/pip',
        target='/usr/bin/pip3',
    )

pip.packages(
    name='Install virtualenv using pip',
    packages='virtualenv',
)

pip.virtualenv(
    name='Create a virtualenv',
    path='/usr/local/bin/venv',
)

# use that virtualenv to install pyinfra
pip.packages(
    name='Install pyinfra into a virtualenv',
    packages='pyinfra',
    virtualenv='/usr/local/bin/venv',
)


# Show that we can actually run the pyinfra command from that virtualenv
def run_pyinfra_version(state, host):
    status, stdout, stderr = host.run_shell_command(
        'sh -c -c "source /usr/local/bin/venv/bin/activate && pyinfra --version"',
    )
    assert status
    print(stdout)

python.call(run_pyinfra_version)  # noqa: E305

@@ -1491,25 +1491,8 @@
    '
-sh -c -c %22source 
 /usr
@@ -1515,20 +1515,8 @@
 bin/
-activate && 
 pyin
@@ -1532,53 +1532,181 @@
 sion
-%22',%0A    )%0A    assert status%0A    print(
+',%0A        env=%7B'LC_ALL': 'C.UTF-8', 'LANG': 'C.UTF-8,'%7D,%0A    )%0A    assert status, 'pyinfra command failed: %7B0%7D'.format((stdout, stderr))%0A    assert 'pyinfra: ' in 
 stdout
-)
+%5B0%5D
 %0A%0Apy

Fix pip example/test.