Repository navigation
Expand file tree
/
Copy pathpyproject.py
More file actions
116 lines (87 loc) · 3.61 KB
/
Copy pathpyproject.py
File metadata and controls
116 lines (87 loc) · 3.61 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
from __future__ import annotations
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from tomlkit.toml_document import TOMLDocument
import asyncio
from pathlib import Path
from pyproject_metadata import StandardMetadata
import afterpython as ap
from afterpython._io.toml import _to_tomlkit, read_toml, write_toml
def read_pyproject() -> TOMLDocument:
"""Read pyproject.toml"""
return read_toml(ap.paths.pyproject_path)
def write_pyproject(data: TOMLDocument):
write_toml(ap.paths.pyproject_path, data)
def read_metadata() -> StandardMetadata:
"""Read metadata from pyproject.toml.
Pass `project_dir` so relative paths (e.g. `readme = "README.md"`) resolve
against the project root rather than the caller's CWD.
"""
return StandardMetadata.from_pyproject(
read_pyproject(), project_dir=ap.paths.user_path
)
def find_package_directory() -> Path:
"""Find the user's package directory.
Supports both common Python project layouts:
- src layout: user_project/src/package_name/__init__.py
- flat layout: user_project/package_name/__init__.py
Returns:
Path to the package directory containing __init__.py
Raises:
FileNotFoundError: If package directory cannot be found in either layout
"""
metadata = read_metadata()
package_name = metadata.name
project_root = ap.paths.user_path
# Try src layout first (recommended layout)
src_layout_dir = project_root / "src" / package_name
if (src_layout_dir / "__init__.py").exists():
return src_layout_dir
# Try flat layout
flat_layout_dir = project_root / package_name
if (flat_layout_dir / "__init__.py").exists():
return flat_layout_dir
# Package not found in either location
raise FileNotFoundError(
f"Could not find package '{package_name}' in either src layout "
f"({src_layout_dir}) or flat layout ({flat_layout_dir}). "
f"Expected to find __init__.py in one of these locations."
)
def init_pyproject():
"""Initialize pyproject.toml with sensible defaults
- add [build-system] section with uv build backend (same as `uv init --package`)
- add [project.urls] section with homepage, repository, and documentation URLs
"""
import httpx2
from afterpython.tools._git import get_git_user_config, get_github_url
from afterpython.utils import fetch_pypi_json
build_backend = "uv_build"
async def fetch_build_backend_version() -> str | None:
"""Fetch the latest version of build backend package from PyPI."""
async with httpx2.AsyncClient() as client:
data = await fetch_pypi_json(client, build_backend)
return data["info"]["version"] if data else None
data: TOMLDocument = read_pyproject()
is_updated = False
if "build-system" not in data:
uv_build_version = asyncio.run(fetch_build_backend_version())
if uv_build_version:
data["build-system"] = {
"requires": [f"{build_backend}>={uv_build_version}"],
"build-backend": build_backend,
}
is_updated = True
if "project" in data:
if "urls" not in data["project"]:
data["project"]["urls"] = {
"homepage": "",
"repository": get_github_url() or "",
"documentation": "",
}
is_updated = True
if "authors" not in data["project"]:
# convert git user config to tomlkit object
data["project"]["authors"] = _to_tomlkit([get_git_user_config()])
is_updated = True
if is_updated:
write_pyproject(data)