1# Copyright 2019 The RE2 Authors. All Rights Reserved. 2# Use of this source code is governed by a BSD-style 3# license that can be found in the LICENSE file. 4 5import os 6import setuptools 7import setuptools.command.build_ext 8import shutil 9import sys 10 11long_description = r"""A drop-in replacement for the re module. 12 13It uses RE2 under the hood, of course, so various PCRE features 14(e.g. backreferences, look-around assertions) are not supported. 15See https://github.com/google/re2/wiki/Syntax for the canonical 16reference, but known syntactic "gotchas" relative to Python are: 17 18 * PCRE supports \Z and \z; RE2 supports \z; Python supports \z, 19 but calls it \Z. You must rewrite \Z to \z in pattern strings. 20 21Known differences between this module's API and the re module's API: 22 23 * The error class does not provide any error information as attributes. 24 * The Options class replaces the re module's flags with RE2's options as 25 gettable/settable properties. Please see re2.h for their documentation. 26 * The pattern string and the input string do not have to be the same type. 27 Any str will be encoded to UTF-8. 28 * The pattern string cannot be str if the options specify Latin-1 encoding. 29 30Known issues with regard to building the C++ extension: 31 32 * Building requires RE2 to be installed on your system. 33 On Debian, for example, install the libre2-dev package. 34 * Building requires pybind11 to be installed on your system OR venv. 35 On Debian, for example, install the pybind11-dev package. 36 For a venv, install the pybind11 package from PyPI. 37 * Building on macOS is known to work, but has been known to fail. 38 For example, the system Python may not know which compiler flags 39 to set when building bindings for software installed by Homebrew; 40 see https://docs.brew.sh/Homebrew-and-Python#brewed-python-modules. 41 * Building on Windows has not been tested yet and will probably fail. 42""" 43 44 45class BuildExt(setuptools.command.build_ext.build_ext): 46 47 def build_extension(self, ext): 48 if 'GITHUB_ACTIONS' not in os.environ: 49 return super().build_extension(ext) 50 51 # For @pybind11_bazel's `python_configure()`. 52 os.environ['PYTHON_BIN_PATH'] = sys.executable 53 54 cmd = ['bazel', 'build'] 55 try: 56 cmd.append(f'--cpu={os.environ["BAZEL_CPU"].lower()}') 57 except KeyError: 58 pass 59 cmd += ['--compilation_mode=opt', '--', ':all'] 60 self.spawn(cmd) 61 62 # This ensures that f'_re2.{importlib.machinery.EXTENSION_SUFFIXES[0]}' 63 # is the filename in the destination directory, which is what's needed. 64 shutil.copyfile('../bazel-bin/python/_re2.so', 65 self.get_ext_fullpath(ext.name)) 66 67 cmd = ['bazel', 'clean', '--expunge'] 68 self.spawn(cmd) 69 70 71def options(): 72 bdist_wheel = {} 73 try: 74 bdist_wheel['plat_name'] = os.environ['PLAT_NAME'] 75 except KeyError: 76 pass 77 return {'bdist_wheel': bdist_wheel} 78 79 80def include_dirs(): 81 try: 82 import pybind11 83 yield pybind11.get_include() 84 except ModuleNotFoundError: 85 pass 86 87 88ext_module = setuptools.Extension( 89 name='_re2', 90 sources=['_re2.cc'], 91 include_dirs=list(include_dirs()), 92 libraries=['re2'], 93 extra_compile_args=['-fvisibility=hidden'], 94) 95 96setuptools.setup( 97 name='google-re2', 98 version='1.1', 99 description='RE2 Python bindings', 100 long_description=long_description, 101 long_description_content_type='text/plain', 102 author='The RE2 Authors', 103 author_email='re2-dev@googlegroups.com', 104 url='https://github.com/google/re2', 105 py_modules=['re2'], 106 ext_modules=[ext_module], 107 classifiers=[ 108 'Development Status :: 5 - Production/Stable', 109 'Intended Audience :: Developers', 110 'License :: OSI Approved :: BSD License', 111 'Programming Language :: C++', 112 'Programming Language :: Python :: 3.8', 113 ], 114 options=options(), 115 cmdclass={'build_ext': BuildExt}, 116 python_requires='~=3.8', 117) 118