import sys
import sh
import yaml

from fabric import api as fab

TRAVIS_YAML = 'travis.yml'
WARNING = """
#############################################################
# WARNING: .travis.yml is automatically generated by fabric.
# Please modify travis.yml if you want your changes to stick.
#############################################################
"""


def is_dirty():
    return "" != sh.git.status(porcelain=True).strip()


def generate_yaml(language, channel):
    with open(TRAVIS_YAML, "r") as f:
        travis = yaml.load(f)
    travis['language'] = language
    travis['env']['global'].append('ANACONDA_CHANNEL={}'.format(channel))
    with open('.travis.yml', 'w') as f:
        current = yaml.dump(travis)
        f.write(WARNING + current)


def _release(language, message, channel):
    print message, "...",
    if is_dirty():
        sys.exit("Repo must be in clean state before deploying. Please commit changes.")
    generate_yaml(language, channel)

    if is_dirty():
        sh.git.add('.travis.yml')
    sh.git.commit(m=message, allow_empty=True)
    sh.git.pull(rebase=True)
    sh.git.push()
    print "done."


@fab.task
def update():
    """Update all submodules to Github versions"""
    if is_dirty():
        sys.exit("Repo must be in clean state before deploying. Please commit changes.")
    sh.git.submodule.update(remote=True, rebase=True)
    if is_dirty():
        print "Updated repositories:"
        print sh.git.status(porcelain=True).strip()
        sh.git.add(all=True)
        sh.git.commit(m="Update submodules to origin")
    else:
        sys.exit('Nothing to update.')


@fab.task
def release_all(channel="main"):
    """Release datamicroscopes to Anaconda.org for OS X and Linux"""
    release_osx(channel)
    release_linux(channel)


@fab.task
def release_osx(channel="main"):
    """Release datamicroscopes to Anaconda.org for OS X"""
    _release('objective-c', "Release OS X", channel)


@fab.task
def release_linux(channel="main"):
    """Release datamicroscopes to Anaconda.org for Linux"""
    _release('python', "Release Linux", channel)

@@ -356,16 +356,17 @@
 %22%0A%0A%0Adef 
+_
 is_dirty
@@ -430,16 +430,17 @@
 )%0A%0A%0Adef 
+_
 generate
@@ -821,32 +821,33 @@
 , %22...%22,%0A    if 
+_
 is_dirty():%0A    
@@ -935,16 +935,17 @@
 .%22)%0A    
+_
 generate
@@ -969,32 +969,33 @@
 hannel)%0A%0A    if 
+_
 is_dirty():%0A    
@@ -1212,32 +1212,33 @@
 sions%22%22%22%0A    if 
+_
 is_dirty():%0A    
@@ -1383,16 +1383,17 @@
 %0A    if 
+_
 is_dirty

make private functions private