# Copyright (c) 2020 CloudZero - ALL RIGHTS RESERVED - PROPRIETARY AND CONFIDENTIAL
# Unauthorized copying of this file and/or project, via any medium is strictly prohibited.
# Direct all questions to legal@cloudzero.com

# Note: To use the 'upload' functionality of this file, you must:
#   $ pip install twine

import io
import os
import sys
from shutil import rmtree

import boto3
from setuptools import setup, Command, find_packages

# Package meta-data.
NAME = 'cz-lambda-layers-cz-python-39'
DESCRIPTION = 'Reusable set of libraries for Python3.8 Lambdas'
URL = 'https://github.com/Cloudzero/cz-lambda-layers'
EMAIL = 'support@cloudzero.com'
AUTHOR = 'CloudZero'

client = boto3.client('lambda')
layer_versions = client.list_layer_versions(LayerName=NAME)['LayerVersions']
latest_layer_version = layer_versions[0]['Version']
__version__ = f'1.0.{latest_layer_version}'

here = os.path.abspath(os.path.dirname(__file__))

def _is_requirement(line):
    comment = line.startswith('#')
    extra_index = line.startswith('--extra-index')
    empty = not line
    return not (comment or extra_index or empty)

# What packages are required for this module to be executed?
with open(os.path.join(here, 'requirements.txt')) as f:
    REQUIRED = [line for line in f.read().splitlines() if _is_requirement(line)]

# Import the README and use it as the long-description.
# Note: this will only work if 'README.rst' is present in your MANIFEST.in file!
with io.open(os.path.join(here, 'README.md'), encoding='utf-8') as f:
    long_description = '\n' + f.read()


class UploadCommand(Command):
    """Support setup.py upload."""

    description = 'Build and publish the package.'
    user_options = []

    @staticmethod
    def status(s):
        """Prints things in bold."""
        print('\033[1m{0}\033[0m'.format(s))

    def initialize_options(self):
        pass

    def finalize_options(self):
        pass

    def run(self):
        try:
            self.status('Removing previous builds…')
            rmtree(os.path.join(here, 'dist'))
        except OSError:
            pass

        self.status('Building Source distribution…')
        os.system('{0} setup.py sdist'.format(sys.executable))

        self.status('Uploading the package to PyPi via Twine…')
        os.system('twine upload dist/*')

        sys.exit()


# Where the magic happens:
setup(
    name=NAME,
    version=__version__,
    description=DESCRIPTION,
    long_description=long_description,
    long_description_content_type='text/markdown',
    author=AUTHOR,
    author_email=EMAIL,
    url=URL,
    packages=find_packages(exclude=['tests*']),
    install_requires=REQUIRED,
    include_package_data=True,
    license='UNLICENSED',
    classifiers=[
        'Development Status :: 4 - Beta',
        'Intended Audience :: Developers',
        'Natural Language :: English',
        'Programming Language :: Python',
        'Programming Language :: Python :: 3',
        'Programming Language :: Python :: 3.9',
    ],
    # $ setup.py publish support.
    cmdclass={
        'upload': UploadCommand,
    },
)
