From 2f6ea3497ad52bdbcfc4e64c3dd600e2e8d33237 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Mon, 16 Oct 2023 18:32:12 +0200 Subject: [PATCH] Use custom build backend for the client package --- client/build_backend/backend.py | 62 +++++++++++++++++++++++++++++++++ client/pyproject.toml | 3 +- stubs/setuptools.build_meta.pyi | 15 ++++++++ stubs/setuptools.pyi | 0 4 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 client/build_backend/backend.py create mode 100644 stubs/setuptools.build_meta.pyi create mode 100644 stubs/setuptools.pyi diff --git a/client/build_backend/backend.py b/client/build_backend/backend.py new file mode 100644 index 00000000..cdb18aaa --- /dev/null +++ b/client/build_backend/backend.py @@ -0,0 +1,62 @@ +import sys +from pathlib import Path +from typing import Any, Dict, Optional + +from setuptools import build_meta as _orig +from setuptools.build_meta import * # noqa: F403 + + +def gen_types_if_needed() -> None: + tl_root = Path("src/telethon/_impl/tl") + layer_py = tl_root / "layer.py" + mtproto_root = tl_root / "mtproto" + + generator_path = "../generator/src" + api_tl = Path("../generator/tl/api.tl") + mtproto_tl = Path("../generator/tl/mtproto.tl") + + if not layer_py.exists(): + print(layer_py, "is missing; attempting to generate types", file=sys.stderr) + + # Import generator and clean-up path + sys.path.append(generator_path) + import telethon_generator as gen + + sys.path.remove(generator_path) + + # api.tl + fs = gen.codegen.FakeFs() + gen.codegen.generate(fs, gen.tl_parser.load_tl_file(api_tl)) + fs.materialize(tl_root) + print("written api.tl files:", tl_root, file=sys.stderr) + + # mtproto.tl + fs = gen.codegen.FakeFs() + gen.codegen.generate(fs, gen.tl_parser.load_tl_file(mtproto_tl)) + fs.materialize(mtproto_root) + print("written mtproto.tl files:", mtproto_root, file=sys.stderr) + + +def build_wheel( # type: ignore [no-redef] + wheel_directory: str, + config_settings: Optional[Dict[Any, Any]] = None, + metadata_directory: Optional[str] = None, +) -> str: + gen_types_if_needed() + return _orig.build_wheel(wheel_directory, config_settings, metadata_directory) + + +def build_sdist( # type: ignore [no-redef] + sdist_directory: str, config_settings: Optional[Dict[Any, Any]] = None +) -> str: + gen_types_if_needed() + return _orig.build_sdist(sdist_directory, config_settings) + + +def build_editable( # type: ignore [no-redef] + wheel_directory: str, + config_settings: Optional[Dict[Any, Any]] = None, + metadata_directory: Optional[str] = None, +) -> str: + gen_types_if_needed() + return _orig.build_editable(wheel_directory, config_settings, metadata_directory) diff --git a/client/pyproject.toml b/client/pyproject.toml index dd9a292e..613ddf22 100644 --- a/client/pyproject.toml +++ b/client/pyproject.toml @@ -48,7 +48,8 @@ doc = [ [build-system] requires = ["setuptools>=61.0"] -build-backend = "setuptools.build_meta" +build-backend = "backend" +backend-path = ["build_backend"] [tool.setuptools.dynamic] version = {attr = "telethon.version.__version__"} diff --git a/stubs/setuptools.build_meta.pyi b/stubs/setuptools.build_meta.pyi new file mode 100644 index 00000000..1a6ea948 --- /dev/null +++ b/stubs/setuptools.build_meta.pyi @@ -0,0 +1,15 @@ +from typing import Any, Dict, Optional + +def build_wheel( + wheel_directory: str, + config_settings: Optional[Dict[Any, Any]] = None, + metadata_directory: Optional[str] = None, +) -> str: ... +def build_sdist( + sdist_directory: str, config_settings: Optional[Dict[Any, Any]] = None +) -> str: ... +def build_editable( + wheel_directory: str, + config_settings: Optional[Dict[Any, Any]] = None, + metadata_directory: Optional[str] = None, +) -> str: ... diff --git a/stubs/setuptools.pyi b/stubs/setuptools.pyi new file mode 100644 index 00000000..e69de29b