Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use standard Python tool to build Python extension #493

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/lib/libsnowboy-detect.a
snowboy-detect-swig.cc
snowboydetect.py
snowboy-detect-swig_wrap.cpp
Snowboy.pm
.DS_Store

Expand Down Expand Up @@ -52,3 +52,4 @@ Snowboy.pm
/dist
**/snowboy.egg-info
/.idea
.vscode
2 changes: 1 addition & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ exclude *.pyc
global-exclude .DS_Store _snowboydetect.so
prune resources/alexa
prune lib/ios
prune lib/android
prune lib/android
1 change: 0 additions & 1 deletion examples/Perl/Snowboy.so

This file was deleted.

1 change: 0 additions & 1 deletion examples/Python/_snowboydetect.so

This file was deleted.

3 changes: 2 additions & 1 deletion examples/Python/demo.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import snowboydecoder
import sys
import signal

from snowboy import snowboydecoder

interrupted = False


Expand Down
3 changes: 2 additions & 1 deletion examples/Python/demo2.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import snowboydecoder
import sys
import signal

from snowboy import snowboydecoder

# Demo code for listening to two hotwords at the same time

interrupted = False
Expand Down
3 changes: 2 additions & 1 deletion examples/Python/demo3.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import snowboydecoder
import sys
import wave

from snowboy import snowboydecoder

# Demo code for detecting hotword in a .wav file
# Example Usage:
# $ python demo3.py resources/snowboy.wav resources/models/snowboy.umdl
Expand Down
5 changes: 3 additions & 2 deletions examples/Python/demo4.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import snowboydecoder
import sys
import signal
import speech_recognition as sr
import os

import speech_recognition as sr
from snowboy import snowboydecoder

"""
This demo file shows you how to use the new_message_callback to interact with
the recorded audio after a keyword is spoken. It uses the speech recognition
Expand Down
3 changes: 2 additions & 1 deletion examples/Python/demo_arecord.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import snowboydecoder_arecord
import sys
import signal

from snowboy import snowboydecoder_arecord

interrupted = False


Expand Down
3 changes: 2 additions & 1 deletion examples/Python/demo_threaded.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import snowboythreaded
import sys
import signal
import time

from snowboy import snowboythreaded

stop_program = False

# This a demo that shows running Snowboy in another thread
Expand Down
1 change: 0 additions & 1 deletion examples/Python/resources

This file was deleted.

File renamed without changes.
1 change: 1 addition & 0 deletions examples/Python/snowboy/resources
1 change: 1 addition & 0 deletions examples/Python/snowboy/snowboydetect.py
1 change: 0 additions & 1 deletion examples/Python/snowboydetect.py

This file was deleted.

1 change: 0 additions & 1 deletion examples/Python3/_snowboydetect.so

This file was deleted.

3 changes: 2 additions & 1 deletion examples/Python3/demo.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import snowboydecoder
import sys
import signal

from snowboy import snowboydecoder

interrupted = False


Expand Down
3 changes: 2 additions & 1 deletion examples/Python3/demo2.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import snowboydecoder
import sys
import signal

from snowboy import snowboydecoder

# Demo code for listening to two hotwords at the same time

interrupted = False
Expand Down
3 changes: 2 additions & 1 deletion examples/Python3/demo3.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import snowboydecoder
import sys
import wave

from snowboy import snowboydecoder

# Demo code for detecting hotword in a .wav file
# Example Usage:
# $ python demo3.py resources/snowboy.wav resources/models/snowboy.umdl
Expand Down
5 changes: 3 additions & 2 deletions examples/Python3/demo4.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import snowboydecoder
import sys
import signal
import speech_recognition as sr
import os

import speech_recognition as sr
from snowboy import snowboydecoder

"""
This demo file shows you how to use the new_message_callback to interact with
the recorded audio after a keyword is spoken. It uses the speech recognition
Expand Down
1 change: 0 additions & 1 deletion examples/Python3/resources

This file was deleted.

