Skip to content

Commit

Permalink
add generated python bindings (fixes #14)
Browse files Browse the repository at this point in the history
  • Loading branch information
Francesco149 committed Feb 26, 2019
1 parent c1462db commit 3d837ee
Show file tree
Hide file tree
Showing 13 changed files with 192 additions and 2 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Expand Up @@ -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
11 changes: 9 additions & 2 deletions README.md
Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand Down
17 changes: 17 additions & 0 deletions 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
```
19 changes: 19 additions & 0 deletions 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
8 changes: 8 additions & 0 deletions 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"
32 changes: 32 additions & 0 deletions 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
10 changes: 10 additions & 0 deletions 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
16 changes: 16 additions & 0 deletions 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
9 changes: 9 additions & 0 deletions 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)
16 changes: 16 additions & 0 deletions 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)
3 changes: 3 additions & 0 deletions swig/python/publish.sh
@@ -0,0 +1,3 @@
#!/bin/sh

twine upload dist/*
2 changes: 2 additions & 0 deletions swig/python/setup.cfg
@@ -0,0 +1,2 @@
[build_ext]
swig-opts=-includeall
44 changes: 44 additions & 0 deletions 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"
)

0 comments on commit 3d837ee

Please sign in to comment.