forked from kivy/python-for-android
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
102 lines (87 loc) · 3.22 KB
/
Copy path__init__.py
File metadata and controls
102 lines (87 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
from os.path import basename, dirname, exists, join
import zipfile
from pythonforandroid.logger import info
from pythonforandroid.recipe import PythonRecipe, current_directory, shprint
from pythonforandroid.util import BuildInterruptingException, ensure_dir
class VoskRecipe(PythonRecipe):
version = "0.3.45"
url = "https://github.com/alphacep/vosk-api/archive/refs/tags/v{version}.tar.gz"
site_packages_name = "vosk"
depends = ["cffi"]
python_depends = ["requests", "tqdm", "srt", "websockets"]
hostpython_prerequisites = [
"setuptools",
"wheel",
"cffi",
"requests",
"tqdm",
"srt",
"websockets",
]
call_hostpython_via_targetpython = False
android_aar_url = (
"https://repo.maven.apache.org/maven2/com/alphacephei/"
"vosk-android/{version}/vosk-android-{version}.aar"
)
android_aar_abis = {
"arm64-v8a": "arm64-v8a",
"armeabi-v7a": "armeabi-v7a",
"x86": "x86",
"x86_64": "x86_64",
}
def build_arch(self, arch):
self.install_hostpython_prerequisites()
env = self.get_recipe_env(arch)
python_dir = join(self.get_build_dir(arch.arch), "python")
install_dir = self.ctx.get_python_install_dir(arch.arch)
info("Installing Vosk Python bindings into site-packages")
with current_directory(python_dir):
shprint(
self._host_recipe.pip,
"install",
".",
"--compile",
"--no-deps",
"--target",
install_dir,
_env=env,
)
self.install_android_libvosk(arch)
def install_android_libvosk(self, arch):
aar_abi = self.android_aar_abis.get(arch.arch)
if aar_abi is None:
supported = ", ".join(sorted(self.android_aar_abis))
raise BuildInterruptingException(
f"Vosk does not provide an Android library for {arch.arch}. "
f"Supported ABIs: {supported}"
)
aar_path = self.download_android_aar()
aar_lib_path = f"jni/{aar_abi}/libvosk.so"
target_path = join(
self.ctx.get_python_install_dir(arch.arch),
self.site_packages_name,
"libvosk.so",
)
ensure_dir(dirname(target_path))
info(f"Installing Android {arch.arch} libvosk.so")
try:
with zipfile.ZipFile(aar_path) as aar:
with aar.open(aar_lib_path) as source, open(target_path, "wb") as target:
target.write(source.read())
except KeyError as exc:
raise BuildInterruptingException(
f"{basename(aar_path)} does not contain {aar_lib_path}"
) from exc
def download_android_aar(self):
aar_dir = join(self.ctx.packages_path, "vosk-android")
ensure_dir(aar_dir)
aar_name = f"vosk-android-{self.version}.aar"
aar_path = join(aar_dir, aar_name)
if not exists(aar_path):
self.download_file(
self.android_aar_url.format(version=self.version),
aar_name,
cwd=aar_dir,
)
return aar_path
recipe = VoskRecipe()