mirror of
https://github.com/astral-sh/uv.git
synced 2025-07-07 13:25:00 +00:00

See https://github.com/astral-sh/uv/issues/2617
Note this also includes:
- #2918
- #2931 (pending)
A first step towards Python toolchain management in Rust.
First, we add a new crate to manage Python download metadata:
- Adds a new `uv-toolchain` crate
- Adds Rust structs for Python version download metadata
- Duplicates the script which downloads Python version metadata
- Adds a script to generate Rust code from the JSON metadata
- Adds a utility to download and extract the Python version
I explored some alternatives like a build script using things like
`serde` and `uneval` to automatically construct the code from our
structs but deemed it to heavy. Unlike Rye, I don't generate the Rust
directly from the web requests and have an intermediate JSON layer to
speed up iteration on the Rust types.
Next, we add add a `uv-dev` command `fetch-python` to download Python
versions per the bootstrapping script.
- Downloads a requested version or reads from `.python-versions`
- Extracts to `UV_BOOTSTRAP_DIR`
- Links executables for path extension
This command is not really intended to be user facing, but it's a good
PoC for the `uv-toolchain` API. Hash checking (via the sha256) isn't
implemented yet, we can do that in a follow-up.
Finally, we remove the `scripts/bootstrap` directory, update CI to use
the new command, and update the CONTRIBUTING docs.
<img width="1023" alt="Screenshot 2024-04-08 at 17 12 15"
src="57bd3cf1
-7477-4bb8-a8e9-802a00d772cb">
99 lines
2.4 KiB
Python
99 lines
2.4 KiB
Python
#!/usr/bin/env python3.12
|
|
"""
|
|
Generate static Rust code from Python version metadata.
|
|
|
|
Generates the `python_versions.rs` file from the `python_versions.rs.mustache` template.
|
|
|
|
Usage:
|
|
|
|
python template-version-metadata.py
|
|
"""
|
|
|
|
import sys
|
|
import logging
|
|
import argparse
|
|
import json
|
|
import subprocess
|
|
from pathlib import Path
|
|
|
|
CRATE_ROOT = Path(__file__).parent
|
|
WORKSPACE_ROOT = CRATE_ROOT.parent.parent
|
|
VERSION_METADATA = CRATE_ROOT / "python-version-metadata.json"
|
|
TEMPLATE = CRATE_ROOT / "src" / "python_versions.inc.mustache"
|
|
TARGET = TEMPLATE.with_suffix("")
|
|
|
|
|
|
try:
|
|
import chevron_blue
|
|
except ImportError:
|
|
print(
|
|
"missing requirement `chevron-blue`",
|
|
file=sys.stderr,
|
|
)
|
|
exit(1)
|
|
|
|
|
|
def prepare_value(value: dict) -> dict:
|
|
# Convert fields from snake case to camel case for enums
|
|
for key in ["arch", "os", "libc", "name"]:
|
|
value[key] = value[key].title()
|
|
return value
|
|
|
|
|
|
def main():
|
|
debug = logging.getLogger().getEffectiveLevel() <= logging.DEBUG
|
|
|
|
data = {}
|
|
data["generated_with"] = Path(__file__).relative_to(WORKSPACE_ROOT)
|
|
data["generated_from"] = TEMPLATE.relative_to(WORKSPACE_ROOT)
|
|
data["versions"] = [
|
|
{"key": key, "value": prepare_value(value)}
|
|
for key, value in json.loads(VERSION_METADATA.read_text()).items()
|
|
]
|
|
|
|
# Render the template
|
|
logging.info(f"Rendering `{TEMPLATE.name}`...")
|
|
output = chevron_blue.render(
|
|
template=TEMPLATE.read_text(), data=data, no_escape=True, warn=debug
|
|
)
|
|
|
|
# Update the file
|
|
logging.info(f"Updating `{TARGET}`...")
|
|
TARGET.write_text(output)
|
|
subprocess.check_call(
|
|
["rustfmt", str(TARGET)],
|
|
stderr=subprocess.STDOUT,
|
|
stdout=sys.stderr if debug else subprocess.DEVNULL,
|
|
)
|
|
|
|
logging.info("Done!")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
parser = argparse.ArgumentParser(
|
|
description="Generates Rust code for Python version metadata.",
|
|
)
|
|
parser.add_argument(
|
|
"-v",
|
|
"--verbose",
|
|
action="store_true",
|
|
help="Enable debug logging",
|
|
)
|
|
parser.add_argument(
|
|
"-q",
|
|
"--quiet",
|
|
action="store_true",
|
|
help="Disable logging",
|
|
)
|
|
|
|
args = parser.parse_args()
|
|
if args.quiet:
|
|
log_level = logging.CRITICAL
|
|
elif args.verbose:
|
|
log_level = logging.DEBUG
|
|
else:
|
|
log_level = logging.INFO
|
|
|
|
logging.basicConfig(level=log_level, format="%(message)s")
|
|
|
|
main()
|