Update `[project].version` in pyproject.toml. Args: version: Release version to write. Returns: Path to the modified pyproject.toml file. Raises: ReleaseError: The project version field cannot be found or parsed.
(version: str)
| 149 | |
| 150 | |
| 151 | def update_pyproject_version(version: str) -> Path: |
| 152 | """Update `[project].version` in pyproject.toml. |
| 153 | |
| 154 | Args: |
| 155 | version: Release version to write. |
| 156 | |
| 157 | Returns: |
| 158 | Path to the modified pyproject.toml file. |
| 159 | |
| 160 | Raises: |
| 161 | ReleaseError: The project version field cannot be found or parsed. |
| 162 | """ |
| 163 | pyproject_path = REPO_ROOT / "pyproject.toml" |
| 164 | lines = pyproject_path.read_text(encoding="utf-8").splitlines(keepends=True) |
| 165 | in_project_section = False |
| 166 | |
| 167 | for index, line in enumerate(lines): |
| 168 | stripped = line.strip() |
| 169 | if stripped.startswith("[") and stripped.endswith("]"): |
| 170 | in_project_section = stripped == "[project]" |
| 171 | continue |
| 172 | if not in_project_section: |
| 173 | continue |
| 174 | |
| 175 | key, separator, _raw_value = stripped.partition("=") |
| 176 | if key.strip() != "version": |
| 177 | continue |
| 178 | if not separator: |
| 179 | raise ReleaseError("Unsupported pyproject.toml project.version format") |
| 180 | |
| 181 | match = re.match( |
| 182 | r"^(\s*version\s*=\s*)([\"'])(.*?)(\2)(\s*(?:#.*)?)(\n?)$", |
| 183 | line, |
| 184 | ) |
| 185 | if not match: |
| 186 | raise ReleaseError("Unsupported pyproject.toml project.version format") |
| 187 | |
| 188 | prefix, quote, _current, _closing_quote, suffix, newline = match.groups() |
| 189 | lines[index] = f"{prefix}{quote}{version}{quote}{suffix}{newline}" |
| 190 | pyproject_path.write_text("".join(lines), encoding="utf-8") |
| 191 | return pyproject_path |
| 192 | |
| 193 | raise ReleaseError("Missing [project].version in pyproject.toml") |
| 194 | |
| 195 | |
| 196 | def update_package_version(version: str) -> Path: |
no test coverage detected