# 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 remove_directory(top, remove_top=True, filter=None):
#     '''
#     Removes all files and directories, bottom-up.
# 
#     :param str top: The top-level directory to clean out
#     :param bool remove_top: Whether or not to delete the top
#         directory when cleared.
#     :param code filter: A function that returns True or False
#         based on the name of the file or folder.  Returning
#         True means "delete it", False means "keep it".
#     '''
#     if not os.path.isdir(top):
#         return
# 
#     if filter is None:
#         filter = lambda x: True
# 
#     for root, dirs, files in os.walk(top, topdown=False):
#         for name in [x for x in files if filter(x)]:
#             os.remove(os.path.join(root, name))
# 
#         for name in [x for x in dirs if filter(x)]:
#             os.rmdir(os.path.join(root, name))
# 
#     if remove_top:
#         try:
#             os.rmdir(top)
#         except OSError as e:
#             print("error removing top:", e.message, file=sys.stderr)
# 
# Path: fabfile/constants.py
# LIB_DIR = os.path.join(THIS_DIR, '..')


import os
import sys
from fabric.api import task
from fabric.colors import green
from .helpers import ex, remove_directory
from .constants import LIB_DIR
__creationDate__ = '14-APR-2017'


# Globals #####################################################################
def _build():
    pass


@task
def build():
    '''Build the release'''
    dist_dir = os.path.join(LIB_DIR, 'dist')
    setup_path = os.path.join(LIB_DIR, 'setup.py')
    command = [
        sys.executable,
        setup_path,
        'build',
        'sdist',
        '--formats',
        'gztar',
        'bdist_wheel'
    ]

    # Delete older releases
    remove_directory(dist_dir, remove_top=False)

    ex(command)

    # Remove the egg-info directory
    egg_info = next((

# 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 remove_directory(top, remove_top=True, filter=None):
#     '''
#     Removes all files and directories, bottom-up.
# 
#     :param str top: The top-level directory to clean out
#     :param bool remove_top: Whether or not to delete the top
#         directory when cleared.
#     :param code filter: A function that returns True or False
#         based on the name of the file or folder.  Returning
#         True means "delete it", False means "keep it".
#     '''
#     if not os.path.isdir(top):
#         return
# 
#     if filter is None:
#         filter = lambda x: True
# 
#     for root, dirs, files in os.walk(top, topdown=False):
#         for name in [x for x in files if filter(x)]:
#             os.remove(os.path.join(root, name))
# 
#         for name in [x for x in dirs if filter(x)]:
#             os.rmdir(os.path.join(root, name))
# 
#     if remove_top:
#         try:
#             os.rmdir(top)
#         except OSError as e:
#             print("error removing top:", e.message, file=sys.stderr)
# 
# Path: fabfile/constants.py
# LIB_DIR = os.path.join(THIS_DIR, '..')


# Path: fabfile/build.py
import os
import sys
from fabric.api import task
from fabric.colors import green
from .helpers import ex, remove_directory
from .constants import LIB_DIR

__creationDate__ = '14-APR-2017'


# Globals #####################################################################
def _build():
    pass


@task
def build():
    '''Build the release'''
    dist_dir = os.path.join(LIB_DIR, 'dist')
    setup_path = os.path.join(LIB_DIR, 'setup.py')
    command = [
        sys.executable,
        setup_path,
        'build',
        'sdist',
        '--formats',
        'gztar',
        'bdist_wheel'
    ]

    # Delete older releases
    remove_directory(dist_dir, remove_top=False)

    ex(command)

    # Remove the egg-info directory
    egg_info = next((

        x for x in os.listdir(LIB_DIR) if (x == 'climenu.egg-info')), None)