Skip to content

Commit

Permalink
Update pep508_rs and pep440_rs
Browse files Browse the repository at this point in the history
Update pep508_rs and pep440_rs crates

Main reason is to use VerbatimUrl from pep508_rs.

Note that monotrail-utils is still using the older pep508_rs.
  • Loading branch information
bluss committed Mar 23, 2024
1 parent 153213c commit 81ac63f
Show file tree
Hide file tree
Showing 11 changed files with 157 additions and 95 deletions.
60 changes: 52 additions & 8 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions rye/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ minijinja = { version = "1.0.0", features = ["json"] }
nix = { version = "0.27.1", default-features = false, features = ["process"] }
once_cell = "1.17.1"
pathdiff = "0.2.1"
pep440_rs = "0.4.0"
pep508_rs = "0.3.0"
pep440_rs = "0.5.0"
pep508_rs = "0.4.2"
regex = "1.8.1"
same-file = "1.0.6"
serde = { version = "1.0.160", features = ["derive"] }
Expand Down
49 changes: 26 additions & 23 deletions rye/src/cli/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ use std::str::FromStr;

use anyhow::{anyhow, bail, Context, Error};
use clap::{Parser, ValueEnum};
use pep440_rs::{Operator, Version, VersionSpecifier, VersionSpecifiers};
use pep508_rs::{Requirement, VersionOrUrl};
use pep440_rs::{Operator, Version, VersionPattern, VersionSpecifier, VersionSpecifiers};
use pep508_rs::{ExtraName, PackageName, Requirement, VerbatimUrl, VersionOrUrl};
use serde::Deserialize;
use url::Url;

use crate::bootstrap::ensure_self_venv;
use crate::config::Config;
Expand Down Expand Up @@ -157,9 +156,11 @@ impl ReqExtras {
// and use ${PROJECT_ROOT} will cause error in hatchling, so force absolute path.
let is_hatchling =
PyProject::discover()?.build_backend() == Some(BuildSystem::Hatchling);
let absolute_url =
VerbatimUrl::from_absolute_path(env::current_dir()?.join(path).to_string_lossy())
.map_err(|_| anyhow!("unable to interpret '{}' as path", path.display()))?;
let file_url = if self.absolute || is_hatchling {
Url::from_file_path(env::current_dir()?.join(path))
.map_err(|_| anyhow!("unable to interpret '{}' as path", path.display()))?
absolute_url
} else {
let base = env::current_dir()?;
let rv = pathdiff::diff_paths(base.join(path), &base).ok_or_else(|| {
Expand All @@ -169,20 +170,20 @@ impl ReqExtras {
path.display()
)
})?;
let mut url = Url::parse("file://")?;
url.set_path(&Path::new("/${PROJECT_ROOT}").join(rv).to_string_lossy());
url
absolute_url.with_given(
String::from("file://")
+ &Path::new("/${PROJECT_ROOT}").join(rv).to_string_lossy(),
)
};
req.version_or_url = match req.version_or_url {
Some(_) => bail!("requirement already has a version marker"),
None => Some(pep508_rs::VersionOrUrl::Url(file_url)),
};
}
for feature in self.features.iter().flat_map(|x| x.split(',')) {
let feature = feature.trim();
let extras = req.extras.get_or_insert_with(Vec::new);
if !extras.iter().any(|x| x == feature) {
extras.push(feature.into());
let feature = ExtraName::from_str(feature.trim())?;
if !req.extras.iter().any(|x| *x == feature) {
req.extras.push(feature);
}
}
Ok(())
Expand Down Expand Up @@ -388,20 +389,19 @@ fn resolve_requirements_with_unearth(
// use ~= but need to use ==.
match *default_operator {
_ if version.is_local() => Operator::Equal,
Operator::TildeEqual if version.release.len() < 2 => {
Operator::TildeEqual if version.release().len() < 2 => {
Operator::GreaterThanEqual
}
ref other => other.clone(),
other => other,
},
Version::from_str(m.version.as_ref().unwrap())
VersionPattern::from_str(m.version.as_ref().unwrap())
.map_err(|msg| anyhow!("invalid version: {}", msg))?,
false,
)
.map_err(|msg| anyhow!("invalid version specifier: {}", msg))?,
)),
));
}
requirement.name = m.name;
requirement.name = PackageName::new(m.name)?;
Ok(())
}

Expand Down Expand Up @@ -476,22 +476,25 @@ fn resolve_requirements_with_uv(
for spec in specs.iter() {
let op = match *default_operator {
_ if spec.version().is_local() => Operator::Equal,
Operator::TildeEqual if spec.version().release.len() < 2 => {
Operator::TildeEqual if spec.version().release().len() < 2 => {
Operator::GreaterThanEqual
}
ref other => other.clone(),
other => other,
};
new_specs.push(
VersionSpecifier::new(op, spec.version().clone(), false)
.map_err(|msg| anyhow!("invalid version specifier: {}", msg))?,
VersionSpecifier::new(
op,
VersionPattern::verbatim(spec.version().clone()),
)
.map_err(|msg| anyhow!("invalid version specifier: {}", msg))?,
);
}
new_specs
}));
}
}
if let Some(old_extras) = &req.extras {
new_req.extras = Some(old_extras.clone());
if !req.extras.is_empty() {
new_req.extras = req.extras.clone();
}
*req = new_req;
}
Expand Down
3 changes: 2 additions & 1 deletion rye/src/cli/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,8 @@ fn import_requirements_file(
data.requirements.iter().for_each(|x| {
requirements
.entry(x.requirement.name.to_string())
.or_insert(format_requirement(&x.requirement).to_string());
// todo: will to_string this work?
.or_insert(x.requirement.to_string());
});
Ok(())
}
4 changes: 2 additions & 2 deletions rye/src/cli/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use same_file::is_same_file;

