-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathgenerate_pyproject.py
More file actions
executable file
·284 lines (247 loc) · 10.2 KB
/
Copy pathgenerate_pyproject.py
File metadata and controls
executable file
·284 lines (247 loc) · 10.2 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#!/usr/bin/env python3
"""Selective pyproject.toml templating - only updates metadata, preserves dependencies."""
import argparse
import logging
import subprocess
import sys
from pathlib import Path
from typing import Any
# Get project root
project_root = Path(__file__).parent.parent.parent
def _get_config_value(key: str) -> str:
"""Get value from .project.yml using yq."""
try:
result = subprocess.run(
["yq", key, ".project.yml"],
cwd=project_root,
capture_output=True,
text=True,
check=True,
)
return result.stdout.strip()
except (subprocess.CalledProcessError, FileNotFoundError) as e:
logging.error(f"Error reading config key '{key}': {e}")
logging.error("Make sure yq is installed and .project.yml exists")
sys.exit(1)
def write_toml_section(lines: list, section_name: str, data: Any, indent: int = 0) -> None:
"""Write a TOML section to lines list."""
prefix = " " * indent
if isinstance(data, dict):
if indent == 0:
lines.append(f"[{section_name}]")
else:
lines.append(f"{prefix}[{section_name}]")
for key, value in data.items():
if isinstance(value, dict):
write_toml_section(lines, f"{section_name}.{key}", value, indent)
elif isinstance(value, list):
if all(isinstance(item, str) for item in value):
lines.append(f"{prefix}{key} = [")
for item in value:
lines.append(f'{prefix} "{item}",')
lines.append(f"{prefix}]")
elif all(isinstance(item, dict) for item in value):
lines.append(f"{prefix}{key} = [")
for item in value:
lines.append(
f"{prefix} {{"
+ ", ".join(f'{k} = "{v}"' for k, v in item.items())
+ "},"
)
lines.append(f"{prefix}]")
elif isinstance(value, str):
lines.append(f'{prefix}{key} = "{value}"')
else:
lines.append(f"{prefix}{key} = {value}")
lines.append("")
def update_pyproject_selective(pyproject_path: Path) -> None:
"""Update only metadata sections in pyproject.toml, preserve dependencies."""
# Get metadata updates
package_name = _get_config_value(".project.name")
package_name_short = _get_config_value(".project.short_name")
package_root = _get_config_value(".build.package_root")
# Get PyPI-specific name, fallback to package_name if not set
pypi_name = _get_config_value(".project.pypi_name")
if not pypi_name or pypi_name == "null":
pypi_name = package_name
version = _get_config_value(".project.version")
description = _get_config_value(".project.description")
author = _get_config_value(".project.author")
email = _get_config_value(".project.email")
min_python = _get_config_value(".python.versions[0]")
default_python = _get_config_value(".python.default_version")
# Generate URLs
org = _get_config_value(".repository.org")
repo_name = _get_config_value(".repository.name")
repo_url = f"https://github.com/{org}/{repo_name}"
docs_url = f"https://{org}.github.io/{repo_name}/"
issues_url = f"{repo_url}/issues"
# Read existing file
if not pyproject_path.exists():
logging.error(f"pyproject.toml not found at {pyproject_path}")
sys.exit(1)
with open(pyproject_path) as f:
content = f.read()
lines = content.split("\n")
new_lines = []
i = 0
while i < len(lines):
line = lines[i].strip()
# Check if we're entering a metadata section we want to replace
if line.startswith("[project]"):
# Write updated project metadata
new_lines.extend(
[
"[project]",
f'name = "{pypi_name}"',
f'version = "{version}"',
f'description = "{description}"',
'readme = "README.md"',
'license = "Apache-2.0"',
"authors = [",
f' {{name = "{author}", email = "{email}"}},',
"]",
"maintainers = [",
f' {{name = "{author}", email = "{email}"}},',
"]",
"keywords = [",
' "aws",',
' "ec2",',
' "hostfactory",',
' "symphony",',
' "hpc",',
' "cluster",',
' "cloud",',
' "infrastructure",',
"]",
"classifiers = [",
' "Development Status :: 4 - Beta",',
' "Intended Audience :: Developers",',
' "Natural Language :: English",',
' "Operating System :: OS Independent",',
' "Programming Language :: Python :: 3",',
]
)
# Add Python version classifiers
python_versions = _get_config_value(".python.versions[]").split("\n")
for py_version in python_versions:
if py_version.strip():
new_lines.append(
f' "Programming Language :: Python :: {py_version.strip()}",'
)
new_lines.extend(
[
' "Topic :: Software Development :: Libraries :: Python Modules",',
' "Topic :: System :: Clustering",',
' "Topic :: System :: Distributed Computing",',
"]",
f'requires-python = ">={min_python}"',
"",
]
)
# Skip until we find dependencies or next section
i += 1
while i < len(lines) and not lines[i].strip().startswith(("dependencies", "[")):
i += 1
continue
elif line.startswith("[project.urls]"):
# Replace URLs section
new_lines.extend(
[
"[project.urls]",
f'Homepage = "{repo_url}"',
f'Documentation = "{docs_url}"',
f'Repository = "{repo_url}"',
f'"Bug Reports" = "{issues_url}"',
"",
]
)
# Skip existing URLs section
i += 1
while i < len(lines) and not lines[i].strip().startswith("["):
i += 1
continue
elif line.startswith("[project.scripts]"):
# Replace scripts section
new_lines.extend(
[
"[project.scripts]",
f'{package_name_short} = "orb.run:cli_main"',
"",
]
)
# Skip existing scripts section
i += 1
while i < len(lines) and not lines[i].strip().startswith("["):
i += 1
continue
elif line.startswith("[tool.mypy]"):
# Replace mypy section
new_lines.extend(
[
"[tool.mypy]",
f'python_version = "{default_python}"',
"warn_return_any = true",
"warn_unused_configs = true",
"disallow_untyped_defs = false",
"disallow_incomplete_defs = false",
"check_untyped_defs = true",
"disallow_untyped_decorators = false",
"no_implicit_optional = true",
"warn_redundant_casts = true",
"warn_unused_ignores = true",
"warn_no_return = true",
"warn_unreachable = true",
"strict_equality = true",
'mypy_path = "src"',
"namespace_packages = true",
"",
]
)
# Skip existing mypy section
i += 1
while i < len(lines) and not lines[i].strip().startswith("["):
i += 1
continue
elif line.startswith("[tool.coverage.run]"):
# Re-template only source (from package_root) + branch; preserve every
# other hand-authored key in this section (e.g. omit) instead of
# dropping it — the generator manages source, not the whole policy.
i += 1
preserved = []
while i < len(lines) and not lines[i].strip().startswith("["):
existing = lines[i].strip()
if not existing.startswith("source ") and not existing.startswith("branch "):
preserved.append(lines[i])
i += 1
new_lines.append("[tool.coverage.run]")
new_lines.append(f'source = ["{package_root}"]')
new_lines.append("branch = true")
new_lines.extend(preserved)
continue
else:
# Keep all other lines (including dependencies)
new_lines.append(lines[i])
i += 1
# Write updated content
with open(pyproject_path, "w") as f:
f.write("\n".join(new_lines))
logging.info(f"Updated metadata in {pyproject_path}")
def main():
"""Main entry point."""
parser = argparse.ArgumentParser(
description="Generate pyproject.toml with selective templating"
)
parser.add_argument("--config", default=".project.yml", help="Config file path")
parser.add_argument("--output", default="pyproject.toml", help="Output file path")
parser.add_argument("--verbose", "-v", action="store_true", help="Verbose logging")
args = parser.parse_args()
logging.basicConfig(
level=logging.DEBUG if args.verbose else logging.INFO, format="%(levelname)s: %(message)s"
)
# Update pyproject.toml selectively
output_path = project_root / args.output
update_pyproject_selective(output_path)
logging.info("Selective pyproject.toml templating completed")
if __name__ == "__main__":
main()