|
| 1 | +"""Update any project files that point to a private package registry to use public ones. |
| 2 | +
|
| 3 | +Since the CI pipelines for testing these copier templates don't have access to private registries, we can't test installing from them as part of CI. |
| 4 | +
|
| 5 | +Seems minimal risk, since the only problem we'd be missing is if the pyproject.toml (or similar config files) had syntax errors that would have been |
| 6 | +caught by pre-commit. |
| 7 | +""" |
| 8 | + |
| 9 | +import re |
| 10 | +from pathlib import Path |
| 11 | + |
| 12 | + |
| 13 | +def process_file(file_path: Path): |
| 14 | + # Read the entire file content |
| 15 | + content = file_path.read_text() |
| 16 | + |
| 17 | + # Regex to match a block starting with [[tool.uv.index]] |
| 18 | + # until the next block header (a line starting with [[) or the end of the file. |
| 19 | + pattern = re.compile(r"(\[\[tool\.uv\.index\]\].*?)(?=\n\[\[|$)", re.DOTALL) |
| 20 | + |
| 21 | + # Find all uv.index blocks. |
| 22 | + blocks = pattern.findall(content) |
| 23 | + |
| 24 | + # Check if any block contains "default = true" |
| 25 | + if not any("default = true" in block for block in blocks): |
| 26 | + print(f"No changes in: {file_path}") |
| 27 | + return |
| 28 | + |
| 29 | + # If at least one block contains "default = true", remove all uv.index blocks. |
| 30 | + new_content = pattern.sub("", content) |
| 31 | + |
| 32 | + # Ensure file ends with a newline before appending the new block. |
| 33 | + if not new_content.endswith("\n"): |
| 34 | + new_content += "\n" |
| 35 | + |
| 36 | + # Append the new block. |
| 37 | + new_block = '[[tool.uv.index]]\nname = "pypi"\nurl = "https://pypi.org/simple/"\n' |
| 38 | + new_content += new_block |
| 39 | + |
| 40 | + # Write the updated content back to the file. |
| 41 | + _ = file_path.write_text(new_content) |
| 42 | + print(f"Updated file: {file_path}") |
| 43 | + |
| 44 | + |
| 45 | +def main(): |
| 46 | + base_dir = Path(".") |
| 47 | + # Use rglob to find all pyproject.toml files recursively. |
| 48 | + for file_path in base_dir.rglob("pyproject.toml"): |
| 49 | + # Check if the file is at most two levels deep. |
| 50 | + # The relative path's parts count should be <= 3 (e.g. "pyproject.toml" is 1 part, |
| 51 | + # "subdir/pyproject.toml" is 2 parts, and "subdir/subsubdir/pyproject.toml" is 3 parts). |
| 52 | + if len(file_path.relative_to(base_dir).parts) <= 3: |
| 53 | + process_file(file_path) |
| 54 | + |
| 55 | + |
| 56 | +if __name__ == "__main__": |
| 57 | + main() |
0 commit comments