11from pathlib import Path
22import sys
3- from os import path as osp
43import subprocess
54from setuptools import setup , Extension , find_packages
65from setuptools .command .build_ext import build_ext
76
87""" You may want to change the following variables to customize your project """
8+ # Root directory:
9+ ROOT_DIR = Path (__file__ ).parent
910# Name of your package; Must match the directory name under `CSRC_DIR`:
1011PKG_NAME = "example_package"
1112# Path to the directory of setup.py file:
@@ -31,7 +32,6 @@ def __init__(self, name, source_dir, build_dir, install_dir):
3132
3233
3334class CMakeBuild (build_ext ):
34-
3535 def run (self ):
3636 try :
3737 subprocess .check_output (["cmake" , "--version" ])
@@ -42,20 +42,19 @@ def run(self):
4242 self .build_extension (ext )
4343
4444 def build_extension (self , ext : CMakeExtension ):
45- build_args = [
46- "-S" ,
47- ext .source_dir ,
48- "-B" ,
49- ext .build_dir ,
50- "Release" ,
51- ]
45+ build_args = ["-S" , ext .source_dir ,
46+ "-B" , ext .build_dir ,
47+ "Release" ] # fmt: skip
5248 # If Current Platform is Windows
5349 if sys .platform == "win32" :
5450 subprocess .check_call (
55- [R"csrc\scripts\msvc-bash.bat" , R"csrc\scripts\build.sh" ] + build_args
51+ [R"csrc\scripts\msvc-bash.bat" , R"csrc\scripts\build.sh" ]
52+ + build_args
5653 )
5754 else :
58- subprocess .check_call (["bash" , "csrc/scripts/build.sh" ] + build_args )
55+ subprocess .check_call (
56+ ["bash" , "csrc/scripts/build.sh" ] + build_args
57+ )
5958 install_args = [
6059 "--install" ,
6160 ext .build_dir ,
@@ -65,6 +64,29 @@ def build_extension(self, ext: CMakeExtension):
6564 subprocess .check_call (["cmake" ] + install_args )
6665
6766
67+ def get_requirements (root_dir : Path ) -> list [str ]:
68+ """Get Python package dependencies from requirements.txt."""
69+ requirements_dir = root_dir / "requirements"
70+
71+ def _read_requirements (filename : str ) -> list [str ]:
72+ with open (requirements_dir / filename ) as f :
73+ requirements = f .read ().strip ().split ("\n " )
74+ resolved_requirements = []
75+ for line in requirements :
76+ if line .startswith ("-r " ):
77+ resolved_requirements += _read_requirements (line .split ()[1 ])
78+ elif (
79+ not line .startswith ("--" )
80+ and not line .startswith ("#" )
81+ and line .strip () != ""
82+ ):
83+ resolved_requirements .append (line )
84+ return resolved_requirements
85+
86+ requirements = _read_requirements ("common.txt" )
87+ return requirements
88+
89+
6890setup (
6991 ext_modules = [
7092 CMakeExtension (
@@ -81,4 +103,5 @@ def build_extension(self, ext: CMakeExtension):
81103 # Use relative path here
82104 PKG_NAME : ["_torch_ops/lib/*.so" , "_torch_ops/lib/*.dll" ]
83105 },
106+ install_requires = get_requirements (ROOT_DIR ),
84107)
0 commit comments