#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# SPDX-License-Identifier: MIT

import os
import shutil
import sys

from pathlib import Path
from distutils import log
from setuptools import setup
from setuptools.command.sdist import sdist as SdistCommand
from cmake_build_extension import BuildExtension, CMakeExtension

TOP_DIR = (Path(__file__).parent).resolve()

# Where the Python library is actually found.
PYTHON_DIR = "api/python"

setup_kw = {}

# Read in the package version when not in a git repository.
VERSION_FILE = os.path.join(PYTHON_DIR, 'ryml', 'version.py')
if not (TOP_DIR / '.git').exists() and os.path.exists(VERSION_FILE):
    exec(open(VERSION_FILE).read())
    setup_kw['version'] = version
else:
    setup_kw['use_scm_version']= {
        "version_scheme": "post-release",
        "local_scheme": "no-local-version",
        "write_to": VERSION_FILE,
    }

# Read in the module description from the README.md file.
README_FILE = TOP_DIR / "README.md"
if README_FILE.exists():
    with open(TOP_DIR / "README.md", "r") as fh:
        setup_kw['long_description'] = fh.read()
        setup_kw['long_description_content_type'] = "text/markdown"

# define a CMake package
cmake_args = dict(
    name='ryml.ryml',
    install_prefix='',
    source_dir='',
    cmake_component='python',
    cmake_configure_options=[
        "-DRYML_BUILD_API:BOOL=ON",
        # Force cmake to use the Python interpreter we are currently using to
        # run setup.py
        "-DPython3_EXECUTABLE:FILEPATH="+sys.executable,
    ],
)

try:
    ext = CMakeExtension(**cmake_args)
except TypeError:
    del cmake_args['cmake_component']
    ext = CMakeExtension(**cmake_args)

    # If the CMakeExtension doesn't support `cmake_component` then we have to
    # do some manual cleanup.
    _BuildExtension=BuildExtension
    class BuildExtension(_BuildExtension):
        def build_extension(self, ext):
            _BuildExtension.build_extension(self, ext)
            ext_dir = Path(self.get_ext_fullpath(ext.name)).parent.absolute()
            cmake_install_prefix = ext_dir / ext.install_prefix
            assert cmake_install_prefix.exists(), cmake_install_prefix
            try:
                lib_path = cmake_install_prefix / "lib"
                assert lib_path.exists(), lib_path
                log.info("Removing everything under: %s", lib_path)
                shutil.rmtree(lib_path)

                inc_path = cmake_install_prefix / "include"
                assert inc_path.exists(), inc_path
                log.info("Removing everything under: %s", inc_path)
                shutil.rmtree(inc_path)

                # Windows only
                cm_path = cmake_install_prefix / "cmake"
                if cm_path.exists():
                    log.info("Removing everything under: %s", cm_path)
                    shutil.rmtree(cm_path)
            except:
                log.info('Found following installed files:')
                for f in cmake_install_prefix.rglob("*"):
                    log.info(' - %s', f)
                raise

setup(
    # Package human readable information
    name='rapidyaml',
    #author='Joao Paulo Magalhaes',
    description='Rapid YAML - a library to parse and emit YAML, and do it fast.',
    url='https://github.com/biojppm/rapidyaml',
    license='MIT',
    license_files=['LICENSE.txt'],
    # Package contents control
    cmdclass={
        "build_ext": BuildExtension,
    },
    package_dir={"": PYTHON_DIR},
    packages=['ryml'],
    ext_modules=[ext],
    include_package_data=True,
    # Requirements
    python_requires=">=3.7",
    setup_requires=['setuptools_scm'],
    # Extra arguments
    **setup_kw,
)

[((23, 15, 23, 61), 'os.path.join', 'os.path.join', ({(23, 28, 23, 38): 'PYTHON_DIR', (23, 40, 23, 46): '"""ryml"""', (23, 48, 23, 60): '"""version.py"""'}, {}), "(PYTHON_DIR, 'ryml', 'version.py')", False, 'import os\n'), ((92, 0, 113, 1), 'setuptools.setup', 'setup', (), '', False, 'from setuptools import setup\n'), ((24, 39, 24, 67), 'os.path.exists', 'os.path.exists', ({(24, 54, 24, 66): 'VERSION_FILE'}, {}), '(VERSION_FILE)', False, 'import os\n'), ((56, 10, 56, 38), 'cmake_build_extension.CMakeExtension', 'CMakeExtension', ({}, {}), '(**cmake_args)', False, 'from cmake_build_extension import BuildExtension, CMakeExtension\n'), ((59, 10, 59, 38), 'cmake_build_extension.CMakeExtension', 'CMakeExtension', ({}, {}), '(**cmake_args)', False, 'from cmake_build_extension import BuildExtension, CMakeExtension\n'), ((15, 11, 15, 25), 'pathlib.Path', 'Path', ({(15, 16, 15, 24): '__file__'}, {}), '(__file__)', False, 'from pathlib import Path\n'), ((73, 16, 73, 67), 'distutils.log.info', 'log.info', ({(73, 25, 73, 56): '"""Removing everything under: %s"""', (73, 58, 73, 66): 'lib_path'}, {}), "('Removing everything under: %s', lib_path)", False, 'from distutils import log\n'), ((74, 16, 74, 39), 'shutil.rmtree', 'shutil.rmtree', ({(74, 30, 74, 38): 'lib_path'}, {}), '(lib_path)', False, 'import shutil\n'), ((78, 16, 78, 67), 'distutils.log.info', 'log.info', ({(78, 25, 78, 56): '"""Removing everything under: %s"""', (78, 58, 78, 66): 'inc_path'}, {}), "('Removing everything under: %s', inc_path)", False, 'from distutils import log\n'), ((79, 16, 79, 39), 'shutil.rmtree', 'shutil.rmtree', ({(79, 30, 79, 38): 'inc_path'}, {}), '(inc_path)', False, 'import shutil\n'), ((84, 20, 84, 70), 'distutils.log.info', 'log.info', ({(84, 29, 84, 60): '"""Removing everything under: %s"""', (84, 62, 84, 69): 'cm_path'}, {}), "('Removing everything under: %s', cm_path)", False, 'from distutils import log\n'), ((85, 20, 85, 42), 'shutil.rmtree', 'shutil.rmtree', ({(85, 34, 85, 41): 'cm_path'}, {}), '(cm_path)', False, 'import shutil\n'), ((87, 16, 87, 60), 'distutils.log.info', 'log.info', ({(87, 25, 87, 59): '"""Found following installed files:"""'}, {}), "('Found following installed files:')", False, 'from distutils import log\n'), ((89, 20, 89, 40), 'distutils.log.info', 'log.info', ({(89, 29, 89, 36): '""" - %s"""', (89, 38, 89, 39): 'f'}, {}), "(' - %s', f)", False, 'from distutils import log\n')]