# Path: fabgis/common.py
# def setup_env():
#     """Things to do regardless of whether command is local or remote.
# 
#     Todo - many of these env.fg settings should be module specific...
# 
#     """
#     if env.fg is not None:
#         fastprint('Environment already set!\n')
#         return
# 
#     fastprint('Setting environment!\n')
#     env.fg = fdict()
#     with hide('output'):
#         env.fg.user = run('whoami')
#         # Workaround for
#         env.fg.hostname = run('hostname')
#         # this which fails in docker - see
#         # https://github.com/dotcloud/docker/issues/1301
#         #env.fg.hostname = fabtools.system.get_hostname()
#         env.fg.home = os.path.join('/home/', env.fg.user)
#         env.fg.workspace = os.path.join(env.fg.home, 'dev')
#         env.fg.inasafe_git_url = 'git://github.com/AIFDR/inasafe.git'
#         env.fg.qgis_git_url = 'git://github.com/qgis/QGIS.git'
#         env.fg.kandan_git_url = 'git://github.com/kandanapp/kandan.git'
#         env.fg.gdal_svn_url = 'https://svn.osgeo.org/gdal/trunk/gdal'
#         env.fg.inasafe_checkout_alias = 'inasafe-fabric'
#         env.fg.qgis_checkout_alias = 'qgis-fabric'
#         env.fg.inasafe_code_path = os.path.join(
#             env.fg.workspace, env.fg.inasafe_checkout_alias)
#         env.fg.qgis_code_path = os.path.join(
#             env.fg.workspace, env.fg.qgis_checkout_alias)


import os
from fabric.api import fastprint, run, cd, task
from fabric.contrib.files import sed
from fabric.colors import blue, green
from fabtools.require.deb import packages as require_packages
from fabtools.require.python import virtualenv
from .common import setup_env
# coding=utf-8
"""
Tools relating to the use of python virtualenv.
===============================================

"""




@task
def setup_venv(code_path, requirements_file='requirements.txt'):
    """Initialise or update the virtual environment.

    It will also ensure build-essential is installed, though you may need to
    install required dev library using your own script.

    :param code_path: Base directory under which the venv dir should be made.
    :type code_path: str

    :param requirements_file: Name of the requirements file to use in the
        base directory. Defaults to ``requirements.txt``. If you pass in None
        no attempt will be made to install packages from a requirements file.
    :type requirements_file: str

    To run e.g.::

        fab -H 192.168.1.1:2222 setup_venv

    """

# Path: fabgis/common.py
# def setup_env():
#     """Things to do regardless of whether command is local or remote.
# 
#     Todo - many of these env.fg settings should be module specific...
# 
#     """
#     if env.fg is not None:
#         fastprint('Environment already set!\n')
#         return
# 
#     fastprint('Setting environment!\n')
#     env.fg = fdict()
#     with hide('output'):
#         env.fg.user = run('whoami')
#         # Workaround for
#         env.fg.hostname = run('hostname')
#         # this which fails in docker - see
#         # https://github.com/dotcloud/docker/issues/1301
#         #env.fg.hostname = fabtools.system.get_hostname()
#         env.fg.home = os.path.join('/home/', env.fg.user)
#         env.fg.workspace = os.path.join(env.fg.home, 'dev')
#         env.fg.inasafe_git_url = 'git://github.com/AIFDR/inasafe.git'
#         env.fg.qgis_git_url = 'git://github.com/qgis/QGIS.git'
#         env.fg.kandan_git_url = 'git://github.com/kandanapp/kandan.git'
#         env.fg.gdal_svn_url = 'https://svn.osgeo.org/gdal/trunk/gdal'
#         env.fg.inasafe_checkout_alias = 'inasafe-fabric'
#         env.fg.qgis_checkout_alias = 'qgis-fabric'
#         env.fg.inasafe_code_path = os.path.join(
#             env.fg.workspace, env.fg.inasafe_checkout_alias)
#         env.fg.qgis_code_path = os.path.join(
#             env.fg.workspace, env.fg.qgis_checkout_alias)


# Path: fabgis/virtualenv.py
import os
from fabric.api import fastprint, run, cd, task
from fabric.contrib.files import sed
from fabric.colors import blue, green
from fabtools.require.deb import packages as require_packages
from fabtools.require.python import virtualenv
from .common import setup_env

# coding=utf-8
"""
Tools relating to the use of python virtualenv.
===============================================

"""




@task
def setup_venv(code_path, requirements_file='requirements.txt'):
    """Initialise or update the virtual environment.

    It will also ensure build-essential is installed, though you may need to
    install required dev library using your own script.

    :param code_path: Base directory under which the venv dir should be made.
    :type code_path: str

    :param requirements_file: Name of the requirements file to use in the
        base directory. Defaults to ``requirements.txt``. If you pass in None
        no attempt will be made to install packages from a requirements file.
    :type requirements_file: str

    To run e.g.::

        fab -H 192.168.1.1:2222 setup_venv

    """

    setup_env()