# 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
import fabtools
from fabric.api import fastprint, run, cd, task, env
from fabric.colors import red, cyan, green
from fabric.contrib.files import exists
from .common import setup_env
def update_git_checkout(code_path, url, repo_alias, branch='master', tag=None):
    """Make sure there is a read only git checkout.

    :param code_path: Path to where the repo should be checked out.
    :type code_path: str

    :param url: Complete http url for cloning/checking out the repo.
    :type url: str

    :param repo_alias: Alias name under which the repo should be checked out.
    :type repo_alias: str

    :param branch: The name of the branch to build from. Defaults to 'master'.
    :type branch: str

    :param tag: Tag to checkout. If None this is ignored. If not none will be
        used in preference to branch.
    :type tag: None, str

    To run e.g.::

        fab -H foo:1234 remote update_git_checkout

    """
    fastprint(cyan('Updating git checkout.\n'))
    fastprint(cyan('code_path: %s\n' % code_path))
    fastprint(cyan('url: %s\n' % url))
    fastprint(cyan('repo_alias: %s\n' % repo_alias))
    fastprint(cyan('branch: %s\n' % branch))
    fastprint(cyan('tag: %s\n' % tag))

# 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/git.py
import os
import fabtools
from fabric.api import fastprint, run, cd, task, env
from fabric.colors import red, cyan, green
from fabric.contrib.files import exists
from .common import setup_env

def update_git_checkout(code_path, url, repo_alias, branch='master', tag=None):
    """Make sure there is a read only git checkout.

    :param code_path: Path to where the repo should be checked out.
    :type code_path: str

    :param url: Complete http url for cloning/checking out the repo.
    :type url: str

    :param repo_alias: Alias name under which the repo should be checked out.
    :type repo_alias: str

    :param branch: The name of the branch to build from. Defaults to 'master'.
    :type branch: str

    :param tag: Tag to checkout. If None this is ignored. If not none will be
        used in preference to branch.
    :type tag: None, str

    To run e.g.::

        fab -H foo:1234 remote update_git_checkout

    """
    fastprint(cyan('Updating git checkout.\n'))
    fastprint(cyan('code_path: %s\n' % code_path))
    fastprint(cyan('url: %s\n' % url))
    fastprint(cyan('repo_alias: %s\n' % repo_alias))
    fastprint(cyan('branch: %s\n' % branch))
    fastprint(cyan('tag: %s\n' % tag))

    setup_env()