-
-
Save KD-MM2/6a8f832cdf9ebd608de86930cec5e0ed to your computer and use it in GitHub Desktop.
Cython setup.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from setuptools import setup, find_packages | |
from Cython.Build import cythonize | |
import os | |
import shutil | |
import glob | |
# Set build directory for Cython-generated files | |
build = "build" | |
src = "src" | |
package_name = "whl_packaging_demo" # Thay đổi nếu cần | |
# Xóa thư mục build hiện tại nếu tồn tại | |
if os.path.exists(build): | |
shutil.rmtree(build) | |
# Tạo thư mục build mới | |
os.makedirs(build) | |
# Danh sách để lưu tất cả các file .pyx cần biên dịch | |
pyx_files = [] | |
# Hàm đệ quy để sao chép cấu trúc thư mục và file | |
def process_directory(src_dir, dest_dir): | |
# Tạo thư mục đích nếu không tồn tại | |
if not os.path.exists(dest_dir): | |
os.makedirs(dest_dir) | |
# Duyệt qua tất cả các file và thư mục con | |
for item in os.listdir(src_dir): | |
src_item = os.path.join(src_dir, item) | |
dest_item = os.path.join(dest_dir, item) | |
if os.path.isdir(src_item): | |
# Nếu là thư mục, gọi đệ quy | |
process_directory(src_item, dest_item) | |
elif os.path.isfile(src_item): | |
if item.endswith('.py'): | |
# Xử lý theo quy tắc cho file .py | |
if item in ("__init__.py", "__main__.py"): | |
# Giữ nguyên extension cho file đặc biệt | |
shutil.copy(src_item, dest_item) | |
else: | |
# Đổi extension .py thành .pyx cho các file khác | |
dest_pyx = dest_item.replace('.py', '.pyx') | |
shutil.copy(src_item, dest_pyx) | |
pyx_files.append(dest_pyx) | |
else: | |
# Sao chép các file không phải .py mà không thay đổi | |
shutil.copy(src_item, dest_item) | |
# Bắt đầu quá trình xử lý từ thư mục nguồn | |
src_path = os.path.join(src, package_name) | |
dest_path = os.path.join(build, package_name) | |
process_directory(src_path, dest_path) | |
# Tìm tất cả các package trong thư mục build | |
packages = [] | |
for root, dirs, files in os.walk(os.path.join(build, package_name)): | |
if '__init__.py' in files or '__init__.pyx' in files: | |
package = root.replace(build + os.sep, '').replace(os.sep, '.') | |
packages.append(package) | |
print(f"Compiling {len(pyx_files)} .pyx files") | |
print(f"Found {len(packages)} packages: {packages}") | |
# Biên dịch các file .pyx | |
ext_modules = cythonize( | |
pyx_files, | |
compiler_directives={ | |
"language_level": "3", | |
"embedsignature": True, | |
}, | |
build_dir=build, | |
) | |
# Thiết lập cài đặt | |
setup( | |
name="whl-packaging-demo", | |
version="0.1.0", | |
packages=packages, | |
package_dir={package_name: dest_path}, | |
ext_modules=ext_modules, | |
include_package_data=False, | |
zip_safe=False, | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment