forked from PaddlePaddle/PaddleFleet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_backend.py
More file actions
166 lines (128 loc) · 5.52 KB
/
build_backend.py
File metadata and controls
166 lines (128 loc) · 5.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# Copyright (c) 2026 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Lightweight PEP 517 build backend for the root paddlefleet package.
Generates ``src/paddlefleet/_version.py`` at build time (same pattern as
``packages/paddlefleet_ops/build_backend.py``) and delegates every actual
build hook to ``setuptools.build_meta``.
At build time the ``paddlefleet-ops`` dependency is pinned to the exact
version of the ops package built in the same workspace (xFormers-style),
so the published wheel carries a precise ``Requires-Dist: paddlefleet-ops==X``
rather than an unconstrained bare name.
"""
import logging
import os
import subprocess
from datetime import datetime
from pathlib import Path
from setuptools import build_meta as orig # type: ignore[import-untyped]
logger = logging.getLogger(__name__)
_pkg_root = Path(__file__).parent.resolve()
def is_git_repo() -> bool:
return (_pkg_root / ".git").exists()
def get_git_commit_hash(cwd: Path) -> str:
return (
subprocess.check_output(["git", "rev-parse", "HEAD"], cwd=cwd)
.strip()
.decode("utf-8")
)
def _generate_version_info() -> str:
"""Generate ``src/paddlefleet/_version.py`` with git metadata."""
version_file = _pkg_root / "version.txt"
with open(version_file) as f:
version = f.read().strip()
git_commit_hash = get_git_commit_hash(_pkg_root)
version_py = _pkg_root / "src" / "paddlefleet" / "_version.py"
# If file exists and not in git repo (installing from sdist), keep existing
if version_py.exists() and not is_git_repo():
logger.info("_version.py already exists (not in git repo), keeping it")
return version
if os.environ.get("PADDLEFLEET_VERSION") is not None:
final_version = os.environ["PADDLEFLEET_VERSION"]
else:
date_str = datetime.now().strftime("%Y%m%d")
branch = (
subprocess.check_output(
["git", "rev-parse", "--abbrev-ref", "HEAD"],
cwd=_pkg_root,
stderr=subprocess.DEVNULL,
)
.decode("utf-8")
.strip()
)
commit_short = (
subprocess.check_output(
["git", "rev-parse", "--short=11", "HEAD"],
cwd=_pkg_root,
)
.strip()
.decode("utf-8")
)
if branch.startswith("release/"):
final_version = f"{version}.post{date_str}+{commit_short}"
else:
final_version = f"{version}.dev{date_str}+{commit_short}"
with open(version_py, "w") as f:
f.write(
"# Copyright (c) 2026 PaddlePaddle Authors. All Rights Reserved.\n"
"#\n"
'# Licensed under the Apache License, Version 2.0 (the "License");\n'
"# you may not use this file except in compliance with the License.\n"
"# You may obtain a copy of the License at\n"
"#\n"
"# http://www.apache.org/licenses/LICENSE-2.0\n"
"#\n"
"# Unless required by applicable law or agreed to in writing, software\n"
'# distributed under the License is distributed on an "AS IS" BASIS,\n'
"# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n"
"# See the License for the specific language governing permissions and\n"
"# limitations under the License.\n"
"\n"
'"""Auto-generated version info — do not edit."""\n'
"\n"
f'__version__ = "{final_version}"\n'
f'commit = "{git_commit_hash}"\n'
)
logger.info(f"Created _version.py with version {final_version}")
return final_version
# Run at import time so both wheel and editable builds pick up the patches.
_generate_version_info()
# -- PEP 517 hooks (pure delegation to setuptools) --
def get_requires_for_build_wheel(config_settings=None):
return orig.get_requires_for_build_wheel(config_settings)
def get_requires_for_build_sdist(config_settings=None):
return orig.get_requires_for_build_sdist(config_settings)
def get_requires_for_build_editable(config_settings=None):
return orig.get_requires_for_build_editable(config_settings)
def prepare_metadata_for_build_wheel(metadata_directory, config_settings=None):
return orig.prepare_metadata_for_build_wheel(
metadata_directory, config_settings
)
def prepare_metadata_for_build_editable(
metadata_directory, config_settings=None
):
return orig.prepare_metadata_for_build_editable(
metadata_directory, config_settings
)
def build_wheel(wheel_directory, config_settings=None, metadata_directory=None):
return orig.build_wheel(
wheel_directory, config_settings, metadata_directory
)
def build_editable(
wheel_directory, config_settings=None, metadata_directory=None
):
return orig.build_editable(
wheel_directory, config_settings, metadata_directory
)
def build_sdist(sdist_directory, config_settings=None):
return orig.build_sdist(sdist_directory, config_settings)