# Path: fabfile/constants.py
# LIB_DIR = os.path.join(THIS_DIR, '..')
# 
# DIST_DIR = os.path.join(LIB_DIR, 'dist')
# 
# 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 abspath(*args):
#     '''Returns the absolute path of join(*args).'''
#     return os.path.abspath(os.path.join(*args))
# 
# Path: fabfile/git.py
# @task
# 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))
# 
# Path: fabfile/relnote.py
# @task
# def get(version=None, path=RELEASE_NOTES_FILE, html='n', display='y'):
#     '''Return only the release notes for the specified version'''
#     return _get(version, path, html, display)


import os
import github
from fabric.api import task
from fabric.utils import abort
from .constants import LIB_DIR, DIST_DIR
from .helpers import ex, abspath
from .git import get_tags
from .relnote import get as get_release_notes

@task
def create_release(tag=None, message=None):
    '''Create a GitHub release'''
    if not tag:
        tag = get_tags()[-1].string

    if not message:
        message = get_release_notes(tag)

    r = repo()
    r.create_git_release(tag, tag, message)


def _upload():
    tag = get_tags()[-1]

    # Make sure the release exists
    r = repo()
    github_release = next((
        x for x in r.get_releases() if x.tag_name == tag.string),
        None)

    if not github_release:
        create_release(tag.string)

    # Find the tarball
    path = None
    for fname in os.listdir(DIST_DIR):
        if (str(tag.version) in fname) and fname.endswith('.tar.gz'):

# Path: fabfile/constants.py
# LIB_DIR = os.path.join(THIS_DIR, '..')
# 
# DIST_DIR = os.path.join(LIB_DIR, 'dist')
# 
# 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 abspath(*args):
#     '''Returns the absolute path of join(*args).'''
#     return os.path.abspath(os.path.join(*args))
# 
# Path: fabfile/git.py
# @task
# 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))
# 
# Path: fabfile/relnote.py
# @task
# def get(version=None, path=RELEASE_NOTES_FILE, html='n', display='y'):
#     '''Return only the release notes for the specified version'''
#     return _get(version, path, html, display)


# Path: fabfile/gh.py
import os
import github
from fabric.api import task
from fabric.utils import abort
from .constants import LIB_DIR, DIST_DIR
from .helpers import ex, abspath
from .git import get_tags
from .relnote import get as get_release_notes


@task
def create_release(tag=None, message=None):
    '''Create a GitHub release'''
    if not tag:
        tag = get_tags()[-1].string

    if not message:
        message = get_release_notes(tag)

    r = repo()
    r.create_git_release(tag, tag, message)


def _upload():
    tag = get_tags()[-1]

    # Make sure the release exists
    r = repo()
    github_release = next((
        x for x in r.get_releases() if x.tag_name == tag.string),
        None)

    if not github_release:
        create_release(tag.string)

    # Find the tarball
    path = None
    for fname in os.listdir(DIST_DIR):
        if (str(tag.version) in fname) and fname.endswith('.tar.gz'):

            path = abspath(DIST_DIR, fname)