################################################################################
#
# python.cygclass - functions for installing Python modules
#
# Part of cygport - Cygwin packaging application
# Copyright (C) 2006-2016 Yaakov Selkowitz
# Provided by the Cygwin Ports project <http://sourceware.org/cygwinports/>
#
# cygport is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# cygport is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with cygport.  If not, see <http://www.gnu.org/licenses/>.
#
################################################################################

#****h* Cygclasses/python.cygclass
#  SYNOPSIS
#  inherit python
#  DESCRIPTION
#  Python is a general-purpose, interpreted programming language used in a
#  wide variety of software.  It comes with a large standard library, and is
#  easily extendible with modules written in Python and/or C/C++.  Programs
#  can also embed a Python interpreter for use with their own extensions.
#
#  This cygclass provides definitions and some install helpers which can be used
#  by any Python-based package.  Python modules and programs are built in a
#  number of ways, so this cygclass does not provide any build functions.  The
#  two most common build systems for Python packages are Distutils and autotools.
#  NOTE
#  python.cygclass is for the 2.x versions of CPython. For the 3.x versions,
#  see python3.cygclass; for PyPy, see pypy.cygclass.
#  INHERITED BY
#  distutils.cygclass, pygtk.cygclass, rox.cygclass
#  REQUIRES
#  python
#****

# cross-compiling is not (yet?) supported
__cross_compiling_error

check_prog_req python

#****d* python.cygclass/PYTHON
#  DESCRIPTION
#  Absolute path to the Python interpreter.
#****
PYTHON=/usr/bin/python

#****d* python.cygclass/PYTHON_VERSION
#  DESCRIPTION
#  The major.minor version of the current Python interpreter.
#****
PYTHON_VERSION=$(${PYTHON} -c 'from distutils.sysconfig import * ; print get_python_version();')

#****d* python.cygclass/PYTHON_INCLUDEDIR
#  DESCRIPTION
#  Path containing the Python C library headers.
#****
PYTHON_INCLUDEDIR=$(${PYTHON} -c 'from distutils.sysconfig import * ; print get_python_inc();')

#****d* python.cygclass/PYTHON_LIB
#  DESCRIPTION
#  Path containing the Python standard library.
#  NOTE
#  Never install third-party extensions into PYTHON_LIB; use PYTHON_SITELIB instead.
#****
PYTHON_LIB=$(${PYTHON} -c 'from distutils.sysconfig import * ; print get_python_lib(0,1);')

#****d* python.cygclass/PYTHON_SITELIB
#  DESCRIPTION
#  Installation path for all Python extension modules.
#****
PYTHON_SITELIB=$(${PYTHON} -c 'from distutils.sysconfig import * ; print get_python_lib(0,0);')

#****d* python.cygclass/LIBPYTHON
#  DESCRIPTION
#  Link flags for the Python C library.  This definition must be properly quoted.
#****
LIBPYTHON="-L${PYTHON_LIB}/config -lpython${PYTHON_VERSION}"

#****I* python.cygclass/pythoninto
#  SYNOPSIS
#  pythoninto SUBDIRECTORY
#  DESCRIPTION
#  Subdirectory of PYTHON_SITELIB into which dopython should install.  This is
#  usually unnecessary.
#****
pythoninto() {
	if (( $# != 1 ))
	then
	    error "pythoninto accepts exactly one argument";
	fi

	case ${1} in
    	/*) error "pythoninto argument should be only a subdirectory" ;;
	esac

	_pythoninto_dir=${1};
}

#****I* python.cygclass/dopython
#  SYNOPSIS
#  [pythoninto SUBDIRECTORY]
#  dopython MODULE1 [MODULE2] ...
#  DESCRIPTION
#  Installs the given Python module(s) (.py or .dll) into PYTHON_SITELIB under $D,
#  or a subdirectory thereof if pythoninto was previously called.
#****
dopython() {
	local pydir
	local i
	local mode

	if defined _pythoninto_dir
	then
		pydir=${PYTHON_SITELIB}/${_pythoninto_dir}
	else
		pydir=${PYTHON_SITELIB}
	fi

	dodir ${pydir}

	for i
	do
		if [ ! -e ${i} ]
		then
			error "dopython: ${i}: file not found"
		fi

		case ${i} in
			*.dll|*.la)	mode=0755 ;;
			*)	mode=0644 ;;
		esac

		__doinstall ${mode} ${i} ${pydir} || error "dopython ${i} failed"
	done
}

#****I* python.cygclass/python_optimize
#  SYNOPSIS
#  python_optimize [DIRECTORY1] [DIRECTORY2] ...
#  DESCRIPTION
#  Bytecode-compile all Python modules found in the given directories under $D.
#  If no directories are specified, PYTHON_SITELIB under $D is assumed.
#  NOTE
#  Python modules installed with distutils_install are automatically compiled,
#  as are those installed by automake-based packages *if* they have been declared
#  as _PYTHON files (in which case a py-compile script will be present in $S).
#  Some automake-based packages mistakenly declare these as _DATA instead, in
#  which case either the Makefile.am must be patched or this function called
#  after cyginstall.
#****
python_optimize() {
	local pyd

	for pyd in "${@:-${PYTHON_SITELIB}}"
	do
		if [ ! -d ${D}${pyd} ]
		then
			error "directory ${pyd} does not exist"
		fi

		inform "Compiling ${pyd}..."
		${PYTHON} ${PYTHON_LIB}/compileall.py -f -q -d ${pyd} ${D}${pyd}
		${PYTHON} -OO ${PYTHON_LIB}/compileall.py -f -q -d ${pyd} ${D}${pyd}
	done
}

# obsolete: name may be misleading (*_compile() funcs usually build something)
python_compile() { python_optimize ${@} ; }

#****I* python.cygclass/python_fix_shebang
#  SYNOPSIS
#  python_fix_shebang SCRIPT [SCRIPT ...]
#  DESCRIPTION
#  Fixes the designated interpreter of SCRIPT to PYTHON.  This would be necessary
#  if the original uses an incorrect path (e.g. /usr/local/bin) or an
#  incorrectly versioned binary.  SCRIPT need not be prefixed by $D.
#****
python_fix_shebang() {
    __fix_shebang ${PYTHON} ${D}/${@#${D}}
}

readonly -f pythoninto dopython python_optimize python_compile python_fix_shebang
