Skip to content

Commit

Permalink
Fix regression in the metadata management command on Windows (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
ofek committed Jun 18, 2023
1 parent fa1afd4 commit 3bc607d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 30 deletions.
1 change: 1 addition & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
***Fixed:***

- Properly handle cases where temporary files are on different filesystems
- Fix regression in the `metadata` management command on Windows

## 0.8.0 - 2023-06-09

Expand Down
71 changes: 41 additions & 30 deletions src/commands/self_cmd/metadata.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::fs;
use std::path::PathBuf;

use anyhow::Result;
use clap::Args;
Expand All @@ -19,8 +18,8 @@ impl Cli {
}

let python = app::python_path(&installation_directory);
let site_packages: Option<PathBuf> = if cfg!(windows) {
(|| Some(python.parent()?.join("Lib").join("site-packages")))()
let site_packages = if cfg!(windows) {
(|| Some(python.parent()?.parent()?.join("Lib").join("site-packages")))()
} else {
(|| {
let lib_dir = python.parent()?.parent()?.join("lib");
Expand All @@ -37,34 +36,46 @@ impl Cli {
})()
};

if let Some(site_packages) = site_packages.filter(|p| p.is_dir()) {
let metadata_file: Option<PathBuf> =
fs::read_dir(site_packages).ok().and_then(|entries| {
for entry in entries.flatten() {
let name = entry.file_name().to_string_lossy().to_string();
if name.ends_with(".dist-info")
&& name
.starts_with(&format!("{}-", app::project_name().replace('-', "_")))
{
return Some(entry.path().join("METADATA"));
}
}
None
});
let site_packages = if let Some(site_packages) = site_packages.filter(|p| p.is_dir()) {
site_packages
} else {
return Ok(());
};

let expected_prefix = format!("{}-", app::project_name().replace('-', "_"));
let metadata_file = fs::read_dir(site_packages).ok().and_then(|entries| {
for entry in entries.flatten() {
let name = entry.file_name().to_string_lossy().to_string();
if name.ends_with(".dist-info")
&& name.starts_with(&expected_prefix)
&& name
.chars()
.nth(&expected_prefix.len() + 1)
.map(|c| c.is_numeric())
.is_some()
{
return Some(entry.path().join("METADATA"));
}
}
None
});

let metadata_file = if let Some(metadata_file) = metadata_file.filter(|p| p.is_file()) {
metadata_file
} else {
return Ok(());
};

if let Some(metadata_file) = metadata_file.filter(|p| p.is_file()) {
if let Ok(metadata) = fs::read_to_string(metadata_file) {
for line in metadata.lines() {
if line.starts_with("Version: ") {
println!(
"{}",
app::metadata_template()
.replace("{project}", &app::project_name())
.replace("{version}", line.split_at(9).1)
);
break;
}
}
if let Ok(metadata) = fs::read_to_string(metadata_file) {
for line in metadata.lines() {
if line.starts_with("Version: ") {
println!(
"{}",
app::metadata_template()
.replace("{project}", &app::project_name())
.replace("{version}", line.split_at(9).1)
);
break;
}
}
}
Expand Down

0 comments on commit 3bc607d

Please sign in to comment.