# Path: py_mina/echo.py
# def echo_subtask(message, error=False):
#     color = red if error == True else cyan
# 
#     print(cyan('\n-----> %s' % message))
# 
# def echo_task(message, error=False):
#     color = red if error == True else yellow
#     
#     print(color('\n=====> %s' % message))
# 
# Path: py_mina/config.py
# def fetch(key, default_value=None):
#     """
#     Gets and ensures config setting or returns `default_value` if provided
#     """
# 
#     if key in config.keys():
#         return config.get(key)
#     else:
#         if default_value != None:
#             return default_value
#         else:
#             raise Exception('"%s" is not defined' % key)
# 
# def ensure(key):
#     """
#     Ensures presence of config setting. Prevents from running unnecessary commands,
#     before discovering, that config setting is not set
#     """
# 
#     if not key in config.keys():
#         raise Exception('"%s" must be defined' % key)
# 
# Path: py_mina/subtasks/setup.py
# def create_entity(entity_path, entity_type = 'file', protected=False):
#     """
#     Creates directory/file and sets proper owner/mode.
#     """
# 
#     chmod_sudo = 'sudo ' if fetch('sudo_on_chmod') else ''
#     chown_sudo = 'sudo ' if fetch('sudo_on_chown') else ''
# 
#     def change_owner(owner_triple):
#         """
#         Changes unix owner/mode
#         """
# 
#         run(chmod_sudo + 'chmod 0755 ' + entity_path)
#         run(chown_sudo + 'chown %s:%s %s' % owner_triple)
# 
# 
#     # 1) Create file/directory
# 
#     if entity_type == 'file':
#         run('touch %s' % entity_path)
#     else:
#         run('mkdir -p %s' % entity_path)
# 
#     # 2) Change owner/mode
# 
#     if protected:
#         protected_owner_user = fetch('protected_owner_user')
#         protected_owner_group = fetch('protected_owner_group')
# 
#         change_owner((protected_owner_user, protected_owner_group, entity_path))
#     else:
#         owner_user = fetch('owner_user', default_value=False)
#         owner_group = fetch('owner_group', default_value=False)
#         
#         if owner_user != False and owner_group != False: 
#             change_owner((owner_user, owner_group, entity_path))
#         else:
#             run(chmod_sudo + 'chmod 0755 ' + entity_path)


import os
from fabric.api import settings, hide, run, cd, settings
from py_mina.echo import echo_subtask, echo_task
from py_mina.config import fetch, ensure
from py_mina.subtasks.setup import create_entity
        if run('test -f %s' % '/'.join([scm_path, 'HEAD'])).failed:
            echo_subtask("Cloning bare git repository")

            with settings(hide('output')):
                run('git clone {0} {1} --bare'.format(fetch('repository'), scm_path))


def fetch_new_commits():
    """
    Fetches new git commits
    """

    ensure('scm')
    ensure('repository')
    ensure('branch')

    echo_subtask("Fetching new commits")

    with cd(fetch('scm')):
        run('git fetch {0} "{1}:{1}" --force'.format(fetch('repository'), fetch('branch')))


def use_git_branch():
    """
    Clones repository to build dir
    """

    ensure('scm')
    ensure('branch')


# Path: py_mina/echo.py
# def echo_subtask(message, error=False):
#     color = red if error == True else cyan
# 
#     print(cyan('\n-----> %s' % message))
# 
# def echo_task(message, error=False):
#     color = red if error == True else yellow
#     
#     print(color('\n=====> %s' % message))
# 
# Path: py_mina/config.py
# def fetch(key, default_value=None):
#     """
#     Gets and ensures config setting or returns `default_value` if provided
#     """
# 
#     if key in config.keys():
#         return config.get(key)
#     else:
#         if default_value != None:
#             return default_value
#         else:
#             raise Exception('"%s" is not defined' % key)
# 
# def ensure(key):
#     """
#     Ensures presence of config setting. Prevents from running unnecessary commands,
#     before discovering, that config setting is not set
#     """
# 
#     if not key in config.keys():
#         raise Exception('"%s" must be defined' % key)
# 
# Path: py_mina/subtasks/setup.py
# def create_entity(entity_path, entity_type = 'file', protected=False):
#     """
#     Creates directory/file and sets proper owner/mode.
#     """
# 
#     chmod_sudo = 'sudo ' if fetch('sudo_on_chmod') else ''
#     chown_sudo = 'sudo ' if fetch('sudo_on_chown') else ''
# 
#     def change_owner(owner_triple):
#         """
#         Changes unix owner/mode
#         """
# 
#         run(chmod_sudo + 'chmod 0755 ' + entity_path)
#         run(chown_sudo + 'chown %s:%s %s' % owner_triple)
# 
# 
#     # 1) Create file/directory
# 
#     if entity_type == 'file':
#         run('touch %s' % entity_path)
#     else:
#         run('mkdir -p %s' % entity_path)
# 
#     # 2) Change owner/mode
# 
#     if protected:
#         protected_owner_user = fetch('protected_owner_user')
#         protected_owner_group = fetch('protected_owner_group')
# 
#         change_owner((protected_owner_user, protected_owner_group, entity_path))
#     else:
#         owner_user = fetch('owner_user', default_value=False)
#         owner_group = fetch('owner_group', default_value=False)
#         
#         if owner_user != False and owner_group != False: 
#             change_owner((owner_user, owner_group, entity_path))
#         else:
#             run(chmod_sudo + 'chmod 0755 ' + entity_path)


# Path: py_mina/subtasks/git.py
import os
from fabric.api import settings, hide, run, cd, settings
from py_mina.echo import echo_subtask, echo_task
from py_mina.config import fetch, ensure
from py_mina.subtasks.setup import create_entity

        if run('test -f %s' % '/'.join([scm_path, 'HEAD'])).failed:
            echo_subtask("Cloning bare git repository")

            with settings(hide('output')):
                run('git clone {0} {1} --bare'.format(fetch('repository'), scm_path))


def fetch_new_commits():
    """
    Fetches new git commits
    """

    ensure('scm')
    ensure('repository')
    ensure('branch')

    echo_subtask("Fetching new commits")

    with cd(fetch('scm')):
        run('git fetch {0} "{1}:{1}" --force'.format(fetch('repository'), fetch('branch')))


def use_git_branch():
    """
    Clones repository to build dir
    """

    ensure('scm')
    ensure('branch')


    build_to = fetch('build_to')