Skip to content

Instantly share code, notes, and snippets.

@ties
Last active January 2, 2025 14:03
Show Gist options
  • Save ties/9b8c96da9eaf774fdb5c577afdbb0e2c to your computer and use it in GitHub Desktop.
Save ties/9b8c96da9eaf774fdb5c577afdbb0e2c to your computer and use it in GitHub Desktop.
fix the result of uv export to add extra index urls
"""
Append --extra-index-url statements in requirements.txt (not supported by uv export)
https://github.com/astral-sh/uv/issues/7496
run from pre-commit with
```yaml
- repo: local
hooks:
- id: fix-requirements
name: fix extra-index-urls in generated requirements.txt
language: system
entry: python fix_requirements.py
files: ^requirements.*\.txt$
```
"""
import os
import sys
from pathlib import Path
EXTRA_URLS = [
"https://gitlab.example.org/api/v4/projects/526/packages/pypi/simple"
]
if __name__ == '__main__':
path = Path(sys.argv[1] if len(sys.argv) >= 2 else 'requirements.txt')
lines = path.read_text().splitlines()
with path.open("a") as f:
for missing_url in EXTRA_URLS:
if not any(missing_url in line for line in lines):
f.write(f'--extra-index-url {missing_url}{os.linesep}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment