Skip to content

Commit

Permalink
save leaves to environment.yml #2
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszKielar authored Sep 7, 2020
2 parents dc9a229 + c468c66 commit 552833e
Show file tree
Hide file tree
Showing 5 changed files with 238 additions and 86 deletions.
104 changes: 104 additions & 0 deletions src/env.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
use std::collections::HashMap;
use std::env;
use std::fs;
use std::io;
use std::io::prelude::*;

use crate::metadata::Metadata;
use crate::package::{Installer, Package};

// TODO add unit tests
pub struct CondaEnv {
name: String,
installed_packages: Vec<Package>,
}

impl CondaEnv {
// TODO add information about version
pub fn to_yml(&self) -> io::Result<()> {
// create a file in current directory
// TODO path could be passed as an argument
let path = env::current_dir()?.join("environment2.yml");
let mut file = fs::File::create(path)?;

// write name to the file
file.write_all(b"name: ")?;
file.write_all(self.name.as_bytes())?;
file.write_all(b"\n")?;

// write dependencies
file.write_all(b"dependencies:")?;

// get deps installed by conda
let conda_deps: Vec<&Package> = self
.installed_packages
.iter()
.filter(|&p| p.installer == Installer::Conda)
.collect();

// get deps installed by pip
let pip_deps: Vec<&Package> = self
.installed_packages
.iter()
.filter(|&p| p.installer == Installer::Pip)
.collect();

for &d in conda_deps.iter() {
let p_string: String = d.to_owned().into();
file.write_all(b"\n")?;
file.write_all(b" - ")?;
file.write_all(p_string.as_bytes())?;
}
file.write_all(b"\n")?;

if pip_deps.len() > 0 {
file.write_all(b" - pip:")?;
for &d in pip_deps.iter() {
let p_string: String = d.to_owned().into();
file.write_all(b"\n")?;
file.write_all(b" - ")?;
file.write_all(p_string.as_bytes())?;
}
file.write_all(b"\n")?;
}

Ok(())
}
}

impl From<HashMap<String, Metadata>> for CondaEnv {
fn from(conda_metadata: HashMap<String, Metadata>) -> Self {
// get conda name
let name =
env::var("CONDA_DEFAULT_ENV").expect("Please make sure you have conda env activated.");
let installed_packages: Vec<Package> = conda_metadata
.values()
.map(|m| {
let p: Package = m.to_owned().into();
p
})
.collect();

Self {
name,
installed_packages,
}
}
}

impl From<Vec<String>> for CondaEnv {
fn from(leaves: Vec<String>) -> Self {
// get conda name
let name =
env::var("CONDA_DEFAULT_ENV").expect("Please make sure you have conda env activated.");
let installed_packages: Vec<Package> = leaves
.iter()
.map(|name| Metadata::from_name(name).unwrap().into())
.collect();

Self {
name,
installed_packages,
}
}
}
9 changes: 8 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod env;
mod metadata;
mod package;
mod utils;
Expand All @@ -6,6 +7,7 @@ use std::io;

use dotenv::dotenv;

use crate::env::CondaEnv;
use crate::metadata::Metadata;
use crate::package::{print_package, Package};
use crate::utils::get_dependent_packages;
Expand All @@ -25,9 +27,14 @@ fn main() -> io::Result<()> {
println!();

println!("----leaves----");
for leaf in get_leaves().iter() {
let leaves = get_leaves();
for leaf in leaves.iter() {
println!("{}", leaf)
}

println!("----leaves-to-environment.yml----");
let env: CondaEnv = leaves.into();
env.to_yml()?;

Ok(())
}
88 changes: 50 additions & 38 deletions src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,6 @@ pub struct Metadata {
}

impl Metadata {
pub fn new(name: String, version: String, requires_dist: Vec<String>) -> Self {
Self {
name,
version,
requires_dist,
}
}

// TODO use AsPath as an argument
pub fn from_metadata_file<'a, P>(path: &'a P) -> Result<Self, io::Error>
where
Expand Down Expand Up @@ -61,7 +53,11 @@ impl Metadata {
}
}

let metadata = Self::new(name, version, requires_dist);
let metadata = Self {
name,
version,
requires_dist,
};
Ok(metadata)
}

