
'''
Manage Python virtual environments
'''

from __future__ import unicode_literals

from pyinfra.api import operation
from pyinfra.modules import files


@operation
def virtualenv(
    state, host,
    path, python=None, site_packages=False, always_copy=False,
    present=True,
):
    '''
    Manage virtualenv.

    + python: python interpreter to use
    + site_packages: give access to the global site-packages
    + always_copy: always copy files rather than symlinking
    + present: whether the virtualenv should be installed
    '''

    if present is False and host.fact.directory(path):
        # Ensure deletion of unwanted virtualenv
        # no 'yield from' in python 2.7
        yield files.directory(state, host, path, present=False)

    elif present and not host.fact.directory(path):
        # Create missing virtualenv
        command = '/usr/bin/virtualenv'
        if python:
            command += ' -p {}'.format(python)
        if site_packages:
            command += ' --system-site-packages'
        if always_copy:
            command += ' --always-copy'
        command += ' ' + path
        yield command


'''
Manage Python virtual environments
'''

from __future__ import unicode_literals

from pyinfra.api import operation
from pyinfra.modules import files


@operation
def virtualenv(
    state, host,
    path, python=None, site_packages=False, always_copy=False,
    present=True,
):
    '''
    Manage virtualenv.

    + python: python interpreter to use
    + site_packages: give access to the global site-packages
    + always_copy: always copy files rather than symlinking
    + present: whether the virtualenv should be installed
    '''

    if present is False and host.fact.directory(path):
        # Ensure deletion of unwanted virtualenv
        # no 'yield from' in python 2.7
        yield files.directory(state, host, path, present=False)

    elif present and not host.fact.directory(path):
        # Create missing virtualenv
        command = ['/usr/bin/virtualenv']
        if python:
            command.append('-p {}'.format(python))
        if site_packages:
            command.append('--system-site-packages')
        if always_copy:
            command.append('--always-copy')
        command.append(path)
        yield ' '.join(command)

Refactor building command using join()
Refactor building command using join()

  
  '''
  Manage Python virtual environments
  '''
  
  from __future__ import unicode_literals
  
  from pyinfra.api import operation
  from pyinfra.modules import files
  
  
  @operation
  def virtualenv(
      state, host,
      path, python=None, site_packages=False, always_copy=False,
      present=True,
  ):
      '''
      Manage virtualenv.
  
      + python: python interpreter to use
      + site_packages: give access to the global site-packages
      + always_copy: always copy files rather than symlinking
      + present: whether the virtualenv should be installed
      '''
  
      if present is False and host.fact.directory(path):
          # Ensure deletion of unwanted virtualenv
          # no 'yield from' in python 2.7
          yield files.directory(state, host, path, present=False)
  
      elif present and not host.fact.directory(path):
          # Create missing virtualenv
-         command = '/usr/bin/virtualenv'
+         command = ['/usr/bin/virtualenv']
          if python:
-             command += ' -p {}'.format(python)
+             command.append('-p {}'.format(python))
          if site_packages:
-             command += ' --system-site-packages'
+             command.append('--system-site-packages')
          if always_copy:
-             command += ' --always-copy'
+             command.append('--always-copy')
-         command += ' ' + path
+         command.append(path)
-         yield command
+         yield ' '.join(command)
  
Refactor building command using join()
## Code Before:

'''
Manage Python virtual environments
'''

from __future__ import unicode_literals

from pyinfra.api import operation
from pyinfra.modules import files


@operation
def virtualenv(
    state, host,
    path, python=None, site_packages=False, always_copy=False,
    present=True,
):
    '''
    Manage virtualenv.

    + python: python interpreter to use
    + site_packages: give access to the global site-packages
    + always_copy: always copy files rather than symlinking
    + present: whether the virtualenv should be installed
    '''

    if present is False and host.fact.directory(path):
        # Ensure deletion of unwanted virtualenv
        # no 'yield from' in python 2.7
        yield files.directory(state, host, path, present=False)

    elif present and not host.fact.directory(path):
        # Create missing virtualenv
        command = '/usr/bin/virtualenv'
        if python:
            command += ' -p {}'.format(python)
        if site_packages:
            command += ' --system-site-packages'
        if always_copy:
            command += ' --always-copy'
        command += ' ' + path
        yield command

## Instruction:
Refactor building command using join()
## Code After:

'''
Manage Python virtual environments
'''

from __future__ import unicode_literals

from pyinfra.api import operation
from pyinfra.modules import files


@operation
def virtualenv(
    state, host,
    path, python=None, site_packages=False, always_copy=False,
    present=True,
):
    '''
    Manage virtualenv.

    + python: python interpreter to use
    + site_packages: give access to the global site-packages
    + always_copy: always copy files rather than symlinking
    + present: whether the virtualenv should be installed
    '''

    if present is False and host.fact.directory(path):
        # Ensure deletion of unwanted virtualenv
        # no 'yield from' in python 2.7
        yield files.directory(state, host, path, present=False)

    elif present and not host.fact.directory(path):
        # Create missing virtualenv
        command = ['/usr/bin/virtualenv']
        if python:
            command.append('-p {}'.format(python))
        if site_packages:
            command.append('--system-site-packages')
        if always_copy:
            command.append('--always-copy')
        command.append(path)
        yield ' '.join(command)
