-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup_cython.py
176 lines (149 loc) · 5.54 KB
/
setup_cython.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# coding: utf-8
import os
import sys
import fnmatch
import subprocess
import sysconfig
from setuptools import setup, find_packages
from setuptools.command.build_py import build_py as _build_py
from setuptools.extension import Extension
from Cython.Build import cythonize
from Cython.Distutils import build_ext
from pathlib import Path
import shutil
# NOTICE: For compatible with python windows, all build package must use Microsoft build tool
# TODO: replace Microsoft Build Tool cmd with your system
if sys.platform == 'win32':
# patch env with vcvarsall.bat from vs2015 (vc14)
target_s = 'x86_amd64'
cmd = '"C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\BuildTools\\VC\\Auxiliary\\Build\\vcvarsall.bat" ' \
'{} >nul 2>&1 && set'.format(target_s)
try:
out = subprocess.check_output(cmd, stderr=subprocess.STDOUT, universal_newlines=True)
except Exception as e:
print("Error executing {}".format(cmd))
raise e
for key, _, value in (line.partition('=') for line in out.splitlines()):
if key and value:
os.environ[key] = value
# inform setuptools that the env is already set
os.environ['DISTUTILS_USE_SDK'] = '1'
# The module folder, your application will be named as its also
NAME_APP = 'app'
# Exclude file, when you don't want to compile these files
# Must use OS independent path name
EXCLUDE_FILES = [
# os.path.join('app','main.py'),
]
# noinspection PyPep8Naming
class build_py(_build_py):
def find_package_modules(self, package, package_dir):
ext_suffix = sysconfig.get_config_var('EXT_SUFFIX')
modules = super().find_package_modules(package, package_dir)
filtered_modules = []
for (pkg, mod, filepath) in modules:
if os.path.exists(filepath.replace('.py', ext_suffix)):
continue
filtered_modules.append((pkg, mod, filepath,))
return filtered_modules
def build_packages(self):
"""
https://stackoverflow.com/a/56043918
"""
pass
def get_ext_paths(root_dir, exclude_files):
"""
Get path of all .py files for compilation
@param root_dir: Root directory for searching .py file
@param exclude_files: exclude files list
@return: list of .py files, except exclude_files
example [Extension("app.*", ["app/*.py"]),]
"""
paths = []
for root, dirs, files in os.walk(root_dir):
for filename in files:
# ignore file without .py extension
if os.path.splitext(filename)[1] != '.py':
continue
# join to root dir
file_path = os.path.join(root, filename)
# filter out exclude files
if file_path in exclude_files:
continue
paths.append(file_path)
return paths
def get_export_symbols_fixed(self, ext):
"""
https://stackoverflow.com/a/58826688
Function to find all module __init__.py for Windows compilation
@param self:
@param ext:
@return:
"""
names = ext.name.split('.')
if names[-1] != "__init__":
initfunc_name = "PyInit_" + names[-1]
else:
# take name of the package if it is an __init__-file
initfunc_name = "PyInit_" + names[-2]
if initfunc_name not in ext.export_symbols:
ext.export_symbols.append(initfunc_name)
return ext.export_symbols
class MyBuildExt(build_ext):
def run(self):
build_ext.get_export_symbols = get_export_symbols_fixed
build_ext.run(self)
build_dir = Path(self.build_lib)
root_dir = Path(__file__).parent
target_dir = build_dir if not self.inplace else root_dir
# TODO: Manually copy all your module __init__.py here
self.copy_file(Path(NAME_APP) / '__init__.py', root_dir, target_dir)
# TODO: When you have submodule __init__.py, please do copy them also
# self.copy_file(Path(NAME_APP) / 'submodule' / '__init__.py', root_dir, target_dir)
# or
# self.copy_file(Path('submodule') / '__init__.py', root_dir, target_dir)
# TODO: Copy include files
# for f in EXCLUDE_FILES:
# self.copy_file(Path(f), Path('.'), target_dir)
@classmethod
def copy_file(cls, path, source_dir, destination_dir):
"""
Copy a path from source_dir to destination_dir
@param path: file in Path format
@param source_dir: source directory in Path format
@param destination_dir: destination directory in in Path format
@return: None
"""
if not (source_dir / path).exists():
return
shutil.copyfile(str(source_dir / path), str(destination_dir / path))
setup(
name=NAME_APP,
version='0.1.0',
packages=find_packages(),
ext_modules=cythonize(
get_ext_paths(NAME_APP, EXCLUDE_FILES),
build_dir="build",
compiler_directives={'language_level': 3,
'always_allow_keywords': True,
}
),
# data_files=[('includes', ['app/includes/example.bin', 'app/includes/example.py'])],
package_data={
NAME_APP: ['includes/example.bin', 'includes/example.py'],
},
cmdclass={
'build_py': build_py,
'build_ext': MyBuildExt,
},
entry_points={
'console_scripts': [
'app = app.main:main',
],
},
)
# build command
# python3 setup_cython.py bdist_wheel
# python3 setup_cython.py install
# rm -rf app.egg-info dist build
# unzip dist/app-0.1.0-cp36-cp36m-linux_x86_64.whl -d dist/app