@@ -62,7 +62,16 @@ pub struct CondaPackageInfo {
6262
6363impl CondaPackageInfo {
6464 pub fn from ( path : & Path , package : & Package ) -> Option < Self > {
65- get_conda_package_info ( path, package)
65+ let history = fs:: read_to_string ( path. join ( "conda-meta" ) . join ( "history" ) ) . ok ( ) ;
66+ Self :: from_history ( path, package, history. as_deref ( ) )
67+ }
68+
69+ pub ( crate ) fn from_history (
70+ path : & Path ,
71+ package : & Package ,
72+ history : Option < & str > ,
73+ ) -> Option < Self > {
74+ get_conda_package_info ( path, package, history)
6675 }
6776}
6877
@@ -73,8 +82,14 @@ struct CondaMetaPackageStructure {
7382}
7483
7584/// Get the details of a conda package from the 'conda-meta' directory.
76- fn get_conda_package_info ( path : & Path , name : & Package ) -> Option < CondaPackageInfo > {
77- if let Some ( info) = get_conda_package_info_from_history ( path, name) {
85+ fn get_conda_package_info (
86+ path : & Path ,
87+ name : & Package ,
88+ history : Option < & str > ,
89+ ) -> Option < CondaPackageInfo > {
90+ if let Some ( info) =
91+ history. and_then ( |history| get_conda_package_info_from_history ( path, name, history) )
92+ {
7893 Some ( info)
7994 } else {
8095 warn ! (
@@ -86,14 +101,15 @@ fn get_conda_package_info(path: &Path, name: &Package) -> Option<CondaPackageInf
86101 }
87102}
88103
89- fn get_conda_package_info_from_history ( path : & Path , name : & Package ) -> Option < CondaPackageInfo > {
104+ fn get_conda_package_info_from_history (
105+ path : & Path ,
106+ name : & Package ,
107+ history_contents : & str ,
108+ ) -> Option < CondaPackageInfo > {
90109 // conda-meta is in the root of the conda installation folder
91110 let path = path. join ( "conda-meta" ) ;
92- let history = path. join ( "history" ) ;
93111 let package_entry = format ! ( ":{}-" , name. to_name( ) ) ;
94112
95- let history_contents = fs:: read_to_string ( history) . ok ( ) ?;
96-
97113 // Filter to only include lines that:
98114 // 1. Start with '+' (installed packages, not '-' for removed packages)
99115 // 2. Contain the package entry (e.g., ":python-")
@@ -107,13 +123,9 @@ fn get_conda_package_info_from_history(path: &Path, name: &Package) -> Option<Co
107123 // ...
108124 // -defaults::python-3.9.18-h123456_0 <- removed during upgrade
109125 // +defaults::python-3.9.21-h789abc_0 <- current version (we want this)
110- let matching_lines : Vec < & str > = history_contents
126+ let line = history_contents
111127 . lines ( )
112- . filter ( |l| l. starts_with ( '+' ) && l. contains ( & package_entry) )
113- . collect ( ) ;
114-
115- // Get the last matching line (most recent installation)
116- let line = matching_lines. last ( ) ?;
128+ . rfind ( |line| line. starts_with ( '+' ) && line. contains ( & package_entry) ) ?;
117129
118130 // Sample entry in the history file
119131 // +conda-forge/osx-arm64::psutil-5.9.8-py312he37b823_0
0 commit comments