Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Refactor uv-toolchain::platform to use target-lexicon #4236

Merged
merged 1 commit into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ serde = { version = "1.0.197", features = ["derive"] }
serde_json = { version = "1.0.114" }
sha2 = { version = "0.10.8" }
sys-info = { version = "0.9.1" }
target-lexicon = {version = "0.12.14" }
tempfile = { version = "3.9.0" }
textwrap = { version = "0.16.1" }
thiserror = { version = "1.0.56" }
Expand Down
1 change: 1 addition & 0 deletions crates/uv-toolchain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ same-file = { workspace = true }
schemars = { workspace = true, optional = true }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
target-lexicon = { workspace = true }
tempfile = { workspace = true }
thiserror = { workspace = true }
tokio-util = { workspace = true, features = ["compat"] }
Expand Down
2,854 changes: 1,471 additions & 1,383 deletions crates/uv-toolchain/download-metadata.json

Large diffs are not rendered by default.

10 changes: 3 additions & 7 deletions crates/uv-toolchain/fetch-download-metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,7 @@
ARCH_MAP = {
"ppc64": "powerpc64",
"ppc64le": "powerpc64le",
"i686": "x86",
"i386": "x86",
"armv7": "armv7l",
}
OS_MAP = {"darwin": "macos"}


def parse_filename(filename):
Expand All @@ -107,8 +103,8 @@ def parse_filename(filename):


def normalize_triple(triple):
if "-static" in triple or "-gnueabihf" in triple or "-gnueabi" in triple:
logging.debug("Skipping %r: unknown triple", triple)
if "-static" in triple:
logging.debug("Skipping %r: static unsupported", triple)
return
triple = SPECIAL_TRIPLES.get(triple, triple)
pieces = triple.split("-")
Expand All @@ -134,7 +130,7 @@ def normalize_arch(arch):


def normalize_os(os):
return OS_MAP.get(os, os)
return os
Comment on lines 132 to +133
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just keeping this for consistency for now



def read_sha256(url):
Expand Down
4,326 changes: 1,958 additions & 2,368 deletions crates/uv-toolchain/src/downloads.inc

Large diffs are not rendered by default.

12 changes: 8 additions & 4 deletions crates/uv-toolchain/src/downloads.inc.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@
pub(crate) const PYTHON_DOWNLOADS: &[PythonDownload] = &[
{{#versions}}
PythonDownload {
key: "{{key}}",
major: {{value.major}},
minor: {{value.minor}},
patch: {{value.patch}},
implementation: ImplementationName::{{value.name}},
arch: Arch::{{value.arch}},
os: Os::{{value.os}},
libc: Libc::{{value.libc}},
arch: Arch(target_lexicon::Architecture::{{value.arch}}),
os: Os(target_lexicon::OperatingSystem::{{value.os}}),
{{#value.libc}}
libc: Libc::Some(target_lexicon::Environment::{{.}}),
{{/value.libc}}
{{^value.libc}}
libc: Libc::None,
{{/value.libc}}
url: "{{value.url}}",
{{#value.sha256}}
sha256: Some("{{.}}")
Expand Down
35 changes: 18 additions & 17 deletions crates/uv-toolchain/src/downloads.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::path::{Path, PathBuf};
use std::str::FromStr;

use crate::implementation::{Error as ImplementationError, ImplementationName};
use crate::platform::{Arch, Error as PlatformError, Libc, Os};
use crate::platform::{Arch, Libc, Os};
use crate::{PythonVersion, ToolchainRequest, VersionRequest};
use thiserror::Error;
use uv_client::BetterReqwestError;
Expand All @@ -21,8 +21,6 @@ pub enum Error {
#[error(transparent)]
IO(#[from] io::Error),
#[error(transparent)]
PlatformError(#[from] PlatformError),
#[error(transparent)]
ImplementationError(#[from] ImplementationError),
#[error("Invalid python version: {0}")]
InvalidPythonVersion(String),
Expand Down Expand Up @@ -59,7 +57,6 @@ pub enum Error {

#[derive(Debug, PartialEq)]
pub struct PythonDownload {
key: &'static str,
implementation: ImplementationName,
arch: Arch,
os: Os,
Expand Down Expand Up @@ -157,10 +154,10 @@ impl PythonDownloadRequest {
self.implementation = Some(ImplementationName::CPython);
}
if self.arch.is_none() {
self.arch = Some(Arch::from_env()?);
self.arch = Some(Arch::from_env());
}
if self.os.is_none() {
self.os = Some(Os::from_env()?);
self.os = Some(Os::from_env());
}
if self.libc.is_none() {
self.libc = Some(Libc::from_env());
Expand All @@ -173,8 +170,8 @@ impl PythonDownloadRequest {
Ok(Self::new(
None,
None,
Some(Arch::from_env()?),
Some(Os::from_env()?),
Some(Arch::from_env()),
Some(Os::from_env()),
Some(Libc::from_env()),
))
}
Expand Down Expand Up @@ -252,11 +249,6 @@ pub enum DownloadResult {
}

impl PythonDownload {
/// Return the [`PythonDownload`] corresponding to the key, if it exists.
pub fn from_key(key: &str) -> Option<&PythonDownload> {
PYTHON_DOWNLOADS.iter().find(|&value| value.key == key)
}

/// Return the first [`PythonDownload`] matching a request, if any.
pub fn from_request(request: &PythonDownloadRequest) -> Result<&'static PythonDownload, Error> {
request
Expand All @@ -274,8 +266,17 @@ impl PythonDownload {
self.url
}

pub fn key(&self) -> &str {
self.key
pub fn key(&self) -> String {
format!(
"{}-{}.{}.{}-{}-{}-{}",
self.implementation.as_str().to_ascii_lowercase(),
self.major,
self.minor,
self.patch,
self.os,
self.arch,
self.libc
)
}

pub fn sha256(&self) -> Option<&str> {
Expand All @@ -289,7 +290,7 @@ impl PythonDownload {
parent_path: &Path,
) -> Result<DownloadResult, Error> {
let url = Url::parse(self.url)?;
let path = parent_path.join(self.key).clone();
let path = parent_path.join(self.key()).clone();

// If it already exists, return it
if path.is_dir() {
Expand Down Expand Up @@ -363,6 +364,6 @@ impl From<reqwest_middleware::Error> for Error {

impl Display for PythonDownload {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.key)
f.write_str(&self.key())
}
}
10 changes: 5 additions & 5 deletions crates/uv-toolchain/src/managed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ impl InstalledToolchains {
pub fn find_matching_current_platform(
&self,
) -> Result<impl DoubleEndedIterator<Item = InstalledToolchain>, Error> {
let platform_key = platform_key_from_env()?;
let platform_key = platform_key_from_env();

let iter = InstalledToolchains::from_settings()?
.find_all()?
Expand Down Expand Up @@ -286,11 +286,11 @@ impl InstalledToolchain {
}

/// Generate a platform portion of a key from the environment.
fn platform_key_from_env() -> Result<String, Error> {
let os = Os::from_env()?;
let arch = Arch::from_env()?;
fn platform_key_from_env() -> String {
let os = Os::from_env();
let arch = Arch::from_env();
let libc = Libc::from_env();
Ok(format!("{os}-{arch}-{libc}").to_lowercase())
format!("{os}-{arch}-{libc}").to_lowercase()
}

impl fmt::Display for InstalledToolchain {
Expand Down
Loading
Loading