Empty file.
1 change: 1 addition & 0 deletions examples/Python3/snowboy/resources
1 change: 1 addition & 0 deletions examples/Python3/snowboy/snowboydetect.py
1 change: 0 additions & 1 deletion examples/Python3/snowboydetect.py

This file was deleted.

79 changes: 47 additions & 32 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,58 @@
import os
import sys
from setuptools import setup, find_packages
import ctypes.util
from setuptools import setup, find_packages, Extension
from distutils.command.build import build
from distutils.dir_util import copy_tree
from subprocess import call


py_dir = 'Python' if sys.version_info[0] < 3 else 'Python3'
PY_DIR = 'Python' if sys.version_info[0] < 3 else 'Python3'
PACKAGES_SEARCH = os.path.join('examples', PY_DIR)
SNOWBOY_PACKAGE_DIR = os.path.join(PACKAGES_SEARCH, 'snowboy')

class SnowboyBuild(build):

def run(self):
def get_libsnowboy_folder():
if sys.platform == 'darwin':
return 'lib/osx'
machine = os.uname().machine
folder = ''
if machine.startswith('arm'):
folder = 'rpi'
elif machine == 'x86_64':
folder = 'ubuntu64'
elif machine == 'aarch64':
folder = 'aarch64-ubuntu1604'
else:
raise OSError("Unsupported platform {}".format(machine))
return os.path.join('lib', folder)

cmd = ['make']
swig_dir = os.path.join('swig', py_dir)
def compile():
call(cmd, cwd=swig_dir)

self.execute(compile, [], 'Compiling snowboy...')
cxx_flags = ['-O3', '-D_GLIBCXX_USE_CXX11_ABI=0']
libraries = ['m', 'dl', 'snowboy-detect']
link_args = []

# copy generated .so to build folder
self.mkpath(self.build_lib)
snowboy_build_lib = os.path.join(self.build_lib, 'snowboy')
self.mkpath(snowboy_build_lib)
target_file = os.path.join(swig_dir, '_snowboydetect.so')
if not self.dry_run:
self.copy_file(target_file,
snowboy_build_lib)
if sys.platform == 'darwin':
link_args = ['-framework', 'Accelerate', '-bundle', '-flat_namespace', '-undefined', 'suppress']
else:
cxx_flags.append('-std=c++0x')
libraries.extend(['f77blas', 'cblas', 'atlas'])

# copy resources too since it is a symlink
resources_dir = 'resources'
resources_dir_on_build = os.path.join(snowboy_build_lib,
'resources')
copy_tree(resources_dir, resources_dir_on_build)

build.run(self)
if ctypes.util.find_library('lapack_atlas'):
libraries.append('lapack_atlas')
else:
libraries.append('lapack')

ext_modules = [
Extension(
'_snowboydetect',
['swig/{}/snowboy-detect-swig.i'.format(PY_DIR)],
swig_opts=['-c++'],
include_dirs=['.'],
libraries=libraries,
extra_compile_args=cxx_flags,
extra_link_args=link_args,
library_dirs=[get_libsnowboy_folder()]
)
]

setup(
name='snowboy',
Expand All @@ -45,17 +62,15 @@ def compile():
maintainer_email='[email protected]',
license='Apache-2.0',
url='https://snowboy.kitt.ai',
packages=find_packages(os.path.join('examples', py_dir)),
package_dir={'snowboy': os.path.join('examples', py_dir)},
ext_modules=ext_modules,
packages=find_packages(PACKAGES_SEARCH),
package_dir={'snowboy': SNOWBOY_PACKAGE_DIR},
py_modules=['snowboy.snowboydecoder', 'snowboy.snowboydetect'],
package_data={'snowboy': ['resources/*']},
package_data={'snowboy': ['resources/*', 'resources/models/*']},
zip_safe=False,
long_description="",
classifiers=[],
install_requires=[
'PyAudio',
],
cmdclass={
'build': SnowboyBuild
}
)
Loading