diff --git a/.common/go-binding-utils/fix_venv.sh b/.common/go-binding-utils/fix_venv.sh new file mode 100755 index 0000000..9464f98 --- /dev/null +++ b/.common/go-binding-utils/fix_venv.sh @@ -0,0 +1,16 @@ +#!/usr/bin/env bash + +set -e -x + +if [[ "${VIRTUAL_ENV}" == "" ]]; then + echo "error: VIRTUAL_ENV not set" + exit 1 +fi + +DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" + +# this is needed so that Go builds can find the Python headers etc +cp -frv "${DIR}/python-config" "${VIRTUAL_ENV}/bin/python-config" + +# this is needed so that our Go builds can find the common tooling when invoked from within a Virtualenv +cp -frv "${DIR}/native_build.sh" "${VIRTUAL_ENV}/bin/native_build.sh" diff --git a/.common/go-binding-utils/native_build.sh b/.common/go-binding-utils/native_build.sh new file mode 100755 index 0000000..aaab81b --- /dev/null +++ b/.common/go-binding-utils/native_build.sh @@ -0,0 +1,60 @@ +#!/usr/bin/env bash + +set -e -x + +if [[ "${1}" == "" ]]; then + echo "error: first argument must be name of lib (e.g. gomssql)" + exit 1 +fi + +NAME_OF_LIB="${1}" + +if [[ "${VIRTUAL_ENV}" == "" ]]; then + echo "error: a virtualenv must be activated" + exit 1 +fi + +# TODO +# GO_VERSION="$(go version)" +# if [[ ${GO_VERSION} != *"go1.13"* ]]; then +# echo "error: Go version is not 1.13 (was ${GO_VERSION})" +# exit 1 +# fi + +PKG_CONFIG_PATH="$(pwd)" +export PKG_CONFIG_PATH + +OUTPUT_PATH="${NAME_OF_LIB}_python/built" + +if [[ "${2}" != "fast" ]]; then + echo "installing python deps..." + pip install --upgrade -r requirements-dev.txt + echo "" + + echo "installing goimports..." + go get golang.org/x/tools/cmd/goimports + echo "" + + echo "installing gopy..." + go get github.com/go-python/gopy@v0.3.4 + echo "" +fi + +echo "cleaning up output folder..." +rm -frv "${OUTPUT_PATH:?}/*" || true +mkdir -p "${OUTPUT_PATH}" || true +touch "${OUTPUT_PATH}/__init__.py" || true +echo "" + +echo "building ${NAME_OF_LIB}-python..." +export PATH=${PATH}:~/go/bin/ +export CFLAGS +export C_INCLUDE_PATH +gopy build -output="${OUTPUT_PATH}" -symbols=true -vm="$(command -v python)" "./${NAME_OF_LIB}_python_go" +echo "" + +echo "hacking in some sed fixes..." +sed -i'.bak' "s/import _${NAME_OF_LIB}_python_go/from ${NAME_OF_LIB}_python.built import _${NAME_OF_LIB}_python_go/g" "${NAME_OF_LIB}_python/built/${NAME_OF_LIB}_python_go.py" +sed -i'.bak' "s/import go/from ${NAME_OF_LIB}_python.built import go/g" "${NAME_OF_LIB}_python/built/${NAME_OF_LIB}_python_go.py" +sed -i'.bak' "s/import _${NAME_OF_LIB}_python_go/from ${NAME_OF_LIB}_python.built import _${NAME_OF_LIB}_python_go/g" "${NAME_OF_LIB}_python/built/go.py" +echo "" diff --git a/.common/go-binding-utils/native_test.sh b/.common/go-binding-utils/native_test.sh new file mode 100755 index 0000000..57dc6f7 --- /dev/null +++ b/.common/go-binding-utils/native_test.sh @@ -0,0 +1,24 @@ +#!/usr/bin/env bash + +set -e + +if [[ "${VIRTUAL_ENV}" == "" ]]; then + echo "error: a virtualenv must be activated" + exit 1 +fi + +export TEST_COMMUNITY +export TEST_DATABASE +export TEST_HOSTNAME +export TEST_PASSWORD +export TEST_PORT +export TEST_RETRIES +export TEST_TIMEOUT +export TEST_USERNAME + +FOCUS="test/smoke_test.py" +if [[ "${1}" != "" ]]; then + FOCUS="${*}" +fi + +GODEBUG=cgocheck=0 python -m pytest --junit-xml "/srv/test_results/junit_results.xml" -vv -s "${FOCUS}" diff --git a/.common/go-binding-utils/python-config b/.common/go-binding-utils/python-config new file mode 100755 index 0000000..60b25f0 --- /dev/null +++ b/.common/go-binding-utils/python-config @@ -0,0 +1,74 @@ +#!/usr/local/Cellar/python@3.9/3.9.1_6/Frameworks/Python.framework/Versions/3.9/bin/python3.9 +# -*- python -*- + +# Keep this script in sync with python-config.sh.in + +import getopt +import os +import sys +import sysconfig + +valid_opts = ['prefix', 'exec-prefix', 'includes', 'libs', 'cflags', + 'ldflags', 'extension-suffix', 'help', 'abiflags', 'configdir', + 'embed'] + +def exit_with_usage(code=1): + print("Usage: {0} [{1}]".format( + sys.argv[0], '|'.join('--'+opt for opt in valid_opts)), file=sys.stderr) + sys.exit(code) + +try: + opts, args = getopt.getopt(sys.argv[1:], '', valid_opts) +except getopt.error: + exit_with_usage() + +if not opts: + exit_with_usage() + +getvar = sysconfig.get_config_var +pyver = getvar('VERSION') + +opt_flags = [flag for (flag, val) in opts] + +if '--help' in opt_flags: + exit_with_usage(code=0) + +for opt in opt_flags: + if opt == '--prefix': + print(getvar('prefix')) + + elif opt == '--exec-prefix': + print(getvar('exec_prefix')) + + elif opt in ('--includes', '--cflags'): + flags = ['-I' + sysconfig.get_path('include'), + '-I' + sysconfig.get_path('platinclude')] + if opt == '--cflags': + flags.extend(getvar('CFLAGS').split()) + print(' '.join(flags)) + + elif opt in ('--libs', '--ldflags'): + libs = [] + if '--embed' in opt_flags: + libs.append('-lpython' + pyver + sys.abiflags) + else: + libpython = getvar('LIBPYTHON') + if libpython: + libs.append(libpython) + libs.extend(getvar('LIBS').split() + getvar('SYSLIBS').split()) + + # add the prefix/lib/pythonX.Y/config dir, but only if there is no + # shared library in prefix/lib/. + if opt == '--ldflags': + if not getvar('Py_ENABLE_SHARED'): + libs.insert(0, '-L' + getvar('LIBPL')) + print(' '.join(libs)) + + elif opt == '--extension-suffix': + print(getvar('EXT_SUFFIX')) + + elif opt == '--abiflags': + print(sys.abiflags) + + elif opt == '--configdir': + print(getvar('LIBPL')) diff --git a/.common/go-binding-utils/python2.pc b/.common/go-binding-utils/python2.pc new file mode 100644 index 0000000..e7fb74b --- /dev/null +++ b/.common/go-binding-utils/python2.pc @@ -0,0 +1,12 @@ +prefix=/System/Library/Frameworks/Python.framework/Versions/2.7 +exec_prefix=${prefix} +libdir=${exec_prefix}/lib +includedir=${prefix}/include + +Name: Python +Description: Python library +Requires: +Version: 2.7 +Libs.private: -ldl -framework CoreFoundation +Libs: -L${libdir} -lpython2.7 +Cflags: -I${includedir}/python2.7 diff --git a/.gitignore b/.gitignore index 3e576dd..4445b6a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ *.iml .git .idea +go*_python/built +go*_python/py2 diff --git a/fix_venv.sh b/fix_venv.sh new file mode 100755 index 0000000..dd7241c --- /dev/null +++ b/fix_venv.sh @@ -0,0 +1,5 @@ +#!/usr/bin/env bash + +set -e -x + +.common/go-binding-utils/fix_venv.sh diff --git a/native_build.sh b/native_build.sh new file mode 100755 index 0000000..21f296a --- /dev/null +++ b/native_build.sh @@ -0,0 +1,14 @@ +#!/usr/bin/env bash + +set -e -x + +DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +pushd "${DIR}" + +if [[ -f "/tmp/native_build.sh" ]]; then + /tmp/native_build.sh gomssql "${1}" +elif which native_build.sh; then + native_build.sh gomssql "${1}" +else + .common/go-binding-utils/native_build.sh gomssql "${1}" +fi diff --git a/python2.pc b/python2.pc new file mode 100644 index 0000000..e7fb74b --- /dev/null +++ b/python2.pc @@ -0,0 +1,12 @@ +prefix=/System/Library/Frameworks/Python.framework/Versions/2.7 +exec_prefix=${prefix} +libdir=${exec_prefix}/lib +includedir=${prefix}/include + +Name: Python +Description: Python library +Requires: +Version: 2.7 +Libs.private: -ldl -framework CoreFoundation +Libs: -L${libdir} -lpython2.7 +Cflags: -I${includedir}/python2.7 diff --git a/requirements-dev.txt b/requirements-dev.txt new file mode 100644 index 0000000..1ba11bd --- /dev/null +++ b/requirements-dev.txt @@ -0,0 +1,4 @@ +-r requirements.txt + +PyBindGen==0.21.0 +pytest==4.6.11