Expand Down Expand Up @@ -242,7 +238,11 @@ mod tests {
fn test_from_json_no_dependencies() {
// given:
let path = "./tests/data/conda-meta/pkg1-0.0.1.json";
let expected_m = Metadata::new(String::from("pkg1"), String::from("0.0.1"), vec![]);
let expected_m = Metadata {
name: String::from("pkg1"),
version: String::from("0.0.1"),
requires_dist: vec![],
};
// when:
let m = Metadata::from_json(path).unwrap();
// then:
Expand All @@ -253,11 +253,11 @@ mod tests {
fn test_from_json_one_dependency() {
// given:
let path = "./tests/data/conda-meta/pkg2a-0.0.1.json";
let expected_m = Metadata::new(
String::from("pkg2a"),
String::from("0.0.1"),
vec![String::from("pkg1")],
);
let expected_m = Metadata {
name: String::from("pkg2a"),
version: String::from("0.0.1"),
requires_dist: vec![String::from("pkg1")],
};
// when:
let m = Metadata::from_json(path).unwrap();
// then:
Expand All @@ -268,11 +268,11 @@ mod tests {
fn test_from_json_multiple_dependencies() {
// given:
let path = "./tests/data/conda-meta/pkg3-0.0.1.json";
let expected_m = Metadata::new(
String::from("pkg3"),
String::from("0.0.1"),
vec![String::from("pkg2a"), String::from("pkg2b")],
);
let expected_m = Metadata {
name: String::from("pkg3"),
version: String::from("0.0.1"),
requires_dist: vec![String::from("pkg2a"), String::from("pkg2b")],
};
// when:
let m = Metadata::from_json(path).unwrap();
// then:
Expand Down Expand Up @@ -310,11 +310,11 @@ mod tests {
"version": "0.0.1",
"depends": "pkg2"
}"#;
let expected_m = Metadata::new(
String::from("pkg1"),
String::from("0.0.1"),
vec![String::from("pkg2")],
);
let expected_m = Metadata {
name: String::from("pkg1"),
version: String::from("0.0.1"),
requires_dist: vec![String::from("pkg2")],
};
// when:
let m: Metadata = serde_json::from_str(string).unwrap();
// then:
Expand All @@ -329,7 +329,11 @@ mod tests {
"version": "0.0.1",
"depends": "python"
}"#;
let expected_m = Metadata::new(String::from("pkg1"), String::from("0.0.1"), vec![]);
let expected_m = Metadata {
name: String::from("pkg1"),
version: String::from("0.0.1"),
requires_dist: vec![],
};
// when:
let m: Metadata = serde_json::from_str(string).unwrap();
// then:
Expand All @@ -344,7 +348,11 @@ mod tests {
"version": "0.0.1",
"depends": "libsome"
}"#;
let expected_m = Metadata::new(String::from("pkg1"), String::from("0.0.1"), vec![]);
let expected_m = Metadata {
name: String::from("pkg1"),
version: String::from("0.0.1"),
requires_dist: vec![],
};
// when:
let m: Metadata = serde_json::from_str(string).unwrap();
// then:
Expand All @@ -359,7 +367,11 @@ mod tests {
"version": "0.0.1",
"depends": "_liblowlevel"
}"#;
let expected_m = Metadata::new(String::from("pkg1"), String::from("0.0.1"), vec![]);
let expected_m = Metadata {
name: String::from("pkg1"),
version: String::from("0.0.1"),
requires_dist: vec![],
};
// when:
let m: Metadata = serde_json::from_str(string).unwrap();
// then:
Expand All @@ -374,11 +386,11 @@ mod tests {
"version": "0.0.1",
"depends": ["pkg2a", "pkg2b"]
}"#;
let expected_m = Metadata::new(
String::from("pkg1"),
String::from("0.0.1"),
vec![String::from("pkg2a"), String::from("pkg2b")],
);
let expected_m = Metadata {
name: String::from("pkg1"),
version: String::from("0.0.1"),
requires_dist: vec![String::from("pkg2a"), String::from("pkg2b")],
};
// when:
let m: Metadata = serde_json::from_str(string).unwrap();
// then:
Expand All @@ -393,11 +405,11 @@ mod tests {
"version": "0.0.1",
"depends": ["pkg2a", "pkg2b", "python", "libsome", "_liblowlevel"]
}"#;
let expected_m = Metadata::new(
String::from("pkg1"),
String::from("0.0.1"),
vec![String::from("pkg2a"), String::from("pkg2b")],
);
let expected_m = Metadata {
name: String::from("pkg1"),
version: String::from("0.0.1"),
requires_dist: vec![String::from("pkg2a"), String::from("pkg2b")],
};
// when:
let m: Metadata = serde_json::from_str(string).unwrap();
// then:
Expand Down
Loading

0 comments on commit 552833e

Please sign in to comment.