# Path: bts_tools/core.py
# def run(cmd, capture_io=False, verbose=True, log_on_fail=True,
#         run_dir=None, forward_signals=False, pidfile=None, shell=False):
#     r = _run(cmd, capture_io=capture_io, verbose=verbose,
#              run_dir=run_dir, forward_signals=forward_signals, pidfile=pidfile, shell=shell)
#     if r.status != 0:
#         if log_on_fail:
#             log.warning('Failed running: %s' % cmd)
#             if capture_io:
#                 log.warning('\nSTDOUT:\n{}\nSTDERR:\n:{}'.format(r.stdout, r.stderr))
#         raise RuntimeError('Failed running: %s' % cmd)
#     return r


from bts_tools.core import run
from jinja2 import Template
import sys
import re


def task_upload_pypi():
    """Build and upload the package on PyPI"""
    # FIXME: only apply "git stash apply" if "git stash" did stash something.
    #git stash && rm -fr dist/ && python setup.py sdist upload && git stash apply && python setup.py develop

    def check_valid_git_state():
        # check that we are on a git tag before uploading a dev version number...
        tag_version = run('git describe --tags', capture_io=True, verbose=False).stdout
        if '-g' in tag_version:
            # we're not on a git tag
            raise RuntimeError('Not on a git tag, probably not a good idea to upload this to PyPI')

    return {'actions': [check_valid_git_state,
                        'git stash && (python setup.py register sdist upload; git stash apply)'],
            'verbosity': 2}


def task_git_update():
    """Update from git, while stashing before and popping the stash after"""
    # note: if we stashed and the pull failed, we still want to stash apply
    return {'actions': ['git stash && (git pull; git stash apply)'],
            'verbosity': 2}


def task_update_installed_tools():
    """Update the tools installed in the current virtualenv with the sdist
    created from the current dir"""
    return {'actions': ['rm -fr dist; python setup.py sdist && (pip uninstall -y bts_tools; pip install dist/bts_tools-*.tar.gz)'],

# Path: bts_tools/core.py
# def run(cmd, capture_io=False, verbose=True, log_on_fail=True,
#         run_dir=None, forward_signals=False, pidfile=None, shell=False):
#     r = _run(cmd, capture_io=capture_io, verbose=verbose,
#              run_dir=run_dir, forward_signals=forward_signals, pidfile=pidfile, shell=shell)
#     if r.status != 0:
#         if log_on_fail:
#             log.warning('Failed running: %s' % cmd)
#             if capture_io:
#                 log.warning('\nSTDOUT:\n{}\nSTDERR:\n:{}'.format(r.stdout, r.stderr))
#         raise RuntimeError('Failed running: %s' % cmd)
#     return r


# Path: dodo.py
from bts_tools.core import run
from jinja2 import Template
import sys
import re



def task_upload_pypi():
    """Build and upload the package on PyPI"""
    # FIXME: only apply "git stash apply" if "git stash" did stash something.
    #git stash && rm -fr dist/ && python setup.py sdist upload && git stash apply && python setup.py develop

    def check_valid_git_state():
        # check that we are on a git tag before uploading a dev version number...
        tag_version = run('git describe --tags', capture_io=True, verbose=False).stdout
        if '-g' in tag_version:
            # we're not on a git tag
            raise RuntimeError('Not on a git tag, probably not a good idea to upload this to PyPI')

    return {'actions': [check_valid_git_state,
                        'git stash && (python setup.py register sdist upload; git stash apply)'],
            'verbosity': 2}


def task_git_update():
    """Update from git, while stashing before and popping the stash after"""
    # note: if we stashed and the pull failed, we still want to stash apply
    return {'actions': ['git stash && (git pull; git stash apply)'],
            'verbosity': 2}


def task_update_installed_tools():
    """Update the tools installed in the current virtualenv with the sdist
    created from the current dir"""
    return {'actions': ['rm -fr dist; python setup.py sdist && (pip uninstall -y bts_tools; pip install dist/bts_tools-*.tar.gz)'],

            'verbosity': 2}