tree-sitter implementation for the confindent configuration language
1from os import path
2from platform import system
3from sysconfig import get_config_var
4
5from setuptools import Extension, find_packages, setup
6from setuptools.command.build import build
7from setuptools.command.egg_info import egg_info
8from wheel.bdist_wheel import bdist_wheel
9
10sources = [
11 "bindings/python/tree_sitter_confindent/binding.c",
12 "src/parser.c",
13]
14if path.exists("src/scanner.c"):
15 sources.append("src/scanner.c")
16
17macros: list[tuple[str, str | None]] = [
18 ("PY_SSIZE_T_CLEAN", None),
19 ("TREE_SITTER_HIDE_SYMBOLS", None),
20]
21if limited_api := not get_config_var("Py_GIL_DISABLED"):
22 macros.append(("Py_LIMITED_API", "0x030A0000"))
23
24if system() != "Windows":
25 cflags = ["-std=c11", "-fvisibility=hidden"]
26else:
27 cflags = ["/std:c11", "/utf-8"]
28
29
30class Build(build):
31 def run(self):
32 if path.isdir("queries"):
33 dest = path.join(self.build_lib, "tree_sitter_confindent", "queries")
34 self.copy_tree("queries", dest)
35 super().run()
36
37
38class BdistWheel(bdist_wheel):
39 def get_tag(self):
40 python, abi, platform = super().get_tag()
41 if python.startswith("cp"):
42 python, abi = "cp310", "abi3"
43 return python, abi, platform
44
45
46class EggInfo(egg_info):
47 def find_sources(self):
48 super().find_sources()
49 self.filelist.recursive_include("queries", "*.scm")
50 self.filelist.include("src/tree_sitter/*.h")
51
52
53setup(
54 packages=find_packages("bindings/python"),
55 package_dir={"": "bindings/python"},
56 package_data={
57 "tree_sitter_confindent": ["*.pyi", "py.typed"],
58 "tree_sitter_confindent.queries": ["*.scm"],
59 },
60 ext_package="tree_sitter_confindent",
61 ext_modules=[
62 Extension(
63 name="_binding",
64 sources=sources,
65 extra_compile_args=cflags,
66 define_macros=macros,
67 include_dirs=["src"],
68 py_limited_api=limited_api,
69 )
70 ],
71 cmdclass={
72 "build": Build,
73 "bdist_wheel": BdistWheel,
74 "egg_info": EggInfo,
75 },
76 zip_safe=False
77)