-
Notifications
You must be signed in to change notification settings - Fork 11
/
setup.py
109 lines (90 loc) · 3.04 KB
/
setup.py
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
103
104
105
106
107
108
109
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import pathlib
import setuptools
import shutil
import sys
import tempfile
import zipfile
# the directory that contains all RIFE models
# required only for building wheels
MODELS_PATH = (
pathlib.Path(__file__).parent
/ "rife_ncnn_vulkan_python"
/ "rife-ncnn-vulkan"
/ "models"
)
# HACK: allows pip to load user site packages when running setup
if "PYTHONNOUSERSITE" in os.environ:
os.environ.pop("PYTHONNOUSERSITE")
sys.path.append(
str(
pathlib.Path.home()
/ ".local/lib/python{}.{}/site-packages".format(
sys.version_info.major, sys.version_info.minor
)
)
)
sys.path.append(
"/usr/lib/python{}.{}/site-packages".format(
sys.version_info.major, sys.version_info.minor
)
)
# external modules must be imported after the hack
import cmake_build_extension
import requests
def download(url, chunk_size=4096):
# create a temporary file to contain the downloaded file
handle, path_str = tempfile.mkstemp()
path = pathlib.Path(path_str)
# create download stream and size
stream = requests.get(url, stream=True, allow_redirects=True)
total_size = int(stream.headers.get("content-length", 0))
# print download information summary
print(f"Downloading: {url}")
print(f"Total size: {total_size}")
print(f"Chunk size: {chunk_size}")
print(f"Saving to: {path}")
# Write content into file
with os.fdopen(handle, "wb") as output:
for chunk in stream.iter_content(chunk_size=chunk_size):
output.write(chunk) if chunk else None
# return the full path of saved file
return path
def download_models() -> None:
rife_ncnn_vulkan_zip = download(
"https://github.com/nihui/rife-ncnn-vulkan/archive/"
"c806e66490679aebc1b4a6832985e004fd552f46.zip"
)
with zipfile.ZipFile(rife_ncnn_vulkan_zip) as archive:
with tempfile.TemporaryDirectory() as tempdir:
archive.extractall(tempdir)
shutil.move(
pathlib.Path(tempdir) / archive.namelist()[0] / "models", MODELS_PATH
)
# remove the temporary files/dirs
rife_ncnn_vulkan_zip.unlink()
cmake_flags = [
"-DBUILD_SHARED_LIBS:BOOL=OFF",
"-DCALL_FROM_SETUP_PY:BOOL=ON",
]
cmake_flags.extend(os.environ.get("CMAKE_FLAGS", "").split())
# when building bdist wheels/installing the package
if sys.argv[1] == "bdist_wheel":
# if the models are not present
# download and extract the models from nihui/rife-ncnn-vulkan
if not MODELS_PATH.exists():
download_models()
setuptools.setup(
ext_modules=[
cmake_build_extension.CMakeExtension(
name="rife-ncnn-vulkan-python",
install_prefix="rife_ncnn_vulkan_python",
write_top_level_init="from .rife_ncnn_vulkan import Rife, RIFE, wrapped",
source_dir=str(pathlib.Path(__file__).parent / "rife_ncnn_vulkan_python"),
cmake_configure_options=cmake_flags,
)
],
cmdclass={"build_ext": cmake_build_extension.BuildExtension},
)