minify and filter embed managed pythons json on compile time (#12967)

## Summary

In #10939 I added the generated
`crates/uv-python/src/download-metadata-minified.json` file which is a
minified version of `crates/uv-python/download-metadata.json`.

The main reason for this PR is to avoid bloating the git objects as this
is a single-line file.

As a bonus, I also filtered the embed json to include only the versions
for the compiled target. Which should improve the binary size and
performance by a bit.

## Test Plan

<!-- How was it tested? -->
This commit is contained in:
Meitar Reihan 2025-04-30 22:51:03 +03:00 committed by GitHub
parent 481d05d8df
commit 5ee54b4fa3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 45 additions and 45 deletions

1
crates/uv-python/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
download-metadata-minified.json

View file

@ -80,3 +80,6 @@ itertools = { version = "0.14.0" }
temp-env = { version = "0.3.6" }
tempfile = { workspace = true }
test-log = { version = "0.2.16", features = ["trace"], default-features = false }
[build-dependencies]
serde_json = { workspace = true }

41
crates/uv-python/build.rs Normal file
View file

@ -0,0 +1,41 @@
use std::fs;
use std::path::Path;
fn process_json(data: &serde_json::Value) -> serde_json::Value {
let mut out_data = serde_json::Map::new();
if let Some(obj) = data.as_object() {
for (key, value) in obj {
if let Some(variant) = value.get("variant") {
// Exclude debug variants for now, we don't support them
if variant == "debug" {
continue;
}
}
out_data.insert(key.clone(), value.clone());
}
}
serde_json::Value::Object(out_data)
}
fn main() {
let version_metadata = Path::new("download-metadata.json");
let target = Path::new("src/download-metadata-minified.json");
let json_data: serde_json::Value = serde_json::from_str(
#[allow(clippy::disallowed_methods)]
&fs::read_to_string(version_metadata).expect("Failed to read download-metadata.json"),
)
.expect("Failed to parse JSON");
let filtered_data = process_json(&json_data);
#[allow(clippy::disallowed_methods)]
fs::write(
target,
serde_json::to_string(&filtered_data).expect("Failed to serialize JSON"),
)
.expect("Failed to write minified JSON");
}

View file

@ -1,43 +0,0 @@
# /// script
# requires-python = ">=3.12"
# ///
"""
Generate minified Python version download metadata json to embed in the binary.
Generates the `download-metadata-minified.json` file from the `download-metadata.json` file.
Usage:
uv run -- crates/uv-python/minify-download-metadata.py
"""
import json
from pathlib import Path
CRATE_ROOT = Path(__file__).parent
VERSION_METADATA = CRATE_ROOT / "download-metadata.json"
TARGET = CRATE_ROOT / "src" / "download-metadata-minified.json"
def process_json(data: dict) -> dict:
out_data = {}
for key, value in data.items():
# Exclude debug variants for now, we don't support them in the Rust side
if value["variant"] == "debug":
continue
out_data[key] = value
return out_data
def main() -> None:
json_data = json.loads(Path(VERSION_METADATA).read_text())
json_data = process_json(json_data)
json_string = json.dumps(json_data, separators=(",", ":"))
TARGET.write_text(json_string)
if __name__ == "__main__":
main()

File diff suppressed because one or more lines are too long