Skip to content

Commit

Permalink
bugfix(poetry): dependency parsing issues, issues paring ^, >=, <= in…
Browse files Browse the repository at this point in the history
… dependencies in certain situations
  • Loading branch information
stvnksslr committed Nov 9, 2024
1 parent 0f83349 commit db6f753
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 31 deletions.
41 changes: 24 additions & 17 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use clap::{Arg, Command};
use log::{error, info};
use std::env;
use std::path::Path;
use std::process::exit;
use clap::{Arg, Command};

mod migrator;
mod types;
mod utils;
mod migrator;

fn main() {
if env::var_os("RUST_LOG").is_none() {
Expand All @@ -20,20 +20,26 @@ fn main() {
}

let matches = Command::new("uv-migrator")
.version("1.0")
.version(env!("CARGO_PKG_VERSION"))
.about("Migrates Python projects to use uv")
.arg(Arg::new("PATH")
.help("The path to the project directory")
.required(true)
.index(1))
.arg(Arg::new("import-global-pip-conf")
.long("import-global-pip-conf")
.help("Import extra index URLs from ~/.pip/pip.conf")
.action(clap::ArgAction::SetTrue))
.arg(Arg::new("import-index")
.long("import-index")
.help("Additional index URL to import")
.action(clap::ArgAction::Append))
.arg(
Arg::new("PATH")
.help("The path to the project directory")
.required(true)
.index(1),
)
.arg(
Arg::new("import-global-pip-conf")
.long("import-global-pip-conf")
.help("Import extra index URLs from ~/.pip/pip.conf")
.action(clap::ArgAction::SetTrue),
)
.arg(
Arg::new("import-index")
.long("import-index")
.help("Additional index URL to import")
.action(clap::ArgAction::Append),
)
.get_matches();

let input_path = Path::new(matches.get_one::<String>("PATH").unwrap());
Expand All @@ -44,7 +50,8 @@ fn main() {
};

let import_global_pip_conf = matches.get_flag("import-global-pip-conf");
let additional_index_urls: Vec<String> = matches.get_many::<String>("import-index")
let additional_index_urls: Vec<String> = matches
.get_many::<String>("import-index")
.map(|values| values.cloned().collect())
.unwrap_or_default();

Expand All @@ -55,4 +62,4 @@ fn main() {
exit(1);
}
}
}
}
16 changes: 15 additions & 1 deletion src/migrator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,20 @@ impl MigrationTool for UvTool {

for dep in deps {
let mut dep_str = if let Some(version) = &dep.version {
format!("{}=={}", dep.name, version)
let version = version.trim();
if version.starts_with('^') {
// Convert caret version to >= format
format!("{}>={}", dep.name, &version[1..])
} else if version.starts_with('~') {
// Convert tilde version to ~= format
format!("{}~={}", dep.name, &version[1..])
} else if version.starts_with(|c: char| c == '>' || c == '<' || c == '=') {
// Other version constraints remain as is
format!("{}{}", dep.name, version)
} else {
// For exact versions
format!("{}=={}", dep.name, version)
}
} else {
dep.name.clone()
};
Expand All @@ -95,6 +108,7 @@ impl MigrationTool for UvTool {
command.arg(dep_str);
}

info!("Running uv add command with dependencies: {:?}", command);
let output = command
.output()
.map_err(|e| format!("Failed to execute uv command: {}", e))?;
Expand Down
17 changes: 4 additions & 13 deletions src/migrator/poetry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ impl PoetryMigrationSource {

let version = match value {
toml::Value::String(v) => {
let v = v.trim_start_matches('^');
// Handle "*" version specifier
let v = v.trim();
if v == "*" {
None
} else {
Expand All @@ -63,16 +62,8 @@ impl PoetryMigrationSource {
toml::Value::Table(t) => {
t.get("version")
.and_then(|v| v.as_str())
.map(|v| {
let v = v.trim_start_matches('^');
// Handle "*" version specifier in table format
if v == "*" {
String::new()
} else {
v.to_string()
}
})
.filter(|v| !v.is_empty())
.map(|v| v.trim().to_string())
.filter(|v| v != "*")
}
_ => None,
};
Expand All @@ -84,4 +75,4 @@ impl PoetryMigrationSource {
environment_markers: None,
})
}
}
}

0 comments on commit db6f753

Please sign in to comment.