Add a warn_user_once! macro (#442)

Closes https://github.com/astral-sh/puffin/issues/429.
This commit is contained in:
Charlie Marsh 2023-11-16 18:34:06 -08:00 committed by GitHub
parent 25fcee0d9f
commit 2094680cdd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 68 additions and 13 deletions

View file

@ -75,6 +75,10 @@ Functionality for installing Python packages into a virtual environment.
Functionality for detecting and leveraging the current Python interpreter.
## [puffin-macros](./puffin-macros)
Reusable procedural macros for Puffin.
## [puffin-normalize](./puffin-normalize)
Normalize package and extra names as per Python specifications.

View file

@ -1,6 +1,6 @@
[package]
name = "puffin-dispatch"
version = "0.1.0"
version = "0.0.1"
description = "Avoid cyclic crate dependencies between resolver, installer and builder"
edition = { workspace = true }
rust-version = { workspace = true }

View file

@ -1,6 +1,6 @@
[package]
name = "puffin-distribution"
version = "0.1.0"
version = "0.0.1"
edition = { workspace = true }
rust-version = { workspace = true }
homepage = { workspace = true }

View file

@ -0,0 +1,16 @@
[package]
name = "puffin-macros"
version = "0.0.1"
edition = { workspace = true }
rust-version = { workspace = true }
homepage = { workspace = true }
documentation = { workspace = true }
repository = { workspace = true }
authors = { workspace = true }
license = { workspace = true }
[dependencies]
colored = { workspace = true }
fxhash = { workspace = true }
once_cell = { workspace = true }
tracing = { workspace = true }

View file

@ -0,0 +1,23 @@
use std::sync::Mutex;
use fxhash::FxHashSet;
use once_cell::sync::Lazy;
pub static WARNINGS: Lazy<Mutex<FxHashSet<String>>> = Lazy::new(Mutex::default);
/// Warn a user once, with uniqueness determined by the content of the message.
#[macro_export]
macro_rules! warn_once {
($($arg:tt)*) => {
use colored::Colorize;
use tracing::warn;
if let Ok(mut states) = $crate::WARNINGS.lock() {
let message = format!("{}", format_args!($($arg)*));
let formatted = message.bold();
if states.insert(message) {
warn!("{formatted}");
}
}
};
}

View file

@ -21,6 +21,7 @@ puffin-client = { path = "../puffin-client" }
puffin-distribution = { path = "../puffin-distribution" }
puffin-git = { path = "../puffin-git" }
puffin-interpreter = { path = "../puffin-interpreter" }
puffin-macros = { path = "../puffin-macros" }
puffin-normalize = { path = "../puffin-normalize" }
puffin-traits = { path = "../puffin-traits" }
pypi-types = { path = "../pypi-types" }

View file

@ -5,6 +5,7 @@ use tracing::warn;
use pep508_rs::{MarkerEnvironment, Requirement, VersionOrUrl};
use puffin_cache::CanonicalUrl;
use puffin_macros::warn_once;
use puffin_normalize::{ExtraName, PackageName};
use crate::pubgrub::specifier::PubGrubSpecifier;
@ -30,8 +31,7 @@ impl PubGrubDependencies {
for requirement in requirements {
// Avoid self-dependencies.
if source.is_some_and(|source| source == &requirement.name) {
// TODO(konstin): Warn only once here
warn!("{} depends on itself", requirement.name);
warn_once!("{} has a dependency on itself", requirement.name);
continue;
}
@ -78,8 +78,7 @@ impl PubGrubDependencies {
for constraint in constraints {
// Avoid self-dependencies.
if source.is_some_and(|source| source == &constraint.name) {
// TODO(konstin): Warn only once here
warn!("{} depends on itself", constraint.name);
warn_once!("{} has a dependency on itself", constraint.name);
continue;
}

View file

@ -8,6 +8,7 @@ use tracing::warn;
use distribution_filename::{SourceDistFilename, WheelFilename};
use pep440_rs::Version;
use platform_tags::{TagPriority, Tags};
use puffin_macros::warn_once;
use puffin_normalize::PackageName;
use pypi_types::{SimpleJson, Yanked};
@ -55,10 +56,10 @@ impl VersionMap {
continue;
}
None => {
// TODO(konstin): Implement and use `warn_once` here.
warn!(
warn_once!(
"{} is missing an upload date, but user provided {}",
file.filename, exclude_newer,
file.filename,
exclude_newer,
);
continue;
}

View file

@ -1,6 +1,6 @@
[package]
name = "puffin-traits"
version = "0.1.0"
version = "0.0.1"
edition = { workspace = true }
rust-version = { workspace = true }
homepage = { workspace = true }