# Path: fabfile/helpers.py
# def ex(command, cwd=None, shell=None, raise_on_nonzero=True):
#     """Execute a command and return the output.  This will raise an Exception if
#     the return code is non-zero.
# 
#     :param str/list command: Either a string or a list of arguments.
#     :param bool shell: If None, "shell" will be determined by the type of
#         `command` (str -> shell=True; list -> shell=False)
#     :param bool raise_on_nonzero: Raise an exception if the return code of the
#         process is not zero.
#     :returns: tuple(text, returncode)
#     """
#     shell = shell if (shell is not None) else (type(command) is not list)
# 
#     p = subprocess.Popen(
#         command, shell=shell, cwd=cwd, stderr=subprocess.STDOUT,
#         stdout=subprocess.PIPE)
# 
#     output, _ = p.communicate()
# 
#     if p.returncode and raise_on_nonzero:
#         raise Exception("command failed: %s" % output)
# 
#     return (output, p.returncode)
# 
# def user_input(prompt, default=None):
#     '''Gets input from the user'''
#     if sys.version_info[0] == 2:
#         answer = raw_input(prompt)
#     else:
#         answer = input(prompt)
# 
#     return answer or default
# 
# def true(value):
#     '''Since fabric task inputs are always text, we need a way to convert it
#     to a boolean.
#     '''
#     if isinstance(value, bool):
#         return value
#     elif isinstance(value, basestring):
#         return value.lower()[0] in ['y', 't', '1']
# 
#     # Use the default representation
#     return bool(value)
# 
# Path: fabfile/ver.py
# def get_version():
#     '''Gets the current version'''
#     text = open(VER_FILE).read()
#     match = RE_VERSION.search(text)
# 
#     if not match:
#         raise Exception("Could not get version")
# 
#     return match.group('version')


from collections import namedtuple
from pkg_resources import parse_version
from fabric.api import task
from fabric.utils import abort
from .helpers import ex, user_input, true
from .ver import get_version
def get_tags():
    '''Returns a list of tags'''
    result = []
    (text, _) = ex('git tag -l --sort=version:refname "*"')

    for line in text.splitlines():
        result.append(VERSIONED_TAG(line, parse_version(line)))

    return sorted(result, cmp=lambda x, y: cmp(x.version, y.version))


@task
def tag():
    '''Create a tag'''
    current_version = get_version()
    tags = get_tags()
    latest_tag = tags[-1] if tags else ''

    print("...current version is: v%s" % current_version)
    if latest_tag:
        print("...latest tag is: %s" % latest_tag.string)

    default_version = 'v%s' % current_version

    name = user_input("Enter tag to create [default to: %s]: " % default_version)

    name = name or default_version
    name = "v" + name if not name.startswith('v') else name
    message = "creating tag %s" % name


# Path: fabfile/helpers.py
# def ex(command, cwd=None, shell=None, raise_on_nonzero=True):
#     """Execute a command and return the output.  This will raise an Exception if
#     the return code is non-zero.
# 
#     :param str/list command: Either a string or a list of arguments.
#     :param bool shell: If None, "shell" will be determined by the type of
#         `command` (str -> shell=True; list -> shell=False)
#     :param bool raise_on_nonzero: Raise an exception if the return code of the
#         process is not zero.
#     :returns: tuple(text, returncode)
#     """
#     shell = shell if (shell is not None) else (type(command) is not list)
# 
#     p = subprocess.Popen(
#         command, shell=shell, cwd=cwd, stderr=subprocess.STDOUT,
#         stdout=subprocess.PIPE)
# 
#     output, _ = p.communicate()
# 
#     if p.returncode and raise_on_nonzero:
#         raise Exception("command failed: %s" % output)
# 
#     return (output, p.returncode)
# 
# def user_input(prompt, default=None):
#     '''Gets input from the user'''
#     if sys.version_info[0] == 2:
#         answer = raw_input(prompt)
#     else:
#         answer = input(prompt)
# 
#     return answer or default
# 
# def true(value):
#     '''Since fabric task inputs are always text, we need a way to convert it
#     to a boolean.
#     '''
#     if isinstance(value, bool):
#         return value
#     elif isinstance(value, basestring):
#         return value.lower()[0] in ['y', 't', '1']
# 
#     # Use the default representation
#     return bool(value)
# 
# Path: fabfile/ver.py
# def get_version():
#     '''Gets the current version'''
#     text = open(VER_FILE).read()
#     match = RE_VERSION.search(text)
# 
#     if not match:
#         raise Exception("Could not get version")
# 
#     return match.group('version')


# Path: fabfile/git.py
from collections import namedtuple
from pkg_resources import parse_version
from fabric.api import task
from fabric.utils import abort
from .helpers import ex, user_input, true
from .ver import get_version

def get_tags():
    '''Returns a list of tags'''
    result = []
    (text, _) = ex('git tag -l --sort=version:refname "*"')

    for line in text.splitlines():
        result.append(VERSIONED_TAG(line, parse_version(line)))

    return sorted(result, cmp=lambda x, y: cmp(x.version, y.version))


@task
def tag():
    '''Create a tag'''
    current_version = get_version()
    tags = get_tags()
    latest_tag = tags[-1] if tags else ''

    print("...current version is: v%s" % current_version)
    if latest_tag:
        print("...latest tag is: %s" % latest_tag.string)

    default_version = 'v%s' % current_version

    name = user_input("Enter tag to create [default to: %s]: " % default_version)

    name = name or default_version
    name = "v" + name if not name.startswith('v') else name
    message = "creating tag %s" % name


    answer = user_input("creating tag [%s].  OK? " % name)