Skip to content

Commit 26c71f1

Browse files
committed
Move csm env unpack out
1 parent e215b6a commit 26c71f1

2 files changed

Lines changed: 79 additions & 73 deletions

File tree

src/env/mod.rs

Lines changed: 2 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@ pub mod unpack;
88

99
use crate::csmrc::Config;
1010
use crate::env::parsing::env_file::RobotmkEnv;
11-
use crate::micromamba::{self, MicromambaResult, micromamba};
11+
use crate::micromamba::{MicromambaResult, micromamba};
1212

1313
use log::{debug, error, info};
14-
use std::fs;
1514
use std::path::{Component, Path, PathBuf};
1615
use std::process::ExitCode;
1716

@@ -125,74 +124,6 @@ pub fn run(config: Config, subcommand: Subcommand) -> Result<(), ExitCode> {
125124
Subcommand::Activate(args) => activate::run(config, args),
126125
Subcommand::Deactivate => deactivate::run(),
127126
Subcommand::Pack(args) => pack::run(config, args),
128-
Subcommand::Unpack(args) => {
129-
fn archive_name_to_env_name(archive_path: &Path) -> Option<String> {
130-
let filename = archive_path.file_name()?.to_str()?;
131-
filename
132-
.strip_suffix(".tar.gz")
133-
.or_else(|| filename.strip_suffix(".tgz"))
134-
.map(String::from)
135-
}
136-
let env_name = match args.common.name {
137-
Some(name) => name,
138-
None => match archive_name_to_env_name(&args.archive_path) {
139-
Some(name) => {
140-
info!(
141-
"Using '{}' as environment name, based on archive filename",
142-
name
143-
);
144-
name
145-
}
146-
None => {
147-
error!(
148-
"Could not determine environment name from archive filename. Please specify an environment name with --name."
149-
);
150-
return Err(ExitCode::FAILURE);
151-
}
152-
},
153-
};
154-
155-
// TODO: We really need a more generic error type to avoid this kind of mapping everywhere
156-
let target_env_path = micromamba::create_env_dir(&config, &env_name).map_err(|e| {
157-
error!("{}", e);
158-
ExitCode::FAILURE
159-
})?;
160-
161-
// Send the archive to flate2 to decompress and untar
162-
info!(
163-
"Unpacking archive '{}' to create environment '{}'",
164-
args.archive_path.display(),
165-
env_name
166-
);
167-
debug!("Opening '{}' for read", args.archive_path.display());
168-
let archive_file = fs::File::open(&args.archive_path).map_err(|e| {
169-
error!("{}", e);
170-
ExitCode::FAILURE
171-
})?;
172-
let decompressor = flate2::read::GzDecoder::new(archive_file);
173-
let mut archive = tar::Archive::new(decompressor);
174-
archive.unpack(&target_env_path).map_err(|e| {
175-
error!(
176-
"Could not unpack archive to '{}': {}",
177-
target_env_path.display(),
178-
e
179-
);
180-
ExitCode::FAILURE
181-
})?;
182-
info!(
183-
"Successfully unpacked environment to '{}'",
184-
target_env_path.display()
185-
);
186-
187-
info!("Running 'conda-unpack' in the new environment to fix paths...");
188-
let result = micromamba(
189-
&config,
190-
vec!["run", "--name", &env_name, "conda-unpack"],
191-
config.verbose,
192-
);
193-
let rc = result.exit_code();
194-
dump_micromamba_captured_output_on_error(&result, rc);
195-
result.into()
196-
}
127+
Subcommand::Unpack(args) => unpack::run(config, args),
197128
}
198129
}

src/env/unpack.rs

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
use crate::env::CommonArgs;
1+
use crate::csmrc::Config;
2+
use crate::env::{CommonArgs, dump_micromamba_captured_output_on_error};
3+
use crate::micromamba::{self, micromamba};
24

3-
use std::path::PathBuf;
5+
use log::{debug, error, info};
6+
use std::fs;
7+
use std::path::{Path, PathBuf};
8+
use std::process::ExitCode;
49

510
#[derive(Debug, clap::Args)]
611
pub struct Args {
@@ -11,3 +16,73 @@ pub struct Args {
1116
#[arg(value_name = "ARCHIVE")]
1217
pub archive_path: PathBuf,
1318
}
19+
20+
pub fn run(config: Config, args: Args) -> Result<(), ExitCode> {
21+
fn archive_name_to_env_name(archive_path: &Path) -> Option<String> {
22+
let filename = archive_path.file_name()?.to_str()?;
23+
filename
24+
.strip_suffix(".tar.gz")
25+
.or_else(|| filename.strip_suffix(".tgz"))
26+
.map(String::from)
27+
}
28+
let env_name = match args.common.name {
29+
Some(name) => name,
30+
None => match archive_name_to_env_name(&args.archive_path) {
31+
Some(name) => {
32+
info!(
33+
"Using '{}' as environment name, based on archive filename",
34+
name
35+
);
36+
name
37+
}
38+
None => {
39+
error!(
40+
"Could not determine environment name from archive filename. Please specify an environment name with --name."
41+
);
42+
return Err(ExitCode::FAILURE);
43+
}
44+
},
45+
};
46+
47+
// TODO: We really need a more generic error type to avoid this kind of mapping everywhere
48+
let target_env_path = micromamba::create_env_dir(&config, &env_name).map_err(|e| {
49+
error!("{}", e);
50+
ExitCode::FAILURE
51+
})?;
52+
53+
// Send the archive to flate2 to decompress and untar
54+
info!(
55+
"Unpacking archive '{}' to create environment '{}'",
56+
args.archive_path.display(),
57+
env_name
58+
);
59+
debug!("Opening '{}' for read", args.archive_path.display());
60+
let archive_file = fs::File::open(&args.archive_path).map_err(|e| {
61+
error!("{}", e);
62+
ExitCode::FAILURE
63+
})?;
64+
let decompressor = flate2::read::GzDecoder::new(archive_file);
65+
let mut archive = tar::Archive::new(decompressor);
66+
archive.unpack(&target_env_path).map_err(|e| {
67+
error!(
68+
"Could not unpack archive to '{}': {}",
69+
target_env_path.display(),
70+
e
71+
);
72+
ExitCode::FAILURE
73+
})?;
74+
info!(
75+
"Successfully unpacked environment to '{}'",
76+
target_env_path.display()
77+
);
78+
79+
info!("Running 'conda-unpack' in the new environment to fix paths...");
80+
let result = micromamba(
81+
&config,
82+
vec!["run", "--name", &env_name, "conda-unpack"],
83+
config.verbose,
84+
);
85+
let rc = result.exit_code();
86+
dump_micromamba_captured_output_on_error(&result, rc);
87+
result.into()
88+
}

0 commit comments

Comments
 (0)