Skip to content

Commit

Permalink
Add Python 3 support (#33)
Browse files Browse the repository at this point in the history
Adding backwards-compatible Python 3 support, tested up to 3.8.2.
Verified to still work with Python 2.7.
Also adds a makefile and scripts to build binary manylinux wheels for several python versions.
  • Loading branch information
bjorn-spire authored May 11, 2020
1 parent 2e2e64a commit 810f4b3
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ lib64/
parts/
sdist/
var/
wheelhouse/
wheels/
*.egg-info/
.installed.cfg
Expand Down
14 changes: 14 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
.PHONEY: help
help:
@echo "Targets:"
@echo " clean: Removes distribution folders and artifacts from building"
@echo " manylinux-wheels: Builds binary and manylinux wheels for several python versions"

.PHONY: clean
clean:
rm -rf dist wheelhouse build __pycache__ *.so *.egg-info

.PHONY: manylinux-wheels
manylinux-wheels:
docker run -v $(shell pwd):/io quay.io/pypa/manylinux1_x86_64:latest \
/io/bin/build-manylinux-wheel.sh 27 35 36 37 38
36 changes: 36 additions & 0 deletions bin/build-manylinux-wheel.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/bash

PYTHON_VERSIONS=$@

if [[ -z "${PYTHON_VERSIONS}" ]] ; then
echo "Usage: $(basename $0) <version>..." >&2
echo " A version is the two numbers in the major.minor python that you want to build" >&2
echo " Example: for python 2.7 and 3.8 both" >&2
echo "" >&2
echo " $ $(basename $0) 27 38" >&2
exit 1
fi

function get-variants() {
local version="$1"

if [[ "${version}" = '38' ]] ; then
echo /
elif [[ "${version}" = '27' ]] ; then
echo m mu
else
echo m
fi
}

cd /io

for version in ${PYTHON_VERSIONS} ; do
for variant in $(get-variants "${version}") ; do
/opt/python/cp${version}-cp${version}${variant}/bin/python setup.py bdist_wheel
done
done

for f in dist/*.whl ; do
/opt/python/cp37-cp37m/bin/auditwheel repair "${f}"
done
34 changes: 33 additions & 1 deletion predict.c
Original file line number Diff line number Diff line change
Expand Up @@ -3678,11 +3678,29 @@ static PyMethodDef pypredict_funcs[] = {
{NULL, NULL, 0, NULL}
};

void initcpredict(void)
#if PY_MAJOR_VERSION >= 3
static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
"cpredict", /* m_name */
"Python port of the predict open source satellite tracking library", /* m_doc */
-1, /* m_size */
pypredict_funcs, /* m_methods */
NULL, /* m_reload */
NULL, /* m_traverse */
NULL, /* m_clear */
NULL, /* m_free */
};
#endif

static PyObject * cpredictinit(void)
{
PyObject *m;
#if PY_MAJOR_VERSION >= 3
m = PyModule_Create(&moduledef);
#else
m = Py_InitModule3("cpredict", pypredict_funcs,
"Python port of the predict open source satellite tracking library");
#endif
if (m == NULL) {
fprintf(stderr, "ERROR: Unable to initialize python module 'cpredict'\n");
}
Expand All @@ -3696,4 +3714,18 @@ void initcpredict(void)
NoTransitException = PyErr_NewException("cpredict.NoTransitException", PredictException, NULL);
Py_INCREF(NoTransitException);
PyModule_AddObject(m, "NoTransitException", NoTransitException);

return m;
}

#if PY_MAJOR_VERSION >= 3
PyMODINIT_FUNC PyInit_cpredict(void)
{
return cpredictinit();
}
#else
PyMODINIT_FUNC initcpredict(void)
{
cpredictinit();
}
#endif
5 changes: 4 additions & 1 deletion predict.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
import os
import time

Expand All @@ -19,6 +20,8 @@ def quick_predict(tle, ts, qth):
return _quick_predict(tle, ts, qth)


STR_TYPE = str if sys.version_info.major > 2 else basestring # Quick python3 compatibility

def host_qth(path="~/.predict/predict.qth"):
path = os.path.abspath(os.path.expanduser(path))
try:
Expand All @@ -33,7 +36,7 @@ def host_qth(path="~/.predict/predict.qth"):
def massage_tle(tle):
try:
# TLE may or may not have been split into lines already
if isinstance(tle, basestring):
if isinstance(tle, STR_TYPE):
tle = tle.rstrip().split('\n')
assert len(tle) == 3, "TLE must be 3 lines, not %d: %s" % (len(tle), tle)
return tle
Expand Down
13 changes: 9 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
#!/usr/bin/env python

from distutils.core import setup, Extension
from setuptools import setup, Extension
setup(
name='pypredict',
version='1.5.0',
version='1.6.0',
author="Jesse Trutna",
author_email="[email protected]",
url="https://github.com/nsat/pypredict",
py_modules=['predict'],
ext_modules=[Extension('cpredict', ['predict.c'])],
classifiers=[
'License :: OSI Approved :: GNU General Public License v2 or later (GPLv2+)',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
],
py_modules=['predict'],
ext_modules=[Extension('cpredict', ['predict.c'])]
)

0 comments on commit 810f4b3

Please sign in to comment.