From 3d837ee4917d6c7ef7fcecb2d9f3b2a819c944f1 Mon Sep 17 00:00:00 2001 From: "Franc[e]sco" Date: Tue, 26 Feb 2019 01:19:34 +0100 Subject: [PATCH] add generated python bindings (fixes #14) --- .gitignore | 7 ++++++ README.md | 11 +++++++-- swig/README.md | 17 ++++++++++++++ swig/build.sh | 19 +++++++++++++++ swig/oppai.i | 8 +++++++ swig/python/README.rst | 32 +++++++++++++++++++++++++ swig/python/build.sh | 10 ++++++++ swig/python/build_wheels.sh | 16 +++++++++++++ swig/python/examples/basic.py | 9 +++++++ swig/python/examples/reuse.py | 16 +++++++++++++ swig/python/publish.sh | 3 +++ swig/python/setup.cfg | 2 ++ swig/python/setup.py | 44 +++++++++++++++++++++++++++++++++++ 13 files changed, 192 insertions(+), 2 deletions(-) create mode 100644 swig/README.md create mode 100755 swig/build.sh create mode 100644 swig/oppai.i create mode 100644 swig/python/README.rst create mode 100755 swig/python/build.sh create mode 100755 swig/python/build_wheels.sh create mode 100755 swig/python/examples/basic.py create mode 100755 swig/python/examples/reuse.py create mode 100755 swig/python/publish.sh create mode 100644 swig/python/setup.cfg create mode 100644 swig/python/setup.py diff --git a/.gitignore b/.gitignore index eeb8f39..7ae767f 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,10 @@ tags *.exp /test/test_suite /test/oppai_test +/swig/**/*.c +/swig/*/*.i +/swig/python/oppai.egg-info +/swig/python/build +/swig/python/oppai.py +*.whl +*.pyc diff --git a/README.md b/README.md index eba8475..38c058a 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,7 @@ to taiko). - [installing (osx)](#installing-osx) - [usage](#usage) - [implementations for other programming languages](#implementations-for-other-programming-languages) +- [bindings for other programming languages](#bindings-for-other-programming-languages) - [oppai-ng vs old oppai](#oppai-ng-vs-old-oppai) - [compile from source (windows)](#compile-from-source-windows) - [using oppai as a library or making bindings](#using-oppai-as-a-library-or-making-bindings) @@ -98,8 +99,6 @@ If you feel like making your own implementation and want it listed here, open an issue or pull request. the requirement is that it should pass the same test suite that oppai-ng passes. -note: these aren't just native bindings unless stated otherwise. - * [ojsama (javascript)](https://github.com/Francesco149/ojsama) * [koohii (java)](https://github.com/Francesco149/koohii) . this is currently being used in tillerino. @@ -108,6 +107,14 @@ note: these aren't just native bindings unless stated otherwise. * [OppaiSharp (C#)](https://github.com/HoLLy-HaCKeR/OppaiSharp) (by HoLLy) +# bindings for other programming languages +thanks to swig it's trivial to generate native bindings for other +programming languages. bindings are an interface to the C code, meaning +that you get basically the same performance as C by sacrificing some +portability + +* [python](https://github.com/Francesco149/oppai-ng/swig/python) + # oppai-ng vs old oppai executable size is around 7 times smaller: ```sh diff --git a/swig/README.md b/swig/README.md new file mode 100644 index 0000000..aeb9208 --- /dev/null +++ b/swig/README.md @@ -0,0 +1,17 @@ +these are maintainer instructions, check the readme's in the binding +directories for user guides + +# requirements +* swig +* docker +* twine (pip) + +# building all bindings +```sh +./build.sh +``` + +# publishing all bindings +```sh +PUBLISH=1 ./build.sh +``` diff --git a/swig/build.sh b/swig/build.sh new file mode 100755 index 0000000..161aad0 --- /dev/null +++ b/swig/build.sh @@ -0,0 +1,19 @@ +#!/bin/sh + +runall() { + for d in ./*/; do + [ "$d" = "." ] && continue + cd "$d" + ./build.sh || return $? + [ ! -z $PUBLISH ] && ./publish.sh + cd .. + done +} + +dir="$(dirname "$0")" +olddir="$(pwd)" +cd "$dir" +runall +res=$? +cd "$olddir" +exit $res diff --git a/swig/oppai.i b/swig/oppai.i new file mode 100644 index 0000000..5e770a9 --- /dev/null +++ b/swig/oppai.i @@ -0,0 +1,8 @@ +%module oppai +%feature("autodoc", "3"); +%apply int *OUTPUT {int*} +%{ +#define OPPAI_IMPLEMENTATION +#include "oppai.c" +%} +#include "oppai.c" diff --git a/swig/python/README.rst b/swig/python/README.rst new file mode 100644 index 0000000..8d0c2e1 --- /dev/null +++ b/swig/python/README.rst @@ -0,0 +1,32 @@ +osu! pp and difficulty calculator. automatically generated C bindings for +https://github.com/Francesco149/oppai-ng + +usage +=========== +.. code-block:: sh + + pip install oppai + + +.. code-block:: python + + #!/usr/bin/env python + + import sys + from oppai import * + + ez = ezpp_new() + ezpp(ez, sys.argv[1]) + print("%g pp" % ezpp_pp(ez)) + ezpp_free(ez) + + +.. code-block:: sh + + ./example.py /path/to/file.osu + +.. code-block:: sh + + python -c 'help("oppai")' + +for a list of functions, or just read the top of oppai.c for better doc diff --git a/swig/python/build.sh b/swig/python/build.sh new file mode 100755 index 0000000..4984da2 --- /dev/null +++ b/swig/python/build.sh @@ -0,0 +1,10 @@ +#!/bin/sh + +rm -rf ./dist +cp ../../oppai.c . +cp ../oppai.i . +swig -python -includeall oppai.i || exit +for img in quay.io/pypa/manylinux1_x86_64 quay.io/pypa/manylinux1_i686; do + docker run --user 1000:1000 --rm -v $(pwd):/io -w /io $img \ + ./build_wheels.sh || exit +done diff --git a/swig/python/build_wheels.sh b/swig/python/build_wheels.sh new file mode 100755 index 0000000..b2f721a --- /dev/null +++ b/swig/python/build_wheels.sh @@ -0,0 +1,16 @@ +#!/bin/sh +# this is meant to be used from docker + +rm *.so +for pybin in /opt/python/*/bin +do + "$pybin/python" ./setup.py build_ext --inplace || exit + "$pybin/pip" wheel . -w dist/ || exit +done + +"$pybin/python" ./setup.py sdist || exit + +for w in dist/*linux_*.whl; do + auditwheel repair "$w" -w dist/ || exit +done +rm dist/*linux_*.whl diff --git a/swig/python/examples/basic.py b/swig/python/examples/basic.py new file mode 100755 index 0000000..032301a --- /dev/null +++ b/swig/python/examples/basic.py @@ -0,0 +1,9 @@ +#!/usr/bin/env python + +import sys +from oppai import * + +ez = ezpp_new() +ezpp(ez, sys.argv[1]) +print("%g pp" % ezpp_pp(ez)) +ezpp_free(ez) diff --git a/swig/python/examples/reuse.py b/swig/python/examples/reuse.py new file mode 100755 index 0000000..a6f627b --- /dev/null +++ b/swig/python/examples/reuse.py @@ -0,0 +1,16 @@ +#!/usr/bin/env python + +import sys +from oppai import * + +ez = ezpp_new() +ezpp_set_autocalc(ez, 1) +for osufile in sys.argv[1:]: + ezpp(ez, osufile) + print("%s - %s [%s]" % (ezpp_artist(ez), ezpp_title(ez), ezpp_version(ez))) + print("%g stars" % ezpp_stars(ez)) + for acc in range(95, 101): + ezpp_set_accuracy_percent(ez, acc) + print("%g%% -> %g pp" % (acc, ezpp_pp(ez))) + print("") +ezpp_free(ez) diff --git a/swig/python/publish.sh b/swig/python/publish.sh new file mode 100755 index 0000000..06b019b --- /dev/null +++ b/swig/python/publish.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +twine upload dist/* diff --git a/swig/python/setup.cfg b/swig/python/setup.cfg new file mode 100644 index 0000000..a600e1b --- /dev/null +++ b/swig/python/setup.cfg @@ -0,0 +1,2 @@ +[build_ext] +swig-opts=-includeall diff --git a/swig/python/setup.py b/swig/python/setup.py new file mode 100644 index 0000000..e9f6589 --- /dev/null +++ b/swig/python/setup.py @@ -0,0 +1,44 @@ +import os + +try: + from setuptools import setup, Extension +except ImportError: + from distutils.core import setup, Extension + +try: + from oppai import oppai_version_str +except Exception: + def oppai_version_str(): + return "INVALID" + +oppai_classifiers = [ + "Programming Language :: Python :: 2", + "Programming Language :: Python :: 3", + "Intended Audience :: Developers", + "License :: Public Domain", + "Topic :: Software Development :: Libraries", + "Topic :: Utilities", +] + +f = open("README.rst", "r") +oppai_readme = f.read() +f.close() + +oppai_sources=['oppai.i'] +if os.system('swig') != 0: + oppai_sources=['oppai_wrap.c', 'oppai.c'] + +setup( + name="oppai", + version=oppai_version_str(), + author="Franc[e]sco", + author_email="lolisamurai@tfwno.gf", + url="https://github.com/Francesco149/oppai-ng", + ext_modules=[Extension('_oppai', oppai_sources)], + py_modules=["oppai"], + description="osu! pp and difficulty calculator, C bindings", + long_description=oppai_readme, + license="Unlicense", + classifiers=oppai_classifiers, + keywords="osu! osu" +)