• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 re
7import setuptools
8import setuptools.command.build_ext
9import shutil
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    cmd = ['bazel', 'build']
52    try:
53      cpu = os.environ['BAZEL_CPU']
54      cmd.append(f'--cpu={cpu}')
55      cmd.append(f'--platforms=//python:{cpu}')
56      if cpu == 'x64_x86_windows':
57        # Register the local 32-bit C++ toolchain with highest priority.
58        # (This is likely to break in some release of Bazel after 7.0.0,
59        # but this special case can hopefully be entirely removed then.)
60        cmd.append(f'--extra_toolchains=@local_config_cc//:cc-toolchain-{cpu}')
61    except KeyError:
62      pass
63    try:
64      ver = os.environ['MACOSX_DEPLOYMENT_TARGET']
65      cmd.append(f'--macos_minimum_os={ver}')
66    except KeyError:
67      pass
68    # Register the local Python toolchains with highest priority.
69    cmd.append('--extra_toolchains=//python/toolchains:all')
70    cmd += ['--compilation_mode=opt', '--', ':all']
71    self.spawn(cmd)
72
73    # This ensures that f'_re2.{importlib.machinery.EXTENSION_SUFFIXES[0]}'
74    # is the filename in the destination directory, which is what's needed.
75    shutil.copyfile('../bazel-bin/python/_re2.so',
76                    self.get_ext_fullpath(ext.name))
77
78    cmd = ['bazel', 'clean', '--expunge']
79    self.spawn(cmd)
80
81
82def options():
83  bdist_wheel = {}
84  try:
85    bdist_wheel['plat_name'] = os.environ['PLAT_NAME']
86  except KeyError:
87    pass
88  return {'bdist_wheel': bdist_wheel}
89
90
91def include_dirs():
92  try:
93    import pybind11
94    yield pybind11.get_include()
95  except ModuleNotFoundError:
96    pass
97
98
99ext_module = setuptools.Extension(
100    name='_re2',
101    sources=['_re2.cc'],
102    include_dirs=list(include_dirs()),
103    libraries=['re2'],
104    extra_compile_args=['-fvisibility=hidden'],
105)
106
107# We need `re2` to be a package, not a module, because it appears that
108# modules can't have `.pyi` files, so munge the module into a package.
109PACKAGE = 're2'
110try:
111  # If we are building from the sdist, we are already in package form.
112  if not os.path.exists('PKG-INFO'):
113    os.makedirs(PACKAGE)
114    for filename in (
115        're2.py',
116        # TODO(junyer): Populate as per https://github.com/google/re2/issues/496.
117        # 're2.pyi',
118        # '_re2.pyi',
119    ):
120      with open(filename, 'r') as file:
121        contents = file.read()
122      filename = re.sub(r'^re2(?=\.py)', '__init__', filename)
123      contents = re.sub(r'^(?=import _)', 'from . ', contents, flags=re.MULTILINE)
124      with open(f'{PACKAGE}/{filename}', 'x') as file:
125        file.write(contents)
126    # TODO(junyer): Populate as per https://github.com/google/re2/issues/496.
127    # with open(f'{PACKAGE}/py.typed', 'x') as file:
128    #   pass
129
130  setuptools.setup(
131      name='google-re2',
132      version='1.1.20240702',
133      description='RE2 Python bindings',
134      long_description=long_description,
135      long_description_content_type='text/plain',
136      author='The RE2 Authors',
137      author_email='re2-dev@googlegroups.com',
138      url='https://github.com/google/re2',
139      packages=[PACKAGE],
140      ext_package=PACKAGE,
141      ext_modules=[ext_module],
142      classifiers=[
143          'Development Status :: 5 - Production/Stable',
144          'Intended Audience :: Developers',
145          'License :: OSI Approved :: BSD License',
146          'Programming Language :: C++',
147          'Programming Language :: Python :: 3.8',
148      ],
149      options=options(),
150      cmdclass={'build_ext': BuildExt},
151      python_requires='~=3.8',
152  )
153except:
154  raise
155else:
156  # If we are building from the sdist, we are already in package form.
157  if not os.path.exists('PKG-INFO'):
158    shutil.rmtree(PACKAGE)
159