From: Kefu Chai Date: Wed, 28 Jan 2026 02:43:50 +0000 (+0800) Subject: pybind: respect CYTHON_BUILD_DIR for all Python bindings X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=e57e9c52a43913ea082203c0371df3fe2fccbdb7;p=ceph-ci.git pybind: respect CYTHON_BUILD_DIR for all Python bindings Allow the *_processed.pyx files to be generated in a build directory specified by the CYTHON_BUILD_DIR environment variable, instead of always writing to the current working directory or source directory. Changes for all bindings (rados, cephfs, rbd, rgw): - Add cython_build_dir variable from environment - Refactor Tempita processing to write output to build_dir - Use os.path.relpath for the source path - Pass cython_build_dir to cythonize() function This ensures consistent behavior across all Python bindings and allows for cleaner separation of source and build artifacts. Signed-off-by: Kefu Chai --- diff --git a/src/pybind/cephfs/setup.py b/src/pybind/cephfs/setup.py index a44d4708faa..a2f3b824788 100755 --- a/src/pybind/cephfs/setup.py +++ b/src/pybind/cephfs/setup.py @@ -154,6 +154,7 @@ elif check_sanity(): include_path=include_path) else: sys.exit(1) +cython_build_dir=os.environ.get("CYTHON_BUILD_DIR", None) cmdclass = {} try: @@ -175,26 +176,18 @@ except ImportError: source = "cephfs.c" else: # Process Tempita template - source_pyx = os.path.join( - os.path.dirname(os.path.abspath(__file__)), - "cephfs.pyx" - ) + source_dir = os.path.dirname(os.path.abspath(__file__)) + source_pyx = os.path.join(source_dir, "cephfs.pyx") + build_dir = cython_build_dir or source_dir + output_pyx = os.path.join(build_dir, "cephfs_processed.pyx") - # Read the template from source with open(source_pyx) as f: template_content = f.read() - - # Process the template with cython_constants processed = Tempita.sub(template_content, **cython_constants) - - # Write processed output to current working directory - # (which is the build directory when invoked by CMake) - output_pyx = "cephfs_processed.pyx" - with open(output_pyx, 'w') as f: f.write(processed) - source = output_pyx + source = os.path.relpath(output_pyx, os.getcwd()) # Disable cythonification if we're not really building anything if (len(sys.argv) >= 2 and @@ -226,7 +219,7 @@ setup( **ext_args ) ], - build_dir=os.environ.get("CYTHON_BUILD_DIR", None), + build_dir=cython_build_dir, **cythonize_args ), classifiers=[ diff --git a/src/pybind/rados/setup.py b/src/pybind/rados/setup.py index ec345f9718c..9c4bca6e68b 100755 --- a/src/pybind/rados/setup.py +++ b/src/pybind/rados/setup.py @@ -146,6 +146,7 @@ elif check_sanity(): cython_constants = dict(BUILD_DOC=False, UNAME_SYSNAME=platform.system()) else: sys.exit(1) +cython_build_dir=os.environ.get("CYTHON_BUILD_DIR", None) cmdclass = {} try: @@ -167,26 +168,18 @@ except ImportError: source = "rados.c" else: # Process Tempita template - source_pyx = os.path.join( - os.path.dirname(os.path.abspath(__file__)), - "rados.pyx" - ) + source_dir = os.path.dirname(os.path.abspath(__file__)) + source_pyx = os.path.join(source_dir, "rados.pyx") + build_dir = cython_build_dir or source_dir + output_pyx = os.path.join(build_dir, "rados_processed.pyx") - # Read the template from source with open(source_pyx) as f: template_content = f.read() - - # Process the template with cython_constants processed = Tempita.sub(template_content, **cython_constants) - - # Write processed output to current working directory - # (which is the build directory when invoked by CMake) - output_pyx = "rados_processed.pyx" - with open(output_pyx, 'w') as f: f.write(processed) - source = output_pyx + source = os.path.relpath(output_pyx, os.getcwd()) # Disable cythonification if we're not really building anything if (len(sys.argv) >= 2 and @@ -218,7 +211,7 @@ setup( ) ], compile_time_env=cython_constants, - build_dir=os.environ.get("CYTHON_BUILD_DIR", None), + build_dir=cython_build_dir, ), classifiers=[ 'Intended Audience :: Developers', diff --git a/src/pybind/rbd/setup.py b/src/pybind/rbd/setup.py index b3a1ab4ca9c..f0dafec0a69 100755 --- a/src/pybind/rbd/setup.py +++ b/src/pybind/rbd/setup.py @@ -152,6 +152,7 @@ elif check_sanity(): include_path=include_path) else: sys.exit(1) +cython_build_dir=os.environ.get("CYTHON_BUILD_DIR", None) cmdclass = {} compiler_directives = {} @@ -176,25 +177,19 @@ except ImportError: source = "rbd.c" else: # Process Tempita template - source_pyx = os.path.join( - os.path.dirname(os.path.abspath(__file__)), - "rbd.pyx" - ) + source_dir = os.path.dirname(os.path.abspath(__file__)) + source_pyx = os.path.join(source_dir, "rbd.pyx") + build_dir = cython_build_dir or source_dir + output_pyx = os.path.join(build_dir, "rbd_processed.pyx") - # Read the template from source with open(source_pyx) as f: template_content = f.read() - - # Process the template with cython_constants processed = Tempita.sub(template_content, **cython_constants) - - # Write processed output to current working directory - # (which is the build directory when invoked by CMake) - source = "rbd_processed.pyx" - - with open(source, 'w') as f: + with open(output_pyx, 'w') as f: f.write(processed) + source = os.path.relpath(output_pyx, os.getcwd()) + # Disable cythonification if we're not really building anything if (len(sys.argv) >= 2 and any(i in sys.argv[1:] for i in ('--help', 'clean', 'egg_info', '--version') @@ -226,7 +221,7 @@ setup( ) ], compiler_directives=compiler_directives, - build_dir=os.environ.get("CYTHON_BUILD_DIR", None), + build_dir=cython_build_dir, **cythonize_args ), classifiers=[ diff --git a/src/pybind/rgw/setup.py b/src/pybind/rgw/setup.py index 374167cbbca..dd36c312c47 100755 --- a/src/pybind/rgw/setup.py +++ b/src/pybind/rgw/setup.py @@ -155,6 +155,7 @@ elif check_sanity(): include_path=include_path) else: sys.exit(1) +cython_build_dir=os.environ.get("CYTHON_BUILD_DIR", None) cmdclass = {} try: @@ -176,26 +177,18 @@ except ImportError: source = "rgw.c" else: # Process Tempita template - source_pyx = os.path.join( - os.path.dirname(os.path.abspath(__file__)), - "rgw.pyx" - ) + source_dir = os.path.dirname(os.path.abspath(__file__)) + source_pyx = os.path.join(source_dir, "rgw.pyx") + build_dir = cython_build_dir or source_dir + output_pyx = os.path.join(build_dir, "rgw_processed.pyx") - # Read the template from source with open(source_pyx) as f: template_content = f.read() - - # Process the template with cython_constants processed = Tempita.sub(template_content, **cython_constants) - - # Write processed output to current working directory - # (which is the build directory when invoked by CMake) - output_pyx = "rgw_processed.pyx" - with open(output_pyx, 'w') as f: f.write(processed) - source = output_pyx + source = os.path.relpath(output_pyx, os.getcwd()) # Disable cythonification if we're not really building anything if (len(sys.argv) >= 2 and @@ -226,7 +219,7 @@ setup( **ext_args ) ], - build_dir=os.environ.get("CYTHON_BUILD_DIR", None), + build_dir=cython_build_dir, **cythonize_args ), classifiers=[