# This file is part of QuTiP: Quantum Toolbox in Python.
#
#    Copyright (c) 2011 and later, Paul D. Nation and Robert J. Johansson.
#    All rights reserved.
#
#    Redistribution and use in source and binary forms, with or without
#    modification, are permitted provided that the following conditions are
#    met:
#
#    1. Redistributions of source code must retain the above copyright notice,
#       this list of conditions and the following disclaimer.
#
#    2. Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#
#    3. Neither the name of the QuTiP: Quantum Toolbox in Python nor the names
#       of its contributors may be used to endorse or promote products derived
#       from this software without specific prior written permission.
#
#    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
#    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
#    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
#    PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
#    HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#    SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
#    LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
#    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
#    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
#    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
#    OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
###############################################################################
import os
import sysconfig
from distutils.util import get_platform
from distutils.sysconfig import get_python_version
from qutip.solver import config


def _cython_build_cleanup(tdname, build_dir=None):
    plat_and_py = get_platform()+'-'+get_python_version()+'/'
    sys_so_var = sysconfig.get_config_var('EXT_SUFFIX')
    #for backward compatibility with Py <= 3.4
    if sys_so_var is None:    
        sys_so_var = sysconfig.get_config_var('SO')
    if build_dir is None:
        build_dir = os.path.join(os.path.expanduser('~'), '.pyxbld')
    lib_dir = '/lib.' + plat_and_py
    temp_dir = '/temp.' + plat_and_py
    pyx_file = tdname + ".pyx"
    lib_file = build_dir + lib_dir + tdname + sys_so_var
    temp_c_file = build_dir + temp_dir + 'pyrex/' + tdname + ".c"
    temp_o_file = build_dir + temp_dir + build_dir + temp_dir \
        + 'pyrex/' + tdname + ".o"
    for file in [pyx_file, lib_file, temp_c_file, temp_o_file]:
        try:
            os.remove(file)
        except:
            pass
    return