use crate::config::Config;
use crate::consts::VENV_BIN;
use crate::pyproject::{locate_projects, normalize_package_name, DependencyKind, PyProject};
use crate::pyproject::{locate_projects, DependencyKind, PyProject};
use crate::sync::autosync;
use crate::utils::{CommandOutput, QuietExit};

Expand Down Expand Up @@ -141,7 +141,7 @@ fn has_pytest_dependency(projects: &[PyProject]) -> Result<bool, Error> {
.chain(project.iter_dependencies(DependencyKind::Normal))
{
if let Ok(req) = dep.expand(|name| std::env::var(name).ok()) {
if normalize_package_name(&req.name) == "pytest" {
if req.name.as_ref() == "pytest" {
return Ok(true);
}
}
Expand Down
25 changes: 13 additions & 12 deletions rye/src/cli/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,35 +42,36 @@ pub fn execute(cmd: Args) -> Result<(), Error> {
}
}
None => {
let mut version = pyproject_toml.version()?;
let version = pyproject_toml.version()?;
match cmd.bump {
Some(bump) => bump_version(&mut version, bump, &mut pyproject_toml)?,
Some(bump) => bump_version(version, bump, &mut pyproject_toml)?,
None => echo!("{}", version),
}
}
}
Ok(())
}

fn bump_version(version: &mut Version, bump: Bump, pyproject: &mut PyProject) -> Result<(), Error> {
fn bump_version(mut version: Version, bump: Bump, pyproject: &mut PyProject) -> Result<(), Error> {
if version.is_post() {
version.post = None;
version = version.with_post(None);
}
if version.is_dev() {
version.dev = None;
version = version.with_dev(None);
warn!("dev version will be bumped to release version");
} else {
let mut release = version.release().to_vec();
let index = bump as usize;
if version.release.get(index).is_none() {
version.release.resize(index + 1, 0);
}
version.release[index] += 1;
for i in index + 1..version.release.len() {
version.release[i] = 0;
if release.get(index).is_none() {
release.resize(index + 1, 0);
}
release[index] += 1;
release[index + 1..].fill(0);

version = version.with_release(release);
}

pyproject.set_version(version);
pyproject.set_version(&version);
pyproject.save().unwrap();

echo!("version bumped to {}", version);
Expand Down
21 changes: 11 additions & 10 deletions rye/src/installer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ use std::{env, fs};
use anyhow::{bail, Context, Error};
use console::style;
use once_cell::sync::Lazy;
use pep508_rs::{Requirement, VersionOrUrl};
use pep508_rs::{PackageName, Requirement, VerbatimUrl, VersionOrUrl};
use regex::Regex;
use same_file::is_same_file;
use url::Url;

use crate::bootstrap::{ensure_self_venv, fetch, FetchOptions};
use crate::config::Config;
Expand Down Expand Up @@ -121,7 +120,7 @@ pub fn install(
.map(|x| normalize_package_name(x))
.collect::<Vec<_>>();

let target_venv_path = tool_dir.join(normalize_package_name(&requirement.name));
let target_venv_path = tool_dir.join(requirement.name.as_ref());
if target_venv_path.is_dir() && !force {
bail!("package already installed");
}
Expand All @@ -138,7 +137,7 @@ pub fn install(
&self_venv,
&py_ver,
&target_venv_path,
requirement.name.as_str(),
requirement.name.as_ref(),
)?;

if Config::current().use_uv() {
Expand Down Expand Up @@ -197,7 +196,7 @@ pub fn install(
let out = Command::new(py)
.arg("-c")
.arg(FIND_SCRIPT_SCRIPT)
.arg(&requirement.name)
.arg(requirement.name.as_ref())
.stdout(Stdio::piped())
.output()
.context("unable to dump package manifest from installed package")?;
Expand All @@ -219,7 +218,7 @@ pub fn install(
if package.is_empty() {
continue;
}
if include_deps.contains(&normalize_package_name(package)) {
if include_deps.contains(package) {
installed.extend(install_scripts(files, &target_venv_bin_path, &shim_dir)?);
} else {
let scripts = find_scripts(files, &target_venv_bin_path);
Expand Down Expand Up @@ -437,14 +436,16 @@ pub fn resolve_local_requirement(
let output = String::from_utf8_lossy(&output.stdout);
if let Some(c) = SUCCESSFULLY_DOWNLOADED_RE.captures(&output) {
let version_or_url = Some(VersionOrUrl::Url(
match Url::from_file_path(env::current_dir()?.join(maybe_path)) {
match VerbatimUrl::from_absolute_path(
env::current_dir()?.join(maybe_path).to_string_lossy(),
) {
Ok(url) => url,
Err(()) => bail!("invalid path reference"),
Err(err) => return Err(err).with_context(|| "invalid path reference"),
},
));
let name = c[1].trim().to_string();
let name = PackageName::new(c[1].trim().to_string())?;
Ok(Some(Requirement {
extras: None,
extras: Vec::new(),
name,
version_or_url,
marker: None,
Expand Down

0 comments on commit 81ac63f

Please sign in to comment.